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;
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;
private const int Success = 0;
public override async Task<int> ExecuteAsync(
CommandContext context,
RootCommandSettings settings
)
if (!Directory.Exists(settings.Directory))
{
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;
var finder = new ScriptFinder(settings.Extensions, settings.Directory, settings.Depth);
FileInfo[] files;
var finder = new ScriptFinder(settings.Extensions, settings.Directory, settings.Depth);
if (settings.Group)
{
var dict = finder.GetScriptsByDirectory();
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)
if (dict.Count == 0)
{
AnsiConsole.Markup(
$"[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;
}
var prompt = PromptConstructor.GetScriptPrompt(files, settings.Brief);
var scripts = AnsiConsole.Prompt(prompt);
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;
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);
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
{
[Description("Comma separated list of script extensions")]
[CommandOption("-x|--extensions")]
public string? Extensions { get; init; }
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("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("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("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("Show brief information")]
[CommandOption("-b|--brief")]
public bool Brief { get; init; } = false;
[Description("Starting directory (Default: .)")]
[CommandArgument(0, "<path>")]
public string Directory { get; init; } = ".";
}
[Description("Starting directory (Default: .)")]
[CommandArgument(0, "<path>")]
public string Directory { get; init; } = ".";
}

View file

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

View file

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