From e69d8fdd172ff4570dba87ced368efbc2910bfd6 Mon Sep 17 00:00:00 2001 From: Drombeys Date: Fri, 12 Jan 2024 03:38:51 +0300 Subject: [PATCH] Decompose `UpdaterService` class --- .../App.axaml.cs | 1 + .../ViewModels/MainWindowViewModel.cs | 11 ++++---- .../Services/GitHubApiService.cs | 22 ++++++++++------ .../Services/IGitStorageApiService.cs | 4 +-- .../Services/IUpdaterService.cs | 5 ++++ .../Services/UpdaterService.cs | 26 +++++++++---------- .../Program.cs | 14 +++++++++- 7 files changed, 53 insertions(+), 30 deletions(-) create mode 100644 src/ImeSense.Launchers.Belarus.Core/Services/IUpdaterService.cs diff --git a/src/ImeSense.Launchers.Belarus.Avalonia/App.axaml.cs b/src/ImeSense.Launchers.Belarus.Avalonia/App.axaml.cs index 2ae1f2c..f13ed75 100644 --- a/src/ImeSense.Launchers.Belarus.Avalonia/App.axaml.cs +++ b/src/ImeSense.Launchers.Belarus.Avalonia/App.axaml.cs @@ -54,6 +54,7 @@ private IServiceCollection ConfigureServices() { services.AddSingleton(); services.AddSingleton(); services.AddTransient, ReleaseComparerService>(); + services.AddTransient(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); diff --git a/src/ImeSense.Launchers.Belarus.Avalonia/ViewModels/MainWindowViewModel.cs b/src/ImeSense.Launchers.Belarus.Avalonia/ViewModels/MainWindowViewModel.cs index a4d853e..399fb98 100644 --- a/src/ImeSense.Launchers.Belarus.Avalonia/ViewModels/MainWindowViewModel.cs +++ b/src/ImeSense.Launchers.Belarus.Avalonia/ViewModels/MainWindowViewModel.cs @@ -17,6 +17,7 @@ public class MainWindowViewModel : ReactiveObject, IAsyncInitialization { private readonly ILogger _logger; private readonly InitializerManager _initializerManager; + private readonly IUpdaterService _updaterService; private readonly AuthorizationViewModel _authorizationViewModel; private readonly StartGameViewModel _startGameViewModel; private readonly LauncherViewModel _launcherViewModel; @@ -26,11 +27,11 @@ public class MainWindowViewModel : ReactiveObject, IAsyncInitialization { public Task Initialization { get; private set; } public MainWindowViewModel(ILogger 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)); @@ -46,7 +47,7 @@ public MainWindowViewModel() { _startGameViewModel = null!; _launcherViewModel = null!; _initializerManager = null!; - + _updaterService = null!; Initialization = null!; } @@ -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(); diff --git a/src/ImeSense.Launchers.Belarus.Core/Services/GitHubApiService.cs b/src/ImeSense.Launchers.Belarus.Core/Services/GitHubApiService.cs index d9d905b..bfcbd44 100644 --- a/src/ImeSense.Launchers.Belarus.Core/Services/GitHubApiService.cs +++ b/src/ImeSense.Launchers.Belarus.Core/Services/GitHubApiService.cs @@ -55,20 +55,26 @@ public GitHubApiService(ILogger? logger, HttpClient httpClient return await _httpClient.GetFromJsonAsync(asset?.BrowserDownloadUrl); } - public async Task GetLastReleaseAsync() { - if (_httpClient.BaseAddress == null) { - return default; + public async Task GetLastReleaseAsync(Uri? uriRepository = null) { + if (uriRepository == null) { + if (_httpClient.BaseAddress == null) { + return default; + } + uriRepository = _httpClient.BaseAddress; } - return await _httpClient.GetFromJsonAsync(new Uri(_httpClient.BaseAddress, "releases/latest")); + return await _httpClient.GetFromJsonAsync(new Uri(uriRepository, "releases/latest")); } - public async Task GetReleaseAsync(string tag) { - if (_httpClient.BaseAddress == null) { - return default; + public async Task GetReleaseAsync(string tag, Uri? uriRepository = null) { + if (uriRepository == null) { + if (_httpClient.BaseAddress == null) { + return default; + } + uriRepository = _httpClient.BaseAddress; } - var json = await _httpClient.GetFromJsonAsync>(new Uri(_httpClient.BaseAddress, $"releases")); + var json = await _httpClient.GetFromJsonAsync>(new Uri(uriRepository, $"releases")); return json?.FirstOrDefault(t => t.TagName.Equals(tag)); } diff --git a/src/ImeSense.Launchers.Belarus.Core/Services/IGitStorageApiService.cs b/src/ImeSense.Launchers.Belarus.Core/Services/IGitStorageApiService.cs index aaaa537..13ad9a0 100644 --- a/src/ImeSense.Launchers.Belarus.Core/Services/IGitStorageApiService.cs +++ b/src/ImeSense.Launchers.Belarus.Core/Services/IGitStorageApiService.cs @@ -5,7 +5,7 @@ namespace ImeSense.Launchers.Belarus.Core.Services; public interface IGitStorageApiService { IAsyncEnumerable DownloadJsonArrayAsync(string filename) where T : class; Task DownloadJsonAsync(string filename) where T : class; - Task GetLastReleaseAsync(); + Task GetLastReleaseAsync(Uri? uriRepository = null); Task?> GetTagsAsync(Uri? uriRepository = null); - Task GetReleaseAsync(string tag); + Task GetReleaseAsync(string tag, Uri? uriRepository = null); } diff --git a/src/ImeSense.Launchers.Belarus.Core/Services/IUpdaterService.cs b/src/ImeSense.Launchers.Belarus.Core/Services/IUpdaterService.cs new file mode 100644 index 0000000..db34980 --- /dev/null +++ b/src/ImeSense.Launchers.Belarus.Core/Services/IUpdaterService.cs @@ -0,0 +1,5 @@ +namespace ImeSense.Launchers.Belarus.Core.Services; + +public interface IUpdaterService { + Task UpdaterAsync(Uri uri, string fileSavePath); +} diff --git a/src/ImeSense.Launchers.Belarus.Core/Services/UpdaterService.cs b/src/ImeSense.Launchers.Belarus.Core/Services/UpdaterService.cs index 97040a6..38da2fd 100644 --- a/src/ImeSense.Launchers.Belarus.Core/Services/UpdaterService.cs +++ b/src/ImeSense.Launchers.Belarus.Core/Services/UpdaterService.cs @@ -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!"); @@ -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); diff --git a/src/ImeSense.Launchers.Belarus.Updater/Program.cs b/src/ImeSense.Launchers.Belarus.Updater/Program.cs index 748f36f..de17add 100644 --- a/src/ImeSense.Launchers.Belarus.Updater/Program.cs +++ b/src/ImeSense.Launchers.Belarus.Updater/Program.cs @@ -1,3 +1,4 @@ +using System; using System.Diagnostics; using System.Net.Http.Headers; @@ -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) {