From 6c828b6c12fd46efe53729a2b14724b1c3d3cd98 Mon Sep 17 00:00:00 2001 From: Rob Reynolds Date: Fri, 11 Mar 2016 10:03:59 -0600 Subject: [PATCH] (GH-658) Allow custom key/value args in choco new Allow passing arbitrary key/value arguments to new command when generating packages from templates. This allows adding in custom templating values over what is available by default. --- .../tokens/TokenReplacerSpecs.cs | 28 ++++++++++++++++++- .../services/TemplateService.cs | 9 +++++- .../templates/TemplateValues.cs | 6 ++++ .../infrastructure/tokens/TokenReplacer.cs | 5 ++++ 4 files changed, 46 insertions(+), 2 deletions(-) diff --git a/src/chocolatey.tests/infrastructure/tokens/TokenReplacerSpecs.cs b/src/chocolatey.tests/infrastructure/tokens/TokenReplacerSpecs.cs index cde3a4cab3..09bd739e74 100644 --- a/src/chocolatey.tests/infrastructure/tokens/TokenReplacerSpecs.cs +++ b/src/chocolatey.tests/infrastructure/tokens/TokenReplacerSpecs.cs @@ -15,6 +15,7 @@ namespace chocolatey.tests.infrastructure.tokens { + using System.Collections.Generic; using Should; using chocolatey.infrastructure.app.configuration; using chocolatey.infrastructure.tokens; @@ -66,10 +67,16 @@ public void when_given_brace_brace_commandname_brace_brace_should_replace_with_t public void when_given_brace_brace_COMMANDNAME_brace_brace_should_replace_with_the_Name_from_the_configuration() { TokenReplacer.replace_tokens(configuration, "Hi! My name is [[COMMANDNAME]]").ShouldEqual("Hi! My name is " + name); + } + + [Fact] + public void when_given_brace_brace_cOMmAnDnAMe_brace_brace_should_replace_with_the_Name_from_the_configuration() + { + TokenReplacer.replace_tokens(configuration, "Hi! My name is [[cOMmAnDnAMe]]").ShouldEqual("Hi! My name is " + name); } [Fact] - public void if_given_brace_brace_ServerName_brace_brace_should_NOT_replace_with_the_Name_from_the_configuration() + public void if_given_brace_brace_Version_brace_brace_should_NOT_replace_with_the_Name_from_the_configuration() { TokenReplacer.replace_tokens(configuration, "Go to [[Version]]").ShouldNotContain(name); } @@ -98,5 +105,24 @@ public void if_given_an_null_value_should_return_the_ll_value() TokenReplacer.replace_tokens(configuration, null).ShouldEqual(""); } } + + public class when_using_TokenReplacer_with_a_Dictionary : TokenReplacerSpecsBase + { + public Dictionary tokens = new Dictionary(); + private string value = "sweet"; + + + public override void Because() + { + tokens.Add("dude", value); + } + + [Fact] + public void when_given_a_proper_token_it_should_replace_with_the_dictionary_value() + { + TokenReplacer.replace_tokens(tokens, "Hi! My name is [[dude]]").ShouldEqual("Hi! My name is " + value); + } + + } } } \ No newline at end of file diff --git a/src/chocolatey/infrastructure.app/services/TemplateService.cs b/src/chocolatey/infrastructure.app/services/TemplateService.cs index 0f77224cd4..d77e16a788 100644 --- a/src/chocolatey/infrastructure.app/services/TemplateService.cs +++ b/src/chocolatey/infrastructure.app/services/TemplateService.cs @@ -17,6 +17,7 @@ namespace chocolatey.infrastructure.app.services { using System; using System.IO; + using System.Management.Automation; using System.Reflection; using System.Text; using configuration; @@ -75,7 +76,8 @@ public void generate(ChocolateyConfiguration configuration) } catch (Exception) { - if (configuration.RegularOutput) this.Log().Warn("Property {0} was not found for replacement in the Template Values.".format_with(property.Key)); + if (configuration.RegularOutput) this.Log().Debug("Property {0} will be added to additional properties.".format_with(property.Key)); + tokens.AdditionalProperties.Add(property.Key, property.Value); } } @@ -84,6 +86,10 @@ public void generate(ChocolateyConfiguration configuration) { this.Log().Debug(() => " {0}={1}".format_with(propertyInfo.Name, propertyInfo.GetValue(tokens, null))); } + foreach (var additionalProperty in tokens.AdditionalProperties.or_empty_list_if_null()) + { + this.Log().Debug(() => " {0}={1}".format_with(additionalProperty.Key, additionalProperty.Value)); + } var defaultTemplateOverride = _fileSystem.combine_paths(ApplicationParameters.TemplatesLocation, "default"); if (string.IsNullOrWhiteSpace(configuration.NewCommand.TemplateName) && !_fileSystem.directory_exists(defaultTemplateOverride)) @@ -118,6 +124,7 @@ public void generate(ChocolateyConfiguration configuration) public void generate_file_from_template(ChocolateyConfiguration configuration, TemplateValues tokens, string template, string fileLocation, Encoding encoding) { template = TokenReplacer.replace_tokens(tokens, template); + template = TokenReplacer.replace_tokens(tokens.AdditionalProperties, template); if (configuration.RegularOutput) this.Log().Info(() => "Generating template to a file{0} at '{1}'".format_with(Environment.NewLine, fileLocation)); this.Log().Debug(() => "{0}".format_with(template)); diff --git a/src/chocolatey/infrastructure.app/templates/TemplateValues.cs b/src/chocolatey/infrastructure.app/templates/TemplateValues.cs index 5ab827fa0e..5faeae7b31 100644 --- a/src/chocolatey/infrastructure.app/templates/TemplateValues.cs +++ b/src/chocolatey/infrastructure.app/templates/TemplateValues.cs @@ -15,11 +15,15 @@ namespace chocolatey.infrastructure.app.templates { + using System; + using System.Collections.Generic; + public class TemplateValues { public TemplateValues() { set_normal(); + AdditionalProperties = new Dictionary(StringComparer.InvariantCultureIgnoreCase); } public void set_normal() @@ -73,9 +77,11 @@ public string PackageNameLower public string ChecksumType { get; set; } public string Checksum64 { get; set; } public string ChecksumType64 { get; set; } + public IDictionary AdditionalProperties { get; private set; } public static readonly string NamePropertyName = "PackageName"; public static readonly string VersionPropertyName = "PackageVersion"; public static readonly string MaintainerPropertyName = "MaintainerName"; + } } \ No newline at end of file diff --git a/src/chocolatey/infrastructure/tokens/TokenReplacer.cs b/src/chocolatey/infrastructure/tokens/TokenReplacer.cs index 82b1f0de19..ca28274d11 100644 --- a/src/chocolatey/infrastructure/tokens/TokenReplacer.cs +++ b/src/chocolatey/infrastructure/tokens/TokenReplacer.cs @@ -16,6 +16,7 @@ namespace chocolatey.infrastructure.tokens { using System.Collections.Generic; + using System.Net.Mime; using System.Reflection; using System.Text.RegularExpressions; @@ -26,6 +27,8 @@ public static string replace_tokens(TConfig configuration, string textT if (string.IsNullOrEmpty(textToReplace)) return string.Empty; IDictionary dictionary = create_dictionary_from_configuration(configuration); + if (dictionary.Count == 0) return textToReplace; + var regex = new Regex("{0}(?\\w+){1}".format_with(Regex.Escape(tokenPrefix), Regex.Escape(tokenSuffix))); string output = regex.Replace(textToReplace, m => @@ -48,6 +51,8 @@ public static string replace_tokens(TConfig configuration, string textT private static IDictionary create_dictionary_from_configuration(TConfig configuration) { + if (configuration is IDictionary) return configuration as IDictionary; + var propertyDictionary = new Dictionary(); foreach (PropertyInfo property in configuration.GetType().GetProperties()) {