From 22965604bfa5858a089d842173cdebe6aaed0ed8 Mon Sep 17 00:00:00 2001
From: Jesse Plamondon-Willard <github@jplamondonw.com>
Date: Mon, 9 Apr 2018 19:30:17 -0400
Subject: add support for build message URLs (#471)

---
 src/SMAPI.ModBuildConfig/build/smapi.targets         |  2 +-
 .../Framework/RewriteRules/RedirectToUrlRule.cs      | 20 ++++++++------------
 src/SMAPI.Web/Startup.cs                             |  1 +
 3 files changed, 10 insertions(+), 13 deletions(-)

(limited to 'src')

diff --git a/src/SMAPI.ModBuildConfig/build/smapi.targets b/src/SMAPI.ModBuildConfig/build/smapi.targets
index d2e37101..a177840c 100644
--- a/src/SMAPI.ModBuildConfig/build/smapi.targets
+++ b/src/SMAPI.ModBuildConfig/build/smapi.targets
@@ -142,7 +142,7 @@
   <Target Name="BeforeBuild">
     <Error Condition="'$(OS)' != 'OSX' AND '$(OS)' != 'Unix' AND '$(OS)' != 'Windows_NT'" Text="The mod build package doesn't recognise OS type '$(OS)'." />
 
-    <Error Condition="!Exists('$(GamePath)')" Text="The mod build package can't find your game folder. You can specify where to find it; see details at https://github.com/Pathoschild/SMAPI/blob/develop/docs/mod-build-config.md#game-path." />
+    <Error Condition="!Exists('$(GamePath)')" Text="The mod build package can't find your game folder. You can specify where to find it; see https://smapi.io/buildmsg/game-path." />
     <Error Condition="'$(OS)' == 'Windows_NT' AND !Exists('$(GamePath)\Stardew Valley.exe')" Text="The mod build package found a game folder at $(GamePath), but it doesn't contain the Stardew Valley.exe file. If this folder is invalid, delete it and the package will autodetect another game install path." />
     <Error Condition="'$(OS)' != 'Windows_NT' AND !Exists('$(GamePath)\StardewValley.exe')" Text="The mod build package found a game folder at $(GamePath), but it doesn't contain the StardewValley.exe file. If this folder is invalid, delete it and the package will autodetect another game install path." />
     <Error Condition="!Exists('$(GamePath)\StardewModdingAPI.exe')" Text="The mod build package found a game folder at $(GamePath), but it doesn't contain SMAPI. You need to install SMAPI before building the mod." />
diff --git a/src/SMAPI.Web/Framework/RewriteRules/RedirectToUrlRule.cs b/src/SMAPI.Web/Framework/RewriteRules/RedirectToUrlRule.cs
index 0719e311..4bae0b4c 100644
--- a/src/SMAPI.Web/Framework/RewriteRules/RedirectToUrlRule.cs
+++ b/src/SMAPI.Web/Framework/RewriteRules/RedirectToUrlRule.cs
@@ -12,11 +12,8 @@ namespace StardewModdingAPI.Web.Framework.RewriteRules
         /*********
         ** Properties
         *********/
-        /// <summary>A predicate which indicates when the rule should be applied.</summary>
-        private readonly Func<HttpRequest, bool> ShouldRewrite;
-
-        /// <summary>The new URL to which to redirect.</summary>
-        private readonly string NewUrl;
+        /// <summary>Get the new URL to which to redirect (or <c>null</c> to skip).</summary>
+        private readonly Func<HttpRequest, string> NewUrl;
 
 
         /*********
@@ -27,8 +24,7 @@ namespace StardewModdingAPI.Web.Framework.RewriteRules
         /// <param name="url">The new URL to which to redirect.</param>
         public RedirectToUrlRule(Func<HttpRequest, bool> shouldRewrite, string url)
         {
-            this.ShouldRewrite = shouldRewrite ?? (req => true);
-            this.NewUrl = url;
+            this.NewUrl = req => shouldRewrite(req) ? url : null;
         }
 
         /// <summary>Construct an instance.</summary>
@@ -37,8 +33,7 @@ namespace StardewModdingAPI.Web.Framework.RewriteRules
         public RedirectToUrlRule(string pathRegex, string url)
         {
             Regex regex = new Regex(pathRegex, RegexOptions.IgnoreCase | RegexOptions.Compiled);
-            this.ShouldRewrite = req => req.Path.HasValue && regex.IsMatch(req.Path.Value);
-            this.NewUrl = url;
+            this.NewUrl = req => req.Path.HasValue ? regex.Replace(req.Path.Value, url) : null;
         }
 
         /// <summary>Applies the rule. Implementations of ApplyRule should set the value for <see cref="RewriteContext.Result" /> (defaults to RuleResult.ContinueRules).</summary>
@@ -47,14 +42,15 @@ namespace StardewModdingAPI.Web.Framework.RewriteRules
         {
             HttpRequest request = context.HttpContext.Request;
 
-            // check condition
-            if (!this.ShouldRewrite(request))
+            // check rewrite
+            string newUrl = this.NewUrl(request);
+            if (newUrl == null || newUrl == request.Path.Value)
                 return;
 
             // redirect request
             HttpResponse response = context.HttpContext.Response;
             response.StatusCode = (int)HttpStatusCode.Redirect;
-            response.Headers["Location"] = this.NewUrl;
+            response.Headers["Location"] = newUrl;
             context.Result = RuleResult.EndResponse;
         }
     }
diff --git a/src/SMAPI.Web/Startup.cs b/src/SMAPI.Web/Startup.cs
index 47102e5c..6c7ccecd 100644
--- a/src/SMAPI.Web/Startup.cs
+++ b/src/SMAPI.Web/Startup.cs
@@ -155,6 +155,7 @@ namespace StardewModdingAPI.Web
             // shortcut redirects
             redirects.Add(new RedirectToUrlRule(@"^/compat\.?$", "https://stardewvalleywiki.com/Modding:SMAPI_compatibility"));
             redirects.Add(new RedirectToUrlRule(@"^/docs\.?$", "https://stardewvalleywiki.com/Modding:Index"));
+            redirects.Add(new RedirectToUrlRule(@"^/buildmsg(?:/?(.*))$", "https://github.com/Pathoschild/SMAPI/blob/develop/docs/mod-build-config.md#$1"));
 
             // redirect legacy canimod.com URLs
             var wikiRedirects = new Dictionary<string, string[]>
-- 
cgit