Skip to content
This repository has been archived by the owner on Jul 5, 2024. It is now read-only.

Commit

Permalink
Refactor UpdateManager constructor again;
Browse files Browse the repository at this point in the history
 applicationName is no longer required, but the Constructor will no longer throw if left null in an uninstalled app.
  • Loading branch information
caesay committed Jan 11, 2022
1 parent 09aeeeb commit 0346ddc
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 109 deletions.
48 changes: 23 additions & 25 deletions src/Squirrel/GithubUpdateManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,15 @@ namespace Squirrel
#endif
public class GithubUpdateManager : UpdateManager
{
private readonly string repoUrl;
private readonly string accessToken;
private readonly bool prerelease;
private readonly string _repoUrl;
private readonly string _accessToken;
private readonly bool _prerelease;

/// <inheritdoc cref="UpdateManager(string, string)"/>
/// <inheritdoc cref="UpdateManager(string)"/>
/// <param name="repoUrl">
/// The URL of the GitHub repository to download releases from
/// (e.g. https://github.com/myuser/myrepo)
/// </param>
/// <param name="applicationName">
/// The name of your application should correspond with the
/// appdata directory name, and the name used with Squirrel releasify/pack.
/// </param>
/// <param name="prerelease">
/// If true, the latest pre-release will be downloaded. If false, the latest
/// stable release will be downloaded.
Expand All @@ -41,19 +37,21 @@ public class GithubUpdateManager : UpdateManager
/// If left empty, the GitHub rate limit for unauthenticated requests allows
/// for up to 60 requests per hour, limited by IP address.
/// </param>
public GithubUpdateManager(string repoUrl, string applicationName, bool prerelease = false, string accessToken = null)
: this(repoUrl, applicationName, prerelease, accessToken, null, null)
public GithubUpdateManager(string repoUrl, bool prerelease = false, string accessToken = null)
: this(repoUrl, prerelease, accessToken, null, null, null)
{
}

/// <inheritdoc cref="UpdateManager(string, string)"/>
/// <inheritdoc cref="UpdateManager(string)"/>
/// <param name="repoUrl">
/// The URL of the GitHub repository to download releases from
/// (e.g. https://github.com/myuser/myrepo)
/// </param>
/// <param name="applicationName">
/// The name of your application should correspond with the
/// appdata directory name, and the name used with Squirrel releasify/pack.
/// <param name="applicationIdOverride">
/// The Id of your application should correspond with the
/// appdata directory name, and the Id used with Squirrel releasify/pack.
/// If left null/empty, will attempt to determine the current application Id
/// from the installed app location.
/// </param>
/// <param name="urlDownloader">
/// A custom file downloader, for using non-standard package sources or adding
Expand All @@ -74,16 +72,16 @@ public GithubUpdateManager(string repoUrl, string applicationName, bool prerelea
/// </param>
public GithubUpdateManager(
string repoUrl,
string applicationName,
bool prerelease = false,
string accessToken = null,
string applicationIdOverride = null,
string localAppDataDirectoryOverride = null,
IFileDownloader urlDownloader = null)
: base(null, applicationName, localAppDataDirectoryOverride, urlDownloader)
: base(null, applicationIdOverride, localAppDataDirectoryOverride, urlDownloader)
{
this.repoUrl = repoUrl;
this.accessToken = accessToken;
this.prerelease = prerelease;
_repoUrl = repoUrl;
_accessToken = accessToken;
_prerelease = prerelease;
}

/// <inheritdoc />
Expand All @@ -102,14 +100,14 @@ public override async Task DownloadReleases(IEnumerable<ReleaseEntry> releasesTo

private async Task EnsureReleaseUrl()
{
if (this.updateUrlOrPath == null) {
this.updateUrlOrPath = await GetLatestGithubRelease().ConfigureAwait(false);
if (this._updateUrlOrPath == null) {
this._updateUrlOrPath = await GetLatestGithubRelease().ConfigureAwait(false);
}
}

private async Task<string> GetLatestGithubRelease()
{
var repoUri = new Uri(repoUrl);
var repoUri = new Uri(_repoUrl);
var userAgent = new ProductInfoHeaderValue("Squirrel", AssemblyRuntimeInfo.ExecutingAssemblyName.Version.ToString());

if (repoUri.Segments.Length != 3) {
Expand All @@ -120,8 +118,8 @@ private async Task<string> GetLatestGithubRelease()
.Append(repoUri.AbsolutePath)
.Append("/releases");

if (!string.IsNullOrWhiteSpace(accessToken))
releasesApiBuilder.Append("?access_token=").Append(accessToken);
if (!string.IsNullOrWhiteSpace(_accessToken))
releasesApiBuilder.Append("?access_token=").Append(_accessToken);

Uri baseAddress;

Expand All @@ -145,7 +143,7 @@ private async Task<string> GetLatestGithubRelease()

var releases = SimpleJson.DeserializeObject<List<Release>>(await response.Content.ReadAsStringAsync().ConfigureAwait(false));
var latestRelease = releases
.Where(x => prerelease || !x.Prerelease)
.Where(x => _prerelease || !x.Prerelease)
.OrderByDescending(x => x.PublishedAt)
.First();

Expand Down
5 changes: 3 additions & 2 deletions src/Squirrel/IUpdateManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@ public enum UpdaterIntention
}

/// <summary>
/// An object providing update functionality to applications, and general helper
/// functions for managing installed shortcuts and registry entries.
/// Provides update functionality to applications, and general helper
/// functions for managing installed shortcuts and registry entries. Use this
/// to check if the current app is installed or not before performing an update.
/// </summary>
public interface IUpdateManager : IDisposable, IEnableLogger, IAppTools
{
Expand Down
Loading

0 comments on commit 0346ddc

Please sign in to comment.