@@ -8,6 +8,7 @@ import 'package:meta/meta.dart';
88import 'package:path/path.dart' as path;
99import 'package:very_good_cli/src/commands/commands.dart' ;
1010import 'package:very_good_cli/src/commands/create/templates/templates.dart' ;
11+ import 'package:very_good_cli/src/very_good_config/very_good_config.dart' ;
1112
1213// A valid Dart identifier that can be used for a package, i.e. no
1314// capital letters.
@@ -118,6 +119,14 @@ abstract class CreateSubCommand extends Command<int> {
118119 final Logger logger;
119120 final MasonGeneratorFromBundle _generatorFromBundle;
120121
122+ /// The resolved `very_good create` configuration read from the closest
123+ /// `very_good.yaml` file.
124+ ///
125+ /// Values declared here are used as defaults for any option that was not
126+ /// explicitly passed on the command line. It defaults to an empty
127+ /// configuration until [run] loads it.
128+ VeryGoodCreateConfig createConfig = const VeryGoodCreateConfig ();
129+
121130 /// [ArgResults] which can be overridden for testing.
122131 @visibleForTesting
123132 ArgResults ? argResultOverrides;
@@ -174,7 +183,11 @@ abstract class CreateSubCommand extends Command<int> {
174183 }
175184
176185 /// Gets the description for the project.
177- String get projectDescription => argResults['description' ] as String ? ?? '' ;
186+ String get projectDescription => argResults.resolve <String >(
187+ 'description' ,
188+ createConfig.description,
189+ fallbackValue: '' ,
190+ );
178191
179192 /// Should return the desired template to be created during a command run.
180193 ///
@@ -194,6 +207,10 @@ abstract class CreateSubCommand extends Command<int> {
194207
195208 @override
196209 Future <int > run () async {
210+ final config = VeryGoodConfig .load (Directory .current, logger: logger);
211+ if (config == null ) return ExitCode .config.code;
212+ createConfig = config.create;
213+
197214 final template = this .template;
198215 final bundle = template.bundle;
199216
@@ -273,7 +290,11 @@ abstract class CreateSubCommand extends Command<int> {
273290mixin OrgName on CreateSubCommand {
274291 /// Gets the organization name.
275292 String get orgName {
276- final orgName = argResults['org-name' ] as String ? ?? _defaultOrgName;
293+ final orgName = argResults.resolve <String >(
294+ 'org-name' ,
295+ createConfig.orgName,
296+ fallbackValue: _defaultOrgName,
297+ );
277298 _validateOrgName (orgName);
278299 return orgName;
279300 }
@@ -317,10 +338,26 @@ mixin MultiTemplates on CreateSubCommand {
317338 @nonVirtual
318339 @override
319340 Template get template {
320- final templateName =
321- argResults['template' ] as String ? ?? defaultTemplateName;
341+ final templateName = argResults.resolve <String >(
342+ 'template' ,
343+ createConfig.template,
344+ fallbackValue: defaultTemplateName,
345+ );
322346
323- return templates.firstWhere ((template) => template.name == templateName);
347+ return templates.firstWhere (
348+ (template) => template.name == templateName,
349+ orElse: () {
350+ // When the value came from `very_good.yaml` rather than the command
351+ // line, point the user at the config key instead of the CLI option so
352+ // they debug the right place.
353+ final source = argResults.wasParsed ('template' )
354+ ? 'option "--template"'
355+ : 'the `create.template` key in `$veryGoodConfigFileName `' ;
356+ usageException (
357+ '"$templateName " is not an allowed value for $source .' ,
358+ );
359+ },
360+ );
324361 }
325362}
326363
@@ -331,7 +368,11 @@ mixin MultiTemplates on CreateSubCommand {
331368/// to the brick generator.
332369mixin Publishable on CreateSubCommand {
333370 /// Gets the publishable flag.
334- bool get publishable => argResults['publishable' ] as bool ? ?? false ;
371+ bool get publishable => argResults.resolve <bool >(
372+ 'publishable' ,
373+ createConfig.publishable,
374+ fallbackValue: false ,
375+ );
335376}
336377
337378/// Mixin for [CreateSubCommand] subclasses that receives the workspace flag.
@@ -340,5 +381,9 @@ mixin Publishable on CreateSubCommand {
340381/// to the brick generator.
341382mixin Workspace on CreateSubCommand {
342383 /// Gets the workspace flag.
343- bool get workspace => argResults['workspace' ] as bool ? ?? false ;
384+ bool get workspace => argResults.resolve <bool >(
385+ 'workspace' ,
386+ createConfig.workspace,
387+ fallbackValue: false ,
388+ );
344389}
0 commit comments