Skip to content

Commit 6d5c0e2

Browse files
committed
Add support for creating aliases for refs
1 parent 328fb1a commit 6d5c0e2

8 files changed

Lines changed: 83 additions & 16 deletions

File tree

readme.md

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Run C# code programs from git repos on GitHub, GitLab and Azure DevOps.
1212

1313
```
1414
Usage:
15-
[dnx] runcs <repoRef> [<appArgs>...]
15+
[dnx] runcs [--aot] [--alias ALIAS] <repoRef> [<appArgs>...]
1616
1717
Arguments:
1818
<REPO_REF> Reference to remote file to run, with format [host/]owner/repo[@ref][:path]
@@ -25,7 +25,13 @@ Arguments:
2525
* gitlab.com/kzu/sandbox@main:run.cs (all explicit parts)
2626
* kzu/sandbox (implied host github.com, ref and path defaults)
2727
28+
Can be an alias previously set with --alias.
29+
2830
<appArgs> Arguments passed to the C# program that is being run.
31+
32+
Options:
33+
--aot (optional) Enable dotnet AOT defaults for run file.cs. Defaults to false.
34+
--alias ALIAS (optional) Assign an alias on first usage which can be used instead of the full ref.
2935
```
3036

3137
Example:
@@ -50,16 +56,24 @@ The last download etag is used to avoid downloading on each run.
5056
Run C# code programs from GitHub gists.
5157

5258
```
53-
Usage: [dnx] gist <gistRef> [<appArgs>...]
59+
Usage: [dnx] gist [--aot] [--alias ALIAS] <gistRef> [<appArgs>...]
60+
61+
Arguments:
5462
<GIST_REF> Reference to gist file to run, with format owner/gist[@commit][:path]
55-
@commit optional gist commit (default: default branch)
63+
@commit optional gist commit (default: latest)
5664
:path optional path to file in gist (default: program.cs or first .cs file)
5765
5866
Examples:
5967
* kzu/0ac826dc7de666546aaedd38e5965381 (tip commit and program.cs or first .cs file)
6068
* kzu/0ac826dc7de666546aaedd38e5965381@d8079cf:run.cs (explicit commit and file path)
6169
62-
<appArgs> Arguments passed to the C# program gist that is being run.
70+
Can be an alias previously set with --alias.
71+
72+
<appArgs> Arguments passed to the C# program that is being run.
73+
74+
Options:
75+
--aot (optional) Enable dotnet AOT defaults for run file.cs. Defaults to false.
76+
--alias ALIAS (optional) Assign an alias on first usage which can be used instead of the full ref.
6377
```
6478

6579
> [!TIP]

src/Core/RemoteRunner.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55

66
namespace Devlooped;
77

