chore: enable .NET analyzers

This commit is contained in:
Marcello 2023-07-20 14:29:47 +02:00
parent c62e2684fc
commit 17e99dd0c2
3 changed files with 84 additions and 74 deletions

View file

@ -6,46 +6,29 @@ using Spectre.Console.Cli;
namespace ScriptLauncher; namespace ScriptLauncher;
internal sealed class RootCommand : AsyncCommand<RootCommandSettings> internal sealed class RootCommand : AsyncCommand<RootCommandSettings>
{
private const int Failure = 1;
private const int Success = 0;
public override async Task<int> ExecuteAsync(
CommandContext context,
RootCommandSettings settings
)
{ {
private const int Failure = 1; if (!Directory.Exists(settings.Directory))
private const int Success = 0;
public override async Task<int> ExecuteAsync(
CommandContext context,
RootCommandSettings settings
)
{ {
if (!Directory.Exists(settings.Directory)) AnsiConsole.Markup($"[red]The directory '{settings.Directory}' does not exist.[/]");
{ return Failure;
AnsiConsole.Markup($"[red]The directory '{settings.Directory}' does not exist.[/]"); }
return Failure;
}
FileInfo[] files; FileInfo[] files;
var finder = new ScriptFinder(settings.Extensions, settings.Directory, settings.Depth); var finder = new ScriptFinder(settings.Extensions, settings.Directory, settings.Depth);
if (settings.Group) if (settings.Group)
{ {
var dict = finder.GetScriptsByDirectory(); var dict = finder.GetScriptsByDirectory();
if (dict.Count == 0) 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( AnsiConsole.Markup(
$"[red]No scripts script files found in '{finder.RootDirectory}' with extensions '{string.Join(", ", finder.Extensions)}'[/]" $"[red]No scripts script files found in '{finder.RootDirectory}' with extensions '{string.Join(", ", finder.Extensions)}'[/]"
@ -53,46 +36,65 @@ internal sealed class RootCommand : AsyncCommand<RootCommandSettings>
return Failure; return Failure;
} }
var prompt = PromptConstructor.GetScriptPrompt(files, settings.Brief); var dirPrompt = PromptConstructor.GetDirectoryPrompt(dict.Keys.ToArray());
var scripts = AnsiConsole.Prompt(prompt); var directoryInfo = AnsiConsole.Prompt(dirPrompt);
files = dict[directoryInfo];
try
{
await ScriptExecutor.ExecAsync(scripts, settings.Elevated);
}
catch (Exception ex) when (ex is Win32Exception or InvalidOperationException or PlatformNotSupportedException)
{
AnsiConsole.Markup($"[red]{ex.Message}[/]");
return Failure;
}
return Success;
} }
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);
try
{
await ScriptExecutor.ExecAsync(scripts, settings.Elevated).ConfigureAwait(false);
}
catch (Exception ex)
when (ex is Win32Exception or InvalidOperationException or PlatformNotSupportedException
)
{
AnsiConsole.Markup($"[red]{ex.Message}[/]");
return Failure;
}
return Success;
} }
}
internal class RootCommandSettings : CommandSettings internal class RootCommandSettings : CommandSettings
{ {
[Description("Comma separated list of script extensions")] [Description("Comma separated list of script extensions")]
[CommandOption("-x|--extensions")] [CommandOption("-x|--extensions")]
public string? Extensions { get; init; } public string? Extensions { get; init; }
[Description("Search depth")] [Description("Search depth")]
[CommandOption("-d|--depth")] [CommandOption("-d|--depth")]
public int Depth { get; init; } = 1; public int Depth { get; init; } = 1;
[Description("Run with elevated privileges")] [Description("Run with elevated privileges")]
[CommandOption("-e|--elevated")] [CommandOption("-e|--elevated")]
public bool Elevated { get; init; } = false; public bool Elevated { get; init; } = false;
[Description("Group scripts by folder")] [Description("Group scripts by folder")]
[CommandOption("-g|--group")] [CommandOption("-g|--group")]
public bool Group { get; init; } = false; public bool Group { get; init; } = false;
[Description("Show brief information")] [Description("Show brief information")]
[CommandOption("-b|--brief")] [CommandOption("-b|--brief")]
public bool Brief { get; init; } = false; public bool Brief { get; init; } = false;
[Description("Starting directory (Default: .)")] [Description("Starting directory (Default: .)")]
[CommandArgument(0, "<path>")] [CommandArgument(0, "<path>")]
public string Directory { get; init; } = "."; public string Directory { get; init; } = ".";
} }

View file

@ -1,3 +1,4 @@
using System.Globalization;
using System.Text; using System.Text;
using Spectre.Console; using Spectre.Console;
@ -16,12 +17,18 @@ internal static class PromptConstructor
if (!brief) if (!brief)
{ {
builder.Append($"[blue]{info.DirectoryName}{Path.DirectorySeparatorChar}[/]"); builder.Append(
CultureInfo.InvariantCulture,
$"[blue]{info.DirectoryName}{Path.DirectorySeparatorChar}[/]"
);
} }
builder builder
.Append($"[orangered1]{Path.GetFileNameWithoutExtension(info.Name)}[/]") .Append(
.Append($"[greenyellow]{info.Extension}[/]"); CultureInfo.InvariantCulture,
$"[orangered1]{Path.GetFileNameWithoutExtension(info.Name)}[/]"
)
.Append(CultureInfo.InvariantCulture, $"[greenyellow]{info.Extension}[/]");
return builder.ToString(); return builder.ToString();
} }

View file

@ -5,7 +5,8 @@
<TargetFramework>net6.0</TargetFramework> <TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<Version>0.1.3</Version> <AnalysisMode>All</AnalysisMode>
<Version>0.1.4</Version>
<PackAsTool>true</PackAsTool> <PackAsTool>true</PackAsTool>
<Description>Tool to find and exec shell scripts</Description> <Description>Tool to find and exec shell scripts</Description>
<PackageReadmeFile>README.md</PackageReadmeFile> <PackageReadmeFile>README.md</PackageReadmeFile>