diff --git a/src/chocolatey.tests/infrastructure.app/services/TemplateServiceSpecs.cs b/src/chocolatey.tests/infrastructure.app/services/TemplateServiceSpecs.cs index fb9f9f9659..1ca436debe 100644 --- a/src/chocolatey.tests/infrastructure.app/services/TemplateServiceSpecs.cs +++ b/src/chocolatey.tests/infrastructure.app/services/TemplateServiceSpecs.cs @@ -231,6 +231,7 @@ public class when_generate_is_called : TemplateServiceSpecsBase private readonly ChocolateyConfiguration config = new ChocolateyConfiguration(); private readonly List files = new List(); private readonly HashSet directoryCreated = new HashSet(StringComparer.InvariantCultureIgnoreCase); + private readonly UTF8Encoding utf8WithoutBOM = new UTF8Encoding(false); public override void Context() { @@ -250,6 +251,8 @@ public override void Context() fileSystem.Setup(x => x.directory_exists(It.IsAny())).Returns(dirPath => dirPath.EndsWith("templates\\default")); fileSystem.Setup(x => x.write_file(It.IsAny(), It.IsAny(), Encoding.UTF8)) .Callback((string filePath, string fileContent, Encoding encoding) => files.Add(filePath)); + fileSystem.Setup(x => x.write_file(It.IsAny(), It.IsAny(), utf8WithoutBOM)) + .Callback((string filePath, string fileContent, Encoding encoding) => files.Add(filePath)); fileSystem.Setup(x => x.delete_directory_if_exists(It.IsAny(), true)); fileSystem.Setup(x => x.create_directory_if_not_exists(It.IsAny())).Callback( (string directory) => @@ -322,6 +325,7 @@ public class when_generate_is_called_with_nested_folders : TemplateServiceSpecsB private readonly ChocolateyConfiguration config = new ChocolateyConfiguration(); private readonly List files = new List(); private readonly HashSet directoryCreated = new HashSet(StringComparer.InvariantCultureIgnoreCase); + private readonly UTF8Encoding utf8WithoutBOM = new UTF8Encoding(false); public override void Context() { @@ -341,6 +345,8 @@ public override void Context() fileSystem.Setup(x => x.directory_exists(It.IsAny())).Returns(dirPath => dirPath.EndsWith("templates\\test")); fileSystem.Setup(x => x.write_file(It.IsAny(), It.IsAny(), Encoding.UTF8)) .Callback((string filePath, string fileContent, Encoding encoding) => files.Add(filePath)); + fileSystem.Setup(x => x.write_file(It.IsAny(), It.IsAny(), utf8WithoutBOM)) + .Callback((string filePath, string fileContent, Encoding encoding) => files.Add(filePath)); fileSystem.Setup(x => x.delete_directory_if_exists(It.IsAny(), true)); fileSystem.Setup(x => x.get_files(It.IsAny(), "*.*", SearchOption.AllDirectories)) .Returns(new[] { "templates\\test\\template.nuspec", "templates\\test\\random.txt", "templates\\test\\tools\\chocolateyInstall.ps1", "templates\\test\\tools\\lower\\another.ps1" }); diff --git a/src/chocolatey/infrastructure.app/services/TemplateService.cs b/src/chocolatey/infrastructure.app/services/TemplateService.cs index 3c5095e267..a91c3b72e3 100644 --- a/src/chocolatey/infrastructure.app/services/TemplateService.cs +++ b/src/chocolatey/infrastructure.app/services/TemplateService.cs @@ -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 _templateBinaryExtensions = new List { ".exe", ".msi", ".msu", ".msp", ".mst", @@ -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); @@ -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);