From 217cc7af21a501daabea16dc7331401a19706bf4 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Thu, 16 Sep 2021 17:34:31 -0400 Subject: add IgnoreModFilePaths option to package --- src/SMAPI.ModBuildConfig/DeployModTask.cs | 32 ++++++++++++++++++++-- .../Framework/ModFileManager.cs | 9 ++++-- src/SMAPI.ModBuildConfig/build/smapi.targets | 1 + 3 files changed, 37 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/SMAPI.ModBuildConfig/DeployModTask.cs b/src/SMAPI.ModBuildConfig/DeployModTask.cs index 9ee6be12..0c64aed3 100644 --- a/src/SMAPI.ModBuildConfig/DeployModTask.cs +++ b/src/SMAPI.ModBuildConfig/DeployModTask.cs @@ -8,6 +8,7 @@ using System.Text.RegularExpressions; using Microsoft.Build.Framework; using Microsoft.Build.Utilities; using StardewModdingAPI.ModBuildConfig.Framework; +using StardewModdingAPI.Toolkit.Utilities; namespace StardewModdingAPI.ModBuildConfig { @@ -45,9 +46,12 @@ namespace StardewModdingAPI.ModBuildConfig [Required] public bool EnableModZip { get; set; } - /// Custom comma-separated regex patterns matching files to ignore when deploying or zipping the mod. + /// A comma-separated list of regex patterns matching files to ignore when deploying or zipping the mod. public string IgnoreModFilePatterns { get; set; } + /// A comma-separated list of relative file paths to ignore when deploying or zipping the mod. + public string IgnoreModFilePaths { get; set; } + /********* ** Public methods @@ -70,10 +74,11 @@ namespace StardewModdingAPI.ModBuildConfig try { // parse ignore patterns + string[] ignoreFilePaths = this.GetCustomIgnoreFilePaths().ToArray(); Regex[] ignoreFilePatterns = this.GetCustomIgnorePatterns().ToArray(); // get mod info - ModFileManager package = new ModFileManager(this.ProjectDir, this.TargetDir, ignoreFilePatterns, validateRequiredModFiles: this.EnableModDeploy || this.EnableModZip); + ModFileManager package = new ModFileManager(this.ProjectDir, this.TargetDir, ignoreFilePaths, ignoreFilePatterns, validateRequiredModFiles: this.EnableModDeploy || this.EnableModZip); // deploy mod files if (this.EnableModDeploy) @@ -157,6 +162,29 @@ namespace StardewModdingAPI.ModBuildConfig } } + /// Get the custom relative file paths provided by the user to ignore. + private IEnumerable GetCustomIgnoreFilePaths() + { + if (string.IsNullOrWhiteSpace(this.IgnoreModFilePaths)) + yield break; + + foreach (string raw in this.IgnoreModFilePaths.Split(',')) + { + string path; + try + { + path = PathUtilities.NormalizePath(raw); + } + catch (Exception ex) + { + this.Log.LogWarning($"[mod build package] Ignored invalid <{nameof(this.IgnoreModFilePaths)}> path {raw}:\n{ex}"); + continue; + } + + yield return path; + } + } + /// Copy the mod files into the game's mod folder. /// The files to include. /// The folder path to create with the mod files. diff --git a/src/SMAPI.ModBuildConfig/Framework/ModFileManager.cs b/src/SMAPI.ModBuildConfig/Framework/ModFileManager.cs index 3d9b206b..fbb91193 100644 --- a/src/SMAPI.ModBuildConfig/Framework/ModFileManager.cs +++ b/src/SMAPI.ModBuildConfig/Framework/ModFileManager.cs @@ -28,10 +28,11 @@ namespace StardewModdingAPI.ModBuildConfig.Framework /// Construct an instance. /// The folder containing the project files. /// The folder containing the build output. + /// The custom relative file paths provided by the user to ignore. /// Custom regex patterns matching files to ignore when deploying or zipping the mod. /// Whether to validate that required mod files like the manifest are present. /// The mod package isn't valid. - public ModFileManager(string projectDir, string targetDir, Regex[] ignoreFilePatterns, bool validateRequiredModFiles) + public ModFileManager(string projectDir, string targetDir, string[] ignoreFilePaths, Regex[] ignoreFilePatterns, bool validateRequiredModFiles) { this.Files = new Dictionary(StringComparer.OrdinalIgnoreCase); @@ -47,7 +48,7 @@ namespace StardewModdingAPI.ModBuildConfig.Framework string relativePath = entry.Item1; FileInfo file = entry.Item2; - if (!this.ShouldIgnore(file, relativePath, ignoreFilePatterns)) + if (!this.ShouldIgnore(file, relativePath, ignoreFilePaths, ignoreFilePatterns)) this.Files[relativePath] = file; } @@ -149,8 +150,9 @@ namespace StardewModdingAPI.ModBuildConfig.Framework /// Get whether a build output file should be ignored. /// The file to check. /// The file's relative path in the package. + /// The custom relative file paths provided by the user to ignore. /// Custom regex patterns matching files to ignore when deploying or zipping the mod. - private bool ShouldIgnore(FileInfo file, string relativePath, Regex[] ignoreFilePatterns) + private bool ShouldIgnore(FileInfo file, string relativePath, string[] ignoreFilePaths, Regex[] ignoreFilePatterns) { bool IsAssemblyFile(string baseName) { @@ -181,6 +183,7 @@ namespace StardewModdingAPI.ModBuildConfig.Framework || this.EqualsInvariant(file.Name, "Thumbs.db") // custom ignore patterns + || ignoreFilePaths.Any(p => p == relativePath) || ignoreFilePatterns.Any(p => p.IsMatch(relativePath)); } diff --git a/src/SMAPI.ModBuildConfig/build/smapi.targets b/src/SMAPI.ModBuildConfig/build/smapi.targets index 74ac56e6..b254dd7c 100644 --- a/src/SMAPI.ModBuildConfig/build/smapi.targets +++ b/src/SMAPI.ModBuildConfig/build/smapi.targets @@ -94,6 +94,7 @@ TargetDir="$(TargetDir)" GameModsDir="$(GameModsPath)" IgnoreModFilePatterns="$(IgnoreModFilePatterns)" + IgnoreModFilePaths="$(IgnoreModFilePaths)" /> -- cgit