rework cli stuff a bit

This commit is contained in:
halgari 2022-09-30 18:46:15 -06:00
parent 7e643b1c2b
commit 6a6f95d70f
6 changed files with 112 additions and 28 deletions

View File

@ -1,12 +1,16 @@
using System;
using System.Collections.Generic;
using System.CommandLine;
using System.ComponentModel.Design;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Wabbajack.CLI.Verbs;
using Wabbajack.Services.OSIntegrated;
namespace Wabbajack.CLI;
public class CommandLineBuilder
public partial class CommandLineBuilder
{
private readonly IConsole _console;
private readonly IEnumerable<IVerb> _verbs;
@ -17,6 +21,11 @@ public class CommandLineBuilder
_verbs = verbs;
}
static CommandLineBuilder()
{
RegisterAll();
}
public async Task<int> Run(string[] args)
{
var root = new RootCommand();
@ -25,4 +34,32 @@ public class CommandLineBuilder
return await root.InvokeAsync(args);
}
private static List<(Type, Func<Command>)> _commands { get; set; } = new();
public static IEnumerable<Type> Verbs => _commands.Select(c => c.Item1);
public static void RegisterCommand<T>(Func<Command> makeCommand)
{
_commands.Add((typeof(T), makeCommand));
}
}
public record VerbDefinition()
{
}
public static class CommandLineBuilderExtensions
{
public static IServiceCollection AddCommands(this IServiceCollection services)
{
services.AddSingleton<CommandLineBuilder>();
foreach (var verb in CommandLineBuilder.Verbs)
{
services.AddSingleton(typeof(IVerb), verb);
}
return services;
}
}

View File

@ -57,32 +57,7 @@ internal class Program
services.AddTransient<Context>();
services.AddSingleton<IVerb, HashFile>();
services.AddSingleton<IVerb, VFSIndexFolder>();
services.AddSingleton<IVerb, Encrypt>();
services.AddSingleton<IVerb, Decrypt>();
services.AddSingleton<IVerb, ValidateLists>();
services.AddSingleton<IVerb, DownloadCef>();
services.AddSingleton<IVerb, DownloadUrl>();
services.AddSingleton<IVerb, GenerateMetricsReports>();
services.AddSingleton<IVerb, ForceHeal>();
services.AddSingleton<IVerb, MirrorFile>();
services.AddSingleton<IVerb, SteamLogin>();
services.AddSingleton<IVerb, SteamAppDumpInfo>();
services.AddSingleton<IVerb, SteamDownloadFile>();
services.AddSingleton<IVerb, UploadToNexus>();
services.AddSingleton<IVerb, ListCreationClubContent>();
services.AddSingleton<IVerb, ListModlists>();
services.AddSingleton<IVerb, Extract>();
services.AddSingleton<IVerb, DumpZipInfo>();
services.AddSingleton<IVerb, Install>();
services.AddSingleton<IVerb, Compile>();
services.AddSingleton<IVerb, InstallCompileInstallVerify>();
services.AddSingleton<IVerb, HashUrlString>();
services.AddSingleton<IVerb, DownloadAll>();
services.AddSingleton<IVerb, ModlistReport>();
services.AddSingleton<IVerb, ListGames>();
services.AddCommands();
services.AddSingleton<IUserInterventionHandler, UserInterventionHandler>();
}).Build();

View File

@ -0,0 +1,34 @@

namespace Wabbajack.CLI;
using Wabbajack.CLI.Verbs;
public partial class CommandLineBuilder {
private static void RegisterAll() {
RegisterCommand<Compile>(Compile.MakeCommand);
RegisterCommand<Decrypt>(Decrypt.MakeCommand);
RegisterCommand<DownloadAll>(DownloadAll.MakeCommand);
RegisterCommand<DownloadCef>(DownloadCef.MakeCommand);
RegisterCommand<DownloadUrl>(DownloadUrl.MakeCommand);
RegisterCommand<DumpZipInfo>(DumpZipInfo.MakeCommand);
RegisterCommand<Encrypt>(Encrypt.MakeCommand);
RegisterCommand<Extract>(Extract.MakeCommand);
RegisterCommand<ForceHeal>(ForceHeal.MakeCommand);
RegisterCommand<GenerateMetricsReports>(GenerateMetricsReports.MakeCommand);
RegisterCommand<HashFile>(HashFile.MakeCommand);
RegisterCommand<HashUrlString>(HashUrlString.MakeCommand);
RegisterCommand<Install>(Install.MakeCommand);
RegisterCommand<InstallCompileInstallVerify>(InstallCompileInstallVerify.MakeCommand);
RegisterCommand<ListCreationClubContent>(ListCreationClubContent.MakeCommand);
RegisterCommand<ListGames>(ListGames.MakeCommand);
RegisterCommand<ListModlists>(ListModlists.MakeCommand);
RegisterCommand<MirrorFile>(MirrorFile.MakeCommand);
RegisterCommand<ModlistReport>(ModlistReport.MakeCommand);
RegisterCommand<SteamDownloadFile>(SteamDownloadFile.MakeCommand);
RegisterCommand<SteamDumpAppInfo>(SteamDumpAppInfo.MakeCommand);
RegisterCommand<SteamLogin>(SteamLogin.MakeCommand);
RegisterCommand<UploadToNexus>(UploadToNexus.MakeCommand);
RegisterCommand<ValidateLists>(ValidateLists.MakeCommand);
RegisterCommand<VFSIndex>(VFSIndex.MakeCommand);
}
}

View File

@ -0,0 +1,26 @@
<#@ template language="C#v3.5" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.IO"#>
<#@ import namespace="System.Collections.Generic" #>
namespace Wabbajack.CLI;
using Wabbajack.CLI.Verbs;
public partial class CommandLineBuilder {
private static void RegisterAll() {
<#
foreach (var verb in Directory.EnumerateFiles("Verbs"))
{
var klass = verb.Split('\\').Last().Split('.').First();
if (klass == "IVerb") continue;
#>
RegisterCommand<<#=klass#>>(<#=klass#>.MakeCommand);
<#
}
#>
}
}

View File

@ -38,7 +38,7 @@ public class Compile : IVerb
_inferencer = inferencer;
}
public Command MakeCommand()
public static Command MakeCommand()
{
var command = new Command("compile");
command.Add(new Option<AbsolutePath>(new[] {"-i", "-installPath"}, "Install Path"));

View File

@ -40,6 +40,18 @@
<ItemGroup>
<None Remove="Resources\ModlistReport.html" />
<EmbeddedResource Include="Resources\ModlistReport.html" />
<None Update="VerbRegistration.tt">
<Generator>TextTemplatingFileGenerator</Generator>
<LastGenOutput>VerbRegistration.cs</LastGenOutput>
</None>
</ItemGroup>
<ItemGroup>
<Compile Update="VerbRegistration.cs">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
<DependentUpon>VerbRegistration.tt</DependentUpon>
</Compile>
</ItemGroup>
</Project>