Skip to content

Commit

Permalink
Merge pull request #115 from alexhauser/version_management
Browse files Browse the repository at this point in the history
Version management
  • Loading branch information
alexhauser authored Apr 16, 2020
2 parents 07aaf22 + 3dde351 commit fcbdb4a
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 25 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -342,3 +342,4 @@ healthchecksdb
# Misc project-specific files
packages-microsoft-prod.deb
.DS_Store
GithubAuthToken.txt
74 changes: 51 additions & 23 deletions GithubAPI/GitHubHelper.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using Serilog;
using Newtonsoft.Json;
using Serilog;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace GitHubApi
Expand All @@ -14,7 +16,6 @@ namespace GitHubApi
public static class GitHubHelper
{
public static readonly string SQRLInstallerUserAgent="Open Source Cross Platform SQRL Installer";
public static readonly string SQRLVersionFile = "sqrlversion.json";
public static readonly string SQRLGithubProjectOwner = "sqrldev";
public static readonly string SQRLGithubProjectName = "SQRLDotNetClient";

Expand All @@ -23,15 +24,16 @@ public static class GitHubHelper
/// </summary>
public async static Task<GithubRelease[]> GetReleases()
{
var releases = await Task.Run(() =>
return await Task.Run(() =>
{
string jsonData = "";
var wc = new WebClient
{
CachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.NoCacheNoStore)
};

wc.Headers.Add("User-Agent", SQRLInstallerUserAgent);
AddHeaders(wc);

try
{
jsonData = wc.DownloadString($"https://api.github.com/repos/{SQRLGithubProjectOwner}/{SQRLGithubProjectName}/releases");
Expand All @@ -41,10 +43,9 @@ public async static Task<GithubRelease[]> GetReleases()
Log.Error($"Error Downloading Releases. Error: {ex}");
}
return !string.IsNullOrEmpty(jsonData) ?
(Newtonsoft.Json.JsonConvert.DeserializeObject<List<GithubRelease>>(jsonData)).ToArray() :
(JsonConvert.DeserializeObject<List<GithubRelease>>(jsonData)).ToArray() :
new GithubRelease[] { };
});
return releases;
}

/// <summary>
Expand All @@ -56,49 +57,76 @@ public async static Task<GithubRelease[]> GetReleases()
/// <returns>Returns <c>true</c> on success or <c>false</c> if an exception occures.</returns>
public static bool DownloadFile(string url, string target)
{
bool success = true;
var wc = new WebClient
{
CachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.NoCacheNoStore)
};

wc.Headers.Add("User-Agent", SQRLInstallerUserAgent);
AddHeaders(wc);

try
{
wc.DownloadFile(url,target);
return true;
}
catch (Exception ex)
{
Log.Error($"Error Downloading: {url} Error: {ex}");
success = false;
return false;
}
return success;
}

/// <summary>
/// Checks if a newer version of the app exists in the project's Github releases.
/// </summary>
/// <returns>Returns <c>true</c> if an update exists, or <c>false</c> if no update exists
/// or if the update check could not be perfomred for some reason.</returns>
public async static Task<bool> CheckForUpdates()
public async static Task<bool> CheckForUpdates(Version currentVersion)
{
bool updateAvailable = false;
var directory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
if (File.Exists(Path.Combine(directory, SQRLVersionFile)))
var releases = await GetReleases();
if (releases != null && releases.Length > 0)
{
string tag = Newtonsoft.Json.JsonConvert.DeserializeObject<string>(File.ReadAllText(Path.Combine(directory, SQRLVersionFile)));
var releases = await GitHubApi.GitHubHelper.GetReleases();
if (releases != null && !tag.Equals(releases[0].tag_name, StringComparison.OrdinalIgnoreCase))
Match match = Regex.Match(releases[0].tag_name, @"\d+(?:\.\d+)+");
if (match.Success)
{
updateAvailable = true;
Version releaseVersion;
bool success = Version.TryParse(match.Value, out releaseVersion);

if (success && releaseVersion > currentVersion)
{
return true;
}
}
else
updateAvailable = false;
}
else
updateAvailable = false;

return false;
}

/// <summary>
/// Adds the "User-Agent" header to the given <paramref name="webClient"/>.
/// If a file with a name of "GithubAuthToken.txt" exists in the same directory
/// as the executable, and the file contains a valid Github authorization token
/// in the format of "token 5c19b5ada557335de35ed54642c363f82ac1da18", a
/// corresponding "Authorization" header will be sent as well.
/// </summary>
/// <param name="webClient">The WebClient to which headers should be added.</param>
private static void AddHeaders(WebClient webClient)
{
webClient.Headers.Add("User-Agent", SQRLInstallerUserAgent);

var authFile = Path.Combine(
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),
@"GithubAuthToken.txt");

if (File.Exists(authFile))
{
string authToken = File.ReadAllText(authFile);

return updateAvailable;
if (!string.IsNullOrEmpty(authToken))
{
webClient.Headers.Add("Authorization", authToken);
}
}
}
}
}
4 changes: 3 additions & 1 deletion SQRLDotNetClientUI/ViewModels/AuthenticationViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Avalonia.Controls;
using SQRLDotNetClientUI.Models;
using Serilog;
using System.Reflection;

namespace SQRLDotNetClientUI.ViewModels
{
Expand Down Expand Up @@ -156,7 +157,8 @@ private void Init()

private async void CheckForUpdate()
{
this.NewUpdateAvailable = await GitHubApi.GitHubHelper.CheckForUpdates();
this.NewUpdateAvailable = await GitHubApi.GitHubHelper.CheckForUpdates(
Assembly.GetExecutingAssembly().GetName().Version);
}

/// <summary>
Expand Down
3 changes: 2 additions & 1 deletion SQRLDotNetClientUI/ViewModels/MainMenuViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,8 @@ public void Login()
public async void CheckForUpdates()
{

this.NewUpdateAvailable = await GitHubApi.GitHubHelper.CheckForUpdates();
this.NewUpdateAvailable = await GitHubApi.GitHubHelper.CheckForUpdates(
Assembly.GetExecutingAssembly().GetName().Version);
}


Expand Down
5 changes: 5 additions & 0 deletions SQRLPlatformAwareInstaller/SQRLPlatformAwareInstaller.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,9 @@
<DependentUpon>MainInstallView.xaml</DependentUpon>
</Compile>
</ItemGroup>
<ItemGroup>
<None Update="GithubAuthToken.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>

0 comments on commit fcbdb4a

Please sign in to comment.