mirror of
https://github.com/m-lamonaca/script-launcher.git
synced 2025-04-08 11:06: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
b369926aa1
commit
1c3d96c574
5 changed files with 60 additions and 39 deletions
|
@ -5,7 +5,7 @@ using Spectre.Console.Cli;
|
|||
|
||||
namespace ScriptLauncher;
|
||||
|
||||
internal sealed class RootCommand : AsyncCommand<RootCommandSettings>
|
||||
public sealed class RootCommand : AsyncCommand<RootCommandSettings>
|
||||
{
|
||||
private const int Failure = 1;
|
||||
private const int Success = 0;
|
||||
|
@ -15,6 +15,8 @@ internal sealed class RootCommand : AsyncCommand<RootCommandSettings>
|
|||
RootCommandSettings settings
|
||||
)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(settings);
|
||||
|
||||
if (!Directory.Exists(settings.Directory))
|
||||
{
|
||||
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")]
|
||||
[CommandOption("-x|--extensions")]
|
||||
|
@ -97,4 +99,4 @@ internal class RootCommandSettings : CommandSettings
|
|||
[Description("Starting directory (Default: .)")]
|
||||
[CommandArgument(0, "<path>")]
|
||||
public string Directory { get; init; } = ".";
|
||||
}
|
||||
}
|
|
@ -1,6 +1,7 @@
|
|||
using Spectre.Console;
|
||||
|
||||
using System.Globalization;
|
||||
using System.Text;
|
||||
using Spectre.Console;
|
||||
|
||||
namespace ScriptLauncher;
|
||||
|
||||
|
|
|
@ -4,43 +4,58 @@ namespace ScriptLauncher;
|
|||
|
||||
internal static class ScriptExecutor
|
||||
{
|
||||
public static async Task ExecAsync(List<FileInfo> files, bool elevated) =>
|
||||
await Parallel.ForEachAsync(files, (x, ct) => ExecAsync(x, elevated, ct));
|
||||
public static async Task ExecAsync(List<FileInfo> files, bool elevated) =>
|
||||
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);
|
||||
if (process is null)
|
||||
{
|
||||
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
|
||||
{
|
||||
".bat" or ".cmd" => new ProcessStartInfo
|
||||
private static ProcessStartInfo? GetExecutableProcessInfo(FileInfo file, bool elevated) =>
|
||||
file.Extension switch
|
||||
{
|
||||
FileName = "cmd",
|
||||
Arguments = $"/Q /C .\\{file.Name}",
|
||||
Verb = elevated ? "runas /user:Administrator" : string.Empty,
|
||||
WorkingDirectory = file.DirectoryName
|
||||
},
|
||||
".ps1" => new ProcessStartInfo
|
||||
{
|
||||
FileName = "powershell.exe",
|
||||
Arguments = $"-NoProfile -ExecutionPolicy Bypass -File .\\{file.Name}",
|
||||
Verb = elevated ? "runas /user:Administrator" : string.Empty,
|
||||
WorkingDirectory = file.DirectoryName
|
||||
},
|
||||
".sh" or ".zsh" or ".fish" => new ProcessStartInfo
|
||||
{
|
||||
FileName = "sh",
|
||||
Arguments = $"-c ./{file.Name}",
|
||||
Verb = elevated ? "sudo" : string.Empty,
|
||||
WorkingDirectory = file.DirectoryName
|
||||
},
|
||||
var _ => null
|
||||
};
|
||||
}
|
||||
".bat"
|
||||
or ".cmd"
|
||||
=> new ProcessStartInfo
|
||||
{
|
||||
FileName = "cmd",
|
||||
Arguments = $"/Q /C .\\{file.Name}",
|
||||
Verb = elevated ? "runas /user:Administrator" : string.Empty,
|
||||
WorkingDirectory = file.DirectoryName
|
||||
},
|
||||
".ps1"
|
||||
=> new ProcessStartInfo
|
||||
{
|
||||
FileName = "powershell.exe",
|
||||
Arguments = $"-NoProfile -ExecutionPolicy Bypass -File .\\{file.Name}",
|
||||
Verb = elevated ? "runas /user:Administrator" : string.Empty,
|
||||
WorkingDirectory = file.DirectoryName
|
||||
},
|
||||
".sh"
|
||||
or ".zsh"
|
||||
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
|
||||
{
|
||||
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 RootDirectory { get; }
|
||||
public int Depth { get; }
|
||||
|
||||
private readonly EnumerationOptions _options;
|
||||
|
||||
public ScriptFinder(string? extensions, string directory, int depth)
|
||||
{
|
||||
Extensions =
|
||||
extensions
|
||||
?.Split(new[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries)
|
||||
?.Split(DefaultSeparators, StringSplitOptions.RemoveEmptyEntries)
|
||||
.ToHashSet()
|
||||
.Select(x => $".{x.TrimStart('.')}")
|
||||
.ToArray() ?? DefaultExtensions;
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<TargetFrameworks>net8.0</TargetFrameworks>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<AnalysisMode>All</AnalysisMode>
|
||||
<Version>0.1.4</Version>
|
||||
<Version>0.1.5</Version>
|
||||
<PackAsTool>true</PackAsTool>
|
||||
<Description>Tool to find and exec shell scripts</Description>
|
||||
<PackageReadmeFile>README.md</PackageReadmeFile>
|
||||
|
|
Loading…
Add table
Reference in a new issue