-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add some constructor picking and arg resolve
- Loading branch information
1 parent
ab4b4be
commit cfed157
Showing
11 changed files
with
285 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
using GBX.NET.Tool.CLI.Inputs; | ||
|
||
namespace GBX.NET.Tool.CLI; | ||
|
||
internal sealed class ArgsResolver | ||
{ | ||
private readonly string[] args; | ||
private readonly HttpClient client; | ||
|
||
public bool HasArgs => args.Length > 0; | ||
|
||
public ArgsResolver(string[] args, HttpClient http) | ||
{ | ||
this.args = args; | ||
this.client = http; | ||
} | ||
|
||
public ToolConfiguration Resolve() | ||
{ | ||
if (!HasArgs) | ||
{ | ||
return new(); | ||
} | ||
|
||
var inputs = new List<Input>(); | ||
|
||
var argsEnumerator = args.GetEnumerator(); | ||
|
||
while (argsEnumerator.MoveNext()) | ||
{ | ||
var arg = argsEnumerator.Current as string ?? string.Empty; | ||
|
||
if (arg.StartsWith('-')) | ||
{ | ||
continue; | ||
} | ||
|
||
// - check http:// and https:// for URLs | ||
// - check for individual files and files in zip archives | ||
// - check for folders | ||
// - check for stdin (maybe?) | ||
// - check for configured user data path | ||
if (Directory.Exists(arg)) | ||
{ | ||
inputs.Add(new DirectoryInput(arg)); | ||
continue; | ||
} | ||
|
||
if (File.Exists(arg)) | ||
{ | ||
inputs.Add(new FileInput(arg)); | ||
continue; | ||
} | ||
|
||
if (Uri.TryCreate(arg, UriKind.Absolute, out var uri)) | ||
{ | ||
if (uri.Scheme == Uri.UriSchemeHttp || uri.Scheme == Uri.UriSchemeHttps) | ||
{ | ||
inputs.Add(new UriInput(client, uri)); | ||
} | ||
|
||
continue; | ||
} | ||
} | ||
|
||
return new ToolConfiguration | ||
{ | ||
Inputs = inputs | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace GBX.NET.Tool.CLI; | ||
|
||
public sealed class ConsoleOptions | ||
{ | ||
public bool DisableUpdateCheck { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
| ||
using GBX.NET.Exceptions; | ||
|
||
namespace GBX.NET.Tool.CLI.Inputs; | ||
|
||
internal sealed record DirectoryInput(string DirectoryPath) : Input | ||
{ | ||
public override async Task<object?> ResolveAsync(CancellationToken cancellationToken) | ||
{ | ||
var files = Directory.GetFiles(DirectoryPath, "*.*", SearchOption.AllDirectories); | ||
|
||
var tasks = files.Select<string, Task<object?>>(async file => | ||
{ | ||
try | ||
{ | ||
await using var stream = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, useAsync: true); | ||
return await Gbx.ParseAsync(stream, cancellationToken: cancellationToken); | ||
} | ||
catch (NotAGbxException) | ||
{ | ||
return await File.ReadAllBytesAsync(file, cancellationToken); | ||
} | ||
}); | ||
|
||
return await Task.WhenAll(tasks); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
| ||
using GBX.NET.Exceptions; | ||
|
||
namespace GBX.NET.Tool.CLI.Inputs; | ||
|
||
internal sealed record FileInput(string FilePath) : Input | ||
{ | ||
public override async Task<object?> ResolveAsync(CancellationToken cancellationToken) | ||
{ | ||
try | ||
{ | ||
await using var stream = new FileStream(FilePath, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, useAsync: true); | ||
return await Gbx.ParseAsync(stream, cancellationToken: cancellationToken); | ||
} | ||
catch (NotAGbxException) | ||
{ | ||
return File.ReadAllBytesAsync(FilePath, cancellationToken); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
| ||
namespace GBX.NET.Tool.CLI.Inputs; | ||
|
||
internal abstract record Input | ||
{ | ||
public abstract Task<object?> ResolveAsync(CancellationToken cancellationToken); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
| ||
using GBX.NET.Exceptions; | ||
|
||
namespace GBX.NET.Tool.CLI.Inputs; | ||
|
||
internal sealed record UriInput(HttpClient Http, Uri Uri) : Input | ||
{ | ||
public override async Task<object?> ResolveAsync(CancellationToken cancellationToken) | ||
{ | ||
using var response = await Http.GetAsync(Uri, cancellationToken); | ||
|
||
response.EnsureSuccessStatusCode(); | ||
|
||
try | ||
{ | ||
await using var stream = await response.Content.ReadAsStreamAsync(cancellationToken); | ||
return await Gbx.ParseAsync(stream, cancellationToken: cancellationToken); | ||
} | ||
catch (NotAGbxException) | ||
{ | ||
return await response.Content.ReadAsByteArrayAsync(cancellationToken); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using GBX.NET.Tool.CLI.Inputs; | ||
|
||
namespace GBX.NET.Tool.CLI; | ||
|
||
internal sealed class ToolConfiguration | ||
{ | ||
public ConsoleOptions ConsoleOptions { get; init; } = new(); | ||
public IReadOnlyCollection<Input> Inputs { get; init; } = []; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
namespace GBX.NET.Tool; | ||
using System.Reflection; | ||
|
||
namespace GBX.NET.Tool; | ||
|
||
public sealed class ToolFunctionality<T> where T : ITool | ||
{ | ||
public required object?[] InputParameters { get; init; } | ||
public required ConstructorInfo[] Constructors { get; init; } | ||
} |
Oops, something went wrong.