Skip to content

Commit

Permalink
Modernize codebase
Browse files Browse the repository at this point in the history
  • Loading branch information
kzu committed Oct 19, 2024
1 parent 063b3a3 commit 5619144
Show file tree
Hide file tree
Showing 23 changed files with 1,243 additions and 1,303 deletions.
454 changes: 226 additions & 228 deletions src/File/AddCommand.cs

Large diffs are not rendered by default.

13 changes: 5 additions & 8 deletions src/File/ChangesCommand.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
using DotNetConfig;

namespace Devlooped
{
class ChangesCommand : UpdateCommand
{
public ChangesCommand(Config configuration) : base(configuration) { }
namespace Devlooped;

protected override bool DryRun => true;
class ChangesCommand(Config configuration) : UpdateCommand(configuration)
{
protected override bool DryRun => true;

protected override AddCommand Clone() => new ChangesCommand(Configuration);
}
protected override AddCommand Clone() => new ChangesCommand(Configuration);
}
97 changes: 46 additions & 51 deletions src/File/Command.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,73 +5,68 @@
using System.Threading.Tasks;
using DotNetConfig;

namespace Devlooped
{
abstract class Command
{
public List<FileSpec> Files { get; } = new List<FileSpec>();
namespace Devlooped;

protected Command(Config configuration) => Configuration = configuration;
abstract class Command(Config configuration)
{
public List<FileSpec> Files { get; } = [];

public Config Configuration { get; }
public Config Configuration { get; } = configuration;

public abstract Task<int> ExecuteAsync();
public abstract Task<int> ExecuteAsync();

protected internal IEnumerable<FileSpec> GetConfiguredFiles()
protected internal IEnumerable<FileSpec> GetConfiguredFiles()
{
foreach (var file in Configuration.Where(x => x.Section == "file").GroupBy(x => x.Subsection))
{
foreach (var file in Configuration.Where(x => x.Section == "file").GroupBy(x => x.Subsection))
if (Configuration.GetBoolean("file", file.Key, "skip") == true)
continue;

// If no subsection exists, this is a glob-like URL (like a repo root or dir from GH)
// In this case, there can be many URLs, but there will never be an etag
if (file.Key == null)
{
if (Configuration.GetBoolean("file", file.Key, "skip") == true)
continue;
foreach (var entry in Configuration.GetAll("file", "url").Where(x => x.RawValue != null))
{
yield return new FileSpec(new Uri(entry.GetString()));
}
}
else
{
// If there is a subsection, we might still get multiple URLs for the case where
// the subsection is actually a target folder where multiple glob URLs are to be
// downloaded (i.e. "docs" with multiple GH URLs for docs subdirs)
var urls = Configuration
.GetAll("file", file.Key, "url")
.Where(x => x.RawValue != null)
.Select(x => x.GetString())
.ToArray();

// If no subsection exists, this is a glob-like URL (like a repo root or dir from GH)
// In this case, there can be many URLs, but there will never be an etag
if (file.Key == null)
if (urls.Length > 1)
{
foreach (var entry in Configuration.GetAll("file", "url").Where(x => x.RawValue != null))
foreach (var url in urls)
{
yield return new FileSpec(new Uri(entry.GetString()));
yield return new FileSpec(file.Key, new Uri(url));
}
}
else
else if (urls.Length == 1)
{
// If there is a subsection, we might still get multiple URLs for the case where
// the subsection is actually a target folder where multiple glob URLs are to be
// downloaded (i.e. "docs" with multiple GH URLs for docs subdirs)
var urls = Configuration
.GetAll("file", file.Key, "url")
.Where(x => x.RawValue != null)
.Select(x => x.GetString())
.ToArray();

if (urls.Length > 1)
{
foreach (var url in urls)
{
yield return new FileSpec(file.Key, new Uri(url));
}
}
else if (urls.Length == 1)
{
// Default case is there's a single URL, so we might have an etag in that case,
// and optionally a SHA too.
yield return new FileSpec(file.Key,
new Uri(urls[0]),
Configuration.GetString("file", file.Key, "etag"),
Configuration.GetString("file", file.Key, "sha"));
}
// Default case is there's a single URL, so we might have an etag in that case,
// and optionally a SHA too.
yield return new FileSpec(file.Key,
new Uri(urls[0]),
Configuration.GetString("file", file.Key, "etag"),
Configuration.GetString("file", file.Key, "sha"));
}
}
}
}

public static Command NullCommand { get; } =
new NoOpCommand(Config.Build(Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString())));

class NoOpCommand : Command
{
public NoOpCommand(Config configuration) : base(configuration) { }
public static Command NullCommand { get; } =
new NoOpCommand(Config.Build(Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString())));

public override Task<int> ExecuteAsync() => Task.FromResult(0);
}
class NoOpCommand(Config configuration) : Command(configuration)
{
public override Task<int> ExecuteAsync() => Task.FromResult(0);
}
}
53 changes: 25 additions & 28 deletions src/File/DeleteCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,39 @@
using System.Threading.Tasks;
using DotNetConfig;

namespace Devlooped
namespace Devlooped;

class DeleteCommand(Config configuration) : Command(configuration)
{
class DeleteCommand : Command
public override Task<int> ExecuteAsync()
{
public DeleteCommand(Config configuration) : base(configuration) { }

public override Task<int> ExecuteAsync()
{
var result = 0;
var result = 0;

var length = Files.Select(x => x.Path).Max(x => x.Length) + 1;
void writefixed(string s) => Console.Write(s + new string(' ', length - s.Length));
var length = Files.Select(x => x.Path).Max(x => x.Length) + 1;
void writefixed(string s) => Console.Write(s + new string(' ', length - s.Length));

foreach (var file in Files)
foreach (var file in Files)
{
try
{
try
{
if (File.Exists(file.Path))
File.Delete(file.Path);
if (File.Exists(file.Path))
File.Delete(file.Path);

var url = Configuration.GetString("file", file.Path, "url");
if (url != null)
Configuration.RemoveSection("file", file.Path);
var url = Configuration.GetString("file", file.Path, "url");
if (url != null)
Configuration.RemoveSection("file", file.Path);

writefixed(file.Path);
Console.WriteLine('✓');
}
catch (Exception e)
{
writefixed(file.Path);
Console.WriteLine(" x - " + e.Message);
result = 1;
}
writefixed(file.Path);
Console.WriteLine('✓');
}
catch (Exception e)
{
writefixed(file.Path);
Console.WriteLine(" x - " + e.Message);
result = 1;
}

return Task.FromResult(result);
}

return Task.FromResult(result);
}
}
Loading

0 comments on commit 5619144

Please sign in to comment.