Skip to content

Commit

Permalink
add parameter for file to be patched ("InstallFilesPattern")
Browse files Browse the repository at this point in the history
  • Loading branch information
robertZaufall committed Aug 5, 2024
1 parent e51b3fa commit ddaa0e4
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 36 deletions.
6 changes: 3 additions & 3 deletions ChocolateStoreCore/ChocolateStoreCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
<PropertyGroup>
<Product>ChocolateStoreCore</Product>
<Description>Download, modify and cache chocolatey packages locally</Description>
<Version>1.2.0</Version>
<FileVersion>1.2.0.9</FileVersion>
<AssemblyVersion>1.2.0.9</AssemblyVersion>
<Version>1.2.1</Version>
<FileVersion>1.2.1.8</FileVersion>
<AssemblyVersion>1.2.1.8</AssemblyVersion>
<Authors>Robert Zaufall</Authors>
<Company>Robert Zaufall IT Consulting</Company>
<Copyright>(c)2023 by Robert Zaufall</Copyright>
Expand Down
16 changes: 10 additions & 6 deletions ChocolateStoreCore/Helpers/FileIOHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public interface IFileHelper
List<string> GetDirectoryNames(string path);
List<string> GetNupkgFiles(string path);
MemoryStream GetStream(string path);
string GetContentFromZip(string path, string fileName);
Dictionary<string, string> GetContentFromZip(string path, string fileName);
bool UpdateContentInZip(string path, string fileName, string content);
bool WriteDummyFile(string path);
bool FileCreate(string path, Stream stream);
Expand Down Expand Up @@ -53,19 +53,23 @@ public MemoryStream GetStream(string path)
{
return new MemoryStream(File.ReadAllBytes(path));
}
public string GetContentFromZip(string path, string fileName)
public Dictionary<string, string> GetContentFromZip(string path, string filePattern)
{
var contents = new Dictionary<string, string>();
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))
Expand Down
2 changes: 2 additions & 0 deletions ChocolateStoreCore/Models/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down Expand Up @@ -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()
{
Expand Down
43 changes: 23 additions & 20 deletions ChocolateStoreCore/PackageCacher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PackageCacher> _logger;
private readonly IFileHelper _fileHelper;
Expand Down Expand Up @@ -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);
}
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion ChocolateStoreCore/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"HttpHandlerLifetime": "10",
"LogFile": "log.txt",
"LogLevel": "Warning",
"FolderDelimiter": "."
"FolderDelimiter": ".",
"InstallFilesPattern": "tools/(ChocolateyInstall\\.ps|data\\.ps)"
}
}
8 changes: 4 additions & 4 deletions ChocolateStoreCoreTests/PackageCacherTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public void CachePackage()
var download2 = fixture.Create<Download>();
var downloadPath = fixture.Create<string>();
var downloads = new List<Download>{ download1, download2 };
var installFileContent = fixture.Create<string>();
var installFilesContent = fixture.Create<Dictionary<string, string>>();
string path = fixture.Create<string>();

var chocolateyHelper = fixture.Freeze<Mock<IChocolateyHelper>>();
Expand All @@ -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<string>(), It.IsAny<string>())).Returns(installFileContent);
fileHelper.Setup(_ => _.GetContentFromZip(It.IsAny<string>(), It.IsAny<string>())).Returns(installFilesContent);
fileHelper.Setup(_ => _.UpdateContentInZip(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>())).Returns(true);
fileHelper.Setup(_ => _.DirectoryExists(It.IsAny<string>())).Returns(false);
fileHelper.Setup(_ => _.DirectoryCreateDirectory(It.IsAny<string>())).Returns(true);
Expand Down Expand Up @@ -214,7 +214,7 @@ public void CachePackage_WhatIf()
var download2 = fixture.Create<Download>();
var downloadPath = fixture.Create<string>();
var downloads = new List<Download> { download1, download2 };
var installFileContent = fixture.Create<string>();
var installFilesContent = fixture.Create<Dictionary<string, string>>();

string sourcePath = fixture.Create<string>();
string targetPath = fixture.Create<string>();
Expand All @@ -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<string>(), It.IsAny<string>())).Returns(installFileContent);
fileHelper.Setup(_ => _.GetContentFromZip(It.IsAny<string>(), It.IsAny<string>())).Returns(installFilesContent);
fileHelper.Setup(_ => _.UpdateContentInZip(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>())).Returns(true);
fileHelper.Setup(_ => _.DirectoryExists(It.IsAny<string>())).Returns(false);
fileHelper.Setup(_ => _.DirectoryCreateDirectory(It.IsAny<string>())).Returns(true);
Expand Down
5 changes: 4 additions & 1 deletion ChocolateStoreCoreTests/SettingsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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]
Expand All @@ -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)");
}
}
}
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
}
}
```
Expand All @@ -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
```
Expand Down
2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.2.1.1
1.3.0.1

0 comments on commit ddaa0e4

Please sign in to comment.