From e2123fbdfea0d4c66fab23d622ee05111a0212ba Mon Sep 17 00:00:00 2001 From: Gary Ewan Park Date: Sun, 3 Oct 2021 20:10:02 +0100 Subject: [PATCH] (#2377) Use defaultTemplateName if set If the defaultTemplateName configuration value is set, and the user hasn't specified a template name at the command line, check to see whether a template exists with that name. If it does, use this as the template when generating the new package. Given that the default value for the new defaultTemplateName configuration value is an empty string, if the user is directly overriding the default template files folder, this overriding will still take place. --- .../services/TemplateService.cs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/chocolatey/infrastructure.app/services/TemplateService.cs b/src/chocolatey/infrastructure.app/services/TemplateService.cs index 5bd8d61a49..2f79ff9b4c 100644 --- a/src/chocolatey/infrastructure.app/services/TemplateService.cs +++ b/src/chocolatey/infrastructure.app/services/TemplateService.cs @@ -102,6 +102,25 @@ public void generate(ChocolateyConfiguration configuration) this.Log().Debug(() => " {0}={1}".format_with(additionalProperty.Key, additionalProperty.Value)); } + // Attempt to set the name of the template that will be used to generate the new package + // If no template name has been passed at the command line, check to see if there is a defaultTemplateName set in the + // chocolatey.config file. If there is, and this template exists on disk, use it. + // Otherwise, revert to the built in default template. + var defaultTemplateName = configuration.DefaultTemplateName; + if (string.IsNullOrWhiteSpace(configuration.NewCommand.TemplateName) && !string.IsNullOrWhiteSpace(defaultTemplateName)) + { + var defaultTemplateNameLocation = _fileSystem.combine_paths(ApplicationParameters.TemplatesLocation, defaultTemplateName); + if (!_fileSystem.directory_exists(defaultTemplateNameLocation)) + { + this.Log().Warn(() => "defaultTemplateName configuration value has been set to '{0}', but no template with that name exists in '{1}'. Reverting to default template.".format_with(defaultTemplateName, ApplicationParameters.TemplatesLocation)); + } + else + { + this.Log().Debug(() => "Setting TemplateName to '{0}'".format_with(defaultTemplateName)); + configuration.NewCommand.TemplateName = defaultTemplateName; + } + } + var defaultTemplateOverride = _fileSystem.combine_paths(ApplicationParameters.TemplatesLocation, "default"); if (string.IsNullOrWhiteSpace(configuration.NewCommand.TemplateName) && (!_fileSystem.directory_exists(defaultTemplateOverride) || configuration.NewCommand.UseOriginalTemplate)) {