Skip to content

Commit

Permalink
(chocolatey#449) List installed template names
Browse files Browse the repository at this point in the history
Adds "-l"/"--list" switch to choco new.
This lists the custom templates that are installed in the templates
directory. This is accomplished by simply getting a list of the
sub-directories under the template directory and printing it out.
Also prints out the number of directories found.
  • Loading branch information
TheCakeIsNaOH committed Oct 1, 2021
1 parent d6282f5 commit ab4daca
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,18 @@ public void should_add_outputdirectory_to_the_option_set()
{
optionSet.Contains("outputdirectory").ShouldBeTrue();
}

[Fact]
public void should_add_list_to_the_option_set()
{
optionSet.Contains("list").ShouldBeTrue();
}

[Fact]
public void should_add_short_list_to_the_option_set()
{
optionSet.Contains("l").ShouldBeTrue();
}
}

public class when_handling_additional_argument_parsing : ChocolateyNewCommandSpecsBase
Expand Down Expand Up @@ -374,7 +386,7 @@ public override void Context()

public override void Because()
{
optionSet.Parse(new[] { "--name", "Bob", "--automaticpackage", "--template-name", "custom", "--version", "0.42.0", "--maintainer", "Loyd", "--outputdirectory", "c:\\packages" });
optionSet.Parse(new[] { "--name", "Bob", "--automaticpackage", "--template-name", "custom", "--version", "0.42.0", "--maintainer", "Loyd", "--outputdirectory", "c:\\packages","--list" });
}

[Fact]
Expand Down Expand Up @@ -413,6 +425,12 @@ public void should_outputdirectory_equal_packages()
{
configuration.OutputDirectory.ShouldEqual("c:\\packages");
}

[Fact]
public void should_list_equal_to_true()
{
configuration.NewCommand.List.ShouldBeTrue();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ namespace chocolatey.tests.infrastructure.app.services
using System.IO;
using System.Linq;
using System.Text;
using chocolatey.infrastructure.app;
using chocolatey.infrastructure.app.configuration;
using chocolatey.infrastructure.app.services;
using chocolatey.infrastructure.app.templates;
Expand Down Expand Up @@ -89,6 +90,18 @@ public void should_log_output_directory_if_outputdirectory_is_specified()
infos.Count.ShouldEqual(1);
infos[0].ShouldEqual("Would have generated a new package specification at c:\\packages\\Bob");
}

[Fact]
public void should_log_templates_directory_when_list()
{
config.NewCommand.List = true;

because();

var infos = MockLogger.MessagesFor(LogLevel.Info);
infos.Count.ShouldEqual(1);
infos[0].ShouldEqual("Would have listed templates in {0}".format_with(ApplicationParameters.TemplatesLocation));
}
}

public class when_generate_file_from_template_is_called : TemplateServiceSpecsBase
Expand Down Expand Up @@ -424,5 +437,78 @@ public void should_generate_all_files_and_directories_even_with_outputdirectory(
MockLogger.MessagesFor(LogLevel.Info).Last().ShouldEqual(string.Format(@"Successfully generated Bob package specification files{0} at 'c:\packages\Bob'", Environment.NewLine));
}
}

public class when_list_is_called_without_custom_templates : TemplateServiceSpecsBase
{
private Action because;
private readonly ChocolateyConfiguration config = new ChocolateyConfiguration();
private readonly List<string> files = new List<string>();
private readonly HashSet<string> directoryCreated = new HashSet<string>(StringComparer.InvariantCultureIgnoreCase);

public override void Because()
{
because = () => service.list(config);
}

public override void BeforeEachSpec()
{
MockLogger.reset();
files.Clear();
directoryCreated.Clear();
}

[Fact]
public void should_report_number_and_location_of_templates()
{
because();

var infos = MockLogger.MessagesFor(LogLevel.Info);
infos.Count.ShouldEqual(2);
infos.First().ShouldEqual(string.Format(@"No custom templates installed in {0}".format_with(ApplicationParameters.TemplatesLocation), Environment.NewLine));
infos.Last().ShouldEqual(string.Format(@"0 Templates found.", Environment.NewLine));
}

}

