|
| 1 | +using Spectre.Console.Cli; |
| 2 | + |
| 3 | +namespace SolarWinds.Changesets.Commands.ShellInit; |
| 4 | + |
| 5 | +/// <summary> |
| 6 | +/// Prints a shell snippet that defines a <c>changeset-net</c> command pointing at this net-changesets binary. |
| 7 | +/// Because net-changesets and the Node <c>@changesets</c> CLI share the command name <c>changeset</c> (and a |
| 8 | +/// .NET tool may expose only one command), there is no packaged way to add a second name. A shell function is |
| 9 | +/// the workaround: <c>changeset-net</c> always runs net-changesets, even when <c>changeset</c> resolves to the |
| 10 | +/// Node tool on the user's PATH. Use it via <c>eval "$(changeset shell-init zsh)"</c> (or the pwsh form). |
| 11 | +/// </summary> |
| 12 | +internal sealed class ShellInitCommand : Command<ShellInitCommandSettings> |
| 13 | +{ |
| 14 | + private static readonly string[] s_shells = ["zsh", "bash", "pwsh"]; |
| 15 | + |
| 16 | + public static string Name { get; } = "shell-init"; |
| 17 | + |
| 18 | + public static string Description { get; } = |
| 19 | + "Print a shell snippet that defines a 'changeset-net' command for invoking net-changesets directly."; |
| 20 | + |
| 21 | + protected override int Execute(CommandContext context, ShellInitCommandSettings settings, CancellationToken cancellationToken) |
| 22 | + { |
| 23 | + // The path to this binary, so the emitted command always reaches net-changesets regardless of PATH |
| 24 | + // order. For an installed global tool this is the tool shim (for example ~/.dotnet/tools/changeset). |
| 25 | + string net = Environment.ProcessPath ?? "changeset"; |
| 26 | + |
| 27 | + // Raw stdout throughout (Spectre1000 suppressed deliberately): the snippet is meant to be captured by |
| 28 | + // `eval "$(...)"`, so it must not be wrapped to terminal width or marked up, which AnsiConsole would do. |
| 29 | +#pragma warning disable Spectre1000 // Use AnsiConsole instead of System.Console |
| 30 | + // No shell → show how to enable it (this is what users run first). |
| 31 | + if (string.IsNullOrWhiteSpace(settings.Shell)) |
| 32 | + { |
| 33 | + Console.WriteLine(Instructions(net)); |
| 34 | + return 0; |
| 35 | + } |
| 36 | + |
| 37 | + if (!s_shells.Contains(settings.Shell, StringComparer.OrdinalIgnoreCase)) |
| 38 | + { |
| 39 | + Console.Error.WriteLine($"Unknown shell '{settings.Shell}'. Supported: {string.Join(", ", s_shells)}."); |
| 40 | + return 1; |
| 41 | + } |
| 42 | + |
| 43 | + string snippet = string.Equals(settings.Shell, "pwsh", StringComparison.OrdinalIgnoreCase) |
| 44 | + ? PwshSnippet(net) |
| 45 | + : string.Equals(settings.Shell, "bash", StringComparison.OrdinalIgnoreCase) |
| 46 | + ? PosixSnippet(net, "~/.bashrc", "bash") |
| 47 | + : PosixSnippet(net, "~/.zshrc", "zsh"); |
| 48 | + |
| 49 | + Console.WriteLine(snippet); |
| 50 | +#pragma warning restore Spectre1000 |
| 51 | + return 0; |
| 52 | + } |
| 53 | + |
| 54 | + private static string Instructions(string net) => |
| 55 | + $""" |
| 56 | + Define a `changeset-net` command that always runs net-changesets, even when the shared `changeset` |
| 57 | + command resolves to the Node @changesets CLI on your PATH. |
| 58 | +
|
| 59 | + Add the line for your shell to its startup file, then restart the shell: |
| 60 | +
|
| 61 | + zsh ~/.zshrc eval "$("{net}" shell-init zsh)" |
| 62 | + bash ~/.bashrc eval "$("{net}" shell-init bash)" |
| 63 | + pwsh $PROFILE Invoke-Expression (& "{net}" shell-init pwsh | Out-String) |
| 64 | +
|
| 65 | + Run `changeset shell-init <shell>` to print the snippet itself. |
| 66 | + """; |
| 67 | + |
| 68 | + private static string PosixSnippet(string net, string rcFile, string shell) => |
| 69 | + $$""" |
| 70 | + # net-changesets shell integration. Add this ONE line to {{rcFile}} (don't paste this script): |
| 71 | + # eval "$("{{net}}" shell-init {{shell}})" |
| 72 | + # It defines `changeset-net`, which always runs net-changesets even when the shared `changeset` |
| 73 | + # command resolves to the Node @changesets CLI on your PATH. |
| 74 | + changeset-net() { |
| 75 | + command "{{net}}" "$@" |
| 76 | + } |
| 77 | + """; |
| 78 | + |
| 79 | + private static string PwshSnippet(string net) => |
| 80 | + $$""" |
| 81 | + # net-changesets shell integration. Add this line to $PROFILE (don't paste this script): |
| 82 | + # Invoke-Expression (& "{{net}}" shell-init pwsh | Out-String) |
| 83 | + # It defines `changeset-net`, which always runs net-changesets even when the shared `changeset` |
| 84 | + # command resolves to the Node @changesets CLI on your PATH. |
| 85 | + function changeset-net { |
| 86 | + & "{{net}}" @args |
| 87 | + } |
| 88 | + """; |
| 89 | +} |
0 commit comments