From 32f8b5bd2e8013da954dd0b2440853fe1e2e4510 Mon Sep 17 00:00:00 2001 From: nilsreichardt Date: Mon, 18 Sep 2023 12:28:15 +0200 Subject: [PATCH 1/2] Add `sz build_runner build` command --- .../src/build_runner_build_command.dart | 60 +++++++++++++++++++ .../commands/src/build_runner_command.dart | 18 ++++++ .../lib/src/common/src/package.dart | 6 ++ tools/sz_repo_cli/lib/src/main.dart | 5 +- .../test/common/build_utils_test.dart | 1 + 5 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 tools/sz_repo_cli/lib/src/commands/src/build_runner_build_command.dart create mode 100644 tools/sz_repo_cli/lib/src/commands/src/build_runner_command.dart diff --git a/tools/sz_repo_cli/lib/src/commands/src/build_runner_build_command.dart b/tools/sz_repo_cli/lib/src/commands/src/build_runner_build_command.dart new file mode 100644 index 000000000..93898fe5d --- /dev/null +++ b/tools/sz_repo_cli/lib/src/commands/src/build_runner_build_command.dart @@ -0,0 +1,60 @@ +// Copyright (c) 2022 Sharezone UG (haftungsbeschränkt) +// Licensed under the EUPL-1.2-or-later. +// +// You may obtain a copy of the Licence at: +// https://joinup.ec.europa.eu/software/page/eupl +// +// SPDX-License-Identifier: EUPL-1.2 + +import 'dart:async'; + +import 'package:sz_repo_cli/src/common/common.dart'; + +class BuildRunnerBuild extends ConcurrentCommand { + BuildRunnerBuild(SharezoneRepo repo) : super(repo) { + argParser.addFlag( + 'delete-conflicting-outputs', + help: + 'By default, the user will be prompted to delete any files which already exist but were not known to be generated by this specific build script.', + aliases: ['d'], + defaultsTo: false, + negatable: false, + ); + } + + @override + final String name = 'build'; + + @override + final String description = + 'Performs a single build on the specified targets and then exits.'; + + @override + int get defaultMaxConcurrency => 5; + + @override + Duration get defaultPackageTimeout => const Duration(minutes: 10); + + @override + Stream get packagesToProcess { + return repo + .streamPackages() + .where((package) => package.hasBuildRunnerDependency); + } + + @override + Future runTaskForPackage(Package package) async { + await runProcessSucessfullyOrThrow( + 'fvm', + [ + 'dart', + 'run', + 'build_runner', + 'build', + '--delete-conflicting-outputs', + '${argResults!['delete-conflicting-outputs'] as bool}', + ], + workingDirectory: package.path, + ); + } +} diff --git a/tools/sz_repo_cli/lib/src/commands/src/build_runner_command.dart b/tools/sz_repo_cli/lib/src/commands/src/build_runner_command.dart new file mode 100644 index 000000000..fcf93da1a --- /dev/null +++ b/tools/sz_repo_cli/lib/src/commands/src/build_runner_command.dart @@ -0,0 +1,18 @@ +// Copyright (c) 2022 Sharezone UG (haftungsbeschränkt) +// Licensed under the EUPL-1.2-or-later. +// +// You may obtain a copy of the Licence at: +// https://joinup.ec.europa.eu/software/page/eupl +// +// SPDX-License-Identifier: EUPL-1.2 + +import 'package:args/command_runner.dart'; + +class BuildRunnerCommand extends Command { + @override + String get description => + 'Runs "dart run build_runner" for every package that contains build_runner as a dependency.'; + + @override + String get name => 'build_runner'; +} diff --git a/tools/sz_repo_cli/lib/src/common/src/package.dart b/tools/sz_repo_cli/lib/src/common/src/package.dart index 74b638804..8b8b89574 100644 --- a/tools/sz_repo_cli/lib/src/common/src/package.dart +++ b/tools/sz_repo_cli/lib/src/common/src/package.dart @@ -30,6 +30,7 @@ class Package { bool get isPureDartPackage => type == PackageType.pureDart; final bool hasTestDirectory; final bool hasGoldenTestsDirectory; + final bool hasBuildRunnerDependency; Package({ required this.location, @@ -38,6 +39,7 @@ class Package { required this.hasTestDirectory, required this.hasGoldenTestsDirectory, required this.version, + required this.hasBuildRunnerDependency, }); factory Package.fromDirectory(Directory directory) { @@ -60,6 +62,9 @@ class Package { final hasTestGoldensDirectory = Directory(p.join(directory.path, 'test_goldens')).existsSync(); + final hasBuildRunnerDependency = dependencies.containsKey('build_runner') || + devDependencies.containsKey('build_runner'); + return Package( location: directory, name: name, @@ -67,6 +72,7 @@ class Package { hasGoldenTestsDirectory: hasTestGoldensDirectory, type: containsFlutter ? PackageType.flutter : PackageType.pureDart, version: version, + hasBuildRunnerDependency: hasBuildRunnerDependency, ); } diff --git a/tools/sz_repo_cli/lib/src/main.dart b/tools/sz_repo_cli/lib/src/main.dart index 17c095cd6..27c256b23 100644 --- a/tools/sz_repo_cli/lib/src/main.dart +++ b/tools/sz_repo_cli/lib/src/main.dart @@ -16,6 +16,8 @@ import 'package:sz_repo_cli/src/commands/src/build_android_command.dart'; import 'package:sz_repo_cli/src/commands/src/build_command.dart'; import 'package:sz_repo_cli/src/commands/src/build_ios_command.dart'; import 'package:sz_repo_cli/src/commands/src/build_macos_command.dart'; +import 'package:sz_repo_cli/src/commands/src/build_runner_build_command.dart'; +import 'package:sz_repo_cli/src/commands/src/build_runner_command.dart'; import 'package:sz_repo_cli/src/commands/src/build_web_command.dart'; import 'package:sz_repo_cli/src/commands/src/check_license_headers_command.dart'; import 'package:sz_repo_cli/src/commands/src/deploy_android_command.dart'; @@ -59,7 +61,8 @@ Future main(List args) async { ..addSubcommand(BuildAndroidCommand(repo)) ..addSubcommand(BuildMacOsCommand(repo)) ..addSubcommand(BuildWebCommand(repo)) - ..addSubcommand(BuildIosCommand(repo))); + ..addSubcommand(BuildIosCommand(repo))) + ..addCommand(BuildRunnerCommand()..addSubcommand(BuildRunnerBuild(repo))); await commandRunner.run(args).catchError((Object e) { final toolExit = e as ToolExit; diff --git a/tools/sz_repo_cli/test/common/build_utils_test.dart b/tools/sz_repo_cli/test/common/build_utils_test.dart index 71f3d63f3..fc0017bed 100644 --- a/tools/sz_repo_cli/test/common/build_utils_test.dart +++ b/tools/sz_repo_cli/test/common/build_utils_test.dart @@ -23,6 +23,7 @@ void main() { hasTestDirectory: false, hasGoldenTestsDirectory: false, version: version, + hasBuildRunnerDependency: false, ); } From 691497e36522ae2a57ea9465de479b4c8e073f1b Mon Sep 17 00:00:00 2001 From: nilsreichardt Date: Mon, 18 Sep 2023 18:03:50 +0200 Subject: [PATCH 2/2] Use "--delete-conflicting-outputs" flag --- .../src/build_runner_build_command.dart | 23 +++++++++---------- 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/tools/sz_repo_cli/lib/src/commands/src/build_runner_build_command.dart b/tools/sz_repo_cli/lib/src/commands/src/build_runner_build_command.dart index 93898fe5d..97c501e4c 100644 --- a/tools/sz_repo_cli/lib/src/commands/src/build_runner_build_command.dart +++ b/tools/sz_repo_cli/lib/src/commands/src/build_runner_build_command.dart @@ -11,23 +11,14 @@ import 'dart:async'; import 'package:sz_repo_cli/src/common/common.dart'; class BuildRunnerBuild extends ConcurrentCommand { - BuildRunnerBuild(SharezoneRepo repo) : super(repo) { - argParser.addFlag( - 'delete-conflicting-outputs', - help: - 'By default, the user will be prompted to delete any files which already exist but were not known to be generated by this specific build script.', - aliases: ['d'], - defaultsTo: false, - negatable: false, - ); - } + BuildRunnerBuild(SharezoneRepo repo) : super(repo); @override final String name = 'build'; @override final String description = - 'Performs a single build on the specified targets and then exits.'; + 'Performs a single build on the specified targets and then exits. Uses the "--delete-conflicting-outputs" flag'; @override int get defaultMaxConcurrency => 5; @@ -51,8 +42,16 @@ class BuildRunnerBuild extends ConcurrentCommand { 'run', 'build_runner', 'build', + // We use the "--delete-conflicting-outputs" flag, because we want to + // delete the conflicting outputs. + // + // Normally, the Dart CLI shows three options when there is a conflict: + // delete (1), cancel build (2) or list conflicts (3). But since we + // don't show the output of the `dart run build_runner` command, the + // user has no way to choose between these options. + // + // Therefore, we hard code it to delete the conflicting outputs. '--delete-conflicting-outputs', - '${argResults!['delete-conflicting-outputs'] as bool}', ], workingDirectory: package.path, );