diff --git a/src/chocolatey.sln.DotSettings b/src/chocolatey.sln.DotSettings index 1c48d3f556..47a2ce71dc 100644 --- a/src/chocolatey.sln.DotSettings +++ b/src/chocolatey.sln.DotSettings @@ -6,7 +6,10 @@ <?xml version="1.0" encoding="utf-16"?><Profile name="sensible"><CSArrangeThisQualifier>True</CSArrangeThisQualifier><CSMakeFieldReadonly>True</CSMakeFieldReadonly><CSOptimizeUsings><OptimizeUsings>True</OptimizeUsings><EmbraceInRegion>False</EmbraceInRegion><RegionName></RegionName></CSOptimizeUsings><CSShortenReferences>True</CSShortenReferences><CSReformatCode>True</CSReformatCode><CSRemoveCodeRedundancies>True</CSRemoveCodeRedundancies><CSUseAutoProperty>True</CSUseAutoProperty><CSUseVar><BehavourStyle>CAN_CHANGE_TO_IMPLICIT</BehavourStyle><LocalVariableStyle>IMPLICIT_WHEN_INITIALIZER_HAS_TYPE</LocalVariableStyle><ForeachVariableStyle>ALWAYS_EXPLICIT</ForeachVariableStyle></CSUseVar><CSharpFormatDocComments>True</CSharpFormatDocComments><CSUpdateFileHeader>True</CSUpdateFileHeader></Profile> sensible False - 195 + True + True + True + 220 False True Copyright © 2011 - Present RealDimensions Software, LLC diff --git a/src/chocolatey/infrastructure.app/commands/ChocolateyNewCommand.cs b/src/chocolatey/infrastructure.app/commands/ChocolateyNewCommand.cs index 2f42b938d2..9575f05856 100644 --- a/src/chocolatey/infrastructure.app/commands/ChocolateyNewCommand.cs +++ b/src/chocolatey/infrastructure.app/commands/ChocolateyNewCommand.cs @@ -42,7 +42,10 @@ public void configure_argument_parser(OptionSet optionSet, ChocolateyConfigurati optionSet .Add("a|auto|automaticpackage", "AutomaticPackage - Generate automatic package instead of normal. Defaults to false", - option => configuration.NewCommand.AutomaticPackage = option != null) + option => configuration.NewCommand.AutomaticPackage = option != null) + .Add("t=|template=|template-name=", + "TemplateName - Use a named template in {0}\templates\templatename instead of built-in template.".format_with(ApplicationParameters.InstallLocation), + option => configuration.NewCommand.TemplateName = option) .Add("name=", "Name [Required]- the name of the package. Can be passed as first parameter without \"--name=\".", option => @@ -57,7 +60,7 @@ public void configure_argument_parser(OptionSet optionSet, ChocolateyConfigurati "Maintainer - the name of the maintainer. Can also be passed as the property MaintainerName=somevalue", option => configuration.NewCommand.TemplateProperties.Add(TemplateValues.MaintainerPropertyName, option.remove_surrounding_quotes())) ; - //todo: template type + //todo: more built-in templates } public void handle_additional_argument_parsing(IList unparsedArguments, ChocolateyConfiguration configuration) diff --git a/src/chocolatey/infrastructure.app/configuration/ChocolateyConfiguration.cs b/src/chocolatey/infrastructure.app/configuration/ChocolateyConfiguration.cs index a0f3a8c6b5..1484a42393 100644 --- a/src/chocolatey/infrastructure.app/configuration/ChocolateyConfiguration.cs +++ b/src/chocolatey/infrastructure.app/configuration/ChocolateyConfiguration.cs @@ -344,6 +344,7 @@ public NewCommandConfiguration() TemplateProperties = new Dictionary(StringComparer.InvariantCultureIgnoreCase); } + public string TemplateName { get; set; } public string Name { get; set; } public bool AutomaticPackage { get; set; } public IDictionary TemplateProperties { get; private set; } diff --git a/src/chocolatey/infrastructure.app/services/TemplateService.cs b/src/chocolatey/infrastructure.app/services/TemplateService.cs index 7ebb2ebf9b..a819b0fd12 100644 --- a/src/chocolatey/infrastructure.app/services/TemplateService.cs +++ b/src/chocolatey/infrastructure.app/services/TemplateService.cs @@ -16,6 +16,7 @@ namespace chocolatey.infrastructure.app.services { using System; + using System.IO; using System.Reflection; using System.Text; using configuration; @@ -44,7 +45,8 @@ public void generate(ChocolateyConfiguration configuration) var packageLocation = _fileSystem.combine_paths(_fileSystem.get_current_directory(), configuration.NewCommand.Name); if (_fileSystem.directory_exists(packageLocation) && !configuration.Force) { - throw new ApplicationException("The location for the template already exists. You can:{0} 1. Remove '{1}'{0} 2. Use --force{0} 3. Specify a different name".format_with(Environment.NewLine, packageLocation)); + throw new ApplicationException( + "The location for the template already exists. You can:{0} 1. Remove '{1}'{0} 2. Use --force{0} 3. Specify a different name".format_with(Environment.NewLine, packageLocation)); } if (configuration.RegularOutput) this.Log().Info(() => "Creating a new package specification at {0}".format_with(packageLocation)); @@ -61,10 +63,7 @@ public void generate(ChocolateyConfiguration configuration) _fileSystem.create_directory_if_not_exists(packageToolsLocation); var tokens = new TemplateValues(); - if (configuration.NewCommand.AutomaticPackage) - { - tokens.set_auto(); - } + if (configuration.NewCommand.AutomaticPackage) tokens.set_auto(); // now override those values foreach (var property in configuration.NewCommand.TemplateProperties) @@ -86,12 +85,34 @@ public void generate(ChocolateyConfiguration configuration) this.Log().Debug(() => " {0}={1}".format_with(propertyInfo.Name, propertyInfo.GetValue(tokens, null))); } - generate_file_from_template(configuration, tokens, NuspecTemplate.Template, _fileSystem.combine_paths(packageLocation, "{0}.nuspec".format_with(tokens.PackageNameLower)), Encoding.UTF8); - generate_file_from_template(configuration, tokens, ChocolateyInstallTemplate.Template, _fileSystem.combine_paths(packageToolsLocation, "chocolateyinstall.ps1"), Encoding.UTF8); - generate_file_from_template(configuration, tokens, ChocolateyUninstallTemplate.Template, _fileSystem.combine_paths(packageToolsLocation, "chocolateyuninstall.ps1"), Encoding.UTF8); - generate_file_from_template(configuration, tokens, ChocolateyReadMeTemplate.Template, _fileSystem.combine_paths(packageToolsLocation, "ReadMe.md"), Encoding.UTF8); + var defaultTemplateOverride = _fileSystem.combine_paths(ApplicationParameters.InstallLocation, "templates", "default"); + if (string.IsNullOrWhiteSpace(configuration.NewCommand.TemplateName) && !_fileSystem.directory_exists(defaultTemplateOverride)) + { + generate_file_from_template(configuration, tokens, NuspecTemplate.Template, _fileSystem.combine_paths(packageLocation, "{0}.nuspec".format_with(tokens.PackageNameLower)), Encoding.UTF8); + generate_file_from_template(configuration, tokens, ChocolateyInstallTemplate.Template, _fileSystem.combine_paths(packageToolsLocation, "chocolateyinstall.ps1"), Encoding.UTF8); + generate_file_from_template(configuration, tokens, ChocolateyUninstallTemplate.Template, _fileSystem.combine_paths(packageToolsLocation, "chocolateyuninstall.ps1"), Encoding.UTF8); + generate_file_from_template(configuration, tokens, ChocolateyReadMeTemplate.Template, _fileSystem.combine_paths(packageToolsLocation, "ReadMe.md"), Encoding.UTF8); + } + else + { + configuration.NewCommand.TemplateName = string.IsNullOrWhiteSpace(configuration.NewCommand.TemplateName) ? "default" : configuration.NewCommand.TemplateName; + + var templatePath = _fileSystem.combine_paths(ApplicationParameters.InstallLocation, "templates", configuration.NewCommand.TemplateName); + if (!_fileSystem.directory_exists(templatePath)) throw new ApplicationException("Unable to find path to requested template '{0}'. Path should be '{1}'".format_with(configuration.NewCommand.TemplateName, templatePath)); + + this.Log().Info(ChocolateyLoggers.Important, "Generating package from custom template at '{0}'.".format_with(templatePath)); + foreach (var file in _fileSystem.get_files(templatePath, "*.*", SearchOption.AllDirectories)) + { + var packageFileLocation = file.Replace(templatePath, packageLocation); + if (_fileSystem.get_file_extension(packageFileLocation).is_equal_to(".nuspec")) packageFileLocation = _fileSystem.combine_paths(packageLocation, "{0}.nuspec".format_with(tokens.PackageNameLower)); + generate_file_from_template(configuration, tokens, _fileSystem.read_file(file), packageFileLocation, Encoding.UTF8); + } + } - this.Log().Info(ChocolateyLoggers.Important, "Successfully generated {0}{1} package specification files{2} at '{3}'".format_with(configuration.NewCommand.Name, configuration.NewCommand.AutomaticPackage ? " (automatic)" : string.Empty, Environment.NewLine, packageLocation)); + this.Log().Info( + ChocolateyLoggers.Important, + "Successfully generated {0}{1} package specification files{2} at '{3}'".format_with( + configuration.NewCommand.Name, configuration.NewCommand.AutomaticPackage ? " (automatic)" : string.Empty, Environment.NewLine, packageLocation)); } public void generate_file_from_template(ChocolateyConfiguration configuration, TemplateValues tokens, string template, string fileLocation, Encoding encoding) @@ -103,4 +124,4 @@ public void generate_file_from_template(ChocolateyConfiguration configuration, T _fileSystem.write_file(fileLocation, template, encoding); } } -} \ No newline at end of file +} diff --git a/src/chocolatey/infrastructure/filesystem/IFileSystem.cs b/src/chocolatey/infrastructure/filesystem/IFileSystem.cs index c5f883e642..cebd04fc36 100644 --- a/src/chocolatey/infrastructure/filesystem/IFileSystem.cs +++ b/src/chocolatey/infrastructure/filesystem/IFileSystem.cs @@ -111,7 +111,7 @@ public interface IFileSystem string get_file_name_without_extension(string filePath); /// - /// Gets the extension. + /// Gets the extension (including the "."). /// /// The file path. /// he extension of the specified path (including the period "."), or Nothing, or String.Empty. If path is Nothing, get_file_extension returns Nothing. If path does not have extension information, get_file_extension returns String.Empty.