Skip to content

Commit

Permalink
Merge branch 'stable'
Browse files Browse the repository at this point in the history
* stable:
  (GH-76) Create custom templates
  (maint) comment about extension including dot
  (maint) resharper settings
  • Loading branch information
ferventcoder committed Sep 18, 2015
2 parents 38d3565 + 43bf97a commit 9952345
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 15 deletions.
5 changes: 4 additions & 1 deletion src/chocolatey.sln.DotSettings
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
<s:String x:Key="/Default/CodeStyle/CodeCleanup/Profiles/=sensible/@EntryIndexedValue">&lt;?xml version="1.0" encoding="utf-16"?&gt;&lt;Profile name="sensible"&gt;&lt;CSArrangeThisQualifier&gt;True&lt;/CSArrangeThisQualifier&gt;&lt;CSMakeFieldReadonly&gt;True&lt;/CSMakeFieldReadonly&gt;&lt;CSOptimizeUsings&gt;&lt;OptimizeUsings&gt;True&lt;/OptimizeUsings&gt;&lt;EmbraceInRegion&gt;False&lt;/EmbraceInRegion&gt;&lt;RegionName&gt;&lt;/RegionName&gt;&lt;/CSOptimizeUsings&gt;&lt;CSShortenReferences&gt;True&lt;/CSShortenReferences&gt;&lt;CSReformatCode&gt;True&lt;/CSReformatCode&gt;&lt;CSRemoveCodeRedundancies&gt;True&lt;/CSRemoveCodeRedundancies&gt;&lt;CSUseAutoProperty&gt;True&lt;/CSUseAutoProperty&gt;&lt;CSUseVar&gt;&lt;BehavourStyle&gt;CAN_CHANGE_TO_IMPLICIT&lt;/BehavourStyle&gt;&lt;LocalVariableStyle&gt;IMPLICIT_WHEN_INITIALIZER_HAS_TYPE&lt;/LocalVariableStyle&gt;&lt;ForeachVariableStyle&gt;ALWAYS_EXPLICIT&lt;/ForeachVariableStyle&gt;&lt;/CSUseVar&gt;&lt;CSharpFormatDocComments&gt;True&lt;/CSharpFormatDocComments&gt;&lt;CSUpdateFileHeader&gt;True&lt;/CSUpdateFileHeader&gt;&lt;/Profile&gt;</s:String>
<s:String x:Key="/Default/CodeStyle/CodeCleanup/SilentCleanupProfile/@EntryValue">sensible</s:String>
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CommonFormatter/USE_INDENTS_FROM_MAIN_LANGUAGE_IN_FILE/@EntryValue">False</s:Boolean>
<s:Int64 x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/WRAP_LIMIT/@EntryValue">195</s:Int64>
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/PLACE_CATCH_ON_NEW_LINE/@EntryValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/PLACE_ELSE_ON_NEW_LINE/@EntryValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/PLACE_FINALLY_ON_NEW_LINE/@EntryValue">True</s:Boolean>
<s:Int64 x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/WRAP_LIMIT/@EntryValue">220</s:Int64>
<s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/WRAP_LINES/@EntryValue">False</s:Boolean>
<s:Boolean x:Key="/Default/CodeStyle/CSharpUsing/AddImportsToDeepestScope/@EntryValue">True</s:Boolean>
<s:String x:Key="/Default/CodeStyle/FileHeader/FileHeaderText/@EntryValue">Copyright © 2011 - Present RealDimensions Software, LLC&#xD;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 =>
Expand All @@ -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<string> unparsedArguments, ChocolateyConfiguration configuration)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@ public NewCommandConfiguration()
TemplateProperties = new Dictionary<string, string>(StringComparer.InvariantCultureIgnoreCase);
}

public string TemplateName { get; set; }
public string Name { get; set; }
public bool AutomaticPackage { get; set; }
public IDictionary<string, string> TemplateProperties { get; private set; }
Expand Down
43 changes: 32 additions & 11 deletions src/chocolatey/infrastructure.app/services/TemplateService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
namespace chocolatey.infrastructure.app.services
{
using System;
using System.IO;
using System.Reflection;
using System.Text;
using configuration;
Expand Down Expand Up @@ -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));
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -103,4 +124,4 @@ public void generate_file_from_template(ChocolateyConfiguration configuration, T
_fileSystem.write_file(fileLocation, template, encoding);
}
}
}
}
2 changes: 1 addition & 1 deletion src/chocolatey/infrastructure/filesystem/IFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public interface IFileSystem
string get_file_name_without_extension(string filePath);

/// <summary>
/// Gets the extension.
/// Gets the extension (including the ".").
/// </summary>
/// <param name="filePath">The file path.</param>
/// <returns>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.</returns>
Expand Down

0 comments on commit 9952345

Please sign in to comment.