From 594d176d39691ff46b2c99fdfaa4299a4ea43616 Mon Sep 17 00:00:00 2001
From: Jesse Plamondon-Willard
Date: Thu, 15 Mar 2018 23:36:16 -0400
Subject: prepare home page for upcoming beta (#457)
---
src/SMAPI.Web/Controllers/IndexController.cs | 53 +++++++++++++++++++++++----
src/SMAPI.Web/ViewModels/IndexModel.cs | 28 +++++---------
src/SMAPI.Web/ViewModels/IndexVersionModel.cs | 41 +++++++++++++++++++++
src/SMAPI.Web/Views/Index/Index.cshtml | 35 ++++++++++++++----
4 files changed, 124 insertions(+), 33 deletions(-)
create mode 100644 src/SMAPI.Web/ViewModels/IndexVersionModel.cs
(limited to 'src/SMAPI.Web')
diff --git a/src/SMAPI.Web/Controllers/IndexController.cs b/src/SMAPI.Web/Controllers/IndexController.cs
index 5d45118f..0464e50a 100644
--- a/src/SMAPI.Web/Controllers/IndexController.cs
+++ b/src/SMAPI.Web/Controllers/IndexController.cs
@@ -3,6 +3,7 @@ using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
+using StardewModdingAPI.Common;
using StardewModdingAPI.Web.Framework.Clients.GitHub;
using StardewModdingAPI.Web.ViewModels;
@@ -23,7 +24,10 @@ namespace StardewModdingAPI.Web.Controllers
private readonly IGitHubClient GitHub;
/// The cache time for release info.
- private readonly TimeSpan CacheTime = TimeSpan.FromMinutes(5);
+ private readonly TimeSpan CacheTime = TimeSpan.FromSeconds(1);
+
+ /// The GitHub repository name to check for update.
+ private readonly string RepositoryName = "Pathoschild/SMAPI";
/*********
@@ -42,17 +46,24 @@ namespace StardewModdingAPI.Web.Controllers
[HttpGet]
public async Task Index()
{
- // fetch latest SMAPI release
- GitRelease release = await this.Cache.GetOrCreateAsync("latest-smapi-release", async entry =>
+ // fetch SMAPI releases
+ IndexVersionModel stableVersion = await this.Cache.GetOrCreateAsync("stable-version", async entry =>
+ {
+ entry.AbsoluteExpiration = DateTimeOffset.UtcNow.Add(this.CacheTime);
+ GitRelease release = await this.GitHub.GetLatestReleaseAsync(this.RepositoryName, includePrerelease: false);
+ return new IndexVersionModel(release.Name, release.Body, this.GetMainDownloadUrl(release), this.GetDevDownloadUrl(release));
+ });
+ IndexVersionModel betaVersion = await this.Cache.GetOrCreateAsync("beta-version", async entry =>
{
entry.AbsoluteExpiration = DateTimeOffset.UtcNow.Add(this.CacheTime);
- return await this.GitHub.GetLatestReleaseAsync("Pathoschild/SMAPI");
+ GitRelease release = await this.GitHub.GetLatestReleaseAsync(this.RepositoryName, includePrerelease: true);
+ return release.IsPrerelease
+ ? this.GetBetaDownload(release)
+ : null;
});
- string downloadUrl = this.GetMainDownloadUrl(release);
- string devDownloadUrl = this.GetDevDownloadUrl(release);
// render view
- var model = new IndexModel(release.Name, release.Body, downloadUrl, devDownloadUrl);
+ var model = new IndexModel(stableVersion, betaVersion);
return this.View(model);
}
@@ -89,5 +100,33 @@ namespace StardewModdingAPI.Web.Controllers
// fallback just in case
return "https://github.com/pathoschild/SMAPI/releases";
}
+
+ /// Get the latest beta download for a SMAPI release.
+ /// The SMAPI release.
+ private IndexVersionModel GetBetaDownload(GitRelease release)
+ {
+ // get download with the latest version
+ SemanticVersionImpl latestVersion = null;
+ string latestUrl = null;
+ foreach (GitAsset asset in release.Assets ?? new GitAsset[0])
+ {
+ // parse version
+ Match versionMatch = Regex.Match(asset.FileName, @"SMAPI-([\d\.]+(?:-.+)?)-installer.zip");
+ if (!versionMatch.Success || !SemanticVersionImpl.TryParse(versionMatch.Groups[1].Value, out SemanticVersionImpl version))
+ continue;
+
+ // save latest version
+ if (latestVersion == null || latestVersion.CompareTo(version) < 0)
+ {
+ latestVersion = version;
+ latestUrl = asset.DownloadUrl;
+ }
+ }
+
+ // return if prerelease
+ return latestVersion?.Tag != null
+ ? new IndexVersionModel(latestVersion.ToString(), release.Body, latestUrl, null)
+ : null;
+ }
}
}
diff --git a/src/SMAPI.Web/ViewModels/IndexModel.cs b/src/SMAPI.Web/ViewModels/IndexModel.cs
index 6d3da91e..4268c878 100644
--- a/src/SMAPI.Web/ViewModels/IndexModel.cs
+++ b/src/SMAPI.Web/ViewModels/IndexModel.cs
@@ -6,17 +6,11 @@ namespace StardewModdingAPI.Web.ViewModels
/*********
** Accessors
*********/
- /// The latest SMAPI version.
- public string LatestVersion { get; set; }
+ /// The latest stable SMAPI version.
+ public IndexVersionModel StableVersion { get; set; }
- /// The Markdown description for the release.
- public string Description { get; set; }
-
- /// The main download URL.
- public string DownloadUrl { get; set; }
-
- /// The for-developers download URL.
- public string DevDownloadUrl { get; set; }
+ /// The latest prerelease SMAPI version (if newer than ).
+ public IndexVersionModel BetaVersion { get; set; }
/*********
@@ -26,16 +20,12 @@ namespace StardewModdingAPI.Web.ViewModels
public IndexModel() { }
/// Construct an instance.
- /// The latest SMAPI version.
- /// The Markdown description for the release.
- /// The main download URL.
- /// The for-developers download URL.
- internal IndexModel(string latestVersion, string description, string downloadUrl, string devDownloadUrl)
+ /// The latest stable SMAPI version.
+ /// The latest prerelease SMAPI version (if newer than ).
+ internal IndexModel(IndexVersionModel stableVersion, IndexVersionModel betaVersion)
{
- this.LatestVersion = latestVersion;
- this.Description = description;
- this.DownloadUrl = downloadUrl;
- this.DevDownloadUrl = devDownloadUrl;
+ this.StableVersion = stableVersion;
+ this.BetaVersion = betaVersion;
}
}
}
diff --git a/src/SMAPI.Web/ViewModels/IndexVersionModel.cs b/src/SMAPI.Web/ViewModels/IndexVersionModel.cs
new file mode 100644
index 00000000..4f63b979
--- /dev/null
+++ b/src/SMAPI.Web/ViewModels/IndexVersionModel.cs
@@ -0,0 +1,41 @@
+namespace StardewModdingAPI.Web.ViewModels
+{
+ /// The fields for a SMAPI version.
+ public class IndexVersionModel
+ {
+ /*********
+ ** Accessors
+ *********/
+ /// The release version.
+ public string Version { get; set; }
+
+ /// The Markdown description for the release.
+ public string Description { get; set; }
+
+ /// The main download URL.
+ public string DownloadUrl { get; set; }
+
+ /// The for-developers download URL (not applicable for prerelease versions).
+ public string DevDownloadUrl { get; set; }
+
+
+ /*********
+ ** Public methods
+ *********/
+ /// Construct an instance.
+ public IndexVersionModel() { }
+
+ /// Construct an instance.
+ /// The release number.
+ /// The Markdown description for the release.
+ /// The main download URL.
+ /// The for-developers download URL (not applicable for prerelease versions).
+ internal IndexVersionModel(string version, string description, string downloadUrl, string devDownloadUrl)
+ {
+ this.Version = version;
+ this.Description = description;
+ this.DownloadUrl = downloadUrl;
+ this.DevDownloadUrl = devDownloadUrl;
+ }
+ }
+}
diff --git a/src/SMAPI.Web/Views/Index/Index.cshtml b/src/SMAPI.Web/Views/Index/Index.cshtml
index ad58898e..4efb9f8a 100644
--- a/src/SMAPI.Web/Views/Index/Index.cshtml
+++ b/src/SMAPI.Web/Views/Index/Index.cshtml
@@ -13,7 +13,11 @@