From 79d824cfb0e0cc9ff4fab0e0c546a83c0edaae1c Mon Sep 17 00:00:00 2001 From: vnugent Date: Tue, 7 May 2024 17:01:22 -0400 Subject: initial commit --- src/Modules/ModuleFileManager.cs | 92 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 src/Modules/ModuleFileManager.cs (limited to 'src/Modules/ModuleFileManager.cs') diff --git a/src/Modules/ModuleFileManager.cs b/src/Modules/ModuleFileManager.cs new file mode 100644 index 0000000..4d6ed69 --- /dev/null +++ b/src/Modules/ModuleFileManager.cs @@ -0,0 +1,92 @@ +using System; +using System.IO; +using System.Threading.Tasks; + +using VNLib.Tools.Build.Executor.Model; +using VNLib.Tools.Build.Executor.Extensions; +using VNLib.Tools.Build.Executor.Constants; + +namespace VNLib.Tools.Build.Executor.Modules +{ + + public sealed class ModuleFileManager(BuildConfig config, IModuleData ModData) : IModuleFileManager + { + private readonly IDirectoryIndex Index = config.Index; + + /// + public string OutputDir => Path.Combine(Index.OutputDir.FullName, ModData.ModuleName); + + /// + public async Task CopyArtifactToOutputAsync(IProject project, FileInfo file) + { + string targetDir = GetProjectTargetDir(project); + + //Project artifacts are versioned by the latest git commit hash + string outputFile = Path.Combine(targetDir, file.Name); + + //Create the target directory if it doesn't exist + Directory.CreateDirectory(targetDir); + + //Copy the file to the output directory + FileInfo output = file.CopyTo(outputFile, true); + + //Compute the file hash of the new output file + await output.ComputeFileHashAsync(config.HashFuncName); + } + + /// + public DirectoryInfo GetArtifactOutputDir(IProject project) + { + string path = GetProjectTargetDir(project); + return new DirectoryInfo(path); + } + + /// + public Task ReadCheckSumAsync(IProject project) + { + string sumFile = Path.Combine(Index.SumDir.FullName, $"{ModData.ModuleName}-{project.GetSafeProjectName()}.json"); + return File.Exists(sumFile) ? File.ReadAllBytesAsync(sumFile) : Task.FromResult(null); + } + + /// + public Task WriteChecksumAsync(IProject project, byte[] fileData) + { + //Create sum file inside the sum directory + string sumFile = Path.Combine(Index.SumDir.FullName, $"{ModData.ModuleName}-{project.GetSafeProjectName()}.json"); + return File.WriteAllBytesAsync(sumFile, fileData); + } + + /// + public async Task WriteFileAsync(ModuleFileType type, byte[] fileData) + { + //Get the file path for the given type + string filePath = type switch + { + //Catalog is written to the version pointed to by the latest git commit hash + ModuleFileType.Catalog => $"{OutputDir}/{GetLatestTagOrSha()}/index.json", + ModuleFileType.GitHistory => $"{OutputDir}/git.json", + ModuleFileType.LatestHash => $"{OutputDir}/@latest", + ModuleFileType.VersionHistory => $"{OutputDir}/versions.json", + //Store project archive + ModuleFileType.Archive => $"{OutputDir}/{GetLatestTagOrSha()}/archive.tgz", + _ => throw new ArgumentOutOfRangeException(nameof(type), type, null), + }; + + await File.WriteAllBytesAsync($"{filePath}", fileData); + //Return new file handle + return new FileInfo(filePath); + } + + private string GetProjectTargetDir(IProject project) + { + //get last tag + return Path.Combine(OutputDir, GetLatestTagOrSha(), project.GetSafeProjectName()); + } + + private string GetLatestTagOrSha() + { + return ModData.Repository.Head.Tip.Sha; + } + + } +} \ No newline at end of file -- cgit