|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Net.Http; |
| 5 | +using System.Text.Json; |
| 6 | +using System.Text; |
| 7 | +using System.Threading.Tasks; |
| 8 | +using static System.Runtime.InteropServices.JavaScript.JSType; |
| 9 | +using Newtonsoft.Json; |
| 10 | +using WinRT; |
| 11 | +using System.IO; |
| 12 | +using ICSharpCode.Decompiler.CSharp.Syntax; |
| 13 | + |
| 14 | +namespace AtlasToolbox.Utils |
| 15 | +{ |
| 16 | + public class ToolboxUpdateHelper |
| 17 | + { |
| 18 | + const string RELEASE_URL = "https://api.github.com/repos/atlas-os/atlas-toolbox/releases/latest"; |
| 19 | + public static string commandUpdate; |
| 20 | + public static bool CheckUpdates() |
| 21 | + { |
| 22 | + // get the api result |
| 23 | + string htmlContent = CommandPromptHelper.ReturnRunCommand("curl " + RELEASE_URL); |
| 24 | + var result = JsonDocument.Parse(htmlContent); |
| 25 | + string tagName = result.RootElement.GetProperty("tag_name").GetString(); |
| 26 | + |
| 27 | + // Format everything to compare |
| 28 | + int version = int.Parse(RegistryHelper.GetValue($@"HKLM\SOFTWARE\AtlasOS\Toolbox", "Version").ToString().Replace(".", "")); |
| 29 | + |
| 30 | + if (int.Parse(tagName.Replace(".", "").Replace("v", "")) > version) |
| 31 | + { |
| 32 | + // get the download link and create a temporary directory |
| 33 | + string downloadUrl = result.RootElement.GetProperty("assets")[0].GetProperty("browser_download_url").GetString(); |
| 34 | + string tempDirectory = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); |
| 35 | + Directory.CreateDirectory(tempDirectory); |
| 36 | + |
| 37 | + CommandPromptHelper.RunCommand($"cd {tempDirectory} && curl -LSs {downloadUrl} -O \"setup.exe\""); |
| 38 | + commandUpdate = $"{tempDirectory}\\{downloadUrl.Split('/').Last()} /silent /install"; |
| 39 | + return true; |
| 40 | + } |
| 41 | + return false; |
| 42 | + } |
| 43 | + |
| 44 | + public static void InstallUpdate() |
| 45 | + { |
| 46 | + // Call the installer and close Toolbox |
| 47 | + CommandPromptHelper.RunCommand(commandUpdate, true, false); |
| 48 | + Environment.Exit(0); |
| 49 | + } |
| 50 | + } |
| 51 | +} |
0 commit comments