mirror of
https://github.com/m-lamonaca/script-launcher.git
synced 2025-05-15 09:44:46 +00:00
initial commit
This commit is contained in:
commit
ffff67de30
5 changed files with 590 additions and 0 deletions
100
src/Program.cs
Normal file
100
src/Program.cs
Normal file
|
@ -0,0 +1,100 @@
|
|||
using System.Diagnostics;
|
||||
using Cocona;
|
||||
using Spectre.Console;
|
||||
|
||||
var app = CoconaLiteApp.Create();
|
||||
|
||||
app.AddCommand(RootCommand);
|
||||
app.Run();
|
||||
|
||||
static void RootCommand(
|
||||
[Option("dir", new[] { 'd' }, Description = "Directory from which search the scripts")] string directory = ".",
|
||||
[Option("ext", Description = "Comma separated list of script extensions to search")] string extensions = "*",
|
||||
[Option("elevated", new[] { 'e' }, Description = "Run the script with elevated privileges")] bool elevated = false,
|
||||
[Option("depth")] int depth = 0)
|
||||
{
|
||||
if (!Directory.Exists(directory))
|
||||
{
|
||||
AnsiConsole.Markup($"[red]The directory {directory} does not exist.[/]");
|
||||
Environment.Exit(1);
|
||||
}
|
||||
|
||||
var fileExtensions = extensions.Split(new[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries)
|
||||
.ToHashSet()
|
||||
.ToArray();
|
||||
|
||||
var finder = new ScriptFinder()
|
||||
{
|
||||
Depth = depth,
|
||||
Extensions = fileExtensions,
|
||||
RootFolder = directory,
|
||||
};
|
||||
|
||||
var files = finder.GetScriptFiles(fileExtensions);
|
||||
|
||||
if (files.Length == 0)
|
||||
{
|
||||
AnsiConsole.Markup($"[red]No scripts script files found in {directory} with extensions '{string.Join("|", fileExtensions)}'[/]");
|
||||
Environment.Exit(1);
|
||||
}
|
||||
|
||||
var prompt = new SelectionPrompt<string>()
|
||||
.Title("Select a script")
|
||||
.AddChoices(files.Select(x => x.FullName));
|
||||
|
||||
var script = AnsiConsole.Prompt(prompt);
|
||||
|
||||
ScriptExecutor.Exec(script);
|
||||
};
|
||||
|
||||
static class ScriptExecutor
|
||||
{
|
||||
internal static void Exec(string filename)
|
||||
{
|
||||
var info = new ProcessStartInfo
|
||||
{
|
||||
FileName = "powershell.exe",
|
||||
Arguments = $"-File {filename}",
|
||||
UseShellExecute = false,
|
||||
};
|
||||
|
||||
Exec(info);
|
||||
}
|
||||
|
||||
internal static void Exec(ProcessStartInfo info) => Process.Start(info)?.WaitForExit();
|
||||
}
|
||||
|
||||
class ScriptFinder
|
||||
{
|
||||
public string RootFolder { get; set; } = ".";
|
||||
public string[] Extensions { get; set; } = new[] { "ps1", "*sh", "bat", "cmd" };
|
||||
public int Depth { get; set; } = 0;
|
||||
|
||||
private readonly EnumerationOptions _options;
|
||||
|
||||
public ScriptFinder()
|
||||
{
|
||||
_options = new EnumerationOptions
|
||||
{
|
||||
IgnoreInaccessible = true,
|
||||
RecurseSubdirectories = Depth > 0,
|
||||
MaxRecursionDepth = Depth,
|
||||
};
|
||||
}
|
||||
|
||||
internal FileInfo[] GetScriptFiles(string extension)
|
||||
{
|
||||
try
|
||||
{
|
||||
var filenames = Directory.GetFiles(RootFolder, $"*.{extension}", _options);
|
||||
return filenames.Select(x => new FileInfo(x)).ToArray();
|
||||
}
|
||||
catch (UnauthorizedAccessException)
|
||||
{
|
||||
return Array.Empty<FileInfo>();
|
||||
}
|
||||
}
|
||||
|
||||
internal FileInfo[] GetScriptFiles(string[] extensions) =>
|
||||
extensions.Select(GetScriptFiles).SelectMany(x => x).ToArray();
|
||||
}
|
16
src/ScriptLauncher.csproj
Normal file
16
src/ScriptLauncher.csproj
Normal file
|
@ -0,0 +1,16 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<Version>0.1.0</Version>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Cocona.Lite" Version="2.0.3" />
|
||||
<PackageReference Include="Spectre.Console" Version="0.43.0" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
Loading…
Add table
Add a link
Reference in a new issue