Skip to content

Commit

Permalink
(GH-658) Allow custom key/value args in choco new
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ferventcoder committed Mar 11, 2016
1 parent a347364 commit 6c828b6
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
28 changes: 27 additions & 1 deletion src/chocolatey.tests/infrastructure/tokens/TokenReplacerSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

namespace chocolatey.tests.infrastructure.tokens
{
using System.Collections.Generic;
using Should;
using chocolatey.infrastructure.app.configuration;
using chocolatey.infrastructure.tokens;
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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<string, string> tokens = new Dictionary<string, string>();
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);
}

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}

Expand All @@ -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))
Expand Down Expand Up @@ -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));
Expand Down
6 changes: 6 additions & 0 deletions src/chocolatey/infrastructure.app/templates/TemplateValues.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string>(StringComparer.InvariantCultureIgnoreCase);
}

public void set_normal()
Expand Down Expand Up @@ -73,9 +77,11 @@ public string PackageNameLower
public string ChecksumType { get; set; }
public string Checksum64 { get; set; }
public string ChecksumType64 { get; set; }
public IDictionary<string, string> AdditionalProperties { get; private set; }

public static readonly string NamePropertyName = "PackageName";
public static readonly string VersionPropertyName = "PackageVersion";
public static readonly string MaintainerPropertyName = "MaintainerName";

}
}
5 changes: 5 additions & 0 deletions src/chocolatey/infrastructure/tokens/TokenReplacer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
namespace chocolatey.infrastructure.tokens
{
using System.Collections.Generic;
using System.Net.Mime;
using System.Reflection;
using System.Text.RegularExpressions;

Expand All @@ -26,6 +27,8 @@ public static string replace_tokens<TConfig>(TConfig configuration, string textT
if (string.IsNullOrEmpty(textToReplace)) return string.Empty;

IDictionary<string, string> dictionary = create_dictionary_from_configuration(configuration);
if (dictionary.Count == 0) return textToReplace;

var regex = new Regex("{0}(?<key>\\w+){1}".format_with(Regex.Escape(tokenPrefix), Regex.Escape(tokenSuffix)));

string output = regex.Replace(textToReplace, m =>
Expand All @@ -48,6 +51,8 @@ public static string replace_tokens<TConfig>(TConfig configuration, string textT

private static IDictionary<string, string> create_dictionary_from_configuration<TConfig>(TConfig configuration)
{
if (configuration is IDictionary<string, string>) return configuration as IDictionary<string, string>;

var propertyDictionary = new Dictionary<string, string>();
foreach (PropertyInfo property in configuration.GetType().GetProperties())
{
Expand Down

0 comments on commit 6c828b6

Please sign in to comment.