Skip to content
This repository has been archived by the owner on May 27, 2021. It is now read-only.

Commit

Permalink
add check update
Browse files Browse the repository at this point in the history
  • Loading branch information
0xF6 committed May 1, 2020
1 parent 8fcd186 commit 4b1cedd
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 5 deletions.
11 changes: 7 additions & 4 deletions src/Host.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,22 @@ private static readonly Dictionary<string, Func<string[], Task<int>>> s_builtIns
["config"] = ConfigCommand.Run,
["publish"] = PublishCommand.Run
};
public static Task<int> Main(string[] args)
public static async Task<int> Main(string[] args)
{
InitializeProcess();

RuneSelfUpdater.Check();
var result = 0;
try
{
return ProcessArgs(args);
result = await ProcessArgs(args);
}
catch (Exception e)
{
WriteLine(e.Message.Color(Color.OrangeRed));
return Task.FromResult(1);
result = 1;
}
RuneSelfUpdater.DisplayIfNeeded();
return result;
}
private static void InitializeProcess()
{
Expand Down
2 changes: 1 addition & 1 deletion src/Rune.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<RootNamespace>Rune.CLI</RootNamespace>
<ApplicationIcon>resource\icon.ico</ApplicationIcon>
<StartupObject></StartupObject>
<Version>0.70.557-beta</Version>
<Version>0.70.613-beta</Version>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<PlatformTarget>x64</PlatformTarget>
</PropertyGroup>
Expand Down
132 changes: 132 additions & 0 deletions src/etc/RuneSelfUpdater.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
namespace rune.etc
{
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using cmd;
using Flurl;
using Flurl.Http;
using Newtonsoft.Json;
using NuGet.Versioning;

public static class RuneSelfUpdater
{
private static NuGetVersion latest;
private static NuGetVersion current;
private static string UpdateCmd;
public static void Check() => CheckAsync().Wait();

public static async Task CheckAsync()
{
if(Config.Get("update", "disabled", false))
return;

var url = Config.Get("update", "origin", "https://cluster.ruler.runic.cloud");
var release = await url.AppendPathSegment("/api/@me/version/latest")
.WithHeader("User-Agent", $"RuneCLI/{HelpCommand.GetVersion()}")
.GetJsonAsync<RuneVersion>();


latest = NuGetVersion.Parse(release.Version.Sem.Version);
current = NuGetVersion.Parse(FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location).ProductVersion);
UpdateCmd = release.UpdateCommand;

}

public static void DisplayIfNeeded()
{
if (current < latest)
{
var len = 56;
Console.WriteLine($"{new string('─', len)}".Color(Color.Coral));
Console.WriteLine($"{new string(' ', len)}".Color(Color.Coral));

Console.WriteLine($"{"".Color(Color.Coral)}{CenteredText($"Update available {current}{latest}", len)}{"".Color(Color.Coral)}");
Console.WriteLine($"{"".Color(Color.Coral)}{CenteredText($"Run {UpdateCmd} to update", len)}{"".Color(Color.Coral)}");

Console.WriteLine($"{new string(' ', len)}".Color(Color.Coral));
Console.WriteLine($"{new string('─', len)}".Color(Color.Coral));
}
}

private static string CenteredText(string text, int width)
{
var startPoint = (width - text.Length) / 2;
return $"{new string(' ', startPoint)}{text}{new string(' ', startPoint)}";
}
}




public class RuneVersion
{
[JsonProperty("version")]
public VersionModel Version { get; set; }
[JsonProperty("update_command")]
public string UpdateCommand { get; set; }


public class VersionModel
{
[JsonProperty("full")]
public string Full { get; set; }

[JsonProperty("sem")]
public SemVersionModel Sem { get; set; }

public partial class SemVersionModel
{
[JsonProperty("options")]
public OptionsModel Options { get; set; }

[JsonProperty("loose")]
public bool Loose { get; set; }

[JsonProperty("includePrerelease")]
public bool IncludePrerelease { get; set; }

[JsonProperty("raw")]
public string Raw { get; set; }

[JsonProperty("major")]
public long Major { get; set; }

[JsonProperty("minor")]
public long Minor { get; set; }

[JsonProperty("patch")]
public long Patch { get; set; }

[JsonProperty("prerelease")]
public List<string> Prerelease { get; set; }

[JsonProperty("build")]
public List<object> Build { get; set; }

[JsonProperty("version")]
public string Version { get; set; }


public class OptionsModel
{
[JsonProperty("loose")]
public bool Loose { get; set; }

[JsonProperty("includePrerelease")]
public bool IncludePrerelease { get; set; }
}
}
}
}






}

0 comments on commit 4b1cedd

Please sign in to comment.