Skip to content

Commit

Permalink
Move Deps command to sidekick_core and CleanCommand to template (#85)
Browse files Browse the repository at this point in the history
* Add repository.findAllPackages()

* Add deps command to core

* Remove deps command from template

* Add CleanCommand

This is a great example how to create a custom command that should be extensible

* Ignore hidden folders when searching for packages

Otherwise it catches synthetic_package in .dart-tools/

* Exit with exitCode 1 if deps fails

* Ignore build directories
  • Loading branch information
passsy authored Oct 25, 2022
1 parent 3567457 commit d060f30
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 59 deletions.
Original file line number Diff line number Diff line change
@@ -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<void> run() async {
{{#hasMainProject}}flutter(['clean'], workingDirectory: mainProject?.root);{{/hasMainProject}}
// TODO Please add your own project clean logic here

print('✔️Cleaned project');
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -19,6 +19,7 @@ Future<void> run{{#titleCase}}{{name}}{{/titleCase}}(List<String> args) async {
..addCommand(FlutterCommand())
..addCommand(DartCommand())
..addCommand(DepsCommand())
..addCommand(CleanCommand())
..addCommand(PluginsCommand())
..addCommand(InstallGlobalCommand())
..addCommand(DartAnalyzeCommand());
Expand Down
6 changes: 3 additions & 3 deletions sidekick/lib/src/templates/package_bundle.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions sidekick_core/lib/sidekick_core.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
70 changes: 70 additions & 0 deletions sidekick_core/lib/src/commands/deps_command.dart
Original file line number Diff line number Diff line change
@@ -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<DartPackage> exclude;

DepsCommand({this.exclude = const []}) {
argParser.addOption(
'package',
abbr: 'p',
);
}

@override
Future<void> run() async {
final String? packageName = argResults?['package'] as String?;

final List<DartPackage> 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");
}
}
30 changes: 30 additions & 0 deletions sidekick_core/lib/src/repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,27 @@ class Repository {
}
return file;
}

/// Returns the list of all packages in the repository
List<DartPackage> 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 <dartPackage>/build dir
return false;
}
}
return true;
})
.mapNotNull((it) => DartPackage.fromDirectory(it))
.toList();
}
}

extension FindInDirectory on Directory {
Expand All @@ -136,4 +157,13 @@ extension FindInDirectory on Directory {
dir = dir.parent;
}
}

Iterable<Directory> allSubDirectories(
bool Function(Directory dir) predicate,
) sync* {
yield this;
for (final dir in listSync().whereType<Directory>().where(predicate)) {
yield* dir.allSubDirectories(predicate);
}
}
}

0 comments on commit d060f30

Please sign in to comment.