Skip to content

Commit

Permalink
Merge pull request #2064 from TheCakeIsNaOH/utf8-no-bom
Browse files Browse the repository at this point in the history
(GH-1364) Template create .nuspec encoded without BOM
  • Loading branch information
gep13 authored Apr 16, 2021
2 parents 4b17e01 + 21e8d77 commit 071c9c3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ public class when_generate_is_called : TemplateServiceSpecsBase
private readonly ChocolateyConfiguration config = new ChocolateyConfiguration();
private readonly List<string> files = new List<string>();
private readonly HashSet<string> directoryCreated = new HashSet<string>(StringComparer.InvariantCultureIgnoreCase);
private readonly UTF8Encoding utf8WithoutBOM = new UTF8Encoding(false);

public override void Context()
{
Expand All @@ -250,6 +251,8 @@ public override void Context()
fileSystem.Setup(x => x.directory_exists(It.IsAny<string>())).Returns<string>(dirPath => dirPath.EndsWith("templates\\default"));
fileSystem.Setup(x => x.write_file(It.IsAny<string>(), It.IsAny<string>(), Encoding.UTF8))
.Callback((string filePath, string fileContent, Encoding encoding) => files.Add(filePath));
fileSystem.Setup(x => x.write_file(It.IsAny<string>(), It.IsAny<string>(), utf8WithoutBOM))
.Callback((string filePath, string fileContent, Encoding encoding) => files.Add(filePath));
fileSystem.Setup(x => x.delete_directory_if_exists(It.IsAny<string>(), true));
fileSystem.Setup(x => x.create_directory_if_not_exists(It.IsAny<string>())).Callback(
(string directory) =>
Expand Down Expand Up @@ -322,6 +325,7 @@ public class when_generate_is_called_with_nested_folders : TemplateServiceSpecsB
private readonly ChocolateyConfiguration config = new ChocolateyConfiguration();
private readonly List<string> files = new List<string>();
private readonly HashSet<string> directoryCreated = new HashSet<string>(StringComparer.InvariantCultureIgnoreCase);
private readonly UTF8Encoding utf8WithoutBOM = new UTF8Encoding(false);

public override void Context()
{
Expand All @@ -341,6 +345,8 @@ public override void Context()
fileSystem.Setup(x => x.directory_exists(It.IsAny<string>())).Returns<string>(dirPath => dirPath.EndsWith("templates\\test"));
fileSystem.Setup(x => x.write_file(It.IsAny<string>(), It.IsAny<string>(), Encoding.UTF8))
.Callback((string filePath, string fileContent, Encoding encoding) => files.Add(filePath));
fileSystem.Setup(x => x.write_file(It.IsAny<string>(), It.IsAny<string>(), utf8WithoutBOM))
.Callback((string filePath, string fileContent, Encoding encoding) => files.Add(filePath));
fileSystem.Setup(x => x.delete_directory_if_exists(It.IsAny<string>(), true));
fileSystem.Setup(x => x.get_files(It.IsAny<string>(), "*.*", SearchOption.AllDirectories))
.Returns(new[] { "templates\\test\\template.nuspec", "templates\\test\\random.txt", "templates\\test\\tools\\chocolateyInstall.ps1", "templates\\test\\tools\\lower\\another.ps1" });
Expand Down
11 changes: 8 additions & 3 deletions src/chocolatey/infrastructure.app/services/TemplateService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ namespace chocolatey.infrastructure.app.services

public class TemplateService : ITemplateService
{
private readonly UTF8Encoding utf8WithoutBOM = new UTF8Encoding(false);
private readonly IFileSystem _fileSystem;
private readonly IList<string> _templateBinaryExtensions = new List<string> {
".exe", ".msi", ".msu", ".msp", ".mst",
Expand Down Expand Up @@ -104,7 +105,7 @@ public void generate(ChocolateyConfiguration configuration)
var defaultTemplateOverride = _fileSystem.combine_paths(ApplicationParameters.TemplatesLocation, "default");
if (string.IsNullOrWhiteSpace(configuration.NewCommand.TemplateName) && (!_fileSystem.directory_exists(defaultTemplateOverride) || configuration.NewCommand.UseOriginalTemplate))
{
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, NuspecTemplate.Template, _fileSystem.combine_paths(packageLocation, "{0}.nuspec".format_with(tokens.PackageNameLower)), utf8WithoutBOM);
generate_file_from_template(configuration, tokens, ChocolateyInstallTemplate.Template, _fileSystem.combine_paths(packageToolsLocation, "chocolateyinstall.ps1"), Encoding.UTF8);
generate_file_from_template(configuration, tokens, ChocolateyBeforeModifyTemplate.Template, _fileSystem.combine_paths(packageToolsLocation, "chocolateybeforemodify.ps1"), Encoding.UTF8);
generate_file_from_template(configuration, tokens, ChocolateyUninstallTemplate.Template, _fileSystem.combine_paths(packageToolsLocation, "chocolateyuninstall.ps1"), Encoding.UTF8);
Expand All @@ -125,9 +126,13 @@ public void generate(ChocolateyConfiguration configuration)
{
var packageFileLocation = file.Replace(templatePath, packageLocation);
var fileExtension = _fileSystem.get_file_extension(packageFileLocation);
if (fileExtension.is_equal_to(".nuspec")) packageFileLocation = _fileSystem.combine_paths(packageLocation, "{0}.nuspec".format_with(tokens.PackageNameLower));

if (_templateBinaryExtensions.Contains(fileExtension))
if (fileExtension.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, utf8WithoutBOM);
}
else if (_templateBinaryExtensions.Contains(fileExtension))
{
this.Log().Debug(" Treating template file ('{0}') as binary instead of replacing templated values.".format_with(_fileSystem.get_file_name(file)));
_fileSystem.copy_file(file, packageFileLocation, overwriteExisting:true);
Expand Down

0 comments on commit 071c9c3

Please sign in to comment.