|
| 1 | +using System.CommandLine; |
| 2 | +using System.CommandLine.Invocation; |
| 3 | +using System.CommandLine.NamingConventionBinder; |
| 4 | +using Microsoft.Extensions.DependencyInjection; |
| 5 | +using NexusMods.Interfaces.Components; |
| 6 | +using NexusMods.Paths; |
| 7 | + |
| 8 | +namespace NexusMods.CLI; |
| 9 | + |
| 10 | +public class CommandLineBuilder |
| 11 | +{ |
| 12 | + private static IServiceProvider _provider = null!; |
| 13 | + public CommandLineBuilder(IServiceProvider provider) |
| 14 | + { |
| 15 | + _provider = provider; |
| 16 | + } |
| 17 | + |
| 18 | + public async Task<int> Run(string[] args) |
| 19 | + { |
| 20 | + var root = new RootCommand(); |
| 21 | + foreach (var verb in _commands) |
| 22 | + { |
| 23 | + root.Add(MakeCommend(verb.Type, verb.Handler, verb.Definition)); |
| 24 | + } |
| 25 | + |
| 26 | + return await root.InvokeAsync(args); |
| 27 | + } |
| 28 | + |
| 29 | + private Dictionary<Type, Func<OptionDefinition, Option>> _optionCtors => new() |
| 30 | + { |
| 31 | + { |
| 32 | + typeof(string), |
| 33 | + d => new Option<string>(d.Aliases, description: d.Description) |
| 34 | + }, |
| 35 | + { |
| 36 | + typeof(AbsolutePath), |
| 37 | + d => new Option<AbsolutePath>(d.Aliases, description: d.Description, parseArgument: d => d.Tokens.Single().Value.ToAbsolutePath()) |
| 38 | + }, |
| 39 | + { |
| 40 | + typeof(Uri), |
| 41 | + d => new Option<Uri>(d.Aliases, description: d.Description) |
| 42 | + }, |
| 43 | + { |
| 44 | + typeof(bool), |
| 45 | + d => new Option<bool>(d.Aliases, description: d.Description) |
| 46 | + }, |
| 47 | + { |
| 48 | + typeof(IGame), |
| 49 | + d => new Option<IGame>(d.Aliases, description: d.Description, parseArgument: d => |
| 50 | + { |
| 51 | + var s = d.Tokens.Single().Value; |
| 52 | + var games = _provider.GetRequiredService<IEnumerable<IGame>>(); |
| 53 | + return games.First(d => d.Slug.Equals(s, StringComparison.InvariantCultureIgnoreCase)); |
| 54 | + }) |
| 55 | + }, |
| 56 | + { |
| 57 | + typeof(Version), |
| 58 | + d => new Option<Version>(d.Aliases, description: d.Description, parseArgument: d => Version.Parse(d.Tokens.Single().Value)) |
| 59 | + } |
| 60 | + }; |
| 61 | + |
| 62 | + |
| 63 | + private Command MakeCommend(Type verbType, Func<object, Delegate> verbHandler, VerbDefinition definition) |
| 64 | + { |
| 65 | + var command = new Command(definition.Name, definition.Description); |
| 66 | + foreach (var option in definition.Options) |
| 67 | + { |
| 68 | + command.Add(_optionCtors[option.Type](option)); |
| 69 | + } |
| 70 | + command.Handler = new HandlerDelegate(_provider, verbType, verbHandler); |
| 71 | + return command; |
| 72 | + } |
| 73 | + |
| 74 | + private class HandlerDelegate : ICommandHandler |
| 75 | + { |
| 76 | + private IServiceProvider _provider; |
| 77 | + private Type _type; |
| 78 | + private readonly Func<object, Delegate> _delgate; |
| 79 | + |
| 80 | + public HandlerDelegate(IServiceProvider provider, Type type, Func<object, Delegate> inner) |
| 81 | + { |
| 82 | + _provider = provider; |
| 83 | + _type = type; |
| 84 | + _delgate = inner; |
| 85 | + } |
| 86 | + public int Invoke(InvocationContext context) |
| 87 | + { |
| 88 | + var service = _provider.GetRequiredService(_type); |
| 89 | + var handler = CommandHandler.Create(_delgate(service)); |
| 90 | + return handler.Invoke(context); |
| 91 | + } |
| 92 | + |
| 93 | + public Task<int> InvokeAsync(InvocationContext context) |
| 94 | + { |
| 95 | + var service = _provider.GetRequiredService(_type); |
| 96 | + var handler = CommandHandler.Create(_delgate(service)); |
| 97 | + return handler.InvokeAsync(context); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + private static List<(Type Type, VerbDefinition Definition, Func<object, Delegate> Handler)> _commands { get; set; } = new(); |
| 102 | + public static IEnumerable<Type> Verbs => _commands.Select(c => c.Type); |
| 103 | + |
| 104 | + public static void RegisterCommand<T>(VerbDefinition definition, Func<object, Delegate> handler) |
| 105 | + { |
| 106 | + _commands.Add((typeof(T), definition, handler)); |
| 107 | + |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +public record OptionDefinition(Type Type, string ShortOption, string LongOption, string Description) |
| 112 | +{ |
| 113 | + public string[] Aliases |
| 114 | + { |
| 115 | + get |
| 116 | + { |
| 117 | + return new[] { "-" + ShortOption, "--" + LongOption }; |
| 118 | + } |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +public record VerbDefinition(string Name, string Description, OptionDefinition[] Options) |
| 123 | +{ |
| 124 | +} |
| 125 | + |
0 commit comments