Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/src/command/create_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import '../structure_creators/file/impl_file_creator.dart';
import 'i_command.dart';

class CreateCommand implements ICommand {
final ICommand? nextCommand;

CreateCommand({this.nextCommand});

@override
Future<void> execute() async {
final directoryCreator = ImplDirectoryCreator();
Expand All @@ -14,6 +18,8 @@ class CreateCommand implements ICommand {
fileCreator: fileCreator,
);

// await nextCommand?.execute();

return kanzaCreator.create();
}
}
6 changes: 6 additions & 0 deletions lib/src/command/help_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ class HelpCommand implements ICommand {
void execute() {
stdout.writeln('Usage: kanza_cli <command>\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',
Expand Down
12 changes: 12 additions & 0 deletions lib/src/command/pub_updater_command.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import 'dart:async';
import 'i_command.dart';

import '../structure_creators/pubspec/impl_pubspec_creator.dart';

class PubUpdaterCommand implements ICommand {
@override
Future<void> execute() {
ImplPubspecCreator pubspecCreator = ImplPubspecCreator();
return pubspecCreator.getPackageVersion();
}
}
6 changes: 6 additions & 0 deletions lib/src/constants/constants_data.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const List<String> kPubPackageList = [
'shared_preferences',
'flutter_bloc',
'intl',
'injectable',
];
26 changes: 21 additions & 5 deletions lib/src/kanza_command_runner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,41 @@ 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<String> arguments) {
final argParser = ArgParser();

argParser.addCommand('create');
argParser.addCommand('help');
argParser.addFlag('get-packages');

final res = argParser.parse(arguments);
final ArgResults argResult;

if (res.command != null && res.command!.name != null) {
try {
argResult = argParser.parse(arguments);
} catch (_) {
stderr.writeln('No command or flag available!\n');
HelpCommand().execute();
exit(2);
}

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) {
command = CreateCommand();
bool isNeedPubUpdate = argResult['get-packages'];

print('isNeedPubUpdate: $isNeedPubUpdate');

command = CreateCommand(
nextCommand: isNeedPubUpdate ? PubUpdaterCommand() : null,
);
} else {
exit(0);
}
Expand All @@ -32,7 +48,7 @@ class KanzaCommandRunner {
command = HelpCommand();
break;
default:
_errorAndExit(res.command!.name);
_errorAndExit(argResult.command!.name);
}

command!.execute();
Expand Down
27 changes: 27 additions & 0 deletions lib/src/model/package_detail.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class PackageDetail {
late String name;
late Latest latest;

PackageDetail.fromJson(Map<String, dynamic> json) {
name = json['name'];
latest = Latest.fromJson(json['latest']);
}

@override
String toString() {
return '$name: ${latest.version}';
}
}

class Latest {
late String version;

Latest.fromJson(Map<String, dynamic> json) {
version = json['version'];
}

@override
String toString() {
return version;
}
}
44 changes: 44 additions & 0 deletions lib/src/services/package_detail_service.dart
Original file line number Diff line number Diff line change
@@ -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<PackageDetail?> 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;
}
}
}
4 changes: 4 additions & 0 deletions lib/src/structure_creators/i_creators.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,7 @@ abstract class IDirectoryCreator {
abstract class IFileCreator {
Future<void> createNecessaryFiles();
}

abstract class IPubspecCreator {
Future<void> getPackageVersion();
}
21 changes: 21 additions & 0 deletions lib/src/structure_creators/pubspec/impl_pubspec_creator.dart
Original file line number Diff line number Diff line change
@@ -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<void> getPackageVersion() async {
List<String> solvedPackageList = [];
for (String package in kPubPackageList) {
PackageDetail? data = await PackageDetailService().getPackageDetail(
packageName: package,
);

if (data != null) {
solvedPackageList.add(data.toString());
print(data.toString());
}
}
}
}
25 changes: 25 additions & 0 deletions lib/src/utils/yaml_map_converter.dart
Original file line number Diff line number Diff line change
@@ -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 = <dynamic>[];
for (var e in v) {
list.add(_convertNode(e));
}
return list;
} else {
return v;
}
}

Map<String, dynamic> toMap() {
var map = <String, dynamic>{};
nodes.forEach((k, v) {
map[(k as YamlScalar).value.toString()] = _convertNode(v.value);
});
return map;
}
}
65 changes: 65 additions & 0 deletions lib/src/utils/yaml_writer.dart
Original file line number Diff line number Diff line change
@@ -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, ' ');
}
}
84 changes: 84 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,96 @@ 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:
name: lints
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"
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"
Loading