Skip to content

Commit

Permalink
(GH-542) Manage Packages Templates as Packages
Browse files Browse the repository at this point in the history
Manage package creation templates through regular choco commands. The
package must have a specially named id ("*.template") and have a
"templates" folder that contains the package template.
  • Loading branch information
ferventcoder committed Jan 28, 2016
1 parent 9c2a554 commit 6c13ca1
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/chocolatey/infrastructure.app/ApplicationParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public static class ApplicationParameters
public static readonly string ShimsLocation = _fileSystem.combine_paths(InstallLocation, "bin");
public static readonly string ChocolateyPackageInfoStoreLocation = _fileSystem.combine_paths(InstallLocation, ".chocolatey");
public static readonly string ExtensionsLocation = _fileSystem.combine_paths(InstallLocation, "extensions");
public static readonly string TemplatesLocation = _fileSystem.combine_paths(InstallLocation, "templates");
public static readonly string ChocolateyCommunityFeedPushSource = "https://chocolatey.org/";
public static readonly string ChocolateyCommunityFeedSource = "https://chocolatey.org/api/v2/";
public static readonly string UserAgent = "Chocolatey Command Line";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ public void handle_package_result(PackageResult packageResult, ChocolateyConfigu
if (packageResult.Success)
{
handle_extension_packages(config, packageResult);
handle_template_packages(config, packageResult);
}

_packageInfoService.save_package_information(pkgInfo);
Expand Down Expand Up @@ -694,6 +695,7 @@ private void uninstall_cleanup(ChocolateyConfiguration config, PackageResult pac
ensure_bad_package_path_is_clean(config, packageResult);
remove_rollback_if_exists(packageResult);
handle_extension_packages(config, packageResult);
handle_template_packages(config, packageResult);

if (config.Force)
{
Expand Down Expand Up @@ -722,10 +724,9 @@ private void uninstall_cleanup(ChocolateyConfiguration config, PackageResult pac
private void handle_extension_packages(ChocolateyConfiguration config, PackageResult packageResult)
{
if (packageResult == null) return;
_fileSystem.create_directory_if_not_exists(ApplicationParameters.ExtensionsLocation);

if (!packageResult.Name.to_lower().EndsWith(".extension") && !packageResult.Name.to_lower().EndsWith(".extensions")) return;

_fileSystem.create_directory_if_not_exists(ApplicationParameters.ExtensionsLocation);
var extensionsFolderName = packageResult.Name.to_lower().Replace(".extensions", string.Empty).Replace(".extension", string.Empty);
var pkgExtensions = _fileSystem.combine_paths(ApplicationParameters.ExtensionsLocation, extensionsFolderName);

Expand All @@ -738,9 +739,7 @@ private void handle_extension_packages(ChocolateyConfiguration config, PackageRe
if (packageResult.InstallLocation == null) return;

_fileSystem.create_directory_if_not_exists(pkgExtensions);

var extensionsFolder = _fileSystem.combine_paths(packageResult.InstallLocation, "extensions");

var extensionFolderToCopy = _fileSystem.directory_exists(extensionsFolder) ? extensionsFolder : packageResult.InstallLocation;

FaultTolerance.try_catch_with_logging_exception(
Expand All @@ -759,6 +758,43 @@ private void handle_extension_packages(ChocolateyConfiguration config, PackageRe
}
}

private void handle_template_packages(ChocolateyConfiguration config, PackageResult packageResult)
{
if (packageResult == null) return;
if (!packageResult.Name.to_lower().EndsWith(".template")) return;

_fileSystem.create_directory_if_not_exists(ApplicationParameters.TemplatesLocation);
var templateFolderName = packageResult.Name.to_lower().Replace(".template", string.Empty);
var installTemplatePath = _fileSystem.combine_paths(ApplicationParameters.TemplatesLocation, templateFolderName);

FaultTolerance.try_catch_with_logging_exception(
() => _fileSystem.delete_directory_if_exists(installTemplatePath, recursive: true),
"Attempted to remove '{0}' but had an error".format_with(installTemplatePath));

if (!config.CommandName.is_equal_to(CommandNameType.uninstall.to_string()))
{
if (packageResult.InstallLocation == null) return;

_fileSystem.create_directory_if_not_exists(installTemplatePath);
var templatesPath = _fileSystem.combine_paths(packageResult.InstallLocation, "templates");
var templatesFolderToCopy = _fileSystem.directory_exists(templatesPath) ? templatesPath : packageResult.InstallLocation;

FaultTolerance.try_catch_with_logging_exception(
() => _fileSystem.copy_directory(templatesFolderToCopy, installTemplatePath, overwriteExisting: true),
"Attempted to copy{0} '{1}'{0} to '{2}'{0} but had an error".format_with(Environment.NewLine, templatesFolderToCopy, installTemplatePath));

string logMessage = " Installed/updated {0} template.".format_with(templateFolderName);
this.Log().Warn(logMessage);
packageResult.Messages.Add(new ResultMessage(ResultType.Note, logMessage));
}
else
{
string logMessage = " Uninstalled {0} template.".format_with(templateFolderName);
this.Log().Warn(logMessage);
packageResult.Messages.Add(new ResultMessage(ResultType.Note, logMessage));
}
}

private void ensure_bad_package_path_is_clean(ChocolateyConfiguration config, PackageResult packageResult)
{
if (packageResult.InstallLocation == null) return;
Expand Down
4 changes: 2 additions & 2 deletions src/chocolatey/infrastructure.app/services/TemplateService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public void generate(ChocolateyConfiguration configuration)
this.Log().Debug(() => " {0}={1}".format_with(propertyInfo.Name, propertyInfo.GetValue(tokens, null)));
}

var defaultTemplateOverride = _fileSystem.combine_paths(ApplicationParameters.InstallLocation, "templates", "default");
var defaultTemplateOverride = _fileSystem.combine_paths(ApplicationParameters.TemplatesLocation, "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);
Expand All @@ -97,7 +97,7 @@ public void generate(ChocolateyConfiguration configuration)
{
configuration.NewCommand.TemplateName = string.IsNullOrWhiteSpace(configuration.NewCommand.TemplateName) ? "default" : configuration.NewCommand.TemplateName;

var templatePath = _fileSystem.combine_paths(ApplicationParameters.InstallLocation, "templates", configuration.NewCommand.TemplateName);
var templatePath = _fileSystem.combine_paths(ApplicationParameters.TemplatesLocation, 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));
Expand Down

0 comments on commit 6c13ca1

Please sign in to comment.