mirror of
https://github.com/m-lamonaca/script-launcher.git
synced 2025-04-05 18:06:28 +00:00
feat: use Spectre.Console.Cli
as CLI framework
This commit is contained in:
parent
4ef34ac2bc
commit
113da71e9a
2 changed files with 84 additions and 50 deletions
128
src/Program.cs
128
src/Program.cs
|
@ -1,58 +1,92 @@
|
|||
using Cocona;
|
||||
using System.ComponentModel;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using Spectre.Console;
|
||||
using Spectre.Console.Cli;
|
||||
|
||||
var app = CoconaLiteApp.Create();
|
||||
var app = new CommandApp<RootCommand>();
|
||||
return app.Run(args);
|
||||
|
||||
app.AddCommand(RootCommand);
|
||||
app.Run();
|
||||
|
||||
static async Task RootCommand(
|
||||
[Option("extensions", new[] { 'x' }, Description = "Comma separated list of script extensions")] string? extensions,
|
||||
[Option("depth", new[] { 'd' }, Description = "Search depth")] int depth = 1,
|
||||
[Option("elevated", new[] { 'e' }, Description = "Run with elevated privileges")] bool elevated = false,
|
||||
[Option("group", new[] { 'g' }, Description = "Group scripts by folder")] bool group = false,
|
||||
[Option("brief", new[] { 'b' }, Description = "Show brief information")] bool brief = false,
|
||||
[Argument(Name = "Directory", Description = "Starting directory")] string directory = ".")
|
||||
sealed class RootCommand : AsyncCommand<RootCommandSettings>
|
||||
{
|
||||
if (!Directory.Exists(directory))
|
||||
private const int Failure = 1;
|
||||
private const int Success = 0;
|
||||
|
||||
public override async Task<int> ExecuteAsync(
|
||||
[NotNull] CommandContext context,
|
||||
[NotNull] RootCommandSettings settings
|
||||
)
|
||||
{
|
||||
AnsiConsole.Markup($"[red]The directory '{directory}' does not exist.[/]");
|
||||
Environment.ExitCode = 1;
|
||||
return;
|
||||
}
|
||||
|
||||
FileInfo[] files;
|
||||
var finder = new ScriptFinder(extensions, directory, depth);
|
||||
|
||||
if (group)
|
||||
{
|
||||
var dict = finder.GetScriptsByDirectory();
|
||||
|
||||
if (dict.Count == 0)
|
||||
if (!Directory.Exists(settings.Directory))
|
||||
{
|
||||
AnsiConsole.Markup($"[red]No scripts script files found in '{finder.RootDirectory}' with extensions '{string.Join(", ", finder.Extensions)}'[/]");
|
||||
Environment.ExitCode = 1;
|
||||
return;
|
||||
AnsiConsole.Markup($"[red]The directory '{settings.Directory}' does not exist.[/]");
|
||||
// Environment.ExitCode = 1;
|
||||
return Failure;
|
||||
}
|
||||
|
||||
var dirPrompt = PromptConstructor.GetDirectoryPrompt(dict.Keys.ToArray());
|
||||
var directoryInfo = AnsiConsole.Prompt(dirPrompt);
|
||||
files = dict[directoryInfo];
|
||||
}
|
||||
else
|
||||
{
|
||||
files = finder.GetScripts();
|
||||
}
|
||||
FileInfo[] files;
|
||||
var finder = new ScriptFinder(settings.Extensions, settings.Directory, settings.Depth);
|
||||
|
||||
if (files.Length == 0)
|
||||
{
|
||||
AnsiConsole.Markup($"[red]No scripts script files found in '{finder.RootDirectory}' with extensions '{string.Join(", ", finder.Extensions)}'[/]");
|
||||
Environment.ExitCode = 1;
|
||||
return;
|
||||
if (settings.Group)
|
||||
{
|
||||
var dict = finder.GetScriptsByDirectory();
|
||||
|
||||
if (dict.Count == 0)
|
||||
{
|
||||
AnsiConsole.Markup(
|
||||
$"[red]No scripts script files found in '{finder.RootDirectory}' with extensions '{string.Join(", ", finder.Extensions)}'[/]"
|
||||
);
|
||||
return Failure;
|
||||
}
|
||||
|
||||
var dirPrompt = PromptConstructor.GetDirectoryPrompt(dict.Keys.ToArray());
|
||||
var directoryInfo = AnsiConsole.Prompt(dirPrompt);
|
||||
files = dict[directoryInfo];
|
||||
}
|
||||
else
|
||||
{
|
||||
files = finder.GetScripts();
|
||||
}
|
||||
|
||||
if (files.Length == 0)
|
||||
{
|
||||
AnsiConsole.Markup(
|
||||
$"[red]No scripts script files found in '{finder.RootDirectory}' with extensions '{string.Join(", ", finder.Extensions)}'[/]"
|
||||
);
|
||||
return Failure;
|
||||
}
|
||||
|
||||
var prompt = PromptConstructor.GetScriptPrompt(files, settings.Brief);
|
||||
var scripts = AnsiConsole.Prompt(prompt);
|
||||
|
||||
await ScriptExecutor.ExecAsync(scripts, settings.Elevated);
|
||||
|
||||
return Success;
|
||||
}
|
||||
|
||||
var prompt = PromptConstructor.GetScriptPrompt(files, brief);
|
||||
var scripts = AnsiConsole.Prompt(prompt);
|
||||
|
||||
await ScriptExecutor.ExecAsync(scripts, elevated);
|
||||
}
|
||||
|
||||
internal class RootCommandSettings : CommandSettings
|
||||
{
|
||||
[Description("Comma separated list of script extensions")]
|
||||
[CommandOption("-x|--extensions")]
|
||||
public string? Extensions { get; init; }
|
||||
|
||||
[Description("Search depth")]
|
||||
[CommandOption("-d|--depth")]
|
||||
public int Depth { get; init; } = 1;
|
||||
|
||||
[Description("Run with elevated privileges")]
|
||||
[CommandOption("-e|--elevated")]
|
||||
public bool Elevated { get; init; } = false;
|
||||
|
||||
[Description("Group scripts by folder")]
|
||||
[CommandOption("-g|--group")]
|
||||
public bool Group { get; init; } = false;
|
||||
|
||||
[Description("Show brief information")]
|
||||
[CommandOption("-b|--brief")]
|
||||
public bool Brief { get; init; } = false;
|
||||
|
||||
[Description("Starting directory")]
|
||||
[CommandArgument(0, "[path]")]
|
||||
public string Directory { get; init; } = ".";
|
||||
}
|
||||
|
|
|
@ -5,14 +5,14 @@
|
|||
<TargetFramework>net6.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<Version>0.1.0</Version>
|
||||
<Version>0.1.1</Version>
|
||||
<PackAsTool>true</PackAsTool>
|
||||
<ToolCommandName>script-launcher</ToolCommandName>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Cocona.Lite" Version="2.0.3" />
|
||||
<PackageReference Include="Spectre.Console" Version="0.44.0" />
|
||||
<PackageReference Include="Spectre.Console" Version="0.45.0" />
|
||||
<PackageReference Include="Spectre.Console.Cli" Version="0.45.0" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
|
Loading…
Add table
Reference in a new issue