Skip to content

Commit

Permalink
VCI-688: Add ModuleCompressionOptionsBuilder class
Browse files Browse the repository at this point in the history
  • Loading branch information
krankenbro committed Sep 14, 2023
1 parent 05c4a42 commit d57eb03
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 10 deletions.
24 changes: 14 additions & 10 deletions src/VirtoCommerce.Build/Utils/ArtifactPacker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,38 @@ public static void CompressPlatform(string sourceDirectory, string outputZipPath
CompressionTasks.CompressZip(sourceDirectory, outputZipPath);
}

public static void CompressModule(string sourceDirectory, string outputZipPath, string moduleId, string moduleManifestPath, string webProjectDirectory, IEnumerable<string> ignoreList, IEnumerable<string> keepList, string[] moduleContentFolders)
public static void CompressModule(Action<ModuleCompressionOptionsBuilder> optionsBuilderAction)
{
FileSystemTasks.CopyFileToDirectory(moduleManifestPath, sourceDirectory,
var optionsBuilder = new ModuleCompressionOptionsBuilder();
optionsBuilderAction(optionsBuilder);
var options = optionsBuilder.Build();

FileSystemTasks.CopyFileToDirectory(options.ModuleManifestPath, options.SourceDirectory,
FileExistsPolicy.Overwrite);

//Exclude all ignored files and *module files not related to compressed module
var ignoreModuleFilesRegex = new Regex(@".+Module\..*", RegexOptions.IgnoreCase);
var includeModuleFilesRegex =
new Regex(@$".*{moduleId}(Module)?\..*", RegexOptions.IgnoreCase);
new Regex(@$".*{options.ModuleId}(Module)?\..*", RegexOptions.IgnoreCase);

foreach (var folderName in moduleContentFolders)
foreach (var folderName in options.ModuleContentFolders)
{
var sourcePath = Path.Combine(webProjectDirectory, folderName);
var sourcePath = Path.Combine(options.WebProjectDirectory, folderName);

if (Directory.Exists(sourcePath))
{
FileSystemTasks.CopyDirectoryRecursively(sourcePath, Path.Combine(sourceDirectory, folderName),
FileSystemTasks.CopyDirectoryRecursively(sourcePath, Path.Combine(options.SourceDirectory, folderName),
DirectoryExistsPolicy.Merge, FileExistsPolicy.Overwrite);
}
}

bool FilesFilter(FileInfo x) =>
(!SkipFileByList(x.Name, ignoreList) &&
!SkipFileByRegex(x.Name, ignoreModuleFilesRegex)) || KeepFileByList(x.Name, keepList) ||
(!SkipFileByList(x.Name, options.IgnoreList) &&
!SkipFileByRegex(x.Name, ignoreModuleFilesRegex)) || KeepFileByList(x.Name, options.KeepList) ||
KeepFileByRegex(x.Name, includeModuleFilesRegex);

FileSystemTasks.DeleteFile(outputZipPath);
CompressionTasks.CompressZip(sourceDirectory, outputZipPath, FilesFilter);
FileSystemTasks.DeleteFile(options.OutputZipPath);
CompressionTasks.CompressZip(options.SourceDirectory, options.OutputZipPath, FilesFilter);
}


Expand Down
78 changes: 78 additions & 0 deletions src/VirtoCommerce.Build/Utils/ModuleCompressionOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Utils
{
public class ModuleCompressionOptions
{
public string SourceDirectory { get; set; }
public string OutputZipPath { get; set; }
public string ModuleId { get; set; }
public string ModuleManifestPath { get; set; }
public string WebProjectDirectory { get; set; }
public IEnumerable<string> IgnoreList { get; set; } = new List<string>();
public IEnumerable<string> KeepList { get; set; } = new List<string>();
public IEnumerable<string> ModuleContentFolders { get; set; } = new List<string>();
}

public class ModuleCompressionOptionsBuilder
{
private readonly ModuleCompressionOptions options = new ModuleCompressionOptions();

public ModuleCompressionOptionsBuilder WithSourceDirectory(string sourceDirectory)
{
options.SourceDirectory = sourceDirectory;
return this;
}

public ModuleCompressionOptionsBuilder WithOutputZipPath(string outputZipPath)
{
options.OutputZipPath = outputZipPath;
return this;
}

public ModuleCompressionOptionsBuilder WithModuleId(string moduleId)
{
options.ModuleId = moduleId;
return this;
}

public ModuleCompressionOptionsBuilder WithModuleManifestPath(string moduleManifestPath)
{
options.ModuleManifestPath = moduleManifestPath;
return this;
}

public ModuleCompressionOptionsBuilder WithWebProjectDirectory(string webProjectDirectory)
{
options.WebProjectDirectory = webProjectDirectory;
return this;
}

public ModuleCompressionOptionsBuilder WithIgnoreList(IEnumerable<string> ignoreList)
{
options.IgnoreList = ignoreList;
return this;
}

public ModuleCompressionOptionsBuilder WithKeepList(IEnumerable<string> keepList)
{
options.KeepList = keepList;
return this;
}

public ModuleCompressionOptionsBuilder WithModuleContentFolders(IEnumerable<string> moduleContentFolders)
{
options.ModuleContentFolders = moduleContentFolders;
return this;
}

public ModuleCompressionOptions Build()
{
return options;
}
}
}

0 comments on commit d57eb03

Please sign in to comment.