Skip to content

Commit

Permalink
Add update checking
Browse files Browse the repository at this point in the history
  • Loading branch information
pizzaboxer committed Oct 9, 2024
1 parent 345c9bf commit 96c24ac
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Bloxstrap.Builder/Bloxstrap.Builder.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net6.0-windows</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<ApplicationIcon>Bloxstrap.ico</ApplicationIcon>
Expand Down
10 changes: 10 additions & 0 deletions Bloxstrap.Builder/GitHubRelease.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Text.Json.Serialization;

namespace Bloxstrap.Builder
{
public class GitHubRelease
{
[JsonPropertyName("tag_name")]
public string TagName { get; set; } = null!;
}
}
35 changes: 29 additions & 6 deletions Bloxstrap.Builder/Program.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
using System.Diagnostics;
using System.Net.Http.Json;
using System.Reflection;
using System.Text.Json;

using Crowdin.Api;
using Crowdin.Api.SourceFiles;
using Crowdin.Api.Translations;

using LibGit2Sharp;

namespace Bloxstrap.Builder
Expand Down Expand Up @@ -59,6 +62,8 @@ internal class Program

static HttpClient HttpClient = new();

static System.Version Version = Assembly.GetExecutingAssembly().GetName().Version!;

static void WriteLineColor(string line, ConsoleColor color)
{
Console.ForegroundColor = color;
Expand All @@ -68,6 +73,8 @@ static void WriteLineColor(string line, ConsoleColor color)

static void Main(string[] args)
{
HttpClient.DefaultRequestHeaders.Add("User-Agent", $"Bloxstrap.Builder/{Version}");

Console.ForegroundColor = ConsoleColor.White;

Console.Title = "Bloxstrap Builder v1.0.2";
Expand All @@ -83,7 +90,7 @@ static void Main(string[] args)

try
{
Config = JsonSerializer.Deserialize<Config>(System.IO.File.ReadAllText("config.json"))!;
Config = JsonSerializer.Deserialize<Config>(File.ReadAllText("config.json"))!;
}
catch (Exception)
{
Expand All @@ -92,6 +99,8 @@ static void Main(string[] args)

OriginalConfigValidation = ValidateConfig(true, true);

CheckForUpdate().Wait();

Console.WriteLine("Type \"help\" to see a list of commands.");

while (Running)
Expand Down Expand Up @@ -209,7 +218,7 @@ static void SetLanguage(string prompt)
Save();
}

static void Save() => System.IO.File.WriteAllText("config.json", JsonSerializer.Serialize(Config));
static void Save() => File.WriteAllText("config.json", JsonSerializer.Serialize(Config));

static void ConfigureCrowdin()
{
Expand Down Expand Up @@ -239,7 +248,7 @@ static void Build()
if (!ValidateConfig(true, false))
return;

bool exists = System.IO.Directory.Exists("bloxstrap");
bool exists = Directory.Exists("bloxstrap");

if (!exists)
{
Expand Down Expand Up @@ -270,7 +279,7 @@ static void Build()

{
using var httpStream = HttpClient.GetStreamAsync(response.Link!.Url).Result;
using var fileStream = System.IO.File.Create($"bloxstrap\\Bloxstrap\\Resources\\Strings.{Config.ChosenLocale!}.resx");
using var fileStream = File.Create($"bloxstrap\\Bloxstrap\\Resources\\Strings.{Config.ChosenLocale!}.resx");
httpStream.CopyTo(fileStream);
}

Expand All @@ -282,12 +291,26 @@ static void Build()

Console.WriteLine("");

if (System.IO.File.Exists(exePath))
// TODO: don't do this
if (File.Exists(exePath))
Console.WriteLine("Build succeeded. Type \"run\" to run it.");
else
WriteLineColor("Build failed.", ConsoleColor.Red);
}

static async Task CheckForUpdate()
{
var releaseInfo = await HttpClient.GetFromJsonAsync<GitHubRelease>("https://api.github.com/repos/bloxstraplabs/builder/releases/latest");

if (releaseInfo is null)
return;

var latestVersion = new System.Version(releaseInfo.TagName[1..]);

if (Version < latestVersion)
WriteLineColor($"A new version of Bloxstrap Builder is available (v{Version.ToString()[..^2]} -> v{latestVersion}).\r\nDownload it from https://github.com/bloxstraplabs/builder/releases/latest.", ConsoleColor.Blue);
}

static void Run() => Process.Start(exePath);
}
}

0 comments on commit 96c24ac

Please sign in to comment.