From c99237e7451808b41bb598aa5242fd9a49bfcc57 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sun, 24 Jun 2018 23:33:21 -0400 Subject: add mod build config option to ignore custom files by regex (#549) --- docs/mod-build-config.md | 10 +++++++ src/SMAPI.ModBuildConfig/DeployModTask.cs | 33 +++++++++++++++++++++- .../Framework/ModFileManager.cs | 19 +++++++++---- src/SMAPI.ModBuildConfig/build/smapi.targets | 1 + src/SMAPI.ModBuildConfig/package.nuspec | 2 ++ 5 files changed, 58 insertions(+), 7 deletions(-) diff --git a/docs/mod-build-config.md b/docs/mod-build-config.md index 74ee34e4..32762580 100644 --- a/docs/mod-build-config.md +++ b/docs/mod-build-config.md @@ -121,6 +121,16 @@ or you have multiple installs, you can specify the path yourself. There's two wa The configuration will check your custom path first, then fall back to the default paths (so it'll still compile on a different computer). +### Ignore files +If you don't want to include a file in the mod folder or release zip: +* Make sure it's not copied to the build output. For a DLL, make sure the reference is [not marked 'copy local'](https://msdn.microsoft.com/en-us/library/t1zz5y8c(v=vs.100).aspx). +* Or add this to your `.csproj` file under the `\.txt$, \.pdf$ + ``` + This is a comma-delimited list of regular expression patterns. If any pattern matches a file's + relative path in your mod folder, that file won't be included. + ### Unit test projects **(upcoming in 2.1)** 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; } + /// Custom comma-separated regex patterns matching files to ignore when deploying or zipping the mod. + 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 *********/ + /// Get the custom ignore patterns provided by the user. + private IEnumerable 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; + } + } + /// 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 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 /// Construct an instance. /// The folder containing the project files. /// The folder containing the build output. + /// Custom regex patterns matching files to ignore when deploying or zipping the mod. /// The mod package isn't valid. - public ModFileManager(string projectDir, string targetDir) + public ModFileManager(string projectDir, string targetDir, Regex[] ignoreFilePatterns) { this.Files = new Dictionary(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 *********/ /// Get whether a build output file should be ignored. - /// The file info. - private bool ShouldIgnore(FileInfo file) + /// The file to check. + /// The file's relative path in the package. + /// Custom regex patterns matching files to ignore when deploying or zipping the mod. + 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)); } /// Get a case-insensitive dictionary matching the given JSON. 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)" /> 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. -- cgit