Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removed download of the indicator file as we migrated to a new launcher #434

Merged
merged 2 commits into from
Dec 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 0 additions & 68 deletions src/Sidekick.Apis.GitHub/GitHubClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using Microsoft.Extensions.Logging;
using Sidekick.Apis.GitHub.Api;
using Sidekick.Apis.GitHub.Models;
using Sidekick.Common;

namespace Sidekick.Apis.GitHub;

Expand All @@ -16,19 +15,10 @@ public class GitHubClient
ILogger<GitHubClient> logger
) : IGitHubClient
{
private const string IndicatorPath = "SidekickGitHub";

private DateTimeOffset? LastUpdateCheck { get; set; }

private GitHubRelease? LatestRelease { get; set; }

public int Priority => 0;

public Task Initialize()
{
return DownloadGitHubDownloadIndicatorFile();
}

/// <inheritdoc />
public async Task<GitHubRelease> GetLatestRelease()
{
Expand Down Expand Up @@ -101,46 +91,6 @@ public async Task<bool> DownloadLatest(string downloadPath)
return true;
}

private async Task DownloadGitHubDownloadIndicatorFile()
{
var version = GetCurrentVersion();
if (version == null)
{
logger.LogInformation("[GitHubClient] Could not get current version.");
return;
}

if (HasIndicatorFile(version))
{
logger.LogInformation("[GitHubClient] GitHub download indicator file already exists.");
return;
}

logger.LogInformation("[GitHubClient] Checking for GitHub download indicator file.");
var apiReleases = await GetApiReleases();
var apiRelease = apiReleases?.FirstOrDefault(x => x.Version == version);
if (apiRelease == null)
{
logger.LogInformation("[GitHubClient] Could not find GitHub release for current version.");
return;
}

var downloadUrl = apiRelease.Assets?.FirstOrDefault(x => x.Name == "download-instructions.txt")?.DownloadUrl;
if (string.IsNullOrEmpty(downloadUrl))
{
logger.LogInformation("[GitHubClient] Could not find download indicator file for current version on GitHub.");
return;
}

using var client = GetHttpClient();
var response = await client.GetAsync(downloadUrl);
await using var downloadStream = await response.Content.ReadAsStreamAsync();
await using var fileStream = new FileStream(GetDownloadIndicatorFileName(version), FileMode.Create, FileAccess.Write, FileShare.None);
await downloadStream.CopyToAsync(fileStream);

logger.LogInformation("[GitHubClient] Downloaded indicator file for current version on GitHub.");
}

private HttpClient GetHttpClient()
{
var client = httpClientFactory.CreateClient();
Expand Down Expand Up @@ -174,24 +124,6 @@ private HttpClient GetHttpClient()
return githubReleaseList?.FirstOrDefault(x => !x.Prerelease);
}

private bool HasIndicatorFile(Version version)
{
EnsureIndicatorDirectory();
var fileName = GetDownloadIndicatorFileName(version);
return File.Exists(fileName);
}

private static void EnsureIndicatorDirectory()
{
Directory.CreateDirectory(Path.Combine(path1: SidekickPaths.GetDataFilePath(), IndicatorPath));
}

private static string GetDownloadIndicatorFileName(Version version)
{
var fileName = $"{version.ToString()}.txt";
return Path.Combine(path1: SidekickPaths.GetDataFilePath(), IndicatorPath, fileName);
}

private Version? GetCurrentVersion()
{
return AppDomain.CurrentDomain.GetAssemblies().Select(x => x.GetName()).FirstOrDefault(x => x.Name == "Sidekick")?.Version;
Expand Down
2 changes: 1 addition & 1 deletion src/Sidekick.Apis.GitHub/IGitHubClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Sidekick.Apis.GitHub;
/// <summary>
/// Interface to communicate with GitHub.
/// </summary>
public interface IGitHubClient : IInitializableService
public interface IGitHubClient
{
/// <summary>
/// Gets the latest release.
Expand Down
3 changes: 1 addition & 2 deletions src/Sidekick.Apis.GitHub/StartupExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Microsoft.Extensions.DependencyInjection;
using Sidekick.Common;

namespace Sidekick.Apis.GitHub;

Expand All @@ -8,7 +7,7 @@ public static class StartupExtensions
public static IServiceCollection AddSidekickGitHubApi(this IServiceCollection services)
{
services.AddHttpClient();
services.AddSidekickInitializableService<IGitHubClient, GitHubClient>();
services.AddSingleton<IGitHubClient, GitHubClient>();

return services;
}
Expand Down
Loading