Skip to content

Commit

Permalink
Update to .Net 8
Browse files Browse the repository at this point in the history
  • Loading branch information
leMicin committed Mar 24, 2024
1 parent a889c45 commit d21c0d6
Show file tree
Hide file tree
Showing 52 changed files with 4,334 additions and 573 deletions.
3,758 changes: 3,723 additions & 35 deletions .editorconfig

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion .github/workflows/wpf-beta.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ jobs:
- name: .NET - Setup
uses: actions/setup-dotnet@v1
with:
dotnet-version: 7.x
dotnet-version: 8.x

- name: .NET - Restore
run: dotnet restore
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/wpf-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,16 @@ jobs:
with:
name: GITHUB_TOKEN
value: ${{ github.token }}

- name: Copy files
shell: pwsh
run: |
Copy-Item -Path "src/Sidekick.Common.Blazor/wwwroot/*" -Destination "src/Sidekick.Wpf/wwwroot" -Recurse
- name: .NET 7 - Setup
- name: .NET - Setup
uses: actions/setup-dotnet@v1
with:
dotnet-version: 7.x
dotnet-version: 8.x

- name: .NET - Restore
run: dotnet restore
Expand All @@ -67,7 +67,7 @@ jobs:
uses: webfactory/[email protected]
with:
ssh-private-key: ${{ secrets.DEPLOY_RELEASE_KEY }}

- name: Run release script
shell: pwsh
run: |
Expand Down
7 changes: 7 additions & 0 deletions global.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"sdk": {
"version": "8.0.0",
"rollForward": "latestMajor",
"allowPrerelease": false
}
}
15 changes: 6 additions & 9 deletions src/Sidekick.Apis.GitHub/Api/Asset.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
using System.Text.Json.Serialization;

