diff --git a/Updator.Downloader.CLI/DownloaderMeta.cs b/Updator.Downloader.CLI/DownloaderMeta.cs index d70b0b5..507a8f9 100644 --- a/Updator.Downloader.CLI/DownloaderMeta.cs +++ b/Updator.Downloader.CLI/DownloaderMeta.cs @@ -1,5 +1,5 @@ namespace Updator.Downloader.CLI; public class DownloaderMeta { - public const int Version = 11; + public const int Version = 15; } \ No newline at end of file diff --git a/Updator.Downloader.CLI/Program.cs b/Updator.Downloader.CLI/Program.cs index 7299129..925caa7 100644 --- a/Updator.Downloader.CLI/Program.cs +++ b/Updator.Downloader.CLI/Program.cs @@ -24,12 +24,12 @@ var projectName = string.Empty; // Reads sources.json -var configPath = "./sources.json"; -if (!File.Exists(configPath)) { +var sourcesPath = "./sources.json"; +if (!File.Exists(sourcesPath)) { AnsiConsole.MarkupLine(Strings.SourcesNotFound); return; } -var sources = await JsonSerializer.DeserializeAsync(new MemoryStream(File.ReadAllBytes(configPath)), +var sources = await JsonSerializer.DeserializeAsync(new MemoryStream(File.ReadAllBytes(sourcesPath)), SourcesSerializer.Default.Sources); // If there's custom downloader url, replace it @@ -153,13 +153,24 @@ await AnsiConsole.Status().Spinner(Spinner.Known.Dots2).StartAsync(Strings.Check // Update logs to display if there's any var updateLogs = new List(); +Source GetSelectedSource(Sources srcs) { + // Select the unique enabled source + var sourceCandidate = srcs.sources.Where(a => a.enable).ToList(); + if (sourceCandidate.Count != 1) { + if (string.IsNullOrWhiteSpace(srcs.defaultSourceId)) + return null; + return srcs.sources.FirstOrDefault(a => a.id == srcs.defaultSourceId); + } + return sourceCandidate.First(); +} + await AnsiConsole.Progress() .Columns(new TaskDescriptionColumn(), new ProgressBarColumn(), new PercentageColumn(), new SpinnerColumn(Spinner.Known.Dots2)).StartAsync(async p => { var task = p.AddTask(Strings.UpdateSourcesJson); // Update sources.json if set. - if (!string.IsNullOrWhiteSpace(sources.sourcesUrl)) { + if (!string.IsNullOrWhiteSpace(sources.sourcesUrl) && !sources.disableSourcesUpdate) { try { using var http = new HttpClient(new SocketsHttpHandler() { ConnectTimeout = TimeSpan.FromSeconds(10) @@ -173,8 +184,26 @@ await AnsiConsole.Progress() // Replace the file if the remote one is newer. if (newSourcesObj.version > sources.version) { - await File.WriteAllBytesAsync(configPath, newSources); + // If user changed source, try keep it. + var defaultId = sources.defaultSourceId; + if (!string.IsNullOrWhiteSpace(defaultId)) { + var s = GetSelectedSource(sources); + if (!string.IsNullOrWhiteSpace(s.id) && s.id != defaultId) { + var ns = newSourcesObj.sources.FirstOrDefault(a => a.distributionUrl == s.distributionUrl); + if (ns != null) { + newSourcesObj.sources.ForEach(a => a.enable = false); + ns.enable = true; + } + } + } + sources = newSourcesObj; + var newSourcesStr = JsonSerializer.Serialize(newSourcesObj, new SourcesSerializer( + new JsonSerializerOptions() { + WriteIndented = true, + DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault + }).Sources); + await File.WriteAllTextAsync(sourcesPath, newSourcesStr); } task.Increment(5); @@ -188,14 +217,12 @@ await AnsiConsole.Progress() task.Value = task.MaxValue; } - // Select the unique enabled source - var sourceCandidate = sources.sources.Where(a => a.enable).ToList(); - if (sourceCandidate.Count != 1) { + var source = GetSelectedSource(sources); + if (source == null) { AnsiConsole.MarkupLine(Strings.ShouldUniqueSource); return; } - var source = sourceCandidate.First(); DistDescription desc = null; task = p.AddTask(Strings.DownloadDescription); @@ -371,7 +398,7 @@ await Parallel.ForEachAsync(desc.files, async (f, ct) => { // Print a hint if (!string.IsNullOrWhiteSpace(projectName)) { await AnsiConsole.Status().Spinner(Spinner.Known.Dots2).StartAsync(string.Format(Strings.UpdateDone, projectName), - async ctx => { await Task.Delay(TimeSpan.FromSeconds(3)); }); + async ctx => { await Task.Delay(TimeSpan.FromSeconds(2)); }); } // Wait for user ENTER if there's log. diff --git a/Updator.Downloader.CLI/Sources.cs b/Updator.Downloader.CLI/Sources.cs index 8779b1a..1f0b12c 100644 --- a/Updator.Downloader.CLI/Sources.cs +++ b/Updator.Downloader.CLI/Sources.cs @@ -6,6 +6,7 @@ namespace Updator.Downloader.CLI; public class Source { + public string id { get; set; } // Only one source should be enabled. public bool enable { get; set; } // The distribution root url of the source. @@ -22,6 +23,11 @@ public class Sources { public string sourcesUrl { get; set; } // Custom downloader update url, default is github, check code. public string customDownloaderUrl { get; set; } + // Disable auto update sources.json + public bool disableSourcesUpdate { get; set; } + + // Set the default source id + public string defaultSourceId { get; set; } // Available sources, you can predefine `release`, `debug` channels and let user to enable one of them // if the user wants to switch channel. diff --git a/Updator.Uploader/Program.cs b/Updator.Uploader/Program.cs index cdf43ee..364a3cf 100644 --- a/Updator.Uploader/Program.cs +++ b/Updator.Uploader/Program.cs @@ -183,7 +183,7 @@ await Parallel.ForEachAsync(root.Items, async (item, _) => { // Write logs if there's if (options.UpdateLogs != null) { - var updateLogs = options.UpdateLogs.Where(a => !string.IsNullOrWhiteSpace(a)).ToList(); + var updateLogs = options.UpdateLogs.Where(a => !string.IsNullOrWhiteSpace(a)).Distinct().ToList(); if (updateLogs.Any()) { logger.LogInformation($"Writing update logs."); var updateLog = new DistUpdateLog() {