Skip to content

Commit

Permalink
update 1.1
Browse files Browse the repository at this point in the history
add autoupdate
  • Loading branch information
johnoclockdk committed Dec 27, 2023
1 parent a2803f2 commit 0318018
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 7 deletions.
70 changes: 63 additions & 7 deletions Compiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ static async Task Main(string[] args)

if (config.Update)
{
await UpdateFromGithub();
await UpdateFromGithub(config);
}

if (args.Length > 0)
Expand All @@ -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)
{
Expand Down Expand Up @@ -257,6 +314,5 @@ private static void CleanupArtifacts(string folderPath)
Console.WriteLine($"Error cleaning up artifacts: {ex.Message}");
}
}

}
}
12 changes: 12 additions & 0 deletions ConfigurationManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}
}

0 comments on commit 0318018

Please sign in to comment.