Skip to content

Commit

Permalink
Getting expected asset by assetName for downloading
Browse files Browse the repository at this point in the history
  • Loading branch information
MetaNaveen committed Sep 18, 2024
1 parent c10d094 commit c1befc4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
19 changes: 12 additions & 7 deletions Extensions/SelfUpdate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
public class SelfUpdater {
#region Public

public static async Task<bool> Run (string owner, string repo) {
var (serverVersion, downloadUrl) = await GetLatestReleaseAsync (owner, repo);
if (serverVersion == null || downloadUrl == null) {
//Console.WriteLine ("Failed to retrieve the latest release information.");
public static async Task<bool> Run (string owner, string repo, string assetName) {
var (serverVersion, downloadUrl) = await GetLatestReleaseAsync (owner, repo, assetName);
if (string.IsNullOrEmpty(serverVersion) || string.IsNullOrEmpty (downloadUrl)) {
return false;
}

Expand Down Expand Up @@ -86,7 +85,7 @@ static string GetTempFile (string suffix = "") {

private static readonly HttpClient httpClient = new HttpClient ();

static async Task<(string TagName, string DownloadUrl)> GetLatestReleaseAsync (string owner, string repo) {
static async Task<(string TagName, string DownloadUrl)> GetLatestReleaseAsync (string owner, string repo, string assetName) {
var url = $"https://api.github.com/repos/{owner}/{repo}/releases/latest";
httpClient.DefaultRequestHeaders.Add ("User-Agent", "BackupGitRepo");

Expand All @@ -98,8 +97,14 @@ static string GetTempFile (string suffix = "") {
var tagName = root.GetProperty ("tag_name").GetString ();
var assets = root.GetProperty ("assets");

if (assets.GetArrayLength () > 0) {
var downloadUrl = assets[0].GetProperty ("browser_download_url").GetString ();
var assetsCount = assets.GetArrayLength ();
if (assetsCount > 0) {
var i = 0;
string downloadUrl = "";
do {
downloadUrl = assets[i++].GetProperty ("browser_download_url").GetString () ?? "";
} while (i < assetsCount && !downloadUrl.EndsWith (assetName));

return (tagName, downloadUrl);
}

Expand Down
2 changes: 1 addition & 1 deletion Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Program {

static void Main (string[] args) {
try {
var isUpdated = Task.Run (async () => await SelfUpdater.Run ("MetaNaveen", "BackupGitRepo")).GetAwaiter ().GetResult ();
var isUpdated = Task.Run (async () => await SelfUpdater.Run ("MetaNaveen", "BackupGitRepo", "BackupGitRepo.exe")).GetAwaiter ().GetResult ();
if (!isUpdated) throw new Exception ("Unknown reason.");
} catch (Exception ex) {
Console.WriteLine ($"Self update failed! with error '{ex.Message}'.\n Running with the current version...");
Expand Down

0 comments on commit c1befc4

Please sign in to comment.