|
| 1 | +using DotNetConfig; |
| 2 | +using Spectre.Console; |
| 3 | + |
| 4 | +namespace Devlooped; |
| 5 | + |
| 6 | +public static class AliasCommands |
| 7 | +{ |
| 8 | + public const string CommandName = "alias"; |
| 9 | + internal const string Section = "runfile"; |
| 10 | + |
| 11 | + public static int Execute(string[] args, Config config) |
| 12 | + { |
| 13 | + if (args.Length == 0) |
| 14 | + return List(config); |
| 15 | + |
| 16 | + if (args[0] is "delete") |
| 17 | + return Delete(args, config); |
| 18 | + |
| 19 | + if (args[0] is "rename") |
| 20 | + return Rename(args, config); |
| 21 | + |
| 22 | + ShowUsage(); |
| 23 | + return 1; |
| 24 | + } |
| 25 | + |
| 26 | + /// <summary>Writes the alias table when any are configured. Returns whether a table was written.</summary> |
| 27 | + public static bool WriteTableIfAny(Config config) |
| 28 | + { |
| 29 | + var aliases = GetAliases(config); |
| 30 | + if (aliases.Length == 0) |
| 31 | + return false; |
| 32 | + |
| 33 | + AnsiConsole.WriteLine(); |
| 34 | + AnsiConsole.WriteLine("Aliases:"); |
| 35 | + |
| 36 | + WriteTable(aliases); |
| 37 | + return true; |
| 38 | + } |
| 39 | + |
| 40 | + static int List(Config config) |
| 41 | + { |
| 42 | + var aliases = GetAliases(config); |
| 43 | + if (aliases.Length == 0) |
| 44 | + { |
| 45 | + AnsiConsole.MarkupLine(":information_source: [grey]No aliases configured. Use[/] [yellow]--dnx-alias[/] [grey]to create one.[/]"); |
| 46 | + return 0; |
| 47 | + } |
| 48 | + |
| 49 | + AnsiConsole.MarkupLine($"[grey]Configured ref aliases[/] [dim]({aliases.Length})[/]:"); |
| 50 | + WriteTable(aliases); |
| 51 | + return 0; |
| 52 | + } |
| 53 | + |
| 54 | + static int Delete(string[] args, Config config) |
| 55 | + { |
| 56 | + if (args.Length < 2 || string.IsNullOrWhiteSpace(args[1])) |
| 57 | + { |
| 58 | + AnsiConsole.MarkupLine(":cross_mark: [red]Missing alias name.[/]"); |
| 59 | + ShowUsage(); |
| 60 | + return 1; |
| 61 | + } |
| 62 | + |
| 63 | + var name = args[1]; |
| 64 | + if (config.GetString(Section, name) is null) |
| 65 | + { |
| 66 | + AnsiConsole.MarkupLine($":cross_mark: [red]Alias[/] [cyan bold]{Markup.Escape(name)}[/] [red]not found.[/]"); |
| 67 | + return 1; |
| 68 | + } |
| 69 | + |
| 70 | + config.Unset(Section, name); |
| 71 | + AnsiConsole.MarkupLine($":check_mark_button: [green]Deleted alias[/] [cyan bold]{Markup.Escape(name)}[/][green].[/]"); |
| 72 | + return 0; |
| 73 | + } |
| 74 | + |
| 75 | + static int Rename(string[] args, Config config) |
| 76 | + { |
| 77 | + if (args.Length < 3 || string.IsNullOrWhiteSpace(args[1]) || string.IsNullOrWhiteSpace(args[2])) |
| 78 | + { |
| 79 | + AnsiConsole.MarkupLine(":cross_mark: [red]Missing alias name.[/]"); |
| 80 | + ShowUsage(); |
| 81 | + return 1; |
| 82 | + } |
| 83 | + |
| 84 | + var oldName = args[1]; |
| 85 | + var newName = args[2]; |
| 86 | + |
| 87 | + if (string.Equals(oldName, newName, StringComparison.OrdinalIgnoreCase)) |
| 88 | + { |
| 89 | + AnsiConsole.MarkupLine($":information_source: [grey]Alias[/] [cyan bold]{Markup.Escape(oldName)}[/] [grey]already has that name.[/]"); |
| 90 | + return 0; |
| 91 | + } |
| 92 | + |
| 93 | + if (config.GetString(Section, oldName) is not { } @ref) |
| 94 | + { |
| 95 | + AnsiConsole.MarkupLine($":cross_mark: [red]Alias[/] [cyan bold]{Markup.Escape(oldName)}[/] [red]not found.[/]"); |
| 96 | + return 1; |
| 97 | + } |
| 98 | + |
| 99 | + if (config.GetString(Section, newName) is not null) |
| 100 | + { |
| 101 | + AnsiConsole.MarkupLine($":cross_mark: [red]Alias[/] [cyan bold]{Markup.Escape(newName)}[/] [red]already exists.[/]"); |
| 102 | + return 1; |
| 103 | + } |
| 104 | + |
| 105 | + config.SetString(Section, newName, @ref).Unset(Section, oldName); |
| 106 | + AnsiConsole.MarkupLine($":check_mark_button: [green]Renamed alias[/] [cyan bold]{Markup.Escape(oldName)}[/] [green]to[/] [cyan bold]{Markup.Escape(newName)}[/][green].[/]"); |
| 107 | + return 0; |
| 108 | + } |
| 109 | + |
| 110 | + static (string Name, string Ref)[] GetAliases(Config config) => |
| 111 | + config |
| 112 | + .Where(entry => entry.Section == Section && entry.Subsection == null) |
| 113 | + .Select(entry => entry.Variable) |
| 114 | + .Distinct(StringComparer.OrdinalIgnoreCase) |
| 115 | + .OrderBy(name => name, StringComparer.OrdinalIgnoreCase) |
| 116 | + .Select(name => (Name: name, Ref: config.GetString(Section, name)!)) |
| 117 | + .Where(alias => alias.Ref != null) |
| 118 | + .ToArray(); |
| 119 | + |
| 120 | + static void WriteTable((string Name, string Ref)[] aliases) |
| 121 | + { |
| 122 | + var table = new Table() |
| 123 | + .RoundedBorder() |
| 124 | + .BorderColor(Color.Grey23) |
| 125 | + .AddColumn(new TableColumn("[lime bold]:label: Alias[/]").LeftAligned()) |
| 126 | + .AddColumn(new TableColumn("[lime bold]:link: Ref[/]").LeftAligned()); |
| 127 | + |
| 128 | + foreach (var (name, @ref) in aliases) |
| 129 | + table.AddRow($"[cyan bold]{Markup.Escape(name)}[/]", FormatRefLink(@ref)); |
| 130 | + |
| 131 | + AnsiConsole.Write(table); |
| 132 | + } |
| 133 | + |
| 134 | + static string FormatRef(string @ref) => |
| 135 | + @ref.StartsWith("github.com/", StringComparison.OrdinalIgnoreCase) |
| 136 | + ? @ref["github.com/".Length..] |
| 137 | + : @ref; |
| 138 | + |
| 139 | + static string FormatRefLink(string @ref) |
| 140 | + { |
| 141 | + var display = Markup.Escape(FormatRef(@ref)); |
| 142 | + return RemoteRef.TryParse(@ref, out var parsed) |
| 143 | + ? $"[link={parsed.ToWebUrl()}]{display}[/]" |
| 144 | + : $"[link]{display}[/]"; |
| 145 | + } |
| 146 | + |
| 147 | + static void ShowUsage() => |
| 148 | + AnsiConsole.MarkupLine( |
| 149 | + """ |
| 150 | + [grey]Usage:[/] |
| 151 | + [cyan bold]alias[/] [grey]List configured ref aliases.[/] |
| 152 | + [cyan bold]alias delete[/] [yellow]NAME[/] [grey]Delete a ref alias.[/] |
| 153 | + [cyan bold]alias rename[/] [yellow]OLD[/] [yellow]NEW[/] [grey]Rename a ref alias.[/] |
| 154 | + """); |
| 155 | +} |
0 commit comments