Repository navigation
Expand file tree
/
Copy pathComponentInstaller.cs
More file actions
235 lines (204 loc) · 8.45 KB
/
Copy pathComponentInstaller.cs
File metadata and controls
235 lines (204 loc) · 8.45 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
using System.Text.Json;
using Spectre.Console;
using ShellUI.Native.Core.Models;
using ShellUI.Native.Templates;
namespace ShellUI.Native.CLI.Services;
// Handles the 'add' command - installs components to the project
public static class ComponentInstaller
{
public static async Task InstallComponents(string[] components, bool force)
{
var configPath = Path.Combine(Directory.GetCurrentDirectory(), "shellui-native.json");
if (!File.Exists(configPath))
{
AnsiConsole.MarkupLine("[red]ShellUI Native not initialized![/]");
AnsiConsole.MarkupLine("[yellow]Run 'shellui-native init' first[/]");
return;
}
var configJson = await File.ReadAllTextAsync(configPath);
var config = JsonSerializer.Deserialize<ShellUINativeConfig>(configJson);
if (config == null)
{
AnsiConsole.MarkupLine("[red]Failed to read shellui-native.json[/]");
return;
}
var projectInfo = ProjectDetector.DetectProject();
// Parse comma-separated components; a part installs its whole family
var componentList = new List<string>();
foreach (var comp in components.SelectMany(c => c.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)))
{
var family = ComponentRegistry.GetFamily(comp);
if (family != null)
AnsiConsole.MarkupLine($"[dim]'{comp}' is part of '{family}', installing '{family}'[/]");
var name = family ?? comp;
if (!componentList.Contains(name))
componentList.Add(name);
}
var tally = new InstallTally();
var failedComponents = new List<string>();
var installedSet = new HashSet<string>();
// Show dependency information
foreach (var componentName in componentList)
{
var metadata = ComponentRegistry.GetMetadata(componentName);
if (metadata != null && metadata.Dependencies.Count > 0)
{
AnsiConsole.MarkupLine($"[green]●[/] [bold]{componentName}[/] requires: [yellow]{string.Join(", ", metadata.Dependencies)}[/]");
}
}
AnsiConsole.MarkupLine("");
await Loaders.SnakeStatus()
.StartAsync("Installing components...", async ctx =>
{
foreach (var componentName in componentList)
{
ctx.Status($"Installing {componentName}...");
await InstallComponentWithDependenciesAsync(
componentName, config, projectInfo, force,
installedSet, tally, failedComponents);
}
});
// Update config
var updatedJson = JsonSerializer.Serialize(config, new JsonSerializerOptions { WriteIndented = true });
await File.WriteAllTextAsync(configPath, updatedJson);
// Summary
AnsiConsole.MarkupLine("");
if (tally.Success > 0)
AnsiConsole.MarkupLine($"[green]Installed {tally.Success} component(s) successfully![/]");
if (tally.Skipped > 0)
AnsiConsole.MarkupLine($"[yellow]Skipped {tally.Skipped} component(s) (already exists, use --force to overwrite)[/]");
if (failedComponents.Count > 0)
AnsiConsole.MarkupLine($"[red]Failed: {string.Join(", ", failedComponents)}[/]");
}
// " Did you mean 'x' or 'y'?" for an unknown name, or nothing when no name is close.
internal static string DidYouMean(string name)
{
var suggestions = ComponentRegistry.Suggest(name);
return suggestions.Count == 0
? string.Empty
: $" [yellow]Did you mean {string.Join(" or ", suggestions.Select(s => $"'{s}'"))}?[/]";
}
private static async Task InstallComponentWithDependenciesAsync(
string componentName,
ShellUINativeConfig config,
ProjectInfo projectInfo,
bool force,
HashSet<string> installedSet,
InstallTally tally,
List<string> failedComponents)
{
if (installedSet.Contains(componentName))
return;
if (!ComponentRegistry.Exists(componentName))
{
AnsiConsole.MarkupLine($"[red]Component '{componentName}' not found[/]{DidYouMean(componentName)}");
failedComponents.Add(componentName);
return;
}
var metadata = ComponentRegistry.GetMetadata(componentName);
if (metadata == null)
{
AnsiConsole.MarkupLine($"[red]Failed to get metadata for '{componentName}'[/]");
failedComponents.Add(componentName);
return;
}
// Install dependencies first
if (metadata.Dependencies.Count > 0)
{
AnsiConsole.MarkupLine($"[dim]Installing dependencies for [bold]{componentName}[/]: {string.Join(", ", metadata.Dependencies)}[/]");
foreach (var dep in metadata.Dependencies)
{
if (!installedSet.Contains(dep))
{
await InstallComponentWithDependenciesAsync(dep, config, projectInfo, force, installedSet, tally, failedComponents);
}
}
}
// Install the component
var result = await InstallComponentInternalAsync(componentName, metadata, config, projectInfo, force);
if (result == InstallResult.Success)
{
tally.Success++;
installedSet.Add(componentName);
}
else if (result == InstallResult.Skipped)
{
tally.Skipped++;
installedSet.Add(componentName);
}
else
{
failedComponents.Add(componentName);
}
}
private static async Task<InstallResult> InstallComponentInternalAsync(
string componentName,
ComponentMetadata metadata,
ShellUINativeConfig config,
ProjectInfo projectInfo,
bool force)
{
var componentPath = Path.Combine(Directory.GetCurrentDirectory(), config.ComponentsPath, metadata.FilePath);
if (File.Exists(componentPath) && !force)
{
AnsiConsole.MarkupLine($"[yellow]Skipped '{componentName}' (already exists)[/]");
return InstallResult.Skipped;
}
if (!ComponentRegistry.SupportsPlatform(componentName, config.TargetPlatform))
{
var supported = ComponentRegistry.GetSupportedPlatforms(componentName);
var supportedList = supported.Count == 0 ? "(none)" : string.Join(", ", supported);
AnsiConsole.MarkupLine(
$"[red]Component '{componentName}' has no template for {config.TargetPlatform}.[/] " +
$"[dim]Supported platforms: {supportedList}. See docs/DEVELOPMENT_PLAN.md for the roadmap.[/]");
return InstallResult.Failed;
}
var content = ComponentRegistry.GetComponentContent(componentName, config.TargetPlatform);
if (content == null)
{
AnsiConsole.MarkupLine($"[red]Failed to get content for '{componentName}'[/]");
return InstallResult.Failed;
}
// Replace namespace placeholder
content = TemplateText.Apply(content, projectInfo.RootNamespace, config.ComponentsPath);
// Ensure directory exists
var directory = Path.GetDirectoryName(componentPath);
if (directory != null)
{
Directory.CreateDirectory(directory);
}
await File.WriteAllTextAsync(componentPath, content);
// Update config
var existing = config.InstalledComponents.FirstOrDefault(c => c.Name == componentName);
if (existing != null)
{
existing.Version = metadata.Version;
existing.InstalledAt = DateTime.UtcNow;
existing.IsCustomized = false;
}
else
{
config.InstalledComponents.Add(new InstalledComponent
{
Name = componentName,
Version = metadata.Version,
Platform = config.TargetPlatform,
InstalledAt = DateTime.UtcNow,
IsCustomized = false
});
}
AnsiConsole.MarkupLine($"[green]✓ Installed '{componentName}'[/] [dim]({metadata.FilePath})[/]");
return InstallResult.Success;
}
private enum InstallResult
{
Success,
Skipped,
Failed
}
private sealed class InstallTally
{
public int Success;
public int Skipped;
}
}