summaryrefslogtreecommitdiff
path: root/src/SMAPI.ModBuildConfig
diff options
context:
space:
mode:
authorTyler <gibbstyler7@gmail.com>2022-10-18 20:03:28 -0500
committerTyler <gibbstyler7@gmail.com>2022-10-18 20:03:28 -0500
commit61d6ec12daee843f758e5f828a713a72a767a94b (patch)
treed4f8531038f24e06d0604bf68fcac4fe17d89e61 /src/SMAPI.ModBuildConfig
parent0e4dd8a7b446d85d4603d55043af42aac5968b5a (diff)
downloadSMAPI-61d6ec12daee843f758e5f828a713a72a767a94b.tar.gz
SMAPI-61d6ec12daee843f758e5f828a713a72a767a94b.tar.bz2
SMAPI-61d6ec12daee843f758e5f828a713a72a767a94b.zip
add detailed manifest validation errors at build time
Diffstat (limited to 'src/SMAPI.ModBuildConfig')
-rw-r--r--src/SMAPI.ModBuildConfig/DeployModTask.cs33
-rw-r--r--src/SMAPI.ModBuildConfig/Framework/ModFileManager.cs12
2 files changed, 32 insertions, 13 deletions
diff --git a/src/SMAPI.ModBuildConfig/DeployModTask.cs b/src/SMAPI.ModBuildConfig/DeployModTask.cs
index 88412d92..357e02b5 100644
--- a/src/SMAPI.ModBuildConfig/DeployModTask.cs
+++ b/src/SMAPI.ModBuildConfig/DeployModTask.cs
@@ -7,7 +7,10 @@ using System.Reflection;
using System.Text.RegularExpressions;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
+using Newtonsoft.Json;
using StardewModdingAPI.ModBuildConfig.Framework;
+using StardewModdingAPI.Toolkit.Serialization;
+using StardewModdingAPI.Toolkit.Serialization.Models;
using StardewModdingAPI.Toolkit.Utilities;
namespace StardewModdingAPI.ModBuildConfig
@@ -75,6 +78,34 @@ namespace StardewModdingAPI.ModBuildConfig
this.Log.LogMessage(MessageImportance.High, $"[mod build package] Handling build with options {string.Join(", ", properties)}");
}
+ // check if manifest file exists
+ FileInfo manifestFile = new(Path.Combine(this.ProjectDir, "manifest.json"));
+ if (!manifestFile.Exists)
+ {
+ this.Log.LogError("[mod build package] The mod does not have a manifest.json file.");
+ return false;
+ }
+
+ // check if the json is valid
+ Manifest manifest;
+ try
+ {
+ new JsonHelper().ReadJsonFileIfExists(manifestFile.FullName, out manifest);
+ } catch (JsonReaderException ex)
+ {
+ // log the inner exception, otherwise the message will be generic
+ Exception exToShow = ex.InnerException ?? ex;
+ this.Log.LogError($"[mod build package] Failed to parse manifest.json: {exToShow.Message}");
+ return false;
+ }
+
+ // validate the manifest's fields
+ if (!manifest.TryValidate(out string error))
+ {
+ this.Log.LogError($"[mod build package] The mod manifest is invalid: {error}");
+ return false;
+ }
+
if (!this.EnableModDeploy && !this.EnableModZip)
return true; // nothing to do
@@ -101,7 +132,7 @@ namespace StardewModdingAPI.ModBuildConfig
// create release zip
if (this.EnableModZip)
{
- string zipName = this.EscapeInvalidFilenameCharacters($"{this.ModFolderName} {package.GetManifestVersion()}.zip");
+ string zipName = this.EscapeInvalidFilenameCharacters($"{this.ModFolderName} {manifest.Version}.zip");
string zipPath = Path.Combine(this.ModZipPath, zipName);
this.Log.LogMessage(MessageImportance.High, $"[mod build package] Generating the release zip at {zipPath}...");
diff --git a/src/SMAPI.ModBuildConfig/Framework/ModFileManager.cs b/src/SMAPI.ModBuildConfig/Framework/ModFileManager.cs
index 80955f67..00f3f439 100644
--- a/src/SMAPI.ModBuildConfig/Framework/ModFileManager.cs
+++ b/src/SMAPI.ModBuildConfig/Framework/ModFileManager.cs
@@ -3,8 +3,6 @@ using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
-using StardewModdingAPI.Toolkit.Serialization;
-using StardewModdingAPI.Toolkit.Serialization.Models;
using StardewModdingAPI.Toolkit.Utilities;
namespace StardewModdingAPI.ModBuildConfig.Framework
@@ -113,16 +111,6 @@ namespace StardewModdingAPI.ModBuildConfig.Framework
return new Dictionary<string, FileInfo>(this.Files, StringComparer.OrdinalIgnoreCase);
}
- /// <summary>Get a semantic version from the mod manifest.</summary>
- /// <exception cref="UserErrorException">The manifest is missing or invalid.</exception>
- public string GetManifestVersion()
- {
- if (!this.Files.TryGetValue(this.ManifestFileName, out FileInfo manifestFile) || !new JsonHelper().ReadJsonFileIfExists(manifestFile.FullName, out Manifest manifest))
- throw new InvalidOperationException($"The mod does not have a {this.ManifestFileName} file."); // shouldn't happen since we validate in constructor
-
- return manifest.Version.ToString();
- }
-
/*********
** Private methods