mirror of
https://github.com/m-lamonaca/script-launcher.git
synced 2025-04-18 22:36:26 +00:00
bump dotnet version to net8.0
- bump dotnet verison to .NET 8 - bump tool version to 0.1.5 - fix warnings
This commit is contained in:
parent
61809ded27
commit
aebe1f7aaa
5 changed files with 60 additions and 39 deletions
|
@ -5,7 +5,7 @@ using Spectre.Console.Cli;
|
||||||
|
|
||||||
namespace ScriptLauncher;
|
namespace ScriptLauncher;
|
||||||
|
|
||||||
internal sealed class RootCommand : AsyncCommand<RootCommandSettings>
|
public sealed class RootCommand : AsyncCommand<RootCommandSettings>
|
||||||
{
|
{
|
||||||
private const int Failure = 1;
|
private const int Failure = 1;
|
||||||
private const int Success = 0;
|
private const int Success = 0;
|
||||||
|
@ -15,6 +15,8 @@ internal sealed class RootCommand : AsyncCommand<RootCommandSettings>
|
||||||
RootCommandSettings settings
|
RootCommandSettings settings
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
|
ArgumentNullException.ThrowIfNull(settings);
|
||||||
|
|
||||||
if (!Directory.Exists(settings.Directory))
|
if (!Directory.Exists(settings.Directory))
|
||||||
{
|
{
|
||||||
AnsiConsole.Markup($"[red]The directory '{settings.Directory}' does not exist.[/]");
|
AnsiConsole.Markup($"[red]The directory '{settings.Directory}' does not exist.[/]");
|
||||||
|
@ -72,7 +74,7 @@ internal sealed class RootCommand : AsyncCommand<RootCommandSettings>
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
internal class RootCommandSettings : CommandSettings
|
public sealed class RootCommandSettings : CommandSettings
|
||||||
{
|
{
|
||||||
[Description("Comma separated list of script extensions")]
|
[Description("Comma separated list of script extensions")]
|
||||||
[CommandOption("-x|--extensions")]
|
[CommandOption("-x|--extensions")]
|
||||||
|
@ -97,4 +99,4 @@ internal class RootCommandSettings : CommandSettings
|
||||||
[Description("Starting directory (Default: .)")]
|
[Description("Starting directory (Default: .)")]
|
||||||
[CommandArgument(0, "<path>")]
|
[CommandArgument(0, "<path>")]
|
||||||
public string Directory { get; init; } = ".";
|
public string Directory { get; init; } = ".";
|
||||||
}
|
}
|
|
@ -1,6 +1,7 @@
|
||||||
|
using Spectre.Console;
|
||||||
|
|
||||||
using System.Globalization;
|
using System.Globalization;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using Spectre.Console;
|
|
||||||
|
|
||||||
namespace ScriptLauncher;
|
namespace ScriptLauncher;
|
||||||
|
|
||||||
|
|
|
@ -4,43 +4,58 @@ namespace ScriptLauncher;
|
||||||
|
|
||||||
internal static class ScriptExecutor
|
internal static class ScriptExecutor
|
||||||
{
|
{
|
||||||
public static async Task ExecAsync(List<FileInfo> files, bool elevated) =>
|
public static async Task ExecAsync(List<FileInfo> files, bool elevated) =>
|
||||||
await Parallel.ForEachAsync(files, (x, ct) => ExecAsync(x, elevated, ct));
|
await Parallel
|
||||||
|
.ForEachAsync(files, (file, ct) => ExecAsync(file, elevated, ct))
|
||||||
|
.ConfigureAwait(ConfigureAwaitOptions.None);
|
||||||
|
|
||||||
private static async ValueTask ExecAsync(FileInfo file, bool elevated, CancellationToken cancellationToken = default)
|
private static async ValueTask ExecAsync(
|
||||||
|
FileInfo file,
|
||||||
|
bool elevated,
|
||||||
|
CancellationToken cancellationToken = default
|
||||||
|
)
|
||||||
{
|
{
|
||||||
var process = GetExecutableProcessInfo(file, elevated);
|
var process = GetExecutableProcessInfo(file, elevated);
|
||||||
if (process is null)
|
if (process is null)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
await (Process.Start(process)?.WaitForExitAsync(cancellationToken) ?? Task.CompletedTask);
|
await (
|
||||||
|
Process.Start(process)?.WaitForExitAsync(cancellationToken) ?? Task.CompletedTask
|
||||||
|
).ConfigureAwait(ConfigureAwaitOptions.None);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static ProcessStartInfo? GetExecutableProcessInfo(FileInfo file, bool elevated) => file.Extension switch
|
private static ProcessStartInfo? GetExecutableProcessInfo(FileInfo file, bool elevated) =>
|
||||||
{
|
file.Extension switch
|
||||||
".bat" or ".cmd" => new ProcessStartInfo
|
|
||||||
{
|
{
|
||||||
FileName = "cmd",
|
".bat"
|
||||||
Arguments = $"/Q /C .\\{file.Name}",
|
or ".cmd"
|
||||||
Verb = elevated ? "runas /user:Administrator" : string.Empty,
|
=> new ProcessStartInfo
|
||||||
WorkingDirectory = file.DirectoryName
|
{
|
||||||
},
|
FileName = "cmd",
|
||||||
".ps1" => new ProcessStartInfo
|
Arguments = $"/Q /C .\\{file.Name}",
|
||||||
{
|
Verb = elevated ? "runas /user:Administrator" : string.Empty,
|
||||||
FileName = "powershell.exe",
|
WorkingDirectory = file.DirectoryName
|
||||||
Arguments = $"-NoProfile -ExecutionPolicy Bypass -File .\\{file.Name}",
|
},
|
||||||
Verb = elevated ? "runas /user:Administrator" : string.Empty,
|
".ps1"
|
||||||
WorkingDirectory = file.DirectoryName
|
=> new ProcessStartInfo
|
||||||
},
|
{
|
||||||
".sh" or ".zsh" or ".fish" => new ProcessStartInfo
|
FileName = "powershell.exe",
|
||||||
{
|
Arguments = $"-NoProfile -ExecutionPolicy Bypass -File .\\{file.Name}",
|
||||||
FileName = "sh",
|
Verb = elevated ? "runas /user:Administrator" : string.Empty,
|
||||||
Arguments = $"-c ./{file.Name}",
|
WorkingDirectory = file.DirectoryName
|
||||||
Verb = elevated ? "sudo" : string.Empty,
|
},
|
||||||
WorkingDirectory = file.DirectoryName
|
".sh"
|
||||||
},
|
or ".zsh"
|
||||||
var _ => null
|
or ".fish"
|
||||||
};
|
=> new ProcessStartInfo
|
||||||
}
|
{
|
||||||
|
FileName = "sh",
|
||||||
|
Arguments = $"-c ./{file.Name}",
|
||||||
|
Verb = elevated ? "sudo" : string.Empty,
|
||||||
|
WorkingDirectory = file.DirectoryName
|
||||||
|
},
|
||||||
|
_ => null
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
|
@ -2,17 +2,20 @@ namespace ScriptLauncher;
|
||||||
|
|
||||||
internal readonly struct ScriptFinder
|
internal readonly struct ScriptFinder
|
||||||
{
|
{
|
||||||
static readonly string[] DefaultExtensions = new[] { ".ps1", ".*sh", ".bat", ".cmd" };
|
private static readonly string[] DefaultExtensions = new[] { ".ps1", ".*sh", ".bat", ".cmd" };
|
||||||
|
private static readonly char[] DefaultSeparators = new[] { ',', ' ' };
|
||||||
|
|
||||||
public string[] Extensions { get; }
|
public string[] Extensions { get; }
|
||||||
public string RootDirectory { get; }
|
public string RootDirectory { get; }
|
||||||
public int Depth { get; }
|
public int Depth { get; }
|
||||||
|
|
||||||
private readonly EnumerationOptions _options;
|
private readonly EnumerationOptions _options;
|
||||||
|
|
||||||
public ScriptFinder(string? extensions, string directory, int depth)
|
public ScriptFinder(string? extensions, string directory, int depth)
|
||||||
{
|
{
|
||||||
Extensions =
|
Extensions =
|
||||||
extensions
|
extensions
|
||||||
?.Split(new[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries)
|
?.Split(DefaultSeparators, StringSplitOptions.RemoveEmptyEntries)
|
||||||
.ToHashSet()
|
.ToHashSet()
|
||||||
.Select(x => $".{x.TrimStart('.')}")
|
.Select(x => $".{x.TrimStart('.')}")
|
||||||
.ToArray() ?? DefaultExtensions;
|
.ToArray() ?? DefaultExtensions;
|
||||||
|
|
|
@ -1,12 +1,12 @@
|
||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<OutputType>Exe</OutputType>
|
<OutputType>Exe</OutputType>
|
||||||
<TargetFramework>net6.0</TargetFramework>
|
<TargetFrameworks>net8.0</TargetFrameworks>
|
||||||
<ImplicitUsings>enable</ImplicitUsings>
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
<AnalysisMode>All</AnalysisMode>
|
<AnalysisMode>All</AnalysisMode>
|
||||||
<Version>0.1.4</Version>
|
<Version>0.1.5</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>
|
||||||
|
|
Loading…
Add table
Reference in a new issue