diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/SMAPI.ModBuildConfig/DeployModTask.cs | 33 | ||||
-rw-r--r-- | src/SMAPI.ModBuildConfig/Framework/ModFileManager.cs | 19 | ||||
-rw-r--r-- | src/SMAPI.ModBuildConfig/build/smapi.targets | 1 | ||||
-rw-r--r-- | src/SMAPI.ModBuildConfig/package.nuspec | 2 |
4 files changed, 48 insertions, 7 deletions
diff --git a/src/SMAPI.ModBuildConfig/DeployModTask.cs b/src/SMAPI.ModBuildConfig/DeployModTask.cs index a5725a81..73971279 100644 --- a/src/SMAPI.ModBuildConfig/DeployModTask.cs +++ b/src/SMAPI.ModBuildConfig/DeployModTask.cs @@ -2,6 +2,8 @@ using System; using System.Collections.Generic; using System.IO; using System.IO.Compression; +using System.Linq; +using System.Text.RegularExpressions; using Microsoft.Build.Framework; using Microsoft.Build.Utilities; using StardewModdingAPI.ModBuildConfig.Framework; @@ -42,6 +44,9 @@ namespace StardewModdingAPI.ModBuildConfig [Required] public bool EnableModZip { get; set; } + /// <summary>Custom comma-separated regex patterns matching files to ignore when deploying or zipping the mod.</summary> + public string IgnoreModFilePatterns { get; set; } + /********* ** Public methods @@ -55,8 +60,11 @@ namespace StardewModdingAPI.ModBuildConfig try { + // parse ignore patterns + Regex[] ignoreFilePatterns = this.GetCustomIgnorePatterns().ToArray(); + // get mod info - ModFileManager package = new ModFileManager(this.ProjectDir, this.TargetDir); + ModFileManager package = new ModFileManager(this.ProjectDir, this.TargetDir, ignoreFilePatterns); // deploy mod files if (this.EnableModDeploy) @@ -91,6 +99,29 @@ namespace StardewModdingAPI.ModBuildConfig /********* ** Private methods *********/ + /// <summary>Get the custom ignore patterns provided by the user.</summary> + private IEnumerable<Regex> GetCustomIgnorePatterns() + { + if (string.IsNullOrWhiteSpace(this.IgnoreModFilePatterns)) + yield break; + + foreach (string raw in this.IgnoreModFilePatterns.Split(',')) + { + Regex regex; + try + { + regex = new Regex(raw.Trim(), RegexOptions.IgnoreCase); + } + catch (Exception ex) + { + this.Log.LogWarning($"Ignored invalid <{nameof(this.IgnoreModFilePatterns)}> pattern {raw}:\n{ex}"); + continue; + } + + yield return regex; + } + } + /// <summary>Copy the mod files into the game's mod folder.</summary> /// <param name="files">The files to include.</param> /// <param name="modFolderPath">The folder path to create with the mod files.</param> diff --git a/src/SMAPI.ModBuildConfig/Framework/ModFileManager.cs b/src/SMAPI.ModBuildConfig/Framework/ModFileManager.cs index 41e0201d..524aeaf7 100644 --- a/src/SMAPI.ModBuildConfig/Framework/ModFileManager.cs +++ b/src/SMAPI.ModBuildConfig/Framework/ModFileManager.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.IO; using System.Linq; +using System.Text.RegularExpressions; using System.Web.Script.Serialization; using StardewModdingAPI.Toolkit; @@ -26,8 +27,9 @@ namespace StardewModdingAPI.ModBuildConfig.Framework /// <summary>Construct an instance.</summary> /// <param name="projectDir">The folder containing the project files.</param> /// <param name="targetDir">The folder containing the build output.</param> + /// <param name="ignoreFilePatterns">Custom regex patterns matching files to ignore when deploying or zipping the mod.</param> /// <exception cref="UserErrorException">The mod package isn't valid.</exception> - public ModFileManager(string projectDir, string targetDir) + public ModFileManager(string projectDir, string targetDir, Regex[] ignoreFilePatterns) { this.Files = new Dictionary<string, FileInfo>(StringComparer.InvariantCultureIgnoreCase); @@ -72,8 +74,8 @@ namespace StardewModdingAPI.ModBuildConfig.Framework if (hasProjectTranslations && this.EqualsInvariant(relativeDirPath, "i18n")) continue; - // ignore release zips - if (this.ShouldIgnore(file)) + // handle ignored files + if (this.ShouldIgnore(file, relativePath, ignoreFilePatterns)) continue; // add file @@ -142,8 +144,10 @@ namespace StardewModdingAPI.ModBuildConfig.Framework ** Private methods *********/ /// <summary>Get whether a build output file should be ignored.</summary> - /// <param name="file">The file info.</param> - private bool ShouldIgnore(FileInfo file) + /// <param name="file">The file to check.</param> + /// <param name="relativePath">The file's relative path in the package.</param> + /// <param name="ignoreFilePatterns">Custom regex patterns matching files to ignore when deploying or zipping the mod.</param> + private bool ShouldIgnore(FileInfo file, string relativePath, Regex[] ignoreFilePatterns) { return // release zips @@ -159,7 +163,10 @@ namespace StardewModdingAPI.ModBuildConfig.Framework // OS metadata files || this.EqualsInvariant(file.Name, ".DS_Store") - || this.EqualsInvariant(file.Name, "Thumbs.db"); + || this.EqualsInvariant(file.Name, "Thumbs.db") + + // custom ignore patterns + || ignoreFilePatterns.Any(p => p.IsMatch(relativePath)); } /// <summary>Get a case-insensitive dictionary matching the given JSON.</summary> diff --git a/src/SMAPI.ModBuildConfig/build/smapi.targets b/src/SMAPI.ModBuildConfig/build/smapi.targets index 0869be66..9946e1a6 100644 --- a/src/SMAPI.ModBuildConfig/build/smapi.targets +++ b/src/SMAPI.ModBuildConfig/build/smapi.targets @@ -166,6 +166,7 @@ ProjectDir="$(ProjectDir)" TargetDir="$(TargetDir)" GameDir="$(GamePath)" + IgnoreModFilePatterns="$(IgnoreModFilePatterns)" /> </Target> </Project> diff --git a/src/SMAPI.ModBuildConfig/package.nuspec b/src/SMAPI.ModBuildConfig/package.nuspec index fa26875b..1b1b25fd 100644 --- a/src/SMAPI.ModBuildConfig/package.nuspec +++ b/src/SMAPI.ModBuildConfig/package.nuspec @@ -16,6 +16,8 @@ - Added support for Stardew Valley 1.3. - Added support for unit test projects. - Added C# analyzers to warn about implicit conversions of Netcode fields in Stardew Valley 1.3. + - Added option to ignore files by regex pattern. + - Added reference to new SMAPI DLL. </releaseNotes> </metadata> </package> |