Skip to content

Commit

Permalink
(chocolatey#2377) Use defaultTemplateName if set
Browse files Browse the repository at this point in the history
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.

Also take into consideration the usage of the --built-in command line
option. If this is set, make sure to respect it. The order of
precedence is the following:

1. choco new --template=name
2. choco new --built-in
3. choco config defaultTemplateName
4. Manually created files in $env:chocolateyinstall\templates\default
5. Fall back onto the built-in template.
  • Loading branch information
gep13 committed Dec 23, 2021
1 parent dafae0f commit defb7d3
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/chocolatey/infrastructure.app/services/TemplateService.cs
Expand Up @@ -102,6 +102,27 @@ 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.
// In addition, if the command line option to use the built-in template has been set, respect that
// and use the built in template.
var defaultTemplateName = configuration.DefaultTemplateName;
if (string.IsNullOrWhiteSpace(configuration.NewCommand.TemplateName) && !string.IsNullOrWhiteSpace(defaultTemplateName) && !configuration.NewCommand.UseOriginalTemplate)
{
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))
{
Expand Down

0 comments on commit defb7d3

Please sign in to comment.