namespace Sidekick.Apis.GitHub.Api
{
internal record Asset
{
public string? Url { get; init; }
namespace Sidekick.Apis.GitHub.Api;

public string? Name { get; init; }
internal record Asset
{
public string? Name { get; init; }

[JsonPropertyName("browser_download_url")]
public string? DownloadUrl { get; init; }
}
[JsonPropertyName("browser_download_url")]
public string? DownloadUrl { get; init; }
}
17 changes: 8 additions & 9 deletions src/Sidekick.Apis.GitHub/Api/Release.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
using System.Text.Json.Serialization;

namespace Sidekick.Apis.GitHub.Api
namespace Sidekick.Apis.GitHub.Api;

internal record Release
{
internal record Release
{
[JsonPropertyName("tag_name")]
public string? Tag { get; init; }
[JsonPropertyName("tag_name")]
public string? Tag { get; init; }

public string? Name { get; init; }
public string? Name { get; init; }

public bool Prerelease { get; init; }
public bool Prerelease { get; init; }

public Asset[]? Assets { get; init; }
}
public Asset[]? Assets { get; init; }
}
181 changes: 97 additions & 84 deletions src/Sidekick.Apis.GitHub/GitHubClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,115 +6,128 @@
using Sidekick.Apis.GitHub.Api;
using Sidekick.Apis.GitHub.Models;

namespace Sidekick.Apis.GitHub
namespace Sidekick.Apis.GitHub;

public class GitHubClient : IGitHubClient
{
public class GitHubClient : IGitHubClient
private readonly IHttpClientFactory httpClientFactory;
private readonly ILogger<GitHubClient> logger;

public GitHubClient(
IHttpClientFactory httpClientFactory,
ILogger<GitHubClient> logger)
{
private readonly IHttpClientFactory httpClientFactory;
private readonly ILogger<GitHubClient> logger;
this.httpClientFactory = httpClientFactory;
this.logger = logger;
}

public GitHubClient(
IHttpClientFactory httpClientFactory,
ILogger<GitHubClient> logger)
private GitHubRelease? LatestRelease { get; set; }

/// <inheritdoc />
public async Task<GitHubRelease> GetLatestRelease()
{
if (LatestRelease != null)
{
this.httpClientFactory = httpClientFactory;
this.logger = logger;
return LatestRelease;
}

private GitHubRelease? LatestRelease { get; set; } = null;
var release = await GetApiRelease();
if (release == null || release.Tag == null)
{
logger.LogInformation("[Updater] No latest release found on GitHub.");
LatestRelease = new GitHubRelease();
return LatestRelease;
}

private HttpClient GetHttpClient()
logger.LogInformation("[Updater] Found " + release.Tag + " as latest version on GitHub.");
var latestVersion = new Version(
Regex
.Match(release.Tag, @"(\d+\.){2}\d+")
.ToString());
var currentVersion = AppDomain
.CurrentDomain.GetAssemblies()
.FirstOrDefault(x => x.FullName?.Contains("Sidekick") ?? false)
?.GetName()
.Version;
if (currentVersion == null)
{
var client = httpClientFactory.CreateClient();
client.BaseAddress = new Uri("https://api.github.com");
client.DefaultRequestHeaders.UserAgent.TryParseAdd("request");
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
return client;
LatestRelease = new GitHubRelease();
return LatestRelease;
}

/// <inheritdoc/>
public async Task<GitHubRelease> GetLatestRelease()
LatestRelease = new GitHubRelease
{
if (LatestRelease != null)
{
return LatestRelease;
}
IsNewerVersion = currentVersion.CompareTo(latestVersion) < 0,
IsExecutable = release.Assets?.Any(x => x.Name == "Sidekick-Setup.exe") ?? false,
};
return LatestRelease;
}

var release = await GetApiRelease();
if (release == null || release.Tag == null)
{
logger.LogInformation("[Updater] No latest release found on GitHub.");
LatestRelease = new();
return LatestRelease;
}

logger.LogInformation("[Updater] Found " + release.Tag + " as latest version on GitHub.");
var latestVersion = new Version(Regex.Match(release.Tag, @"(\d+\.){2}\d+").ToString());
var currentVersion = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(x => x.FullName?.Contains("Sidekick") ?? false)?.GetName().Version;
if (currentVersion == null)
{
LatestRelease = new();
return LatestRelease;
}
/// <inheritdoc />
public async Task<bool> DownloadLatest(string downloadPath)
{
var release = await GetApiRelease();
if (release == null)
{
return false;
}

LatestRelease = new()
{
IsNewerVersion = currentVersion.CompareTo(latestVersion) < 0,
IsExecutable = release.Assets?.Any(x => x.Name == "Sidekick-Setup.exe") ?? false,
};
return LatestRelease;
if (File.Exists(downloadPath))
{
File.Delete(downloadPath);
}

/// <inheritdoc/>
public async Task<bool> DownloadLatest(string downloadPath)
var downloadUrl = release.Assets?.FirstOrDefault(x => x.Name == "Sidekick-Setup.exe")
?.DownloadUrl;
if (downloadUrl == null)
{
var release = await GetApiRelease();
if (release == null)
{
return false;
}
return false;
}

if (File.Exists(downloadPath))
{
File.Delete(downloadPath);
}
using var client = GetHttpClient();
var response = await client.GetAsync(downloadUrl);
await using var downloadStream = await response.Content.ReadAsStreamAsync();
await using var fileStream = new FileStream(
downloadPath,
FileMode.Create,
FileAccess.Write,
FileShare.None);
await downloadStream.CopyToAsync(fileStream);
return true;
}

var downloadUrl = release.Assets?.FirstOrDefault(x => x.Name == "Sidekick-Setup.exe")?.DownloadUrl;
if (downloadUrl == null)
{
return false;
}

using var client = GetHttpClient();
var response = await client.GetAsync(downloadUrl);
using var downloadStream = await response.Content.ReadAsStreamAsync();
using var fileStream = new FileStream(downloadPath, FileMode.Create, FileAccess.Write, FileShare.None);
await downloadStream.CopyToAsync(fileStream);
return true;
}
private HttpClient GetHttpClient()
{
var client = httpClientFactory.CreateClient();
client.BaseAddress = new Uri("https://api.github.com");
client.DefaultRequestHeaders.UserAgent.TryParseAdd("request");
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
return client;
}

private async Task<Release?> GetApiRelease()
private async Task<Release?> GetApiRelease()
{
// Get List of releases
using var client = GetHttpClient();
var listResponse = await client.GetAsync("/repos/Sidekick-Poe/Sidekick/releases");
if (!listResponse.IsSuccessStatusCode)
{
// Get List of releases
using var client = GetHttpClient();
var listResponse = await client.GetAsync("/repos/Sidekick-Poe/Sidekick/releases");
if (!listResponse.IsSuccessStatusCode)
{
return null;
}
return null;
}

var githubReleaseList = await JsonSerializer.DeserializeAsync<Release[]>(await listResponse.Content.ReadAsStreamAsync(), new JsonSerializerOptions
var githubReleaseList = await JsonSerializer.DeserializeAsync<Release[]>(
utf8Json: await listResponse.Content.ReadAsStreamAsync(),
options: new JsonSerializerOptions
{
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
PropertyNameCaseInsensitive = true
PropertyNameCaseInsensitive = true,
});

if (githubReleaseList == null)
{
return null;
}

return githubReleaseList.FirstOrDefault(x => !x.Prerelease);
if (githubReleaseList == null)
{
return null;
}

return githubReleaseList.FirstOrDefault(x => !x.Prerelease);
}
}
31 changes: 15 additions & 16 deletions src/Sidekick.Apis.GitHub/IGitHubClient.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
using Sidekick.Apis.GitHub.Models;

namespace Sidekick.Apis.GitHub
namespace Sidekick.Apis.GitHub;

/// <summary>
/// Interface to communicate with GitHub.
/// </summary>
public interface IGitHubClient
{
/// <summary>
/// Interface to communicate with GitHub.
/// Gets the latest release.
/// </summary>
public interface IGitHubClient
{
/// <summary>
/// Gets the latest release.
/// </summary>
/// <returns>True if an update is available.</returns>
Task<GitHubRelease> GetLatestRelease();
/// <returns>True if an update is available.</returns>
Task<GitHubRelease> GetLatestRelease();

/// <summary>
/// Downloads the latest release from github.
/// </summary>
/// <param name="downloadPath">The path where the file is to be downloaded.</param>
/// <returns>True if the file downloaded successfully.</returns>
Task<bool> DownloadLatest(string downloadPath);
}
/// <summary>
/// Downloads the latest release from github.
/// </summary>
/// <param name="downloadPath">The path where the file is to be downloaded.</param>
/// <returns>True if the file downloaded successfully.</returns>
Task<bool> DownloadLatest(string downloadPath);
}
11 changes: 5 additions & 6 deletions src/Sidekick.Apis.GitHub/Models/GitHubRelease.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
namespace Sidekick.Apis.GitHub.Models
namespace Sidekick.Apis.GitHub.Models;

public record GitHubRelease
{
public record GitHubRelease
{
public bool IsNewerVersion { get; set; }
public bool IsNewerVersion { get; init; }

public bool IsExecutable { get; set; }
}
public bool IsExecutable { get; init; }
}
4 changes: 2 additions & 2 deletions src/Sidekick.Apis.GitHub/Sidekick.Apis.GitHub.csproj
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Http" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.0" />
</ItemGroup>

</Project>
15 changes: 7 additions & 8 deletions src/Sidekick.Apis.GitHub/StartupExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
using Microsoft.Extensions.DependencyInjection;

namespace Sidekick.Apis.GitHub
namespace Sidekick.Apis.GitHub;

public static class StartupExtensions
{
public static class StartupExtensions
public static IServiceCollection AddSidekickGitHubApi(this IServiceCollection services)
{
public static IServiceCollection AddSidekickGitHubApi(this IServiceCollection services)
{
services.AddHttpClient();
services.AddTransient<IGitHubClient, GitHubClient>();
services.AddHttpClient();
services.AddTransient<IGitHubClient, GitHubClient>();

return services;
}
return services;
}
}
Loading

0 comments on commit d21c0d6

Please sign in to comment.