Skip to content

Commit

Permalink
Decompose UpdaterService class
Browse files Browse the repository at this point in the history
  • Loading branch information
Drombeys committed Jan 12, 2024
1 parent 3e6020d commit e69d8fd
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 30 deletions.
1 change: 1 addition & 0 deletions src/ImeSense.Launchers.Belarus.Avalonia/App.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ private IServiceCollection ConfigureServices() {
services.AddSingleton<ILauncherStorage, MemoryLauncherStorage>();
services.AddSingleton<ILocaleManager, LocaleManager>();
services.AddTransient<IReleaseComparerService<GitHubRelease>, ReleaseComparerService>();
services.AddTransient<IUpdaterService, UpdaterService>();
services.AddSingleton<AuthenticationViewModelValidator>();
services.AddSingleton<StartGameViewModelValidator>();
services.AddSingleton<UserManager>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class MainWindowViewModel : ReactiveObject, IAsyncInitialization {
private readonly ILogger<MainWindowViewModel> _logger;

private readonly InitializerManager _initializerManager;
private readonly IUpdaterService _updaterService;
private readonly AuthorizationViewModel _authorizationViewModel;
private readonly StartGameViewModel _startGameViewModel;
private readonly LauncherViewModel _launcherViewModel;
Expand All @@ -26,11 +27,11 @@ public class MainWindowViewModel : ReactiveObject, IAsyncInitialization {
public Task Initialization { get; private set; }

public MainWindowViewModel(ILogger<MainWindowViewModel> logger, InitializerManager initializerManager,
LauncherViewModel launcherViewModel, AuthorizationViewModel authorizationViewModel,
StartGameViewModel startGameViewModel) {
IUpdaterService updaterService, LauncherViewModel launcherViewModel,
AuthorizationViewModel authorizationViewModel, StartGameViewModel startGameViewModel) {
_logger = logger;
_initializerManager = initializerManager;

_updaterService = updaterService;
_authorizationViewModel = authorizationViewModel;
_startGameViewModel = startGameViewModel ?? throw new ArgumentNullException(nameof(startGameViewModel));
_launcherViewModel = launcherViewModel ?? throw new ArgumentNullException(nameof(launcherViewModel));
Expand All @@ -46,7 +47,7 @@ public MainWindowViewModel() {
_startGameViewModel = null!;
_launcherViewModel = null!;
_initializerManager = null!;

_updaterService = null!;
Initialization = null!;
}

Expand All @@ -58,7 +59,7 @@ public async Task InitializeAsync() {
if (!isLauncherReleaseCurrent) {
var pathLauncherUpdater = Path.Combine(FileLocations.BaseDirectory,
FileNamesStorage.SBLauncherUpdater);
await UpdaterService.UpdaterAsync(UriStorage.LauncherUri, pathLauncherUpdater);
await _updaterService.UpdaterAsync(UriStorage.LauncherUri, pathLauncherUpdater);

var updater = Launcher.Launch(pathLauncherUpdater);
updater?.Start();
Expand Down
22 changes: 14 additions & 8 deletions src/ImeSense.Launchers.Belarus.Core/Services/GitHubApiService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,26 @@ public GitHubApiService(ILogger<GitHubApiService>? logger, HttpClient httpClient
return await _httpClient.GetFromJsonAsync<T>(asset?.BrowserDownloadUrl);
}

public async Task<GitHubRelease?> GetLastReleaseAsync() {
if (_httpClient.BaseAddress == null) {
return default;
public async Task<GitHubRelease?> GetLastReleaseAsync(Uri? uriRepository = null) {
if (uriRepository == null) {
if (_httpClient.BaseAddress == null) {
return default;
}
uriRepository = _httpClient.BaseAddress;
}

return await _httpClient.GetFromJsonAsync<GitHubRelease>(new Uri(_httpClient.BaseAddress, "releases/latest"));
return await _httpClient.GetFromJsonAsync<GitHubRelease>(new Uri(uriRepository, "releases/latest"));
}

public async Task<GitHubRelease?> GetReleaseAsync(string tag) {
if (_httpClient.BaseAddress == null) {
return default;
public async Task<GitHubRelease?> GetReleaseAsync(string tag, Uri? uriRepository = null) {
if (uriRepository == null) {
if (_httpClient.BaseAddress == null) {
return default;
}
uriRepository = _httpClient.BaseAddress;
}

var json = await _httpClient.GetFromJsonAsync<IList<GitHubRelease>>(new Uri(_httpClient.BaseAddress, $"releases"));
var json = await _httpClient.GetFromJsonAsync<IList<GitHubRelease>>(new Uri(uriRepository, $"releases"));
return json?.FirstOrDefault(t => t.TagName.Equals(tag));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace ImeSense.Launchers.Belarus.Core.Services;
public interface IGitStorageApiService {
IAsyncEnumerable<T?> DownloadJsonArrayAsync<T>(string filename) where T : class;
Task<T?> DownloadJsonAsync<T>(string filename) where T : class;
Task<GitHubRelease?> GetLastReleaseAsync();
Task<GitHubRelease?> GetLastReleaseAsync(Uri? uriRepository = null);
Task<IEnumerable<Tag?>?> GetTagsAsync(Uri? uriRepository = null);
Task<GitHubRelease?> GetReleaseAsync(string tag);
Task<GitHubRelease?> GetReleaseAsync(string tag, Uri? uriRepository = null);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace ImeSense.Launchers.Belarus.Core.Services;

public interface IUpdaterService {
Task UpdaterAsync(Uri uri, string fileSavePath);
}
26 changes: 12 additions & 14 deletions src/ImeSense.Launchers.Belarus.Core/Services/UpdaterService.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
using ImeSense.Launchers.Belarus.Core.Manager;
using ImeSense.Launchers.Belarus.Core.Storage;

using System.Net.Http.Headers;

namespace ImeSense.Launchers.Belarus.Core.Services;

// TODO: Decompose and deploy IoC
public class UpdaterService {
public static async Task UpdaterAsync(Uri uri, string fileSavePath) {
public class UpdaterService : IUpdaterService {
private readonly IGitStorageApiService _gitStorageApiService;
private readonly IFileDownloadManager _fileDownloadManager;

public UpdaterService(IGitStorageApiService gitStorageApiService, IFileDownloadManager fileDownloadManager) {
_gitStorageApiService = gitStorageApiService;
_fileDownloadManager = fileDownloadManager;
}

public async Task UpdaterAsync(Uri uri, string fileSavePath) {
var appName = Path.GetFileNameWithoutExtension(fileSavePath);
var fullAppName = Path.GetFileName(fileSavePath);

using var httpClient = new HttpClient();
httpClient.BaseAddress = uri;
httpClient.DefaultRequestHeaders.Accept.Clear();
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/vnd.github.v3+json"));
httpClient.DefaultRequestHeaders.Add("User-Agent", ".NET Foundation Repository Reporter");

var gitHubService = new GitHubApiService(null, httpClient, null);
var lastRelease = await gitHubService.GetLastReleaseAsync()
var lastRelease = await _gitStorageApiService.GetLastReleaseAsync(uri)
?? throw new NullReferenceException("Latest release is null!");
var sblauncher = lastRelease.Assets?.FirstOrDefault(x => x.Name.Equals(fullAppName))
?? throw new NullReferenceException($"{appName} asset is null!");
Expand All @@ -37,8 +36,7 @@ public static async Task UpdaterAsync(Uri uri, string fileSavePath) {
Directory.CreateDirectory(pathDownloadFolder);
}

var download = new FileDownloadManager(null, httpClient);
await download.DownloadAsync(sblauncher.BrowserDownloadUrl, fileDownloadPath, progress);
await _fileDownloadManager.DownloadAsync(sblauncher.BrowserDownloadUrl, fileDownloadPath, progress);

if (File.Exists(fileSavePath)) {
File.Delete(fileSavePath);
Expand Down
14 changes: 13 additions & 1 deletion src/ImeSense.Launchers.Belarus.Updater/Program.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Diagnostics;
using System.Net.Http.Headers;

Expand All @@ -15,7 +16,18 @@

Console.WriteLine($"Start update");
var fileSavePath = Path.Combine(FileLocations.BaseDirectory, FileNamesStorage.SBLauncher);
await UpdaterService.UpdaterAsync(UriStorage.LauncherUri, fileSavePath);

using var httpClient = new HttpClient();
httpClient.BaseAddress = UriStorage.LauncherUri;
httpClient.DefaultRequestHeaders.Accept.Clear();
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/vnd.github.v3+json"));
httpClient.DefaultRequestHeaders.Add("User-Agent", ".NET Foundation Repository Reporter");

var updaterService = new UpdaterService(
new GitHubApiService(null, httpClient, null),
new FileDownloadManager(null, httpClient));
await updaterService.UpdaterAsync(UriStorage.LauncherUri, fileSavePath);

Console.WriteLine($"Finish!");
Launcher.Launch(fileSavePath)?.Start();
} catch (Exception ex) {
Expand Down

0 comments on commit e69d8fd

Please sign in to comment.