diff --git a/Compiler.cs b/Compiler.cs index 9f3434e..029352b 100644 --- a/Compiler.cs +++ b/Compiler.cs @@ -25,7 +25,7 @@ static async Task Main(string[] args) if (config.Update) { - await UpdateFromGithub(); + await UpdateFromGithub(config); } if (args.Length > 0) @@ -43,32 +43,89 @@ static async Task Main(string[] args) Console.WriteLine("Press Enter to exit..."); Console.ReadLine(); } - private static async Task UpdateFromGithub() + private static async Task UpdateFromGithub(Config config) { - string apiURL = $"https://api.github.com/repos/johnoclock/CssCompiler/releases/latest"; + string apiURL = $"https://api.github.com/repos/johnoclockdk/CssCompiler/releases/latest"; using (var httpClient = new HttpClient()) { - httpClient.DefaultRequestHeaders.UserAgent.TryParseAdd("request"); // GitHub API requires a user-agent + httpClient.DefaultRequestHeaders.UserAgent.TryParseAdd("request"); try { var response = await httpClient.GetStringAsync(apiURL); var latestRelease = JObject.Parse(response); - // Extract data as needed, for example, the tag name (version) string latestVersion = latestRelease["tag_name"].ToString(); Console.WriteLine("Latest Version: " + latestVersion); + Version latestVer = new Version(latestVersion); + Version currentVer = new Version(config.Version); + if (latestVer > currentVer) + { + string downloadUrl = latestRelease["assets"][0]["browser_download_url"].ToString(); + string tempFilePath = Path.Combine(Path.GetTempPath(), "newExecutable.exe"); + + var downloadResponse = await httpClient.GetAsync(downloadUrl, HttpCompletionOption.ResponseHeadersRead); + using (var fs = new FileStream(tempFilePath, FileMode.Create, FileAccess.Write, FileShare.None)) + { + await downloadResponse.Content.CopyToAsync(fs); + } + + config.Version = latestVersion; + ConfigurationManager.SaveConfig(config); + + + ReplaceExecutable(tempFilePath); + RelaunchApplication(); + } + else + { + Console.WriteLine("No update required. Running the latest version."); + } } catch (Exception ex) { - Console.WriteLine("Error fetching latest release: " + ex.Message); + Console.WriteLine("Error: " + ex.Message); } } } + private static void ReplaceExecutable(string newExecutablePath) + { + string currentExecutablePath = Process.GetCurrentProcess().MainModule.FileName; + string backupExecutablePath = currentExecutablePath + ".bak"; + + try + { + // Backup the old executable + File.Copy(currentExecutablePath, backupExecutablePath, true); + + // Replace the executable + File.Copy(newExecutablePath, currentExecutablePath, true); + File.Delete(newExecutablePath); + } + catch (Exception ex) + { + Console.WriteLine($"Error during file replacement: {ex.Message}"); + // Optional: Restore from backup if needed + } + } + + private static void RelaunchApplication() + { + string currentExecutablePath = Process.GetCurrentProcess().MainModule.FileName; + + ProcessStartInfo startInfo = new ProcessStartInfo + { + FileName = currentExecutablePath, + UseShellExecute = true + }; + + Process.Start(startInfo); + Environment.Exit(0); + } private static void ProcessDirectory(string folderPath, Config config) { @@ -257,6 +314,5 @@ private static void CleanupArtifacts(string folderPath) Console.WriteLine($"Error cleaning up artifacts: {ex.Message}"); } } - } } \ No newline at end of file diff --git a/ConfigurationManager.cs b/ConfigurationManager.cs index ae324f0..9393c3f 100644 --- a/ConfigurationManager.cs +++ b/ConfigurationManager.cs @@ -32,5 +32,17 @@ public Config LoadConfig() return new Config(); } } + + public static void SaveConfig(Config config) + { + // Serialize the config object to JSON + string json = JsonSerializer.Serialize(config, new JsonSerializerOptions + { + WriteIndented = true + }); + // Write to the file + File.WriteAllText(ConfigFileName, json); + } + } }