diff --git a/readme.md b/readme.md index 2133b6a..0a9e58b 100644 --- a/readme.md +++ b/readme.md @@ -12,7 +12,7 @@ Run C# code programs from git repos on GitHub, GitLab and Azure DevOps. ``` Usage: - [dnx] runcs [...] + [dnx] runcs [--aot] [--alias ALIAS] [...] Arguments: Reference to remote file to run, with format [host/]owner/repo[@ref][:path] @@ -25,7 +25,13 @@ Arguments: * gitlab.com/kzu/sandbox@main:run.cs (all explicit parts) * kzu/sandbox (implied host github.com, ref and path defaults) + Can be an alias previously set with --alias. + Arguments passed to the C# program that is being run. + +Options: + --aot (optional) Enable dotnet AOT defaults for run file.cs. Defaults to false. + --alias ALIAS (optional) Assign an alias on first usage which can be used instead of the full ref. ``` Example: @@ -50,16 +56,24 @@ The last download etag is used to avoid downloading on each run. Run C# code programs from GitHub gists. ``` -Usage: [dnx] gist [...] +Usage: [dnx] gist [--aot] [--alias ALIAS] [...] + +Arguments: Reference to gist file to run, with format owner/gist[@commit][:path] - @commit optional gist commit (default: default branch) + @commit optional gist commit (default: latest) :path optional path to file in gist (default: program.cs or first .cs file) Examples: * kzu/0ac826dc7de666546aaedd38e5965381 (tip commit and program.cs or first .cs file) * kzu/0ac826dc7de666546aaedd38e5965381@d8079cf:run.cs (explicit commit and file path) - Arguments passed to the C# program gist that is being run. + Can be an alias previously set with --alias. + + Arguments passed to the C# program that is being run. + +Options: + --aot (optional) Enable dotnet AOT defaults for run file.cs. Defaults to false. + --alias ALIAS (optional) Assign an alias on first usage which can be used instead of the full ref. ``` > [!TIP] diff --git a/src/Core/RemoteRunner.cs b/src/Core/RemoteRunner.cs index 4abf9f6..543920d 100644 --- a/src/Core/RemoteRunner.cs +++ b/src/Core/RemoteRunner.cs @@ -5,11 +5,12 @@ namespace Devlooped; -public class RemoteRunner(RemoteRef location, string toolName) +public class RemoteRunner(RemoteRef location, string toolName, Config? config = null) { + Config config = config ?? Config.Build(Config.GlobalLocation); + public async Task RunAsync(string[] args, bool aot) { - var config = Config.Build(Config.GlobalLocation); var etag = config.GetString(toolName, location.ToString(), "etag"); if (etag != null && Directory.Exists(location.TempPath)) { @@ -18,6 +19,7 @@ public async Task RunAsync(string[] args, bool aot) location = location with { ETag = etag }; } + if (config.TryGetString(toolName, location.ToString(), "uri", out var url) && Uri.TryCreate(url, UriKind.Absolute, out var uri)) location = location with { ResolvedUri = uri }; diff --git a/src/gist/Program.cs b/src/gist/Program.cs index 35ba778..47821eb 100644 --- a/src/gist/Program.cs +++ b/src/gist/Program.cs @@ -1,6 +1,8 @@ -using System.Runtime.InteropServices; +using System.CommandLine; +using System.Runtime.InteropServices; using System.Text; using Devlooped; +using DotNetConfig; using GitCredentialManager.UI; using Spectre.Console; @@ -15,12 +17,23 @@ args = [.. args.Where(x => x != "--aot")]; } +var config = Config.Build(Config.GlobalLocation); +if (args.Length > 0 && config.GetString("runcs", args[0]) is string aliased) + args = [aliased, .. args[1..]]; + +// Set alias and remove from args if present +var option = new Option("--alias"); +var parsed = new RootCommand() { Options = { option } }.Parse(args); +var alias = parsed.GetValue(option); +if (alias != null) + args = [.. parsed.UnmatchedTokens]; + if (args.Length == 0 || !RemoteRef.TryParse("gist.github.com/" + args[0], out var location)) { AnsiConsole.MarkupLine( $""" Usage: - [grey][[dnx]][/] [lime]{ThisAssembly.Project.ToolCommandName}[/] [grey][[--aot]][/] [bold][/] [grey italic][[...]][/] + [grey][[dnx]][/] [lime]{ThisAssembly.Project.ToolCommandName}[/] [grey][[--aot]][/] [grey][[--alias ALIAS]][/] [bold][/] [grey italic][[...]][/] Arguments: [bold][/] Reference to gist file to run, with format [yellow]owner/gist[[@commit]][[:path]][/] @@ -30,15 +43,21 @@ Examples: * kzu/0ac826dc7de666546aaedd38e5965381 (tip commit and program.cs or first .cs file) * kzu/0ac826dc7de666546aaedd38e5965381@d8079cf:run.cs (explicit commit and file path) - + + Can be an alias previously set with --alias. + [bold][/] Arguments passed to the C# program that is being run. Options: - [bold]--aot[/] (optional) Enable dotnet AOT defaults for run file.cs. Defaults to false. + [bold]--aot[/] (optional) Enable dotnet AOT defaults for run file.cs. Defaults to false. + [bold]--alias[/] ALIAS (optional) Assign an alias on first usage which can be used instead of the full ref. """); return; } +if (alias != null) + config = config.SetString("runcs", alias, location.ToString()); + // Create the dispatcher on the main thread. This is required // for some platform UI services such as macOS that mandates // all controls are created/accessed on the initial thread @@ -48,7 +67,7 @@ // Run AppMain in a new thread and keep the main thread free // to process the dispatcher's job queue. var main = Task - .Run(() => new RemoteRunner(location, ThisAssembly.Project.ToolCommandName) + .Run(() => new RemoteRunner(location, ThisAssembly.Project.ToolCommandName, config) .RunAsync(args[1..], aot)) .ContinueWith(t => { diff --git a/src/gist/Properties/launchSettings.json b/src/gist/Properties/launchSettings.json index 76a0d30..f5a605f 100644 --- a/src/gist/Properties/launchSettings.json +++ b/src/gist/Properties/launchSettings.json @@ -1,5 +1,8 @@ { "profiles": { + "help": { + "commandName": "Project", + }, "gist": { "commandName": "Project", "commandLineArgs": "kzu/0ac826dc7de666546aaedd38e5965381" diff --git a/src/gist/gist.csproj b/src/gist/gist.csproj index 83b6d04..605a18f 100644 --- a/src/gist/gist.csproj +++ b/src/gist/gist.csproj @@ -12,7 +12,7 @@ readme.md dotnet dotnet-tool - Run gists directly using: dnx gist owner/gist[:path] + Run C# code gists directly using: dnx gist owner/gist[:path] @@ -21,6 +21,7 @@ + diff --git a/src/runcs/Program.cs b/src/runcs/Program.cs index 34ac921..7c3d860 100644 --- a/src/runcs/Program.cs +++ b/src/runcs/Program.cs @@ -1,6 +1,8 @@ -using System.Runtime.InteropServices; +using System.CommandLine; +using System.Runtime.InteropServices; using System.Text; using Devlooped; +using DotNetConfig; using GitCredentialManager.UI; using Spectre.Console; @@ -15,12 +17,23 @@ args = [.. args.Where(x => x != "--aot")]; } +var config = Config.Build(Config.GlobalLocation); +if (args.Length > 0 && config.GetString("runcs", args[0]) is string aliased) + args = [aliased, .. args[1..]]; + +// Set alias and remove from args if present +var option = new Option("--alias"); +var parsed = new RootCommand() { Options = { option } }.Parse(args); +var alias = parsed.GetValue(option); +if (alias != null) + args = [.. parsed.UnmatchedTokens]; + if (args.Length == 0 || !RemoteRef.TryParse(args[0], out var location)) { AnsiConsole.MarkupLine( $""" Usage: - [grey][[dnx]][/] [lime]{ThisAssembly.Project.ToolCommandName}[/] [grey][[--aot]][/] [bold][/] [grey italic][[...]][/] + [grey][[dnx]][/] [lime]{ThisAssembly.Project.ToolCommandName}[/] [grey][[--aot]][/] [grey][[--alias ALIAS]][/] [bold][/] [grey italic][[...]][/] Arguments: [bold][/] Reference to remote file to run, with format [yellow][[host/]]owner/repo[[@ref]][[:path]][/] @@ -33,14 +46,20 @@ * gitlab.com/kzu/sandbox@main:run.cs (all explicit parts) * kzu/sandbox (implied host github.com, ref and path defaults) + Can be an alias previously set with --alias. + [bold][/] Arguments passed to the C# program that is being run. Options: - [bold]--aot[/] (optional) Enable dotnet AOT defaults for run file.cs. Defaults to false. + [bold]--aot[/] (optional) Enable dotnet AOT defaults for run file.cs. Defaults to false. + [bold]--alias[/] ALIAS (optional) Assign an alias on first usage which can be used instead of the full ref. """); return; } +if (alias != null) + config = config.SetString("runcs", alias, location.ToString()); + // Create the dispatcher on the main thread. This is required // for some platform UI services such as macOS that mandates // all controls are created/accessed on the initial thread @@ -50,7 +69,7 @@ // Run AppMain in a new thread and keep the main thread free // to process the dispatcher's job queue. var main = Task - .Run(() => new RemoteRunner(location, ThisAssembly.Project.ToolCommandName) + .Run(() => new RemoteRunner(location, ThisAssembly.Project.ToolCommandName, config) .RunAsync(args[1..], aot)) .ContinueWith(t => { diff --git a/src/runcs/Properties/launchSettings.json b/src/runcs/Properties/launchSettings.json index f092cef..ede11a3 100644 --- a/src/runcs/Properties/launchSettings.json +++ b/src/runcs/Properties/launchSettings.json @@ -1,5 +1,8 @@ { "profiles": { + "help": { + "commandName": "Project" + }, "args": { "commandName": "Project", "commandLineArgs": "--aot kzu/runcs@v1 dotnet rocks" @@ -23,6 +26,11 @@ "vault2secrets": { "commandName": "Project", "commandLineArgs": "kzu/run:vault2secrets.cs" + }, + "clean": { + "commandName": "Project", + "commandLineArgs": "clean", + "workingDirectory": "C:\\Code\\WhatsApp" } } } \ No newline at end of file diff --git a/src/runcs/runcs.csproj b/src/runcs/runcs.csproj index 5f9a114..a2f7d87 100644 --- a/src/runcs/runcs.csproj +++ b/src/runcs/runcs.csproj @@ -23,6 +23,7 @@ +