diff --git a/ChocolateStoreCore/ChocolateStoreCore.csproj b/ChocolateStoreCore/ChocolateStoreCore.csproj index 423da35..eb2e212 100644 --- a/ChocolateStoreCore/ChocolateStoreCore.csproj +++ b/ChocolateStoreCore/ChocolateStoreCore.csproj @@ -17,9 +17,9 @@ ChocolateStoreCore Download, modify and cache chocolatey packages locally - 1.2.0 - 1.2.0.9 - 1.2.0.9 + 1.2.1 + 1.2.1.8 + 1.2.1.8 Robert Zaufall Robert Zaufall IT Consulting (c)2023 by Robert Zaufall diff --git a/ChocolateStoreCore/Helpers/FileIOHelper.cs b/ChocolateStoreCore/Helpers/FileIOHelper.cs index 226c67d..9ff4477 100644 --- a/ChocolateStoreCore/Helpers/FileIOHelper.cs +++ b/ChocolateStoreCore/Helpers/FileIOHelper.cs @@ -18,7 +18,7 @@ public interface IFileHelper List GetDirectoryNames(string path); List GetNupkgFiles(string path); MemoryStream GetStream(string path); - string GetContentFromZip(string path, string fileName); + Dictionary GetContentFromZip(string path, string fileName); bool UpdateContentInZip(string path, string fileName, string content); bool WriteDummyFile(string path); bool FileCreate(string path, Stream stream); @@ -53,19 +53,23 @@ public MemoryStream GetStream(string path) { return new MemoryStream(File.ReadAllBytes(path)); } - public string GetContentFromZip(string path, string fileName) + public Dictionary GetContentFromZip(string path, string filePattern) { + var contents = new Dictionary(); using (ZipArchive archive = ZipFile.Open(path, ZipArchiveMode.Read)) { - var entry = archive.Entries.Where(x => x.FullName.Equals(fileName, StringComparison.OrdinalIgnoreCase)).FirstOrDefault(); - if (entry != null) + var regex = new System.Text.RegularExpressions.Regex(filePattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase); + var entries = archive.Entries.Where(x => regex.IsMatch(x.FullName)).ToList(); + + foreach (var entry in entries) { using var reader = new StreamReader(entry.Open()); - return reader.ReadToEnd(); + contents.Add(entry.FullName, reader.ReadToEnd()); } } - return null; + return contents; } + public bool UpdateContentInZip(string path, string fileName, string content) { using (ZipArchive archive = ZipFile.Open(path, ZipArchiveMode.Update)) diff --git a/ChocolateStoreCore/Models/Settings.cs b/ChocolateStoreCore/Models/Settings.cs index f2d2aa7..2553b52 100644 --- a/ChocolateStoreCore/Models/Settings.cs +++ b/ChocolateStoreCore/Models/Settings.cs @@ -30,6 +30,7 @@ public interface ISettings string LogFile { get; set; } string LogLevel { get; set; } string FolderDelimiter { get; set; } + string InstallFilesPattern { get; set; } LogEventLevel GetLogLevel(); } @@ -97,6 +98,7 @@ public string DownloadListPath public string LogFile { get; set; } public string LogLevel { get; set; } public string FolderDelimiter { get; set; } = "."; + public string InstallFilesPattern { get; set; } = "tools/(ChocolateyInstall\\.ps|data\\.ps)"; public LogEventLevel GetLogLevel() { diff --git a/ChocolateStoreCore/PackageCacher.cs b/ChocolateStoreCore/PackageCacher.cs index 0e1eb8a..0e19642 100644 --- a/ChocolateStoreCore/PackageCacher.cs +++ b/ChocolateStoreCore/PackageCacher.cs @@ -18,7 +18,6 @@ public interface IPackageCacher public class PackageCacher : IPackageCacher { - private const string INSTALL_FILE = "tools/chocolateyInstall.ps1"; private readonly ISettings _settings; private readonly ILogger _logger; private readonly IFileHelper _fileHelper; @@ -164,33 +163,37 @@ public bool CachePackage(ChocolateyPackage package, string sourcePath, string ta try { - var content = _fileHelper.GetContentFromZip(targetPackagePath, INSTALL_FILE); + var filesWithContent = _fileHelper.GetContentFromZip(targetPackagePath, _settings.InstallFilesPattern); - if (!string.IsNullOrWhiteSpace(content)) + foreach (var fileWithContent in filesWithContent) { - (var contentNew, var downloads, _) = _chocolateyHelper.ExtractAndRewriteUrls(content, folder, _settings.LocalRepoUrl, package.Id, package.Version.OriginalVersion); - - if (!string.IsNullOrWhiteSpace(contentNew) && downloads.Count > 0 && !_fileHelper.DirectoryExists(folder)) + var content = fileWithContent.Value; + if (!string.IsNullOrWhiteSpace(content)) { - if (_fileHelper.DirectoryCreateDirectory(folder)) + (var contentNew, var downloads, _) = _chocolateyHelper.ExtractAndRewriteUrls(content, folder, _settings.LocalRepoUrl, package.Id, package.Version.OriginalVersion); + + if (!string.IsNullOrWhiteSpace(contentNew) && downloads.Count > 0 && !_fileHelper.DirectoryExists(folder)) { - downloads.ForEach(x => + if (_fileHelper.DirectoryCreateDirectory(folder)) { - _logger.LogWarning("Downloading {x_Url}", x.Url); - if (!whatif && !_fileHelper.FileExists(x.Path)) + downloads.ForEach(x => { - var returnPath = _httpHelper.DownloadFile(x.Url, x.Path); - if (returnPath == null || !_fileHelper.FileExists(returnPath)) + _logger.LogWarning("Downloading {x_Url}", x.Url); + if (!whatif && !_fileHelper.FileExists(x.Path)) { - throw new DownloadException(string.Format("Download not successful: {0}", x.Url)); + var returnPath = _httpHelper.DownloadFile(x.Url, x.Path); + if (returnPath == null || !_fileHelper.FileExists(returnPath)) + { + throw new DownloadException(string.Format("Download not successful: {0}", x.Url)); + } } - } - if (whatif) - { - _fileHelper.WriteDummyFile(x.Path); - } - }); - return _fileHelper.UpdateContentInZip(targetPackagePath, INSTALL_FILE, contentNew); + if (whatif) + { + _fileHelper.WriteDummyFile(x.Path); + } + }); + return _fileHelper.UpdateContentInZip(targetPackagePath, fileWithContent.Key, contentNew); + } } } } diff --git a/ChocolateStoreCore/appsettings.json b/ChocolateStoreCore/appsettings.json index 0376feb..4185070 100644 --- a/ChocolateStoreCore/appsettings.json +++ b/ChocolateStoreCore/appsettings.json @@ -20,6 +20,7 @@ "HttpHandlerLifetime": "10", "LogFile": "log.txt", "LogLevel": "Warning", - "FolderDelimiter": "." + "FolderDelimiter": ".", + "InstallFilesPattern": "tools/(ChocolateyInstall\\.ps|data\\.ps)" } } diff --git a/ChocolateStoreCoreTests/PackageCacherTests.cs b/ChocolateStoreCoreTests/PackageCacherTests.cs index fee72bc..afe6050 100644 --- a/ChocolateStoreCoreTests/PackageCacherTests.cs +++ b/ChocolateStoreCoreTests/PackageCacherTests.cs @@ -144,7 +144,7 @@ public void CachePackage() var download2 = fixture.Create(); var downloadPath = fixture.Create(); var downloads = new List{ download1, download2 }; - var installFileContent = fixture.Create(); + var installFilesContent = fixture.Create>(); string path = fixture.Create(); var chocolateyHelper = fixture.Freeze>(); @@ -160,7 +160,7 @@ public void CachePackage() fileHelper.Setup(_ => _.FileExists(download1.Path)).Returns(true); fileHelper.Setup(_ => _.FileExists(download2.Path)).Returns(true); fileHelper.Setup(_ => _.FileExists(downloadPath)).Returns(true); - fileHelper.Setup(_ => _.GetContentFromZip(It.IsAny(), It.IsAny())).Returns(installFileContent); + fileHelper.Setup(_ => _.GetContentFromZip(It.IsAny(), It.IsAny())).Returns(installFilesContent); fileHelper.Setup(_ => _.UpdateContentInZip(It.IsAny(), It.IsAny(), It.IsAny())).Returns(true); fileHelper.Setup(_ => _.DirectoryExists(It.IsAny())).Returns(false); fileHelper.Setup(_ => _.DirectoryCreateDirectory(It.IsAny())).Returns(true); @@ -214,7 +214,7 @@ public void CachePackage_WhatIf() var download2 = fixture.Create(); var downloadPath = fixture.Create(); var downloads = new List { download1, download2 }; - var installFileContent = fixture.Create(); + var installFilesContent = fixture.Create>(); string sourcePath = fixture.Create(); string targetPath = fixture.Create(); @@ -233,7 +233,7 @@ public void CachePackage_WhatIf() fileHelper.Setup(_ => _.FileExists(download1.Path)).Returns(true); fileHelper.Setup(_ => _.FileExists(download2.Path)).Returns(true); fileHelper.Setup(_ => _.FileExists(downloadPath)).Returns(true); - fileHelper.Setup(_ => _.GetContentFromZip(It.IsAny(), It.IsAny())).Returns(installFileContent); + fileHelper.Setup(_ => _.GetContentFromZip(It.IsAny(), It.IsAny())).Returns(installFilesContent); fileHelper.Setup(_ => _.UpdateContentInZip(It.IsAny(), It.IsAny(), It.IsAny())).Returns(true); fileHelper.Setup(_ => _.DirectoryExists(It.IsAny())).Returns(false); fileHelper.Setup(_ => _.DirectoryCreateDirectory(It.IsAny())).Returns(true); diff --git a/ChocolateStoreCoreTests/SettingsTests.cs b/ChocolateStoreCoreTests/SettingsTests.cs index dfab5d9..4081c3c 100644 --- a/ChocolateStoreCoreTests/SettingsTests.cs +++ b/ChocolateStoreCoreTests/SettingsTests.cs @@ -35,7 +35,8 @@ public void InstantiationTest() {"ChocolateyConfiguration:LogFile", "16"}, {"ChocolateyConfiguration:HttpTimeoutOverAll", "17"}, {"ChocolateyConfiguration:LogLevel", "19"}, - {"ChocolateyConfiguration:FolderDelimiter", "20"} + {"ChocolateyConfiguration:FolderDelimiter", "20"}, + {"ChocolateyConfiguration:InstallFilesPattern", "22"}, }; var configuration = new ConfigurationBuilder().AddInMemoryCollection(myConfiguration).Build(); @@ -66,6 +67,7 @@ public void InstantiationTest() settings.HttpTimeoutOverAll.Should().Be(17); settings.LogLevel.Should().Be("19"); settings.FolderDelimiter.Should().Be("20"); + settings.InstallFilesPattern.Should().Be("22"); } [Fact] @@ -82,6 +84,7 @@ public void InstantiationDefaultsTest() settings.HttpRetrySleep.Should().Be(30); settings.HttpDelay.Should().Be(0); settings.FolderDelimiter.Should().Be("."); + settings.InstallFilesPattern.Should().Be("tools/(ChocolateyInstall\\.ps|data\\.ps)"); } } } diff --git a/README.md b/README.md index 2d4a8a9..3155348 100644 --- a/README.md +++ b/README.md @@ -112,6 +112,7 @@ Run `ChocolateStoreCore.exe` with existing `appsettings.json` configuration in s "LogFile": "log.txt", "LogLevel": "Warning", "FolderDelimiter": "." + "InstallFilesPattern": "tools/(ChocolateyInstall\\.ps|data\\.ps)" } } ``` @@ -126,6 +127,7 @@ Run `ChocolateStoreCore.exe` with existing `appsettings.json` configuration in s | `HttpHandlerLifetime` | [min] | | `HttpRetrySleep` | [s] - Time to wait for retry (default: 30s) | | `HttpDelay` | [s] - Time to wait after package download (default: 0s) | +| `InstallFilesPattern` | RegEx pattern for files to be patched. Default: ChocolateyInstall.ps, Data.ps | #### `download.txt` example ``` diff --git a/version.txt b/version.txt index e563f37..d0399b8 100644 --- a/version.txt +++ b/version.txt @@ -1 +1 @@ -1.2.1.1 +1.3.0.1