diff --git a/lib/src/commands/create/commands/create_subcommand.dart b/lib/src/commands/create/commands/create_subcommand.dart index 6724ec77b..01bc48c7c 100644 --- a/lib/src/commands/create/commands/create_subcommand.dart +++ b/lib/src/commands/create/commands/create_subcommand.dart @@ -22,6 +22,9 @@ const _defaultDescription = 'A Very Good Project created by Very Good CLI.'; /// A method which returns a [Future] given a [MasonBundle]. typedef MasonGeneratorFromBundle = Future Function(MasonBundle); +/// Lazy resolvers for template vars, keyed by the brick var name. +typedef TemplateVarResolvers = Map; + /// {@template create_subcommand} /// Generic class for sub commands of [CreateCommand]. /// {@endtemplate} @@ -259,28 +262,32 @@ abstract class CreateSubCommand({ return ExitCode.success.code; } - /// Responsible for returns the template parameters to be passed to the - /// template brick. + /// The template vars this command handles, keyed by the brick var name. + /// + /// Each resolver is only invoked by [getTemplateVars], so the keys can be + /// read without parsing any arguments. /// - /// Override if the create sub command requires additional template - /// parameters. + /// Override to handle additional template vars, spreading + /// `super.templateVarResolvers` into the returned map. /// /// For subcommands that mix with [OrgName], it includes 'org_name'. /// For subcommands that mix with [Publishable], it includes 'publishable'. /// For subcommands that mix with [Workspace], it includes 'workspace'. @mustCallSuper - Map getTemplateVars() { - final projectName = this.projectName; - final projectDescription = this.projectDescription; - - return { - 'project_name': projectName, - 'description': projectDescription, - if (this is OrgName) 'org_name': (this as OrgName).orgName, - if (this is Publishable) 'publishable': (this as Publishable).publishable, - if (this is Workspace) 'workspace': (this as Workspace).workspace, - }; - } + TemplateVarResolvers get templateVarResolvers => { + 'project_name': () => projectName, + 'description': () => projectDescription, + }; + + /// Returns the template parameters to be passed to the template brick. + /// + /// Computed from [templateVarResolvers]. Vars that resolve to `null` are + /// omitted so the template falls back to its default value. + @nonVirtual + Map getTemplateVars() => { + for (final MapEntry(:key, value: resolve) in templateVarResolvers.entries) + key: ?resolve(), + }; } /// Mixin for [CreateSubCommand] subclasses that receives the org name @@ -288,6 +295,12 @@ abstract class CreateSubCommand({ /// /// Takes care of parsing from [argResults] and validating the org name. mixin OrgName on CreateSubCommand { + @override + TemplateVarResolvers get templateVarResolvers => { + ...super.templateVarResolvers, + 'org_name': () => orgName, + }; + /// Gets the organization name. String get orgName { final orgName = argResults.resolve( @@ -365,6 +378,12 @@ mixin MultiTemplates on CreateSubCommand { /// Takes care of parsing it from [argResults] and pass it /// to the brick generator. mixin Publishable on CreateSubCommand { + @override + TemplateVarResolvers get templateVarResolvers => { + ...super.templateVarResolvers, + 'publishable': () => publishable, + }; + /// Gets the publishable flag. bool get publishable => argResults.resolve( 'publishable', @@ -378,6 +397,12 @@ mixin Publishable on CreateSubCommand { /// Takes care of parsing it from [argResults] and pass it /// to the brick generator. mixin Workspace on CreateSubCommand { + @override + TemplateVarResolvers get templateVarResolvers => { + ...super.templateVarResolvers, + 'workspace': () => workspace, + }; + /// Gets the workspace flag. bool get workspace => argResults.resolve( 'workspace', diff --git a/lib/src/commands/create/commands/dart_cli.dart b/lib/src/commands/create/commands/dart_cli.dart index 10c0bd7be..189944eed 100644 --- a/lib/src/commands/create/commands/dart_cli.dart +++ b/lib/src/commands/create/commands/dart_cli.dart @@ -25,14 +25,9 @@ class CreateDartCLI({required super.logger, required super.generatorFromBundle}) Template get template => VeryGoodDartCLITemplate(); @override - Map getTemplateVars() { - final vars = super.getTemplateVars(); - - final executableName = - argResults['executable-name'] as String? ?? projectName; - - vars['executable_name'] = executableName; - - return vars; - } + TemplateVarResolvers get templateVarResolvers => { + ...super.templateVarResolvers, + 'executable_name': () => + argResults['executable-name'] as String? ?? projectName, + }; } diff --git a/lib/src/commands/create/commands/docs_site.dart b/lib/src/commands/create/commands/docs_site.dart index 969a3027e..129e361a1 100644 --- a/lib/src/commands/create/commands/docs_site.dart +++ b/lib/src/commands/create/commands/docs_site.dart @@ -28,16 +28,14 @@ class CreateDocsSite({ String get description => 'Generate a Very Good documentation site.'; @override - Map getTemplateVars() { - return { - ...super.getTemplateVars(), - 'org_name': argResults.resolve( - 'org-name', - createConfig.orgName, - fallbackValue: _defaultOrgName, - ), - }; - } + TemplateVarResolvers get templateVarResolvers => { + ...super.templateVarResolvers, + 'org_name': () => argResults.resolve( + 'org-name', + createConfig.orgName, + fallbackValue: _defaultOrgName, + ), + }; @override Template get template => VeryGoodDocsSiteTemplate(); diff --git a/lib/src/commands/create/commands/flame_game.dart b/lib/src/commands/create/commands/flame_game.dart index d49751054..ad4fb1f56 100644 --- a/lib/src/commands/create/commands/flame_game.dart +++ b/lib/src/commands/create/commands/flame_game.dart @@ -37,10 +37,8 @@ class CreateFlameGame({ Template get template => VeryGoodFlameGameTemplate(); @override - Map getTemplateVars() { - final vars = super.getTemplateVars(); - final platforms = argResults['platforms'] as List; - vars['platforms'] = platforms; - return vars; - } + TemplateVarResolvers get templateVarResolvers => { + ...super.templateVarResolvers, + 'platforms': () => argResults['platforms'] as List, + }; } diff --git a/lib/src/commands/create/commands/flutter_app.dart b/lib/src/commands/create/commands/flutter_app.dart index 6ac2d894a..8a643cd95 100644 --- a/lib/src/commands/create/commands/flutter_app.dart +++ b/lib/src/commands/create/commands/flutter_app.dart @@ -42,19 +42,11 @@ class CreateFlutterApp({ String get description => 'Generate a Very Good Flutter application.'; @override - Map getTemplateVars() { - final vars = super.getTemplateVars(); - - final applicationId = argResults['application-id'] as String?; - if (applicationId != null) { - vars['application_id'] = applicationId; - } - - final platforms = argResults['platforms'] as List; - vars['platforms'] = platforms; - - return vars; - } + TemplateVarResolvers get templateVarResolvers => { + ...super.templateVarResolvers, + 'application_id': () => argResults['application-id'] as String?, + 'platforms': () => argResults['platforms'] as List, + }; @override final List