From 5ca1294e4ba7fbd3b965e1284c8e562ec4fa6a85 Mon Sep 17 00:00:00 2001 From: Fernando Ferrara Date: Tue, 16 May 2023 22:37:42 -0300 Subject: [PATCH 1/3] Add watch flag to generate command --- lib/src/commands/generate/generate_command.dart | 16 ++++++++++++++-- lib/src/constants/command_constants.dart | 1 + lib/src/constants/message_constants.dart | 3 +++ lib/src/services/process_service.dart | 10 ++++++++-- lib/src/templates/template_constants.dart | 1 + test/helpers/test_helpers.mocks.dart | 13 ++++++++++--- 6 files changed, 37 insertions(+), 7 deletions(-) diff --git a/lib/src/commands/generate/generate_command.dart b/lib/src/commands/generate/generate_command.dart index fea3a5d..0470395 100644 --- a/lib/src/commands/generate/generate_command.dart +++ b/lib/src/commands/generate/generate_command.dart @@ -1,9 +1,12 @@ import 'dart:async'; import 'package:args/command_runner.dart'; +import 'package:stacked_cli/src/constants/command_constants.dart'; +import 'package:stacked_cli/src/constants/message_constants.dart'; import 'package:stacked_cli/src/locator.dart'; import 'package:stacked_cli/src/services/analytics_service.dart'; import 'package:stacked_cli/src/services/process_service.dart'; +import 'package:stacked_cli/src/templates/template_constants.dart'; class GenerateCommand extends Command { final _analyticsService = locator(); @@ -14,11 +17,20 @@ class GenerateCommand extends Command { '''Generates the code for the stacked application if any changes were made'''; @override - String get name => 'generate'; + String get name => kTemplateNameGenerate; + + GenerateCommand() { + argParser.addFlag( + ksWatch, + abbr: 'w', + defaultsTo: false, + help: kCommandHelpWatch, + ); + } @override Future run() async { unawaited(_analyticsService.generateCodeEvent()); - await _processService.runBuildRunner(); + await _processService.runBuildRunner(shouldWatch: argResults?[ksWatch]); } } diff --git a/lib/src/constants/command_constants.dart b/lib/src/constants/command_constants.dart index 14f5657..bf019c3 100644 --- a/lib/src/constants/command_constants.dart +++ b/lib/src/constants/command_constants.dart @@ -26,6 +26,7 @@ const String ksActivate = 'activate'; const String ksStackedCli = 'stacked_cli'; const String ksAnalyze = 'analyze'; const String ksModel = 'model'; +const String ksWatch = 'watch'; /// A list of strings that are used to run the pub run build runner build --delete-conflicting-outputs command. const List buildRunnerArguments = [ diff --git a/lib/src/constants/message_constants.dart b/lib/src/constants/message_constants.dart index 72c1b14..0840f4d 100644 --- a/lib/src/constants/message_constants.dart +++ b/lib/src/constants/message_constants.dart @@ -56,6 +56,9 @@ const String kCommandHelpCreateBottomSheetTemplate = const String kCommandHelpExcludeDependency = 'When a service is excluded it will not be added to your app.dart dependencies collection.'; +const String kCommandHelpWatch = + 'Generates the code for the Stacked application, watching the file system for updates and rebuilding as appropriate.'; + const String kConfigFileNotFound = 'No stacked.json file found. Default stacked values will be used.'; diff --git a/lib/src/services/process_service.dart b/lib/src/services/process_service.dart index 4ac8b53..05b7ec0 100644 --- a/lib/src/services/process_service.dart +++ b/lib/src/services/process_service.dart @@ -38,10 +38,16 @@ class ProcessService { /// /// Args: /// appName (String): The name of the app. - Future runBuildRunner({String? appName}) async { + Future runBuildRunner({ + String? appName, + bool shouldWatch = false, + }) async { await _runProcess( programName: ksFlutter, - arguments: buildRunnerArguments, + arguments: [ + ...buildRunnerArguments, + if (shouldWatch) ksWatch, + ], workingDirectory: appName, ); } diff --git a/lib/src/templates/template_constants.dart b/lib/src/templates/template_constants.dart index ee23370..720a3a0 100644 --- a/lib/src/templates/template_constants.dart +++ b/lib/src/templates/template_constants.dart @@ -4,6 +4,7 @@ const String kTemplateNameService = 'service'; const String kTemplateNameApp = 'app'; const String kTemplateNameBottomSheet = 'bottom_sheet'; const String kTemplateNameDialog = 'dialog'; +const String kTemplateNameGenerate = 'generate'; // ------- Template Types -------- const String kTemplateTypeEmpty = 'empty'; diff --git a/test/helpers/test_helpers.mocks.dart b/test/helpers/test_helpers.mocks.dart index 252bc63..c7bb397 100644 --- a/test/helpers/test_helpers.mocks.dart +++ b/test/helpers/test_helpers.mocks.dart @@ -1,4 +1,4 @@ -// Mocks generated by Mockito 5.3.2 from annotations +// Mocks generated by Mockito 5.4.0 from annotations // in stacked_cli/test/helpers/test_helpers.dart. // Do not manually edit this file. @@ -1081,11 +1081,18 @@ class MockProcessService extends _i1.Mock implements _i16.ProcessService { returnValueForMissingStub: _i6.Future.value(), ) as _i6.Future); @override - _i6.Future runBuildRunner({String? appName}) => (super.noSuchMethod( + _i6.Future runBuildRunner({ + String? appName, + bool? shouldWatch = false, + }) => + (super.noSuchMethod( Invocation.method( #runBuildRunner, [], - {#appName: appName}, + { + #appName: appName, + #shouldWatch: shouldWatch, + }, ), returnValue: _i6.Future.value(), returnValueForMissingStub: _i6.Future.value(), From 50de5c162dd2cde92064b54b10816e2032d763b1 Mon Sep 17 00:00:00 2001 From: Fernando Ferrara Date: Wed, 17 May 2023 10:54:22 -0300 Subject: [PATCH 2/3] Update build runner command - Updated build_runner command to run with Dart instead of Flutter which was deprecated on version 3.10. - Add delete-conflicting-outputs flag to allow disable of it. --- lib/src/commands/generate/generate_command.dart | 13 ++++++++++++- lib/src/constants/command_constants.dart | 7 +++---- lib/src/constants/message_constants.dart | 3 +++ lib/src/services/process_service.dart | 6 ++++-- test/helpers/test_helpers.mocks.dart | 2 ++ 5 files changed, 24 insertions(+), 7 deletions(-) diff --git a/lib/src/commands/generate/generate_command.dart b/lib/src/commands/generate/generate_command.dart index 0470395..7911a7c 100644 --- a/lib/src/commands/generate/generate_command.dart +++ b/lib/src/commands/generate/generate_command.dart @@ -20,6 +20,14 @@ class GenerateCommand extends Command { String get name => kTemplateNameGenerate; GenerateCommand() { + argParser.addFlag( + ksDeleteConflictOutputs, + abbr: 'd', + defaultsTo: true, + negatable: true, + help: kCommandHelpDeleteConflictingOutputs, + ); + argParser.addFlag( ksWatch, abbr: 'w', @@ -31,6 +39,9 @@ class GenerateCommand extends Command { @override Future run() async { unawaited(_analyticsService.generateCodeEvent()); - await _processService.runBuildRunner(shouldWatch: argResults?[ksWatch]); + await _processService.runBuildRunner( + shouldDeleteConflictingOutputs: argResults?[ksDeleteConflictOutputs], + shouldWatch: argResults?[ksWatch], + ); } } diff --git a/lib/src/constants/command_constants.dart b/lib/src/constants/command_constants.dart index bf019c3..8899bfd 100644 --- a/lib/src/constants/command_constants.dart +++ b/lib/src/constants/command_constants.dart @@ -9,6 +9,7 @@ const String ksGet = 'get'; const String ksFormat = 'format'; const String ksBuild = 'build'; const String ksBuildRunner = 'build_runner'; +const String ksDeleteConflictOutputs = 'delete-conflicting-outputs'; const String ksDeleteConflictingOutputs = '--delete-conflicting-outputs'; const String ksVersion = 'version'; const String ksEnableAnalytics = 'enable-analytics'; @@ -28,13 +29,11 @@ const String ksAnalyze = 'analyze'; const String ksModel = 'model'; const String ksWatch = 'watch'; -/// A list of strings that are used to run the pub run build runner build --delete-conflicting-outputs command. +/// A list of strings that are used to run the run build_runner +/// [build or watch] --delete-conflicting-outputs command. const List buildRunnerArguments = [ - ksPub, ksRun, ksBuildRunner, - ksBuild, - ksDeleteConflictingOutputs, ]; /// A list of strings that are used to run the pub get command. diff --git a/lib/src/constants/message_constants.dart b/lib/src/constants/message_constants.dart index 0840f4d..3805364 100644 --- a/lib/src/constants/message_constants.dart +++ b/lib/src/constants/message_constants.dart @@ -59,6 +59,9 @@ const String kCommandHelpExcludeDependency = const String kCommandHelpWatch = 'Generates the code for the Stacked application, watching the file system for updates and rebuilding as appropriate.'; +const String kCommandHelpDeleteConflictingOutputs = + 'Assume conflicting outputs in the users package are from previous builds, and skip the user prompt that would usually be provided.'; + const String kConfigFileNotFound = 'No stacked.json file found. Default stacked values will be used.'; diff --git a/lib/src/services/process_service.dart b/lib/src/services/process_service.dart index 05b7ec0..a8b9d22 100644 --- a/lib/src/services/process_service.dart +++ b/lib/src/services/process_service.dart @@ -41,12 +41,14 @@ class ProcessService { Future runBuildRunner({ String? appName, bool shouldWatch = false, + bool shouldDeleteConflictingOutputs = true, }) async { await _runProcess( - programName: ksFlutter, + programName: ksDart, arguments: [ ...buildRunnerArguments, - if (shouldWatch) ksWatch, + shouldWatch ? ksWatch : ksBuild, + if (shouldDeleteConflictingOutputs) ksDeleteConflictingOutputs, ], workingDirectory: appName, ); diff --git a/test/helpers/test_helpers.mocks.dart b/test/helpers/test_helpers.mocks.dart index c7bb397..d252f35 100644 --- a/test/helpers/test_helpers.mocks.dart +++ b/test/helpers/test_helpers.mocks.dart @@ -1084,6 +1084,7 @@ class MockProcessService extends _i1.Mock implements _i16.ProcessService { _i6.Future runBuildRunner({ String? appName, bool? shouldWatch = false, + bool? shouldDeleteConflictingOutputs = true, }) => (super.noSuchMethod( Invocation.method( @@ -1092,6 +1093,7 @@ class MockProcessService extends _i1.Mock implements _i16.ProcessService { { #appName: appName, #shouldWatch: shouldWatch, + #shouldDeleteConflictingOutputs: shouldDeleteConflictingOutputs, }, ), returnValue: _i6.Future.value(), From c7a5829850eef106daae02131fd16a6e909bbe93 Mon Sep 17 00:00:00 2001 From: Fernando Ferrara Date: Wed, 17 May 2023 11:07:20 -0300 Subject: [PATCH 3/3] Fix format --- lib/src/templates/compiled_templates.dart | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/src/templates/compiled_templates.dart b/lib/src/templates/compiled_templates.dart index 1f0a352..6bc82a5 100644 --- a/lib/src/templates/compiled_templates.dart +++ b/lib/src/templates/compiled_templates.dart @@ -1925,7 +1925,7 @@ const String kAppWebTemplateSettingsJsonStkContent = ''' { "explorer.fileNesting.enabled": true, "explorer.fileNesting.patterns": { - "*.dart": "\$\{capture}.mobile.dart, \$\{capture}.tablet.dart, \$\{capture}.desktop.dart, \$\{capture}.form.dart, \$\{capture}.g.dart, \$\{capture}.freezed.dart, \$\{capture}.logger.dart, \$\{capture}.locator.dart, \$\{capture}.router.dart, \$\{capture}.dialogs.dart, \$\{capture}.bottomsheets.dart" + "*.dart": "\${capture}.mobile.dart, \${capture}.tablet.dart, \${capture}.desktop.dart, \${capture}.form.dart, \${capture}.g.dart, \${capture}.freezed.dart, \${capture}.logger.dart, \${capture}.locator.dart, \${capture}.router.dart, \${capture}.dialogs.dart, \${capture}.bottomsheets.dart" } } @@ -3138,7 +3138,7 @@ const String kAppMobileTemplateSettingsJsonStkContent = ''' { "explorer.fileNesting.enabled": true, "explorer.fileNesting.patterns": { - "*.dart": "\$\{capture}.mobile.dart, \$\{capture}.tablet.dart, \$\{capture}.desktop.dart, \$\{capture}.form.dart, \$\{capture}.g.dart, \$\{capture}.freezed.dart, \$\{capture}.logger.dart, \$\{capture}.locator.dart, \$\{capture}.router.dart, \$\{capture}.dialogs.dart, \$\{capture}.bottomsheets.dart" + "*.dart": "\${capture}.mobile.dart, \${capture}.tablet.dart, \${capture}.desktop.dart, \${capture}.form.dart, \${capture}.g.dart, \${capture}.freezed.dart, \${capture}.logger.dart, \${capture}.locator.dart, \${capture}.router.dart, \${capture}.dialogs.dart, \${capture}.bottomsheets.dart" } }