From 58d689ed7c89aecd26e812d5aefe8fe471b56b84 Mon Sep 17 00:00:00 2001 From: Nijat Date: Fri, 22 Oct 2021 19:03:15 +0400 Subject: [PATCH 1/3] pub_updater_command and --get-packages flag added --- lib/src/command/create_command.dart | 6 ++++++ lib/src/command/help_command.dart | 6 ++++++ lib/src/command/pub_updater_command.dart | 12 ++++++++++++ lib/src/kanza_command_runner.dart | 24 +++++++++++++++++++++--- 4 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 lib/src/command/pub_updater_command.dart diff --git a/lib/src/command/create_command.dart b/lib/src/command/create_command.dart index ad11f7a..d793a23 100644 --- a/lib/src/command/create_command.dart +++ b/lib/src/command/create_command.dart @@ -4,6 +4,12 @@ import '../structure_creators/file/impl_file_creator.dart'; import 'i_command.dart'; class CreateCommand implements ICommand { + final ICommand? nextCommand; + + CreateCommand({this.nextCommand}); + + + @override Future execute() async { final directoryCreator = ImplDirectoryCreator(); diff --git a/lib/src/command/help_command.dart b/lib/src/command/help_command.dart index 023a032..d7aa490 100644 --- a/lib/src/command/help_command.dart +++ b/lib/src/command/help_command.dart @@ -7,6 +7,12 @@ class HelpCommand implements ICommand { void execute() { stdout.writeln('Usage: kanza_cli \n'); + stdout.writeln('Global options:'); + stdout.writeln( + '--get-packages Get some packages with latest releases ' + 'from pub.dev\n', + ); + stdout.writeln('Available commands:'); stdout.writeln( 'create Create folder and file structure for Fluter Apps', diff --git a/lib/src/command/pub_updater_command.dart b/lib/src/command/pub_updater_command.dart new file mode 100644 index 0000000..f5964f6 --- /dev/null +++ b/lib/src/command/pub_updater_command.dart @@ -0,0 +1,12 @@ +import 'dart:async'; + +import 'i_command.dart'; + +class PubUpdaterCommand implements ICommand { + @override + FutureOr execute() { + // TODO: implement execute + throw UnimplementedError(); + } + +} \ No newline at end of file diff --git a/lib/src/kanza_command_runner.dart b/lib/src/kanza_command_runner.dart index 5226410..735696a 100644 --- a/lib/src/kanza_command_runner.dart +++ b/lib/src/kanza_command_runner.dart @@ -5,15 +5,28 @@ import 'package:args/args.dart'; import 'command/create_command.dart'; import 'command/help_command.dart'; import 'command/i_command.dart'; +import 'command/pub_updater_command.dart'; class KanzaCommandRunner { void run(List arguments) { final argParser = ArgParser(); - argParser.addCommand('create'); + var createCommand = ArgParser(); + + argParser.addCommand('create', createCommand); argParser.addCommand('help'); + createCommand.addFlag('get-packages'); + + final ArgResults res; - final res = argParser.parse(arguments); + try { + res = argParser.parse(arguments); + } catch(_) { + stderr.writeln('No command or flag available!\n'); + HelpCommand().execute(); + exit(2); + } + if (res.command != null && res.command!.name != null) { ICommand? command; @@ -23,7 +36,12 @@ class KanzaCommandRunner { final res = welcomeBoard(); if (res) { - command = CreateCommand(); + bool isNeedPubUpdate =argParser.commands.containsKey('get-packages'); + print('isNeedPubUpdate: $isNeedPubUpdate'); + + command = CreateCommand( + nextCommand: isNeedPubUpdate ? PubUpdaterCommand() : null, + ); } else { exit(0); } From 7bdcf8d8ffd94d06bb5f401bbaf9676dbcfba5e9 Mon Sep 17 00:00:00 2001 From: NijatTagizada Date: Fri, 22 Oct 2021 23:47:33 +0400 Subject: [PATCH 2/3] added pub.dev service, fix command flag --- lib/src/command/create_command.dart | 4 +- lib/src/command/pub_updater_command.dart | 12 +-- lib/src/constants/constants_data.dart | 6 ++ lib/src/kanza_command_runner.dart | 22 +++--- lib/src/model/package_detail.dart | 27 +++++++ lib/src/services/package_detail_service.dart | 44 +++++++++++ lib/src/structure_creators/i_creators.dart | 4 + .../pubspec/impl_pubspec_creator.dart | 21 +++++ pubspec.lock | 77 +++++++++++++++++++ pubspec.yaml | 1 + 10 files changed, 198 insertions(+), 20 deletions(-) create mode 100644 lib/src/constants/constants_data.dart create mode 100644 lib/src/model/package_detail.dart create mode 100644 lib/src/services/package_detail_service.dart create mode 100644 lib/src/structure_creators/pubspec/impl_pubspec_creator.dart diff --git a/lib/src/command/create_command.dart b/lib/src/command/create_command.dart index d793a23..87c4425 100644 --- a/lib/src/command/create_command.dart +++ b/lib/src/command/create_command.dart @@ -8,8 +8,6 @@ class CreateCommand implements ICommand { CreateCommand({this.nextCommand}); - - @override Future execute() async { final directoryCreator = ImplDirectoryCreator(); @@ -20,6 +18,8 @@ class CreateCommand implements ICommand { fileCreator: fileCreator, ); + // await nextCommand?.execute(); + return kanzaCreator.create(); } } diff --git a/lib/src/command/pub_updater_command.dart b/lib/src/command/pub_updater_command.dart index f5964f6..469035a 100644 --- a/lib/src/command/pub_updater_command.dart +++ b/lib/src/command/pub_updater_command.dart @@ -1,12 +1,12 @@ import 'dart:async'; - import 'i_command.dart'; +import '../structure_creators/pubspec/impl_pubspec_creator.dart'; + class PubUpdaterCommand implements ICommand { @override - FutureOr execute() { - // TODO: implement execute - throw UnimplementedError(); + Future execute() { + ImplPubspecCreator pubspecCreator = ImplPubspecCreator(); + return pubspecCreator.getPackageVersion(); } - -} \ No newline at end of file +} diff --git a/lib/src/constants/constants_data.dart b/lib/src/constants/constants_data.dart new file mode 100644 index 0000000..3213217 --- /dev/null +++ b/lib/src/constants/constants_data.dart @@ -0,0 +1,6 @@ +const List kPubPackageList = [ + 'shared_preferences', + 'flutter_bloc', + 'intl', + 'injectable', +]; diff --git a/lib/src/kanza_command_runner.dart b/lib/src/kanza_command_runner.dart index 735696a..0818553 100644 --- a/lib/src/kanza_command_runner.dart +++ b/lib/src/kanza_command_runner.dart @@ -11,32 +11,30 @@ class KanzaCommandRunner { void run(List arguments) { final argParser = ArgParser(); - var createCommand = ArgParser(); - - argParser.addCommand('create', createCommand); + argParser.addCommand('create'); argParser.addCommand('help'); - createCommand.addFlag('get-packages'); + argParser.addFlag('get-packages'); - final ArgResults res; + final ArgResults argResult; try { - res = argParser.parse(arguments); - } catch(_) { + argResult = argParser.parse(arguments); + } catch (_) { stderr.writeln('No command or flag available!\n'); HelpCommand().execute(); exit(2); } - - if (res.command != null && res.command!.name != null) { + if (argResult.command != null && argResult.command!.name != null) { ICommand? command; - switch (res.command!.name) { + switch (argResult.command!.name) { case 'create': final res = welcomeBoard(); if (res) { - bool isNeedPubUpdate =argParser.commands.containsKey('get-packages'); + bool isNeedPubUpdate = argResult['get-packages']; + print('isNeedPubUpdate: $isNeedPubUpdate'); command = CreateCommand( @@ -50,7 +48,7 @@ class KanzaCommandRunner { command = HelpCommand(); break; default: - _errorAndExit(res.command!.name); + _errorAndExit(argResult.command!.name); } command!.execute(); diff --git a/lib/src/model/package_detail.dart b/lib/src/model/package_detail.dart new file mode 100644 index 0000000..70d6b60 --- /dev/null +++ b/lib/src/model/package_detail.dart @@ -0,0 +1,27 @@ +class PackageDetail { + late String name; + late Latest latest; + + PackageDetail.fromJson(Map json) { + name = json['name']; + latest = Latest.fromJson(json['latest']); + } + + @override + String toString() { + return '$name: ${latest.version}'; + } +} + +class Latest { + late String version; + + Latest.fromJson(Map json) { + version = json['version']; + } + + @override + String toString() { + return version; + } +} diff --git a/lib/src/services/package_detail_service.dart b/lib/src/services/package_detail_service.dart new file mode 100644 index 0000000..b459784 --- /dev/null +++ b/lib/src/services/package_detail_service.dart @@ -0,0 +1,44 @@ +import 'dart:convert'; +import 'dart:io'; + +import 'package:http/http.dart' as http; + +import '../model/package_detail.dart'; + +class PackageDetailService { + final String _pubBaseUrl = 'https://pub.dev/api/'; + static PackageDetailService? _packageDetailService; + static http.Client? client; + + PackageDetailService._createInstance(); + + factory PackageDetailService() { + if (_packageDetailService == null) { + _packageDetailService = PackageDetailService._createInstance(); + client = http.Client(); + } + return _packageDetailService!; + } + + Future getPackageDetail({required String packageName}) async { + try { + http.Response response = await client!.get( + Uri.parse('${_pubBaseUrl}packages/$packageName'), + ); + + if (response.statusCode == 200) { + PackageDetail model = PackageDetail.fromJson( + json.decode(utf8.decode(response.bodyBytes)), + ); + + return model; + } else { + stderr.write('${response.statusCode}->${response.body}'); + return null; + } + } catch (e) { + stderr.write(e); + return null; + } + } +} diff --git a/lib/src/structure_creators/i_creators.dart b/lib/src/structure_creators/i_creators.dart index 5d619ea..5a71194 100644 --- a/lib/src/structure_creators/i_creators.dart +++ b/lib/src/structure_creators/i_creators.dart @@ -17,3 +17,7 @@ abstract class IDirectoryCreator { abstract class IFileCreator { Future createNecessaryFiles(); } + +abstract class IPubspecCreator { + Future getPackageVersion(); +} diff --git a/lib/src/structure_creators/pubspec/impl_pubspec_creator.dart b/lib/src/structure_creators/pubspec/impl_pubspec_creator.dart new file mode 100644 index 0000000..95e6552 --- /dev/null +++ b/lib/src/structure_creators/pubspec/impl_pubspec_creator.dart @@ -0,0 +1,21 @@ +import '../../constants/constants_data.dart'; +import '../../services/package_detail_service.dart'; +import '../../model/package_detail.dart'; +import '../i_creators.dart'; + +class ImplPubspecCreator implements IPubspecCreator { + @override + Future getPackageVersion() async { + List solvedPackageList = []; + for (String package in kPubPackageList) { + PackageDetail? data = await PackageDetailService().getPackageDetail( + packageName: package, + ); + + if (data != null) { + solvedPackageList.add(data.toString()); + print(data.toString()); + } + } + } +} diff --git a/pubspec.lock b/pubspec.lock index b6f9f29..77d0374 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -8,6 +8,41 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "2.3.0" + async: + dependency: transitive + description: + name: async + url: "https://pub.dartlang.org" + source: hosted + version: "2.8.2" + charcode: + dependency: transitive + description: + name: charcode + url: "https://pub.dartlang.org" + source: hosted + version: "1.3.1" + collection: + dependency: transitive + description: + name: collection + url: "https://pub.dartlang.org" + source: hosted + version: "1.15.0" + http: + dependency: "direct main" + description: + name: http + url: "https://pub.dartlang.org" + source: hosted + version: "0.13.4" + http_parser: + dependency: transitive + description: + name: http_parser + url: "https://pub.dartlang.org" + source: hosted + version: "4.0.0" lints: dependency: "direct dev" description: @@ -15,5 +50,47 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.0.1" + meta: + dependency: transitive + description: + name: meta + url: "https://pub.dartlang.org" + source: hosted + version: "1.7.0" + path: + dependency: transitive + description: + name: path + url: "https://pub.dartlang.org" + source: hosted + version: "1.8.0" + source_span: + dependency: transitive + description: + name: source_span + url: "https://pub.dartlang.org" + source: hosted + version: "1.8.1" + string_scanner: + dependency: transitive + description: + name: string_scanner + url: "https://pub.dartlang.org" + source: hosted + version: "1.1.0" + term_glyph: + dependency: transitive + description: + name: term_glyph + url: "https://pub.dartlang.org" + source: hosted + version: "1.2.0" + typed_data: + dependency: transitive + description: + name: typed_data + url: "https://pub.dartlang.org" + source: hosted + version: "1.3.0" sdks: dart: ">=2.14.2 <3.0.0" diff --git a/pubspec.yaml b/pubspec.yaml index de4e1e3..daa2234 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -9,6 +9,7 @@ environment: dependencies: args: ^2.3.0 + http: ^0.13.4 dev_dependencies: From b6d09c713d44f54c402d4e94e5ae190a58f84a82 Mon Sep 17 00:00:00 2001 From: NijatTagizada Date: Tue, 2 Nov 2021 01:05:14 +0400 Subject: [PATCH 3/3] added yaml_writer, yaml_map_converter --- lib/src/utils/yaml_map_converter.dart | 25 +++++++++++ lib/src/utils/yaml_writer.dart | 65 +++++++++++++++++++++++++++ pubspec.lock | 7 +++ pubspec.yaml | 1 + 4 files changed, 98 insertions(+) create mode 100644 lib/src/utils/yaml_map_converter.dart create mode 100644 lib/src/utils/yaml_writer.dart diff --git a/lib/src/utils/yaml_map_converter.dart b/lib/src/utils/yaml_map_converter.dart new file mode 100644 index 0000000..1f53289 --- /dev/null +++ b/lib/src/utils/yaml_map_converter.dart @@ -0,0 +1,25 @@ +import 'package:yaml/yaml.dart'; + +extension YamlMapConverter on YamlMap { + dynamic _convertNode(dynamic v) { + if (v is YamlMap) { + return (v).toMap(); + } else if (v is YamlList) { + var list = []; + for (var e in v) { + list.add(_convertNode(e)); + } + return list; + } else { + return v; + } + } + + Map toMap() { + var map = {}; + nodes.forEach((k, v) { + map[(k as YamlScalar).value.toString()] = _convertNode(v.value); + }); + return map; + } +} diff --git a/lib/src/utils/yaml_writer.dart b/lib/src/utils/yaml_writer.dart new file mode 100644 index 0000000..63dd7fc --- /dev/null +++ b/lib/src/utils/yaml_writer.dart @@ -0,0 +1,65 @@ +class YamlWriter { + /// The amount of spaces for each level. + final int spaces; + + /// Initialize the writer with the amount of [spaces] per level. + YamlWriter({ + this.spaces = 2, + }); + + /// Write a dart structure to a YAML string. [yaml] should be a [Map] or [List]. + String write(dynamic yaml) { + return _writeInternal(yaml).trim(); + } + + /// Write a dart structure to a YAML string. [yaml] should be a [Map] or [List]. + String _writeInternal(dynamic yaml, {int indent = 0}) { + String str = ''; + + if (yaml is List) { + str += _writeList(yaml, indent: indent); + } else if (yaml is Map) { + str += _writeMap(yaml, indent: indent); + } else if (yaml is String) { + str += "\"${yaml.replaceAll("\"", "\\\"")}\""; + } else { + str += yaml.toString(); + } + + return str; + } + + /// Write a list to a YAML string. + /// Pass the list in as [yaml] and indent it to the [indent] level. + String _writeList(List yaml, {int indent = 0}) { + String str = '\n'; + + for (var item in yaml) { + str += + "${_indent(indent)}- ${_writeInternal(item, indent: indent + 1)}\n"; + } + + return str; + } + + /// Write a map to a YAML string. + /// Pass the map in as [yaml] and indent it to the [indent] level. + String _writeMap(Map yaml, {int indent = 0}) { + String str = '\n'; + + for (var key in yaml.keys) { + var value = yaml[key]; + str += + "${_indent(indent)}${key.toString()}: ${_writeInternal(value, indent: indent + 1)}\n"; + } + + return str; + } + + /// Create an indented string for the level with the spaces config. + /// [indent] is the level of indent whereas [spaces] is the + /// amount of spaces that the string should be indented by. + String _indent(int indent) { + return ''.padLeft(indent * spaces, ' '); + } +} diff --git a/pubspec.lock b/pubspec.lock index 77d0374..8a0cddb 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -92,5 +92,12 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.3.0" + yaml: + dependency: "direct main" + description: + name: yaml + url: "https://pub.dartlang.org" + source: hosted + version: "3.1.0" sdks: dart: ">=2.14.2 <3.0.0" diff --git a/pubspec.yaml b/pubspec.yaml index daa2234..5fb20da 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -10,6 +10,7 @@ environment: dependencies: args: ^2.3.0 http: ^0.13.4 + yaml: ^3.1.0 dev_dependencies: