Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
419 lines (359 loc) · 15.9 KB
/
Copy pathProgram.cs
File metadata and controls
419 lines (359 loc) · 15.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
using System.CommandLine;
using Spectre.Console;
using ShellUI.Templates;
using ShellUI.Core.Models;
using ShellUI.CLI.Services;
namespace ShellUI.CLI;
class Program
{
static async Task<int> Main(string[] args)
{
// Windows consoles default to the OEM code page; must run before AnsiConsole is first used.
try { Console.OutputEncoding = System.Text.Encoding.UTF8; } catch (IOException) { }
var rootCommand = new RootCommand("ShellUI - CLI-first Blazor component library")
{
Description = "Add beautiful, accessible components to your Blazor app. Inspired by shadcn/ui."
};
rootCommand.AddCommand(CreateInitCommand());
rootCommand.AddCommand(CreateAddCommand());
rootCommand.AddCommand(CreateListCommand());
rootCommand.AddCommand(CreateRemoveCommand());
rootCommand.AddCommand(CreateUpdateCommand());
rootCommand.AddCommand(CreateThemeCommand());
rootCommand.AddCommand(CreateDoctorCommand());
rootCommand.AddCommand(CreateMigrateCommand());
return await rootCommand.InvokeAsync(args);
}
static Command CreateThemeCommand()
{
var theme = new Command("theme", "Fetch and apply themes from tweakcn.com at build time.");
theme.AddCommand(CreateThemeInitCommand());
theme.AddCommand(CreateThemeApplyCommand());
theme.AddCommand(CreateThemeUpdateCommand());
return theme;
}
static Command CreateThemeInitCommand()
{
var command = new Command("init", "Initialize ShellUI in a fresh Blazor project and bake in a tweakcn theme in one shot.");
var urlArg = new Argument<string>("url",
"tweakcn URL or theme id. Accepts https://tweakcn.com/themes/<id>, the raw <id>, or the public https://tweakcn.com/r/themes/<id> endpoint.");
command.AddArgument(urlArg);
var forceOpt = new Option<bool>("--force", "Reinitialize even if already initialized");
var styleOpt = new Option<string>("--style", getDefaultValue: () => "default",
"Component style: default, new-york, minimal");
var tailwindOpt = new Option<string?>("--tailwind", "Tailwind method: standalone (default with --yes) or npm");
var yesOpt = new Option<bool>("--yes", "Non-interactive mode with default options");
var dashboardOpt = CreateDashboardOption();
var replaceLayoutOpt = CreateReplaceLayoutOption();
command.AddOption(forceOpt);
command.AddOption(styleOpt);
command.AddOption(tailwindOpt);
command.AddOption(yesOpt);
command.AddOption(dashboardOpt);
command.AddOption(replaceLayoutOpt);
command.SetHandler(async (url, force, style, tailwind, nonInteractive, dashboard, replaceLayout) =>
{
try
{
AnsiConsole.MarkupLine("[cyan]Step 1/2:[/] initializing ShellUI…");
await InitService.InitializeAsync(style, force, tailwind, nonInteractive, dashboard,
replaceLayout ? LayoutSwitch.Always : LayoutSwitch.Ask);
AnsiConsole.MarkupLine("");
AnsiConsole.MarkupLine("[cyan]Step 2/2:[/] applying tweakcn theme…");
await ApplyThemeAsync(url, emitOverride: null);
}
catch (Exception ex)
{
AnsiConsole.MarkupLine($"[red]Error:[/] {ex.Message.Replace("[", "[[").Replace("]", "]]")}");
Environment.Exit(1);
}
}, urlArg, forceOpt, styleOpt, tailwindOpt, yesOpt, dashboardOpt, replaceLayoutOpt);
return command;
}
static Option<string?> CreateDashboardOption() => new(
"--dashboard",
"Set up the dashboard layout: 02 (sticky header), 01 (scrolling header) or none. Prompts when omitted; --yes alone means none");
static Option<bool> CreateReplaceLayoutOption() => new(
"--replace-layout",
"Make the dashboard the default layout even if the app already has a custom one");
static Command CreateThemeApplyCommand()
{
var command = new Command("apply", "Fetch a theme from tweakcn and bake it into wwwroot/input.css");
var urlArg = new Argument<string>("url",
"tweakcn URL or theme id. Accepts https://tweakcn.com/themes/<id>, the raw <id>, or the public https://tweakcn.com/r/themes/<id> endpoint.");
command.AddArgument(urlArg);
var emitOverrideOpt = new Option<string?>("--emit-override",
"Write the theme block to a standalone CSS file (Path A/D users link this after shellui-all.css). If omitted, updates wwwroot/input.css in-place.");
command.AddOption(emitOverrideOpt);
command.SetHandler(async (url, emitOverride) =>
{
try
{
await ApplyThemeAsync(url, emitOverride);
}
catch (Exception ex)
{
AnsiConsole.MarkupLine($"[red]Error:[/] {ex.Message.Replace("[", "[[").Replace("]", "]]")}");
Environment.Exit(1);
}
}, urlArg, emitOverrideOpt);
return command;
}
static Command CreateThemeUpdateCommand()
{
var command = new Command("update", "Re-fetch the theme recorded in shellui.theme.lock and re-apply it.");
command.SetHandler(async () =>
{
try
{
var cwd = Directory.GetCurrentDirectory();
var lockFile = ThemeService.ReadLockFile(cwd);
if (lockFile is null)
{
AnsiConsole.MarkupLine("[yellow]No shellui.theme.lock in this directory.[/] Run [bold]shellui theme apply <url>[/] first.");
Environment.Exit(2);
return;
}
AnsiConsole.MarkupLine($"[cyan]Updating theme[/] [bold]{lockFile!.ThemeName}[/] from [dim]{lockFile.SourceUrl}[/]");
await ApplyThemeAsync(lockFile.SourceUrl, emitOverride: null);
}
catch (Exception ex)
{
AnsiConsole.MarkupLine($"[red]Error:[/] {ex.Message.Replace("[", "[[").Replace("]", "]]")}");
Environment.Exit(1);
}
});
return command;
}
private static async Task ApplyThemeAsync(string url, string? emitOverride)
{
var cwd = Directory.GetCurrentDirectory();
var json = await Loaders.SnakeStatus().StartAsync($"Fetching theme: {Markup.Escape(url)}", _ => ThemeService.FetchThemeJsonAsync(url));
var theme = ThemeService.ParseTheme(json);
AnsiConsole.MarkupLine($"[green]✓[/] Fetched: [bold]{theme.Name}[/] ({theme.LightVars.Count} light + {theme.DarkVars.Count} dark vars)");
if (!string.IsNullOrEmpty(emitOverride))
{
var outPath = Path.IsPathRooted(emitOverride) ? emitOverride : Path.Combine(cwd, emitOverride);
ThemeService.EmitOverride(outPath, theme);
AnsiConsole.MarkupLine($"[green]✓[/] Wrote override: [dim]{outPath}[/]");
AnsiConsole.MarkupLine("");
AnsiConsole.MarkupLine("[yellow]Next step:[/] link the override AFTER shellui-all.css in your App.razor <head>:");
AnsiConsole.MarkupLine($" [dim]<link rel=\"stylesheet\" href=\"_content/ShellUI.Components/shellui-all.css\" />[/]");
AnsiConsole.MarkupLine($" [bold]<link rel=\"stylesheet\" href=\"{Path.GetFileName(outPath)}\" />[/]");
}
else
{
var inputCss = Path.Combine(cwd, "wwwroot", "input.css");
ThemeService.ApplyToInputCss(inputCss, theme);
AnsiConsole.MarkupLine($"[green]✓[/] Updated: [dim]wwwroot/input.css[/]");
AnsiConsole.MarkupLine("");
AnsiConsole.MarkupLine("[yellow]Next step:[/] rebuild Tailwind (or run [bold]dotnet build[/] if the CLI wired that up during init).");
}
ThemeService.WriteLockFile(cwd, url, json, theme.Name);
AnsiConsole.MarkupLine($"[dim]Lock file written: shellui.theme.lock[/]");
}
static Command CreateInitCommand()
{
var command = new Command("init", "Initialize ShellUI in your Blazor project");
var forceOption = new Option<bool>(
"--force",
"Reinitialize even if already initialized");
var styleOption = new Option<string>(
"--style",
getDefaultValue: () => "default",
"Choose component style (default, new-york, minimal)");
var tailwindOption = new Option<string?>(
"--tailwind",
"Choose Tailwind method: standalone (default with --yes) or npm");
var nonInteractiveOption = new Option<bool>(
"--yes",
"Run in non-interactive mode with default options");
var dashboardOption = CreateDashboardOption();
var replaceLayoutOption = CreateReplaceLayoutOption();
command.AddOption(forceOption);
command.AddOption(styleOption);
command.AddOption(tailwindOption);
command.AddOption(nonInteractiveOption);
command.AddOption(dashboardOption);
command.AddOption(replaceLayoutOption);
command.SetHandler(async (force, style, tailwind, nonInteractive, dashboard, replaceLayout) =>
{
try
{
LogoLoader.WriteHeader("Setting up your Blazor project");
await InitService.InitializeAsync(style, force, tailwind, nonInteractive, dashboard,
replaceLayout ? LayoutSwitch.Always : LayoutSwitch.Ask);
}
catch (Exception ex)
{
AnsiConsole.MarkupLine($"[red]Error:[/] {ex.Message.Replace("[", "[[").Replace("]", "]]")}");
Environment.Exit(1);
}
}, forceOption, styleOption, tailwindOption, nonInteractiveOption, dashboardOption, replaceLayoutOption);
return command;
}
static Command CreateAddCommand()
{
var command = new Command("add", "Add component(s) to your project");
var componentsArg = new Argument<string[]>(
"components",
"Component name(s) to add (space or comma-separated), or all for the whole library")
{
Arity = ArgumentArity.OneOrMore
};
command.AddArgument(componentsArg);
var forceOption = new Option<bool>(
"--force",
"Overwrite existing components");
command.AddOption(forceOption);
var replaceLayoutOption = CreateReplaceLayoutOption();
command.AddOption(replaceLayoutOption);
var exceptOption = new Option<string[]>(
"--except",
"Component(s) to leave out, for example with all (space or comma-separated)")
{
AllowMultipleArgumentsPerToken = true
};
command.AddOption(exceptOption);
command.SetHandler(async (components, force, replaceLayout, except) =>
{
try
{
if (!await ComponentInstaller.InstallComponents(components, force, replaceLayout ? LayoutSwitch.Always : LayoutSwitch.Ask, except))
Environment.Exit(1);
}
catch (Exception ex)
{
AnsiConsole.MarkupLine($"[red]Error:[/] {ex.Message.Replace("[", "[[").Replace("]", "]]")}");
Environment.Exit(1);
}
}, componentsArg, forceOption, replaceLayoutOption, exceptOption);
return command;
}
static Command CreateDoctorCommand()
{
var command = new Command("doctor", "Check the project's ShellUI setup and say how to fix problems");
command.SetHandler(context =>
{
try
{
context.ExitCode = DoctorService.Run();
}
catch (Exception ex)
{
AnsiConsole.MarkupLine($"[red]Error:[/] {Markup.Escape(ex.Message)}");
context.ExitCode = 1;
}
});
return command;
}
static Command CreateMigrateCommand()
{
var command = new Command("migrate", "Rewrite your pages for template API changes, showing a diff and asking first");
var componentsArg = new Argument<string[]>(
"components",
$"Component(s) to migrate (empty = all): {string.Join(", ", MigrateService.Components)}")
{
Arity = ArgumentArity.ZeroOrMore
};
command.AddArgument(componentsArg);
var dryRunOption = new Option<bool>("--dry-run", "Show the changes without writing them");
command.AddOption(dryRunOption);
var yesOption = new Option<bool>("--yes", "Write the changes without asking");
command.AddOption(yesOption);
command.SetHandler(context =>
{
try
{
context.ExitCode = MigrateService.Run(
context.ParseResult.GetValueForArgument(componentsArg),
context.ParseResult.GetValueForOption(dryRunOption),
context.ParseResult.GetValueForOption(yesOption));
}
catch (Exception ex)
{
AnsiConsole.MarkupLine($"[red]Error:[/] {Markup.Escape(ex.Message)}");
context.ExitCode = 1;
}
});
return command;
}
static Command CreateListCommand()
{
var command = new Command("list", "List available components");
var installedOption = new Option<bool>(
"--installed",
"Show only installed components");
var availableOption = new Option<bool>(
"--available",
"Show only available components");
command.AddOption(installedOption);
command.AddOption(availableOption);
command.SetHandler((installed, available) =>
{
try
{
ComponentManager.ListComponents(installed, available);
}
catch (Exception ex)
{
AnsiConsole.MarkupLine($"[red]Error:[/] {ex.Message.Replace("[", "[[").Replace("]", "]]")}");
}
}, installedOption, availableOption);
return command;
}
static Command CreateRemoveCommand()
{
var command = new Command("remove", "Remove component(s) from your project");
var componentsArg = new Argument<string[]>(
"components",
"Component name(s) to remove")
{
Arity = ArgumentArity.OneOrMore
};
command.AddArgument(componentsArg);
command.SetHandler((components) =>
{
try
{
ComponentManager.RemoveComponents(components);
}
catch (Exception ex)
{
AnsiConsole.MarkupLine($"[red]Error:[/] {ex.Message.Replace("[", "[[").Replace("]", "]]")}");
}
}, componentsArg);
return command;
}
static Command CreateUpdateCommand()
{
var command = new Command("update", "Rewrite installed component(s) from the current CLI templates");
var componentsArg = new Argument<string[]>(
"components",
"Component name(s) to update (empty = all)")
{
Arity = ArgumentArity.ZeroOrMore
};
command.AddArgument(componentsArg);
var allOption = new Option<bool>(
"--all",
"Update all installed components");
command.AddOption(allOption);
var forceOption = new Option<bool>(
"--force",
"Also overwrite components you have changed");
command.AddOption(forceOption);
command.SetHandler(async (components, all, force) =>
{
try
{
await ComponentManager.UpdateComponents(components, all, force);
}
catch (Exception ex)
{
AnsiConsole.MarkupLine($"[red]Error:[/] {ex.Message.Replace("[", "[[").Replace("]", "]]")}");
}
}, componentsArg, allOption, forceOption);
return command;
}
}