From 00000db63c50c9100010b00768ed8d05d95f550c Mon Sep 17 00:00:00 2001 From: Konstantin Safonov Date: Fri, 13 Oct 2023 13:36:38 +0300 Subject: [PATCH] Fix default output dir, enable nullables, fix compiler warnings --- src/Core/BellsAndWhistles.cs | 15 ++++++--- src/Core/DownloaderService.cs | 54 +++++++++++++++++---------------- src/Models/DownloaderOptions.cs | 9 +++++- src/Models/ResponseItem.cs | 24 +++++++-------- src/Program.cs | 18 +++++++++-- src/atlassian-downloader.csproj | 1 + 6 files changed, 75 insertions(+), 46 deletions(-) diff --git a/src/Core/BellsAndWhistles.cs b/src/Core/BellsAndWhistles.cs index 7c6fde4..d168b07 100644 --- a/src/Core/BellsAndWhistles.cs +++ b/src/Core/BellsAndWhistles.cs @@ -11,13 +11,13 @@ internal class BellsAndWhistles { private static readonly string assemblyEnvironment = string.Format("[{1}, {0}]", RuntimeInformation.ProcessArchitecture.ToString().ToLowerInvariant(), RuntimeInformation.FrameworkDescription); - private static readonly Assembly entryAssembly = Assembly.GetEntryAssembly(); + private static readonly Assembly entryAssembly = Assembly.GetEntryAssembly()!; - private static readonly string assemblyVersion = entryAssembly.GetName().Version.ToString(); + private static readonly string assemblyVersion = entryAssembly.GetName().Version!.ToString(); - private static readonly string fileVersion = entryAssembly.GetCustomAttribute().Version; + private static readonly string fileVersion = entryAssembly.GetCustomAttribute()!.Version; - private static readonly string assemblyName = entryAssembly.GetCustomAttribute().Product; + private static readonly string assemblyName = entryAssembly.GetCustomAttribute()!.Product; const string assemblyBuildType = #if DEBUG "[Debug]" @@ -31,7 +31,12 @@ internal class BellsAndWhistles public static void ShowVersionInfo(ILogger logger) { - logger.LogInformation($"{assemblyName} {assemblyVersion} {assemblyEnvironment} {assemblyBuildType}"); + logger.LogInformation( + "{assemblyName} {assemblyVersion} {assemblyEnvironment} {assemblyBuildType}", + assemblyName, + assemblyVersion, + assemblyEnvironment, + assemblyBuildType); Console.BackgroundColor = ConsoleColor.Black; WriteColorLine("%╔═╦═══════════════════════════════════════════════════════════════════════════════════════╦═╗"); WriteColorLine("%╠═╝ .''. %╚═%╣"); diff --git a/src/Core/DownloaderService.cs b/src/Core/DownloaderService.cs index 42cf0f9..81b9bf5 100644 --- a/src/Core/DownloaderService.cs +++ b/src/Core/DownloaderService.cs @@ -16,12 +16,15 @@ namespace EpicMorg.Atlassian.Downloader.Core; internal class DownloaderService : IHostedService { + private static readonly JsonSerializerOptions jsonOptions = new() + { + PropertyNameCaseInsensitive = true + }; private readonly ILogger logger; private readonly DownloaderOptions options; private readonly HttpClient client; private readonly IHostApplicationLifetime hostApplicationLifetime; - public DownloaderService(IHostApplicationLifetime hostApplicationLifetime, ILogger logger, HttpClient client, DownloaderOptions options) { this.logger = logger; @@ -87,15 +90,12 @@ public async Task StartAsync(CancellationToken cancellationToken) this.hostApplicationLifetime.StopApplication(); } - private async Task<(string json, IDictionary versions)> GetJson(string feedUrl, string productVersion = null, CancellationToken cancellationToken = default) + private async Task<(string json, IDictionary versions)> GetJson(string feedUrl, string? productVersion = null, CancellationToken cancellationToken = default) { var atlassianJson = await this.client.GetStringAsync(feedUrl, cancellationToken).ConfigureAwait(false); var json = atlassianJson.Trim()["downloads(".Length..^1]; - this.logger.LogTrace($"Downloaded json: {0}", json); - var parsed = JsonSerializer.Deserialize(json, new JsonSerializerOptions - { - PropertyNameCaseInsensitive = true - }); + this.logger.LogTrace("Downloaded json: {json}", json); + var parsed = JsonSerializer.Deserialize(json, jsonOptions)!; this.logger.LogDebug("Found {releaseCount} releases", parsed.Length); var versions = parsed .GroupBy(a => a.Version) @@ -112,7 +112,6 @@ private IReadOnlyList GetFeedUrls() => this.options.CustomFeed != null private async Task DownloadFilesFromFeed(string feedUrl, IDictionary versions, CancellationToken cancellationToken) { - var feedDir = Path.Combine(this.options.OutputDir, feedUrl[(feedUrl.LastIndexOf('/') + 1)..feedUrl.LastIndexOf('.')]); this.logger.LogInformation("Download from JSON \"{feedUrl}\" started", feedUrl); foreach (var version in versions) @@ -137,12 +136,10 @@ private async Task DownloadFilesFromFeed(string feedUrl, IDictionaryOverride target version to download some product. Advice: Use it with "customFeed". /// Skip compare of file sizes if a local file already exists. Existing file will be skipped to check and redownload. /// Set custom user agent via this feature flag. - static async Task Main(string outputDir, Uri[] customFeed = null, DownloadAction action = DownloadAction.Download, bool about = false, string productVersion = null, bool skipFileCheck = false, string userAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:101.0) Gecko/20100101 Firefox/101.0") => await + static async Task Main( + string? outputDir = default, + Uri[]? customFeed = null, + DownloadAction action = DownloadAction.Download, + bool about = false, + string? productVersion = null, + bool skipFileCheck = false, + string userAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:101.0) Gecko/20100101 Firefox/101.0") => await Host .CreateDefaultBuilder() .ConfigureHostConfiguration(configHost => configHost.AddEnvironmentVariables()) @@ -46,7 +53,14 @@ public class Program .AddSerilog(dispose: true); }) .AddHostedService() - .AddSingleton(new DownloaderOptions(outputDir, customFeed, action, about, productVersion, skipFileCheck, userAgent)) + .AddSingleton(new DownloaderOptions( + outputDir ?? Environment.CurrentDirectory, + customFeed, + action, + about, + productVersion, + skipFileCheck, + userAgent)) .AddHttpClient()) .RunConsoleAsync() .ConfigureAwait(false); diff --git a/src/atlassian-downloader.csproj b/src/atlassian-downloader.csproj index 5f4e8ea..493a125 100644 --- a/src/atlassian-downloader.csproj +++ b/src/atlassian-downloader.csproj @@ -10,6 +10,7 @@ win-x64 + enable Exe net8.0 false