diff --git a/sidekick/cli_template/bricks/package/__brick__/lib/src/commands/clean_command.dart b/sidekick/cli_template/bricks/package/__brick__/lib/src/commands/clean_command.dart new file mode 100644 index 00000000..91d5bbc1 --- /dev/null +++ b/sidekick/cli_template/bricks/package/__brick__/lib/src/commands/clean_command.dart @@ -0,0 +1,17 @@ +import 'package:sidekick_core/sidekick_core.dart'; + +class CleanCommand extends Command { + @override + final String description = 'Cleans the project'; + + @override + final String name = 'clean'; + + @override + Future run() async { + {{#hasMainProject}}flutter(['clean'], workingDirectory: mainProject?.root);{{/hasMainProject}} + // TODO Please add your own project clean logic here + + print('✔️Cleaned project'); + } +} diff --git a/sidekick/cli_template/bricks/package/__brick__/lib/src/commands/deps_command.dart b/sidekick/cli_template/bricks/package/__brick__/lib/src/commands/deps_command.dart deleted file mode 100644 index 2fa221bc..00000000 --- a/sidekick/cli_template/bricks/package/__brick__/lib/src/commands/deps_command.dart +++ /dev/null @@ -1,55 +0,0 @@ -import 'package:{{#lowerCase}}{{name}}{{/lowerCase}}_sidekick/{{#lowerCase}}{{name}}{{/lowerCase}}_sidekick.dart'; -import 'package:sidekick_core/sidekick_core.dart'; - -class DepsCommand extends Command { - @override - final String description = 'Gets dependencies for all packages'; - - @override - final String name = 'deps'; - - DepsCommand() { - argParser.addOption( - 'package', - abbr: 'p', - allowed: {{#lowerCase}}{{name}}{{/lowerCase}}Project.allPackages.map((it) => it.name), - ); - } - - @override - Future run() async { - final String? packageArg = argResults?['package'] as String?; - - if (packageArg != null) { - // only get deps for selected package - _getDependenciesForPackageWithName(packageArg); - return; - } - - // fallback to all packages - for (final package in {{#lowerCase}}{{name}}{{/lowerCase}}Project.allPackages) { - _getDependencies(package); - } - } - - void _getDependenciesForPackageWithName(String name) { - // only get deps for selected package - final package = {{#lowerCase}}{{name}}{{/lowerCase}}Project.allPackages.firstOrNullWhere((it) => it.name == name); - if (package == null) { - final packageOptions = {{#lowerCase}}{{name}}{{/lowerCase}}Project.allPackages.map((it) => it.name).toList(growable: false); - error('Could not find package $name. ' - 'Please use one of ${packageOptions.joinToString()}'); - } - _getDependencies(package); - } - - void _getDependencies(DartPackage package) { - print(yellow('=== package ${package.name} ===')); - if (package.isFlutterPackage) { - flutterw(['packages', 'get'], workingDirectory: package.root); - } else { - dart(['pub', 'get'], workingDirectory: package.root); - } - print("\n"); - } -} diff --git a/sidekick/cli_template/bricks/package/__brick__/lib/{{#lowerCase}}{{name}}{{/lowerCase}}_sidekick.dart b/sidekick/cli_template/bricks/package/__brick__/lib/{{#lowerCase}}{{name}}{{/lowerCase}}_sidekick.dart index 6211aa96..aff4aaba 100644 --- a/sidekick/cli_template/bricks/package/__brick__/lib/{{#lowerCase}}{{name}}{{/lowerCase}}_sidekick.dart +++ b/sidekick/cli_template/bricks/package/__brick__/lib/{{#lowerCase}}{{name}}{{/lowerCase}}_sidekick.dart @@ -1,6 +1,6 @@ import 'dart:async'; -import 'package:{{#lowerCase}}{{name}}{{/lowerCase}}_sidekick/src/commands/deps_command.dart'; +import 'package:{{#lowerCase}}{{name}}{{/lowerCase}}_sidekick/src/commands/clean_command.dart'; import 'package:{{#lowerCase}}{{name}}{{/lowerCase}}_sidekick/src/{{#lowerCase}}{{name}}{{/lowerCase}}_project.dart'; import 'package:sidekick_core/sidekick_core.dart'; @@ -19,6 +19,7 @@ Future run{{#titleCase}}{{name}}{{/titleCase}}(List args) async { ..addCommand(FlutterCommand()) ..addCommand(DartCommand()) ..addCommand(DepsCommand()) + ..addCommand(CleanCommand()) ..addCommand(PluginsCommand()) ..addCommand(InstallGlobalCommand()) ..addCommand(DartAnalyzeCommand()); diff --git a/sidekick/lib/src/templates/package_bundle.g.dart b/sidekick/lib/src/templates/package_bundle.g.dart index 059dde8a..81954c99 100644 --- a/sidekick/lib/src/templates/package_bundle.g.dart +++ b/sidekick/lib/src/templates/package_bundle.g.dart @@ -24,9 +24,9 @@ final packageBundle = MasonBundle.fromJson({ "type": "text" }, { - "path": "lib/src/commands/deps_command.dart", + "path": "lib/src/comm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mdEaXJlY3Rvcnk6IHBhY2thZ2Uucm9vdCk7CiAgICB9IGVsc2UgewogICAgICBkYXJ0KFsncHViJywgJ2dldCddLCB3b3JraW5nRGlyZWN0b3J5OiBwYWNrYWdlLnJvb3QpOwogICAgfQogICAgcHJpbnQoIlxuIik7CiAgfQp9Cg==", + "aW1wb3J0ICdwYWNrYWdlOnNpZGVraWNrX2NvcmUvc2lkZWtpY2tfY29yZS5kYXJ0JzsKCmNsYXNzIENsZWFuQ29tbWFuZCBleHRlbmRzIENvbW1hbmQgewogIEBvdmVycmlkZQogIGZpbmFsIFN0cmluZyBkZXNjcmlwdGlvbiA9ICdDbGVhbnMgdGhlIHByb2plY3QnOwoKICBAb3ZlcnJpZGUKICBmaW5hbCBTdHJpbmcgbmFtZSA9ICdjbGVhbic7CgogIEBvdmVycmlkZQogIEZ1dHVyZTx2b2lkPiBydW4oKSBhc3luYyB7CiAgICB7eyNoYXNNYWluUHJvamVjdH19Zmx1dHRlcihbJ2NsZWFuJ10sIHdvcmtpbmdEaXJlY3Rvcnk6IG1haW5Qcm9qZWN0Py5yb290KTt7ey9oYXNNYWluUHJvamVjdH19CiAgICAvLyBUT0RPIFBsZWFzZSBhZGQgeW91ciBvd24gcHJvamVjdCBjbGVhbiBsb2dpYyBoZXJlCgogICAgcHJpbnQoJ+KclO+4j0NsZWFuZWQgcHJvamVjdCcpOwogIH0KfQo=", "type": "text" }, { @@ -38,7 +38,7 @@ final packageBundle = MasonBundle.fromJson({ { "path": "lib/{{#lowerCase}}{{name}}{{/lowerCase}}_sidekick.dart", "data": - "aW1wb3J0ICdkYXJ0OmFzeW5jJzsKCmltcG9ydCAncGFja2FnZTp7eyNsb3dlckNhc2V9fXt7bmFtZX19e3svbG93ZXJDYXNlfX1fc2lkZWtpY2svc3JjL2NvbW1hbmRzL2RlcHNfY29tbWFuZC5kYXJ0JzsKaW1wb3J0ICdwYWNrYWdlOnt7I2xvd2VyQ2FzZX19e3tuYW1lfX17ey9sb3dlckNhc2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bXB0eSkgewogICAgcHJpbnQocnVubmVyLnVzYWdlKTsKICAgIHJldHVybjsKICB9CgogIHRyeSB7CiAgICByZXR1cm4gYXdhaXQgcnVubmVyLnJ1bihhcmdzKTsKICB9IG9uIFVzYWdlRXhjZXB0aW9uIGNhdGNoIChlKSB7CiAgICBwcmludChlLnVzYWdlKTsKICAgIGV4aXQoNjQpOyAvLyB1c2FnZSBlcnJvcgogIH0KfQo=", "type": "text" }, { diff --git a/sidekick_core/lib/sidekick_core.dart b/sidekick_core/lib/sidekick_core.dart index 6103a7b5..48d7fa3d 100644 --- a/sidekick_core/lib/sidekick_core.dart +++ b/sidekick_core/lib/sidekick_core.dart @@ -18,6 +18,7 @@ export 'package:dcli/dcli.dart' hide run, start, startFromArgs, absolute; export 'package:sidekick_core/src/cli_util.dart'; export 'package:sidekick_core/src/commands/analyze_command.dart'; export 'package:sidekick_core/src/commands/dart_command.dart'; +export 'package:sidekick_core/src/commands/deps_command.dart'; export 'package:sidekick_core/src/commands/flutter_command.dart'; export 'package:sidekick_core/src/commands/install_global_command.dart'; export 'package:sidekick_core/src/commands/plugins/plugins_command.dart'; diff --git a/sidekick_core/lib/src/commands/deps_command.dart b/sidekick_core/lib/src/commands/deps_command.dart new file mode 100644 index 00000000..981bd2e3 --- /dev/null +++ b/sidekick_core/lib/src/commands/deps_command.dart @@ -0,0 +1,70 @@ +import 'package:sidekick_core/sidekick_core.dart'; + +/// Downloads dependencies of all Flutter/Dart packages in the repository +class DepsCommand extends Command { + @override + final String description = 'Gets dependencies for all packages'; + + @override + final String name = 'deps'; + + /// packages whose dependencies should not be loaded + final List exclude; + + DepsCommand({this.exclude = const []}) { + argParser.addOption( + 'package', + abbr: 'p', + ); + } + + @override + Future run() async { + final String? packageName = argResults?['package'] as String?; + + final List allPackages = repository.findAllPackages(); + + if (packageName != null) { + final package = + allPackages.where((it) => it.name == packageName).firstOrNull; + if (package == null) { + throw "Package with name $packageName not found in repository " + "${repository.root.path}"; + } + // only get deps for selected package + _getDependencies(package); + return; + } + + final errorBuffer = StringBuffer(); + for (final package in allPackages) { + try { + _getDependencies(package); + } catch (e, stack) { + print('Error while getting dependencies for ${package.name} ' + '(${package.root.path})'); + errorBuffer.writeln("${package.name}: $e\n$stack"); + } + } + final errorText = errorBuffer.toString(); + if (errorText.isNotEmpty) { + printerr("\n\nErrors while getting dependencies:"); + printerr(errorText); + exitCode = 1; + } + } + + void _getDependencies(DartPackage package) { + print(yellow('=== package ${package.name} ===')); + final int exitCode; + if (package.isFlutterPackage) { + exitCode = flutter(['packages', 'get'], workingDirectory: package.root); + } else { + exitCode = dart(['pub', 'get'], workingDirectory: package.root); + } + if (exitCode != 0) { + throw "Failed to get dependencies for package ${package.root.path}"; + } + print("\n"); + } +} diff --git a/sidekick_core/lib/src/repository.dart b/sidekick_core/lib/src/repository.dart index e461b0cc..afc7aa4d 100644 --- a/sidekick_core/lib/src/repository.dart +++ b/sidekick_core/lib/src/repository.dart @@ -115,6 +115,27 @@ class Repository { } return file; } + + /// Returns the list of all packages in the repository + List findAllPackages() { + return repository.root + .allSubDirectories((dir) { + if (dir.name.startsWith('.')) { + // ignore hidden folders + return false; + } + if (dir.name == 'build') { + final package = DartPackage.fromDirectory(dir.parent); + if (package != null) { + // ignore /build dir + return false; + } + } + return true; + }) + .mapNotNull((it) => DartPackage.fromDirectory(it)) + .toList(); + } } extension FindInDirectory on Directory { @@ -136,4 +157,13 @@ extension FindInDirectory on Directory { dir = dir.parent; } } + + Iterable allSubDirectories( + bool Function(Directory dir) predicate, + ) sync* { + yield this; + for (final dir in listSync().whereType().where(predicate)) { + yield* dir.allSubDirectories(predicate); + } + } }