Skip to content

Commit

Permalink
Enhancement 14 (#17)
Browse files Browse the repository at this point in the history
* Update dart sdk version & lint version

* Mark receipt 'kampanja' row as discount counted

* Update version number (0.13.0 -> 0.14.0)

* Update Dart SDK version (2.17.0 -> 2.17.1)

* Change the column order of EAN products CSV

* Create an own method for arg commands handling

* Print help also with empty command

* Update ArgSelector enum to use new Dart 2.17 approach

* Update ShopSelector enum to use new Dart 2.17 approach

* Update a comment in strings2ReceiptProducts method
  • Loading branch information
areee authored May 22, 2022
1 parent def2587 commit cce9834
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 82 deletions.
33 changes: 21 additions & 12 deletions bin/dart_kassakuitti_cli.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
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';
Expand All @@ -16,23 +17,29 @@ void main(List<String> arguments) async {
var parser = getParser();
var argResults = parser.parse(arguments);

if (argResults.command?.name == ArgSelector.help.value!) {
await handleArgCommands(argResults, parser);
}

/// Handles the commands in the arguments.
Future<void> handleArgCommands(ArgResults argResults, ArgParser parser) async {
// Help command
if (argResults.command?.name == ArgSelector.help.value) {
print('Help:\n${parser.usage}');
return;
} else if (argResults.command?.name == ArgSelector.run.value!) {
}
// 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;
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.isEqual(selectedStore)) {
if (ShopSelector.sKaupat.value == selectedStore) {
var receiptProducts =
await readReceiptProducts(selectedTextFile!, csvFilesPath);
var eanProducts = await readEANProducts(
Expand All @@ -42,7 +49,7 @@ void main(List<String> arguments) async {

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

Expand All @@ -57,7 +64,9 @@ void main(List<String> arguments) async {
}

print('\nDone!');
} else {
await printBasicInfo();
}
// Empty command (or other commands, e.g. 'moro' / 'hello')
else {
await printBasicInfo(parser);
}
}
4 changes: 2 additions & 2 deletions bin/ean_products_2_csv.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ void eanProducts2CSV(
List<EANProduct> eanProductList, String csvFilePath, String shopSelector) {
var csv = StringBuffer();

csv.write('EAN code;Name;Quantity;Total price;Price per unit;More details\n');
csv.write('Name;Quantity;Price per unit;Total price;EAN code;More details\n');

for (var item in eanProductList) {
csv.write(
'${item.ean};${item.name};${item.quantity};${item.totalPrice};${item.pricePerUnit};${item.moreDetails}\n');
'${item.name};${item.quantity};${item.pricePerUnit};${item.totalPrice};${item.ean};${item.moreDetails}\n');
}

var file = File(
Expand Down
4 changes: 3 additions & 1 deletion bin/specific/s_kaupat/strings_to_receipt_products.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ List<ReceiptProduct> strings2ReceiptProducts(List<String> rows) {
}
/*
A campaign row
(i.e. usually means that there's a mistake in the previous line):
(i.e. usually means that there's a mistake in the previous row
BUT not always -> let's assume that it's a discount row):
*/
else if (item.contains('kampanja')) {
var items = item.split(RegExp(r'\s{12,33}'));
Expand All @@ -81,6 +82,7 @@ List<ReceiptProduct> strings2ReceiptProducts(List<String> rows) {
.replaceAll(RegExp(r'\.'), ',');

lastProduct.pricePerUnit = fixedPricePerUnit;
lastProduct.discountCounted = 'yes';
}

lastProduct.totalPrice = fixedPriceAsString;
Expand Down
36 changes: 10 additions & 26 deletions bin/utils/arg_selector_helper.dart
Original file line number Diff line number Diff line change
@@ -1,30 +1,14 @@
/// ArgSelector (textFile, htmlFile, foodOnlineStore, csvPath, help)
/// ArgSelector (textFile, htmlFile, foodOnlineStore, csvPath, help, run)
enum ArgSelector {
textFile,
htmlFile,
foodOnlineStore,
csvPath,
help,
run,
}

extension ArgSelectorExtension on ArgSelector {
static const values = {
ArgSelector.textFile: 'text',
ArgSelector.htmlFile: 'html',
ArgSelector.foodOnlineStore: 'store',
ArgSelector.csvPath: 'csv',
ArgSelector.help: 'help',
ArgSelector.run: 'run',
};
textFile('text'),
htmlFile('html'),
foodOnlineStore('store'),
csvPath('csv'),
help('help'),
run('run');

String? get value => values[this];
final String term;
const ArgSelector(this.term);

bool isEqual(dynamic value) {
if (value is String) {
return toString() == value || this.value == value;
} else {
return false;
}
}
String get value => term;
}
22 changes: 11 additions & 11 deletions bin/utils/parse_kassakuitti_arguments.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,30 @@ import 'shop_selector_helper.dart';

ArgParser getParser() {
final parser = ArgParser()
..addCommand(ArgSelector.run.value!)
..addCommand(ArgSelector.run.value)
..addOption(
ArgSelector.textFile.value!,
abbr: ArgSelector.textFile.value!.substring(0, 1),
ArgSelector.textFile.value,
abbr: ArgSelector.textFile.value.substring(0, 1),
help: 'Text file (cash receipt) to read',
)
..addOption(
ArgSelector.htmlFile.value!,
abbr: ArgSelector.htmlFile.value!.substring(0, 1),
ArgSelector.htmlFile.value,
abbr: ArgSelector.htmlFile.value.substring(0, 1),
help: 'HTML (EAN products) file to read',
)
..addOption(
ArgSelector.foodOnlineStore.value!,
abbr: ArgSelector.foodOnlineStore.value!.substring(0, 1),
ArgSelector.foodOnlineStore.value,
abbr: ArgSelector.foodOnlineStore.value.substring(0, 1),
help: 'Food online store',
defaultsTo: ShopSelector.sKaupat.value,
allowed: [ShopSelector.sKaupat.value!, ShopSelector.kRuoka.value!],
allowed: [ShopSelector.sKaupat.value, ShopSelector.kRuoka.value],
)
..addOption(
ArgSelector.csvPath.value!,
abbr: ArgSelector.csvPath.value!.substring(0, 1),
ArgSelector.csvPath.value,
abbr: ArgSelector.csvPath.value.substring(0, 1),
help: 'Path for output CSV files',
)
..addCommand(ArgSelector.help.value!);
..addCommand(ArgSelector.help.value);

return parser;
}
11 changes: 3 additions & 8 deletions bin/utils/printing_helper.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:io';

import 'package:args/args.dart';
import 'package:path/path.dart';
import 'package:yaml/yaml.dart';

Expand All @@ -12,7 +13,7 @@ void printSelectedValues(String? selectedTextFile, String selectedHtmlFile,
'\n- Path where to save CSV files:\t\t$csvFilesPath\n');
}

Future<void> printBasicInfo() async {
Future<void> printBasicInfo(ArgParser parser) async {
String pathToYaml =
join(dirname(Platform.script.toFilePath()), '../pubspec.yaml');
var file = File(pathToYaml);
Expand All @@ -24,11 +25,5 @@ Future<void> printBasicInfo() async {
print('Version: ${yaml['version']}');
print('Homepage: ${yaml['homepage']}');

print('''\nTo get help, run:
dart run bin/dart_kassakuitti_cli.dart help
or when using alias:
kassakuitti help\n''');
print('\nHelp:\n${parser.usage}');
}
22 changes: 5 additions & 17 deletions bin/utils/shop_selector_helper.dart
Original file line number Diff line number Diff line change
@@ -1,22 +1,10 @@
/// Shop selector (sKaupat, kRuoka)
enum ShopSelector {
sKaupat,
kRuoka,
}

extension ShopSelectorExtension on ShopSelector {
static const values = {
ShopSelector.sKaupat: 'S-kaupat',
ShopSelector.kRuoka: 'K-ruoka',
};
sKaupat('S-kaupat'),
kRuoka('K-ruoka');

String? get value => values[this];
final String term;
const ShopSelector(this.term);

bool isEqual(dynamic value) {
if (value is String) {
return toString() == value || this.value == value;
} else {
return false;
}
}
String get value => term;
}
4 changes: 2 additions & 2 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ packages:
name: lints
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.1"
version: "2.0.0"
logging:
dependency: transitive
description:
Expand Down Expand Up @@ -429,4 +429,4 @@ packages:
source: hosted
version: "3.1.0"
sdks:
dart: ">=2.16.2 <3.0.0"
dart: ">=2.17.1 <3.0.0"
6 changes: 3 additions & 3 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
name: dart_kassakuitti_cli
description: A simple Dart CLI app to handle a cash receipt coming from S-kaupat or K-ruoka (two Finnish food online stores).
version: 0.13.0
version: 0.14.0
homepage: https://github.com/areee/dart_kassakuitti_cli

environment:
sdk: '>=2.16.2 <3.0.0'
sdk: '>=2.17.1 <3.0.0'


# dependencies:
Expand All @@ -13,7 +13,7 @@ environment:
dev_dependencies:
build_runner: ^2.1.10
hive_generator: ^1.1.2
lints: ^1.0.1
lints: ^2.0.0
dependencies:
ansicolor: ^2.0.1
args: ^2.3.0
Expand Down

0 comments on commit cce9834

Please sign in to comment.