Repository navigation
Expand file tree
/
Copy pathInitService.cs
More file actions
147 lines (129 loc) · 6.42 KB
/
Copy pathInitService.cs
File metadata and controls
147 lines (129 loc) · 6.42 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
using System.Text.Json;
using Spectre.Console;
using ShellUI.Native.Core.Models;
using ShellUI.Native.Templates;
namespace ShellUI.Native.CLI.Services;
// Handles the 'init' command - initializes ShellUI Native in a project
public static class InitService
{
public static async Task InitializeAsync(string style, bool force, bool nonInteractive = false)
{
var configPath = Path.Combine(Directory.GetCurrentDirectory(), "shellui-native.json");
if (File.Exists(configPath) && !force)
{
AnsiConsole.MarkupLine("[yellow]ShellUI Native is already initialized in this project.[/]");
AnsiConsole.MarkupLine("[dim]Use --force to reinitialize[/]");
return;
}
var projectInfo = ProjectDetector.DetectProject();
var platformName = projectInfo.Platform switch
{
NativePlatform.MAUI => ".NET MAUI",
NativePlatform.Avalonia => "Avalonia UI",
NativePlatform.WinUI => "WinUI 3",
NativePlatform.WPF => "WPF",
_ => "Unknown"
};
AnsiConsole.MarkupLine($"[green]✓ Detected:[/] {platformName}");
AnsiConsole.MarkupLine($"[dim]Project: {Markup.Escape(projectInfo.ProjectName)}[/]");
AnsiConsole.MarkupLine($"[dim]Namespace: {Markup.Escape(projectInfo.RootNamespace)}[/]");
if (projectInfo.Platform == NativePlatform.Unknown && !nonInteractive)
{
var selection = AnsiConsole.Prompt(
new SelectionPrompt<string>()
.Title("[yellow]Could not auto-detect platform. Please select:[/]")
.AddChoices(new[] { "MAUI", "Avalonia", "WinUI", "WPF" }));
projectInfo.Platform = selection switch
{
"MAUI" => NativePlatform.MAUI,
"Avalonia" => NativePlatform.Avalonia,
"WinUI" => NativePlatform.WinUI,
"WPF" => NativePlatform.WPF,
_ => NativePlatform.MAUI
};
}
else if (projectInfo.Platform == NativePlatform.Unknown)
{
// Default to MAUI in non-interactive mode
projectInfo.Platform = NativePlatform.MAUI;
AnsiConsole.MarkupLine("[yellow]Platform not detected, defaulting to MAUI[/]");
}
await LogoLoader.RunAsync("Setting up ShellUI Native...", async status =>
{
// Create Components/UI folder
status("Creating component folders...");
var componentsPath = Path.Combine(Directory.GetCurrentDirectory(), "Components", "UI");
var variantsPath = Path.Combine(componentsPath, "Variants");
Directory.CreateDirectory(componentsPath);
Directory.CreateDirectory(variantsPath);
AnsiConsole.MarkupLine($"[green]✓ Created:[/] Components/UI/");
// Install Shell utilities
status("Installing Shell utilities...");
await InstallShellUtilityAsync(projectInfo, componentsPath);
// Create configuration file
status("Creating configuration...");
var config = new ShellUINativeConfig
{
Style = style,
ComponentsPath = "Components/UI",
TargetPlatform = projectInfo.Platform,
Theme = new ThemeConfig
{
Enabled = true,
Style = "default",
SyncWithShellUI = true
}
};
var json = JsonSerializer.Serialize(config, new JsonSerializerOptions
{
WriteIndented = true
});
await File.WriteAllTextAsync(configPath, json);
AnsiConsole.MarkupLine($"[green]✓ Created:[/] shellui-native.json");
// Create theme resources (MAUI only for now)
if (projectInfo.Platform == NativePlatform.MAUI)
{
status("Creating theme resources...");
await CreateThemeResourcesAsync();
}
});
AnsiConsole.MarkupLine("\n[green]✓ ShellUI Native initialized successfully![/]");
AnsiConsole.MarkupLine("\n[blue]Next steps:[/]");
if (projectInfo.Platform == NativePlatform.Avalonia)
{
AnsiConsole.MarkupLine(" [dim]1. Load the theme in App.Initialize, after AvaloniaXamlLoader.Load(this):[/] Components.UI.ShellTheme.EnsureInitialized();");
AnsiConsole.MarkupLine($" [dim]2. Use the components in XAML:[/] xmlns:ui=\"using:{Markup.Escape(projectInfo.RootNamespace)}.Components.UI\"");
AnsiConsole.MarkupLine(" [dim]3. Add components:[/] shellui-native add theme-toggle");
}
else
{
AnsiConsole.MarkupLine(" [dim]1. Add components:[/] shellui-native add button");
AnsiConsole.MarkupLine(" [dim]2. Browse all:[/] shellui-native list");
}
}
private static async Task InstallShellUtilityAsync(ProjectInfo projectInfo, string componentsPath)
{
if (!ComponentRegistry.SupportsPlatform("shell", projectInfo.Platform))
{
AnsiConsole.MarkupLine(
$"[yellow]Shell utility has no template for {projectInfo.Platform} yet — skipping.[/] " +
$"[dim]Add components once per-platform templates land.[/]");
return;
}
var content = ComponentRegistry.GetComponentContent("shell", projectInfo.Platform);
if (content == null) return;
content = content.Replace("YourProjectNamespace", projectInfo.RootNamespace);
var filePath = Path.Combine(componentsPath, "Shell.cs");
await File.WriteAllTextAsync(filePath, content);
AnsiConsole.MarkupLine($"[green]✓ Installed:[/] Shell.cs");
}
private static async Task CreateThemeResourcesAsync()
{
var resourcesPath = Path.Combine(Directory.GetCurrentDirectory(), "Resources", "Styles");
Directory.CreateDirectory(resourcesPath);
var themePath = Path.Combine(resourcesPath, "ShellUITheme.xaml");
await File.WriteAllTextAsync(themePath, StyleTemplates.ThemeResourceDictionary);
AnsiConsole.MarkupLine($"[green]✓ Created:[/] Resources/Styles/ShellUITheme.xaml");
AnsiConsole.MarkupLine("[dim]Add this resource dictionary to your App.xaml MergedDictionaries[/]");
}
}