public class when_list_is_called_with_custom_templates : TemplateServiceSpecsBase
{
private Action because;
private readonly ChocolateyConfiguration config = new ChocolateyConfiguration();
private readonly List<string> files = new List<string>();
private readonly HashSet<string> directoryCreated = new HashSet<string>(StringComparer.InvariantCultureIgnoreCase);

public override void Context()
{
base.Context();

fileSystem.Setup(x => x.get_current_directory()).Returns("c:\\chocolatey");
fileSystem.Setup(x => x.combine_paths(It.IsAny<string>(), It.IsAny<string>()))
.Returns((string a, string[] b) => { return a + "\\" + b[0]; });
}

public override void Because()
{
because = () => service.list(config);
}

public override void BeforeEachSpec()
{
MockLogger.reset();
files.Clear();
directoryCreated.Clear();
}

[Fact]
public void should_report_number_and_location_of_templates()
{
because();

var infos = MockLogger.MessagesFor(LogLevel.Info);
//infos.Count.ShouldEqual(2);
//infos.First().ShouldEqual(string.Format(@"No custom templates installed in {0}".format_with(ApplicationParameters.TemplatesLocation), Environment.NewLine));
//infos.Last().ShouldEqual(string.Format(@"0 Templates found.", Environment.NewLine));
}

}
}
}
15 changes: 13 additions & 2 deletions src/chocolatey/infrastructure.app/commands/ChocolateyNewCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ public virtual void configure_argument_parser(OptionSet optionSet, ChocolateyCon
.Add("built-in|built-in-template|originaltemplate|original-template|use-original-template|use-built-in-template",
"BuiltInTemplate - Use the original built-in template instead of any override. Available in 0.9.10+.",
option => configuration.NewCommand.UseOriginalTemplate = option != null)
.Add("l|list",
"List - Display templates installed in{0} {1}\\templates Available in 0.12.0+".format_with(Environment.NewLine, ApplicationParameters.InstallLocation),
option => configuration.NewCommand.List = option != null)
;
//todo: more built-in templates
}
Expand Down Expand Up @@ -105,7 +108,7 @@ public virtual void handle_additional_argument_parsing(IList<string> unparsedArg

public virtual void handle_validation(ChocolateyConfiguration configuration)
{
if (string.IsNullOrWhiteSpace(configuration.NewCommand.Name))
if (string.IsNullOrWhiteSpace(configuration.NewCommand.Name) && !configuration.NewCommand.List)
{
throw new ApplicationException("Name is required. Please pass in a name for the new package.");
}
Expand Down Expand Up @@ -156,6 +159,7 @@ This is called automatic recompile.
choco new bob -a --version 1.2.0 maintainername=""'This guy'""
choco new bob silentargs=""'/S'"" url=""'https://somewhere/out/there.msi'""
choco new bob --outputdirectory Packages
choco new --list
NOTE: See scripting in the command reference (`choco -?`) for how to
write proper scripts and integrations.
Expand Down Expand Up @@ -186,7 +190,14 @@ public virtual void noop(ChocolateyConfiguration configuration)

public virtual void run(ChocolateyConfiguration configuration)
{
_templateService.generate(configuration);
if (configuration.NewCommand.List)
{
_templateService.list(configuration);
}
else
{
_templateService.generate(configuration);
}
}

public virtual bool may_require_admin_access()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,7 @@ public NewCommandConfiguration()
public bool AutomaticPackage { get; set; }
public IDictionary<string, string> TemplateProperties { get; private set; }
public bool UseOriginalTemplate { get; set; }
public bool List { get; set; }
}

[Serializable]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ public interface ITemplateService
{
void noop(ChocolateyConfiguration configuration);
void generate(ChocolateyConfiguration configuration);
void list(ChocolateyConfiguration configuration);
}
}
35 changes: 33 additions & 2 deletions src/chocolatey/infrastructure.app/services/TemplateService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ namespace chocolatey.infrastructure.app.services
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using configuration;
Expand All @@ -45,8 +46,15 @@ public TemplateService(IFileSystem fileSystem)

public void noop(ChocolateyConfiguration configuration)
{
var templateLocation = _fileSystem.combine_paths(configuration.OutputDirectory ?? _fileSystem.get_current_directory(), configuration.NewCommand.Name);
this.Log().Info(() => "Would have generated a new package specification at {0}".format_with(templateLocation));
if (configuration.NewCommand.List)
{
this.Log().Info(() => "Would have listed templates in {0}\\templates".format_with(ApplicationParameters.InstallLocation));
}
else
{
var templateLocation = _fileSystem.combine_paths(configuration.OutputDirectory ?? _fileSystem.get_current_directory(), configuration.NewCommand.Name);
this.Log().Info(() => "Would have generated a new package specification at {0}".format_with(templateLocation));
}
}

public void generate(ChocolateyConfiguration configuration)
Expand Down Expand Up @@ -159,5 +167,28 @@ public void generate_file_from_template(ChocolateyConfiguration configuration, T
_fileSystem.create_directory_if_not_exists(_fileSystem.get_directory_name(fileLocation));
_fileSystem.write_file(fileLocation, template, encoding);
}

public void list(ChocolateyConfiguration configuration)
{

var logger = ChocolateyLoggers.Normal;
if (configuration.QuietOutput) logger = ChocolateyLoggers.LogFileOnly;
var templateDirList = _fileSystem.get_directories(ApplicationParameters.TemplatesLocation).ToList();

if (templateDirList.Any())
{
foreach (var templateDir in templateDirList)
{
var templateName = _fileSystem.get_file_name(templateDir) ;
this.Log().Info(() => "{0}".format_with(templateName));
this.Log().Debug(() => "template {0} full path of {1}".format_with(templateName, templateDir));
}
}
else
{
this.Log().Info(() => "No custom templates installed in {0}".format_with(ApplicationParameters.TemplatesLocation));
}
this.Log().Info(configuration.QuietOutput ? logger : ChocolateyLoggers.Important, "{0} Templates found.".format_with(templateDirList.Count()));
}
}
}

0 comments on commit ab4daca

Please sign in to comment.