Skip to content

Commit

Permalink
Save
Browse files Browse the repository at this point in the history
  • Loading branch information
reitowo committed Jan 20, 2023
1 parent 7332120 commit 75e239f
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Updator.Downloader.CLI/DownloaderMeta.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
namespace Updator.Downloader.CLI;

public class DownloaderMeta {
public const int Version = 11;
public const int Version = 15;
}
47 changes: 37 additions & 10 deletions Updator.Downloader.CLI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<DistUpdateLog>();

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)
Expand All @@ -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);

Expand All @@ -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);

Expand Down Expand Up @@ -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.
Expand Down
6 changes: 6 additions & 0 deletions Updator.Downloader.CLI/Sources.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion Updator.Uploader/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 75e239f

Please sign in to comment.