Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add watch argument to generate command #20

Merged
merged 4 commits into from
May 23, 2023
Merged
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
27 changes: 25 additions & 2 deletions lib/src/commands/generate/generate_command.dart
Original file line number Diff line number Diff line change
@@ -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<AnalyticsService>();
Expand All @@ -14,11 +17,31 @@ 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(
ksDeleteConflictOutputs,
abbr: 'd',
defaultsTo: true,
negatable: true,
help: kCommandHelpDeleteConflictingOutputs,
);

argParser.addFlag(
ksWatch,
abbr: 'w',
defaultsTo: false,
help: kCommandHelpWatch,
);
}

@override
Future<void> run() async {
unawaited(_analyticsService.generateCodeEvent());
await _processService.runBuildRunner();
await _processService.runBuildRunner(
shouldDeleteConflictingOutputs: argResults?[ksDeleteConflictOutputs],
shouldWatch: argResults?[ksWatch],
);
}
}
8 changes: 4 additions & 4 deletions lib/src/constants/command_constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -26,14 +27,13 @@ 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.
/// A list of strings that are used to run the run build_runner
/// [build or watch] --delete-conflicting-outputs command.
const List<String> buildRunnerArguments = [
ksPub,
ksRun,
ksBuildRunner,
ksBuild,
ksDeleteConflictingOutputs,
];

/// A list of strings that are used to run the pub get command.
Expand Down
6 changes: 6 additions & 0 deletions lib/src/constants/message_constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ 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 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.';

Expand Down
14 changes: 11 additions & 3 deletions lib/src/services/process_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,18 @@ class ProcessService {
///
/// Args:
/// appName (String): The name of the app.
Future<void> runBuildRunner({String? appName}) async {
Future<void> runBuildRunner({
String? appName,
bool shouldWatch = false,
bool shouldDeleteConflictingOutputs = true,
}) async {
await _runProcess(
programName: ksFlutter,
arguments: buildRunnerArguments,
programName: ksDart,
arguments: [
...buildRunnerArguments,
shouldWatch ? ksWatch : ksBuild,
if (shouldDeleteConflictingOutputs) ksDeleteConflictingOutputs,
],
workingDirectory: appName,
);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/src/templates/compiled_templates.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}

Expand Down Expand Up @@ -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"
}
}

Expand Down
1 change: 1 addition & 0 deletions lib/src/templates/template_constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
15 changes: 12 additions & 3 deletions test/helpers/test_helpers.mocks.dart
Original file line number Diff line number Diff line change
@@ -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.

Expand Down Expand Up @@ -1081,11 +1081,20 @@ class MockProcessService extends _i1.Mock implements _i16.ProcessService {
returnValueForMissingStub: _i6.Future<void>.value(),
) as _i6.Future<void>);
@override
_i6.Future<void> runBuildRunner({String? appName}) => (super.noSuchMethod(
_i6.Future<void> runBuildRunner({
String? appName,
bool? shouldWatch = false,
bool? shouldDeleteConflictingOutputs = true,
}) =>
(super.noSuchMethod(
Invocation.method(
#runBuildRunner,
[],
{#appName: appName},
{
#appName: appName,
#shouldWatch: shouldWatch,
#shouldDeleteConflictingOutputs: shouldDeleteConflictingOutputs,
},
),
returnValue: _i6.Future<void>.value(),
returnValueForMissingStub: _i6.Future<void>.value(),
Expand Down