8-
public class RemoteRunner(RemoteRef location, string toolName)
8+
public class RemoteRunner(RemoteRef location, string toolName, Config? config = null)
99
{
10+
Config config = config ?? Config.Build(Config.GlobalLocation);
11+
1012
public async Task<int> RunAsync(string[] args, bool aot)
1113
{
12-
var config = Config.Build(Config.GlobalLocation);
1314
var etag = config.GetString(toolName, location.ToString(), "etag");
1415
if (etag != null && Directory.Exists(location.TempPath))
1516
{
@@ -18,6 +19,7 @@ public async Task<int> RunAsync(string[] args, bool aot)
1819

1920
location = location with { ETag = etag };
2021
}
22+
2123
if (config.TryGetString(toolName, location.ToString(), "uri", out var url) &&
2224
Uri.TryCreate(url, UriKind.Absolute, out var uri))
2325
location = location with { ResolvedUri = uri };

src/gist/Program.cs

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
using System.Runtime.InteropServices;
1+
using System.CommandLine;
2+
using System.Runtime.InteropServices;
23
using System.Text;
34
using Devlooped;
5+
using DotNetConfig;
46
using GitCredentialManager.UI;
57
using Spectre.Console;
68

@@ -15,12 +17,23 @@
1517
args = [.. args.Where(x => x != "--aot")];
1618
}
1719

20+
var config = Config.Build(Config.GlobalLocation);
21+
if (args.Length > 0 && config.GetString("runcs", args[0]) is string aliased)
22+
args = [aliased, .. args[1..]];
23+
24+
// Set alias and remove from args if present
25+
var option = new Option<string?>("--alias");
26+
var parsed = new RootCommand() { Options = { option } }.Parse(args);
27+
var alias = parsed.GetValue(option);
28+
if (alias != null)
29+
args = [.. parsed.UnmatchedTokens];
30+
1831
if (args.Length == 0 || !RemoteRef.TryParse("gist.github.com/" + args[0], out var location))
1932
{
2033
AnsiConsole.MarkupLine(
2134
$"""
2235
Usage:
23-
[grey][[dnx]][/] [lime]{ThisAssembly.Project.ToolCommandName}[/] [grey][[--aot]][/] [bold]<gistRef>[/] [grey italic][[<appArgs>...]][/]
36+
[grey][[dnx]][/] [lime]{ThisAssembly.Project.ToolCommandName}[/] [grey][[--aot]][/] [grey][[--alias ALIAS]][/] [bold]<gistRef>[/] [grey italic][[<appArgs>...]][/]
2437
2538
Arguments:
2639
[bold]<GIST_REF>[/] Reference to gist file to run, with format [yellow]owner/gist[[@commit]][[:path]][/]
@@ -30,15 +43,21 @@
3043
Examples:
3144
* kzu/0ac826dc7de666546aaedd38e5965381 (tip commit and program.cs or first .cs file)
3245
* kzu/0ac826dc7de666546aaedd38e5965381@d8079cf:run.cs (explicit commit and file path)
33-
46+
47+
Can be an alias previously set with --alias.
48+
3449
[bold]<appArgs>[/] Arguments passed to the C# program that is being run.
3550
3651
Options:
37-
[bold]--aot[/] (optional) Enable dotnet AOT defaults for run file.cs. Defaults to false.
52+
[bold]--aot[/] (optional) Enable dotnet AOT defaults for run file.cs. Defaults to false.
53+
[bold]--alias[/] ALIAS (optional) Assign an alias on first usage which can be used instead of the full ref.
3854
""");
3955
return;
4056
}
4157

58+
if (alias != null)
59+
config = config.SetString("runcs", alias, location.ToString());
60+
4261
// Create the dispatcher on the main thread. This is required
4362
// for some platform UI services such as macOS that mandates
4463
// all controls are created/accessed on the initial thread
@@ -48,7 +67,7 @@
4867
// Run AppMain in a new thread and keep the main thread free
4968
// to process the dispatcher's job queue.
5069
var main = Task
51-
.Run(() => new RemoteRunner(location, ThisAssembly.Project.ToolCommandName)
70+
.Run(() => new RemoteRunner(location, ThisAssembly.Project.ToolCommandName, config)
5271
.RunAsync(args[1..], aot))
5372
.ContinueWith(t =>
5473
{

src/gist/Properties/launchSettings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
{
22
"profiles": {
3+
"help": {
4+
"commandName": "Project",
5+
},
36
"gist": {
47
"commandName": "Project",
58
"commandLineArgs": "kzu/0ac826dc7de666546aaedd38e5965381"

src/gist/gist.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<PackageReadmeFile>readme.md</PackageReadmeFile>
1313
<PackageTags>dotnet dotnet-tool</PackageTags>
1414

15-
<Description>Run gists directly using: dnx gist owner/gist[:path]</Description>
15+
<Description>Run C# code gists directly using: dnx gist owner/gist[:path]</Description>
1616
</PropertyGroup>
1717

1818
<ItemGroup>
@@ -21,6 +21,7 @@
2121
<PackageReference Include="Avalonia" Version="11.3.4" />
2222
<PackageReference Include="Avalonia.Desktop" Version="11.3.4" />
2323
<PackageReference Include="git-credential-manager" Version="2.6.1" IncludeAssets="tools" GeneratePathProperty="true" />
24+
<PackageReference Include="System.CommandLine" Version="2.0.0-rc.1.25451.107" />
2425
</ItemGroup>
2526

2627
<ItemGroup Condition="'$(Pkggit-credential-manager)' != ''">

src/runcs/Program.cs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
using System.Runtime.InteropServices;
1+
using System.CommandLine;
2+
using System.Runtime.InteropServices;
23
using System.Text;
34
using Devlooped;
5+
using DotNetConfig;
46
using GitCredentialManager.UI;
57
using Spectre.Console;
68

@@ -15,12 +17,23 @@
1517
args = [.. args.Where(x => x != "--aot")];
1618
}
1719

20+
var config = Config.Build(Config.GlobalLocation);
21+
if (args.Length > 0 && config.GetString("runcs", args[0]) is string aliased)
22+
args = [aliased, .. args[1..]];
23+
24+
// Set alias and remove from args if present
25+
var option = new Option<string?>("--alias");
26+
var parsed = new RootCommand() { Options = { option } }.Parse(args);
27+
var alias = parsed.GetValue(option);
28+
if (alias != null)
29+
args = [.. parsed.UnmatchedTokens];
30+
1831
if (args.Length == 0 || !RemoteRef.TryParse(args[0], out var location))
1932
{
2033
AnsiConsole.MarkupLine(
2134
$"""
2235
Usage:
23-
[grey][[dnx]][/] [lime]{ThisAssembly.Project.ToolCommandName}[/] [grey][[--aot]][/] [bold]<repoRef>[/] [grey italic][[<appArgs>...]][/]
36+
[grey][[dnx]][/] [lime]{ThisAssembly.Project.ToolCommandName}[/] [grey][[--aot]][/] [grey][[--alias ALIAS]][/] [bold]<repoRef>[/] [grey italic][[<appArgs>...]][/]
2437
2538
Arguments:
2639
[bold]<REPO_REF>[/] Reference to remote file to run, with format [yellow][[host/]]owner/repo[[@ref]][[:path]][/]
@@ -33,14 +46,20 @@
3346
* gitlab.com/kzu/sandbox@main:run.cs (all explicit parts)
3447
* kzu/sandbox (implied host github.com, ref and path defaults)
3548
49+
Can be an alias previously set with --alias.
50+
3651
[bold]<appArgs>[/] Arguments passed to the C# program that is being run.
3752
3853
Options:
39-
[bold]--aot[/] (optional) Enable dotnet AOT defaults for run file.cs. Defaults to false.
54+
[bold]--aot[/] (optional) Enable dotnet AOT defaults for run file.cs. Defaults to false.
55+
[bold]--alias[/] ALIAS (optional) Assign an alias on first usage which can be used instead of the full ref.
4056
""");
4157
return;
4258
}
4359

60+
if (alias != null)
61+
config = config.SetString("runcs", alias, location.ToString());
62+
4463
// Create the dispatcher on the main thread. This is required
4564
// for some platform UI services such as macOS that mandates
4665
// all controls are created/accessed on the initial thread
@@ -50,7 +69,7 @@
5069
// Run AppMain in a new thread and keep the main thread free
5170
// to process the dispatcher's job queue.
5271
var main = Task
53-
.Run(() => new RemoteRunner(location, ThisAssembly.Project.ToolCommandName)
72+
.Run(() => new RemoteRunner(location, ThisAssembly.Project.ToolCommandName, config)
5473
.RunAsync(args[1..], aot))
5574
.ContinueWith(t =>
5675
{

src/runcs/Properties/launchSettings.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
{
22
"profiles": {
3+
"help": {
4+
"commandName": "Project"
5+
},
36
"args": {
47
"commandName": "Project",
58
"commandLineArgs": "--aot kzu/runcs@v1 dotnet rocks"
@@ -23,6 +26,11 @@
2326
"vault2secrets": {
2427
"commandName": "Project",
2528
"commandLineArgs": "kzu/run:vault2secrets.cs"
29+
},
30+
"clean": {
31+
"commandName": "Project",
32+
"commandLineArgs": "clean",
33+
"workingDirectory": "C:\\Code\\WhatsApp"
2634
}
2735
}
2836
}

src/runcs/runcs.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
<PackageReference Include="LibGit2Sharp" Version="0.31.0" />
2424
<PackageReference Include="Spectre.Console" Version="0.50.0" />
2525
<PackageReference Include="ThisAssembly" Version="2.0.14" PrivateAssets="all"/>
26+
<PackageReference Include="System.CommandLine" Version="2.0.0-rc.1.25451.107" />
2627
</ItemGroup>
2728

2829
<ItemGroup Condition="'$(Pkggit-credential-manager)' != ''">

0 commit comments

Comments
 (0)