Skip to content

Commit

Permalink
Enhancement 15 (#18)
Browse files Browse the repository at this point in the history
* Update Dart SDK version (2.17.1 -> 2.17.3) & kassakuitti version

* Fix an error with parsing  an S-kaupat HTML file

* Update dependencies (incl. dev dependencies)

* Update transitive dependencies

* Changes to arg commands

- Move handleArgCommands method into own file
- Update help command
- Add initially hive command

* Some refactoring

* Pull Hive initialization to the root level & add count command for Hive CLI

* Add methods for Hive CLI & add isNullOrEmpty String extension

* Update add, readAll & searchBy methods in Hive CLI

* Make needed Hive methods awaitable / asynchronous

* Make ean_handler awaitable / asynchronous & close the box in the main method

* Update Hive read methods to show the key value (currently an order number)

* Fix delete method to delete based on given order number

* Fix update method of hive_handling

* Refactor printing methods

* Fix div parsing in load_html_s_kaupat

* Removed '_countProducts' from '_updateProduct' method

* Some refactoring into update & delete methods

* Fix a bug with digit-beginning receipt product

* Refactor update & delete methods

* Add installation page & update readme

* Add developing page & update readme

* Update readme

* Update readme
  • Loading branch information
areee authored Jun 24, 2022
1 parent cce9834 commit c71e07e
Show file tree
Hide file tree
Showing 16 changed files with 443 additions and 148 deletions.
9 changes: 9 additions & 0 deletions DEVELOPING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
## Generate `hive_product.g.dart` file

You can generate `hive_product.g.dart` file by running:

```
dart run build_runner build
```

This generating process is needed before running the program for the first time.
23 changes: 23 additions & 0 deletions INSTALLATION.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Installation

## Install Chrome extension & clone this project

1. Download a Chrome extension called "[snapshot-as-html](https://github.com/areee/snapshot-as-html)" (edited by the author of this project).
- In the [latest release page](https://github.com/areee/snapshot-as-html/releases/latest), go to the assets section and click the "Source code (zip)" link. It'll download the source code to your computer.
- Unzip the zip file.
- Open Google Chrome and go to the [Extensions page](chrome://extensions) (chrome://extensions).
- Enable the developer mode and click "Load unpacked". Browse to the unzipped folder (e.g. `snapshot-as-html-0.1.0`) and go to the subdirectory (`snapshot-as-html`). That folder should include all the extension files, e.g. `manifest.json`. Click "Select" in the open dialog and Google Chrome will load the extension.
2. Git clone this project (or Code → Download ZIP) under your Documents folder (or anywhere you like). Set an alias to use it with `kassakuitti` command (see the next section).

## Set an alias

Use `kassakuitti` alias by adding the row below to your profile file. Here we're assuming you have cloned this project under your Documents folder.

```
alias kassakuitti='dart run $HOME/Documents/dart_kassakuitti_cli/bin/dart_kassakuitti_cli.dart'
```

If you're unsure where to save the alias, this might help you:

- If you're using _Zsh_ as your shell, use `~/.zshrc` profile file.
- If you're using _Bash_ as your shell, use `~/.bash_profile` profile file.
60 changes: 26 additions & 34 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,13 @@
## Installation

1. Download a Chrome extension called "[snapshot-as-html](https://github.com/areee/snapshot-as-html)" (edited by the author of this project)
- In the [latest release page](https://github.com/areee/snapshot-as-html/releases/latest), go to the assets section and click the "Source code (zip)" link. It'll download the source code to your computer.
- Unzip the zip file.
- Open Google Chrome and go to the [Extensions page](chrome://extensions) (chrome://extensions).
- Enable the developer mode and click "Load unpacked". Browse to the unzipped folder (e.g. `snapshot-as-html-0.1.0`) and go to the subdirectory (`snapshot-as-html`). That folder should include all the extension files, e.g. `manifest.json`. Click "Select" in the open dialog and Google Chrome will load the extension.
2. Git clone this project (or Code → Download ZIP). Add an [alias](#alias) to use it with `kassakuitti` command.
See the installation in [its own page](https://github.com/areee/dart_kassakuitti_cli/blob/main/INSTALLATION.md).

## Usage

### Needed files
- An EAN file = an HTML file (.html) → needed when using both S-kaupat and K-ruoka
- Generate an HTML file by using `snapshot-as-html` project (see the [installation](#installation)).
- Generate an HTML file by using `snapshot-as-html` project (see the [installation](https://github.com/areee/dart_kassakuitti_cli/blob/main/INSTALLATION.md)).
- To generate that, go to an active order page of an online store:
- [s-kaupat.fi](https://www.s-kaupat.fi) (profile image → "Tilaukset" → "Katso tilaustiedot") or
- [k-ruoka.fi](https://www.k-ruoka.fi) (profile image → "Tilaukset" → expand the latest order).
Expand All @@ -30,15 +25,9 @@
- Select a cash receipt you want to view. Select needed rows by painting from the first product row to the total row. Copy them.
- Open a text editor (e.g. Notepad or TextEdit) and paste copied cash receipt rows. Save the file as a plain text file (.txt).

### Default usage
### Basic usage

The basic usage looks like this (an assumption is that we're in the project folder):

```
dart run bin/dart_kassakuitti_cli.dart run -t [a path to the cash receipt file] -h [a path to the EAN file] -s [S-kaupat or K-ruoka] -c [a path to generated CSV files]
```

Or, by using the [alias](#alias) (works anywhere in your local environment):
The basic usage looks like this:

```
kassakuitti run -t [a path to the cash receipt file] -h [a path to the EAN file] -s [S-kaupat or K-ruoka] -c [a path to generated CSV files]
Expand All @@ -47,27 +36,21 @@ kassakuitti run -t [a path to the cash receipt file] -h [a path to the EAN file]
You can define
- a cash receipt (`-t` = text file),
- an EAN products file (`-h` = html file),
- (optional: which food online store to use (`-s` = store)) and
- optionally: which food online store to use (`-s` = store) and
- where to save the output files (`-c` = CSV file).

S-kaupat is a default choice for the food online store selection (`-s`).

#### An example

```
kassakuitti run -t /Users/username/Downloads/cash_receipt.txt -h /Users/username/Downloads/https___www.s-kaupat.fi_tilaus_product_id-generating_time.html -s S-kaupat -c /Users/username/Downloads
kassakuitti run -t /Users/username/Downloads/cash_receipt.txt -h /Users/username/Downloads/https___www.s-kaupat.fi_tilaus_product_id-generating_time.html -s S-kaupat -c ~/Downloads
```

### Help

If you want to get all the available commands, you can just type:

```
dart run bin/dart_kassakuitti_cli.dart help
```

Or, by using the [alias](#alias):

```
kassakuitti help
```
Expand All @@ -77,27 +60,36 @@ kassakuitti help
If you want some basic information about this program (e.g. the description, the version number or the project homepage), just type:

```
dart run bin/dart_kassakuitti_cli.dart
kassakuitti
```

Or, by using the [alias](#alias):
The CLI gives also the same result when typing anything that's not recognized by the program, e.g.:

```
kassakuitti
kassakuitti moro
```

## Alias
(For non-Finnish speakers: "moro" means hello in Tampere, Finland.)

### Hive handling

If you want to get an easier command, you can create an alias into Zsh or Bash profile file (e.g. `~/.zshrc` when using Zsh and `~/.bashrc` when using Bash). This line adds an alias `kassakuitti` into the profile file (let's assume that this `dart_kassakuitti_cli` project locates under your Documents folder):
You can handle a local Hive database that the program is using by typing:

```
alias kassakuitti='dart run $HOME/Documents/dart_kassakuitti_cli/bin/dart_kassakuitti_cli.dart'
kassakuitti hive
```

## Generate `hive_product.g.dart` file
This starts an own interface for handling the following alternatives:

You can generate `hive_product.g.dart` file by running:
1. Create a receipt name – EAN name product.
2. Read all products (gives index numbers for each product).
3. Search a product by a keyword (gives index numbers for each product).
4. Update a product by an index number.
5. Delete a product by an index number.
6. Count products.

```
dart run build_runner build
```
Each product in the Hive database has an own index number. If a product gets deleted, the next new product won't get the same index number that the previously deleted product had but a next unused index number.

## Participate to developing

See a brief instruction for developing is in [its own page](https://github.com/areee/dart_kassakuitti_cli/blob/main/DEVELOPING.md).
1 change: 1 addition & 0 deletions bin/constants.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const kHiveBoxName = 'hiveProducts';
78 changes: 30 additions & 48 deletions bin/dart_kassakuitti_cli.dart
Original file line number Diff line number Diff line change
@@ -1,72 +1,54 @@
import 'dart:io';

import 'package:args/args.dart';
import 'read_ean_products.dart';
import 'ean_products_2_csv.dart';
import 'specific/s_kaupat/ean_handler.dart';
import 'specific/s_kaupat/read_receipt_products.dart';
import 'specific/s_kaupat/receipt_products_2_csv.dart';
import 'package:hive/hive.dart';

import 'constants.dart';
import 'hive_handling.dart';
import 'models/hive_product.dart';
import 'run_main_program.dart';
import 'utils/arg_selector_helper.dart';
import 'utils/parse_kassakuitti_arguments.dart';
import 'utils/printing_helper.dart';
import 'utils/shop_selector_helper.dart';

void main(List<String> arguments) async {
exitCode = 0; // presume success
var hiveProducts = await _initializeHiveProducts();

var parser = getParser();
var argResults = parser.parse(arguments);

await handleArgCommands(argResults, parser);
await _handleArgCommands(argResults, parser, hiveProducts);

hiveProducts.close();
}

/// Handles the commands in the arguments.
Future<void> handleArgCommands(ArgResults argResults, ArgParser parser) async {
Future<Box<HiveProduct>> _handleArgCommands(ArgResults argResults,
ArgParser parser, Box<HiveProduct> hiveProducts) async {
// Run command
if (argResults.command?.name == ArgSelector.run.value) {
await runMainProgram(argResults, hiveProducts);
}
// Help command
if (argResults.command?.name == ArgSelector.help.value) {
print('Help:\n${parser.usage}');
else if (argResults.command?.name == ArgSelector.help.value) {
printHelpers(parser);
}
// Run command
else if (argResults.command?.name == ArgSelector.run.value) {
print('\nRunning...\n');

var selectedTextFile = argResults[ArgSelector.textFile.value] as String?;
var selectedHtmlFile = argResults[ArgSelector.htmlFile.value] as String;
var selectedStore = argResults[ArgSelector.foodOnlineStore.value] as String;
var csvFilesPath = argResults[ArgSelector.csvPath.value] as String;

printSelectedValues(
selectedTextFile, selectedHtmlFile, selectedStore, csvFilesPath);

try {
if (ShopSelector.sKaupat.value == selectedStore) {
var receiptProducts =
await readReceiptProducts(selectedTextFile!, csvFilesPath);
var eanProducts = await readEANProducts(
selectedHtmlFile, ShopSelector.sKaupat, csvFilesPath);

await eanHandler(receiptProducts, eanProducts.toList());

receiptProducts2CSV(receiptProducts, csvFilesPath);
eanProducts2CSV(eanProducts, csvFilesPath, ShopSelector.sKaupat.name);
} else if (ShopSelector.kRuoka.value == selectedStore) {
var eanProducts = await readEANProducts(
selectedHtmlFile, ShopSelector.kRuoka, csvFilesPath);

eanProducts2CSV(eanProducts, csvFilesPath, ShopSelector.kRuoka.name);
} else {
print('Unknown store: $selectedStore');
exitCode = 1;
}
} on Exception catch (e) {
print('Error: $e');
exitCode = 1;
}

print('\nDone!');
// Hive (storage) command
else if (argResults.command?.name == ArgSelector.hive.value) {
await hiveHandling(hiveProducts);
}
// Empty command (or other commands, e.g. 'moro' / 'hello')
else {
await printBasicInfo(parser);
}

return hiveProducts;
}

/// Initializes the Hive products.
Future<Box<HiveProduct>> _initializeHiveProducts() async {
Hive.init(Directory.current.path);
Hive.registerAdapter(HiveProductAdapter());
return await Hive.openBox<HiveProduct>(kHiveBoxName);
}
Loading

0 comments on commit c71e07e

Please sign in to comment.