-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathProgram.cs
More file actions
365 lines (327 loc) · 13.6 KB
/
Program.cs
File metadata and controls
365 lines (327 loc) · 13.6 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
// AI-HINT: Top-level Program.cs for the `mur` CLI tool.
// Dispatches subcommands: loc (localization), --create (project scaffolding),
// --skill (output SKILL.md for AI agents), --version, --help.
// Uses top-level statements pattern. Entry point returns int exit code.
using System.Reflection;
var assembly = Assembly.GetExecutingAssembly();
var version = assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion
?? assembly.GetName().Version?.ToString() ?? "0.1.0";
if (args.Length == 0)
{
ShowHelp();
return 0;
}
var arg = args[0].ToLowerInvariant();
if (arg is "--help" or "-h" or "-?")
{
ShowHelp();
return 0;
}
if (arg is "--version" or "-v")
{
Console.WriteLine($"mur {version}");
return 0;
}
if (arg == "--skill")
{
return ShowSkill();
}
if (arg == "--api" || arg == "api")
{
return ShowApi();
}
if (arg == "--regen-api" || arg == "regen-api")
{
return RegenApi();
}
if (arg == "--create")
{
if (args.Length < 2)
{
Console.Error.WriteLine("Error: --create requires a project name.");
Console.Error.WriteLine("Usage: mur --create <ProjectName>");
return 1;
}
return CreateProject(args[1]);
}
if (arg == "loc")
{
return Microsoft.UI.Reactor.Cli.Loc.LocCommand.Run(args.Skip(1).ToArray());
}
if (arg == "docs")
{
return Microsoft.UI.Reactor.Cli.Docs.DocsCommand.Run(args.Skip(1).ToArray());
}
if (arg == "devtools")
{
return Microsoft.UI.Reactor.Cli.Devtools.DevtoolsSupervisor.Run(args.Skip(1).ToArray());
}
if (arg == "check")
{
return Microsoft.UI.Reactor.Cli.Check.CheckCommand.Run(args.Skip(1).ToArray());
}
if (arg == "pack-local")
{
return Microsoft.UI.Reactor.Cli.Pack.PackLocalCommand.Run(args.Skip(1).ToArray());
}
if (arg == "clean-local")
{
return Microsoft.UI.Reactor.Cli.Pack.CleanLocalCommand.Run(args.Skip(1).ToArray());
}
Console.Error.WriteLine($"Unknown option: {args[0]}");
Console.Error.WriteLine();
ShowHelp();
return 1;
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
void ShowHelp()
{
Console.WriteLine($"mur {version} — Microsoft.UI.Reactor (Functional UI) CLI");
Console.WriteLine();
Console.WriteLine("Usage: mur [option]");
Console.WriteLine();
Console.WriteLine("Options:");
Console.WriteLine(" --help, -h Show this help message");
Console.WriteLine(" --version, -v Show version information");
Console.WriteLine(" --skill Print the SKILL.md AI reference to stdout");
Console.WriteLine(" --api Print the reactor.api.txt signatures index to stdout");
Console.WriteLine(" --regen-api Regenerate skills/reactor.api.txt from the built Reactor.dll");
Console.WriteLine(" --create <name> Scaffold a new Reactor project");
Console.WriteLine();
Console.WriteLine("Commands:");
Console.WriteLine(" loc extract Extract localizable strings from source files");
Console.WriteLine(" loc translate AI-translate .resw files to target locales");
Console.WriteLine(" loc validate Check ICU syntax and parameter consistency");
Console.WriteLine(" loc status Show translation coverage per locale");
Console.WriteLine(" loc prune Find unused localization keys");
Console.WriteLine(" docs compile Compile documentation from templates and doc apps");
Console.WriteLine(" devtools Launch project with --devtools run and supervise reloads");
Console.WriteLine(" check [path] Build and emit one-line diagnostics with skill-file pointers");
Console.WriteLine(" pack-local Pack the in-source framework to <repo>/local-nupkgs/ as 0.0.0-local");
Console.WriteLine(" clean-local Remove local packages, NuGet cache entries, and templates");
}
int ShowSkill()
{
using var stream = assembly.GetManifestResourceStream("SKILL.md");
if (stream is null)
{
Console.Error.WriteLine("Error: Embedded SKILL.md resource not found.");
return 1;
}
using var reader = new StreamReader(stream);
Console.Write(reader.ReadToEnd());
return 0;
}
int ShowApi()
{
// Prefer the embedded copy (always up-to-date with this `mur` binary).
// Fallback path: a NuGet consumer with no `mur` install can read the file
// directly from their package cache:
// %USERPROFILE%\.nuget\packages\microsoft.ui.reactor\<version>\agentkit\reactor.api.txt
using var stream = assembly.GetManifestResourceStream("reactor.api.txt");
if (stream is null)
{
Console.Error.WriteLine("Error: Embedded reactor.api.txt resource not found.");
Console.Error.WriteLine("Run `mur --regen-api` (selfhost) or read the file from the NuGet package cache.");
return 1;
}
using var reader = new StreamReader(stream);
Console.Write(reader.ReadToEnd());
return 0;
}
int RegenApi()
{
// Walk up from the running mur binary to find the repo root (the dir that
// contains src/Reactor and tools/Reactor.SignaturesGen). Then invoke the
// generator project. Selfhost only — NuGet consumers don't have the source.
var dir = AppContext.BaseDirectory;
string? repoRoot = null;
for (var d = new DirectoryInfo(dir); d is not null; d = d.Parent)
{
if (Directory.Exists(Path.Combine(d.FullName, "tools", "Reactor.SignaturesGen"))
&& Directory.Exists(Path.Combine(d.FullName, "src", "Reactor")))
{
repoRoot = d.FullName;
break;
}
}
if (repoRoot is null)
{
Console.Error.WriteLine("Error: --regen-api must be run from a Reactor source checkout (could not locate tools/Reactor.SignaturesGen).");
return 1;
}
// Build the generator project — its AfterBuild target writes
// skills/reactor.api.txt automatically.
var psi = new System.Diagnostics.ProcessStartInfo("dotnet")
{
WorkingDirectory = repoRoot,
UseShellExecute = false,
};
psi.ArgumentList.Add("build");
psi.ArgumentList.Add(Path.Combine("tools", "Reactor.SignaturesGen", "Reactor.SignaturesGen.csproj"));
psi.ArgumentList.Add("--nologo");
psi.ArgumentList.Add("-v:m");
// WinUI projects require an explicit Platform — match the host arch so
// the AfterBuild target can execute the freshly-built apphost.
var arch = System.Runtime.InteropServices.RuntimeInformation.ProcessArchitecture switch
{
System.Runtime.InteropServices.Architecture.Arm64 => "ARM64",
System.Runtime.InteropServices.Architecture.X64 => "x64",
_ => null,
};
if (arch is not null) psi.ArgumentList.Add($"-p:Platform={arch}");
using var proc = System.Diagnostics.Process.Start(psi)!;
proc.WaitForExit();
return proc.ExitCode;
}
int CreateProject(string name)
{
if (!global::System.Text.RegularExpressions.Regex.IsMatch(name, @"^[A-Za-z_][A-Za-z0-9_\.]*$"))
{
Console.Error.WriteLine($"Error: Invalid project name '{name}'. Use only letters, digits, underscores, and dots. Must start with a letter or underscore.");
return 1;
}
var dir = Path.Combine(Directory.GetCurrentDirectory(), name);
if (Directory.Exists(dir))
{
Console.Error.WriteLine($"Error: Directory '{name}' already exists.");
return 1;
}
Directory.CreateDirectory(dir);
var appGuid = Guid.NewGuid().ToString("D").ToUpperInvariant();
var patchGuid = Guid.NewGuid().ToString("D").ToUpperInvariant();
File.WriteAllText(Path.Combine(dir, "Program.cs"), GenerateProgram(name));
File.WriteAllText(Path.Combine(dir, $"{name}.csproj"), GenerateCsproj());
File.WriteAllText(Path.Combine(dir, $"{name}.sln"), GenerateSln(name, appGuid, patchGuid));
Console.WriteLine($"Created Reactor project '{name}' in .{Path.DirectorySeparatorChar}{name}{Path.DirectorySeparatorChar}");
Console.WriteLine();
Console.WriteLine(" Files:");
Console.WriteLine($" {name}{Path.DirectorySeparatorChar}{name}.sln");
Console.WriteLine($" {name}{Path.DirectorySeparatorChar}{name}.csproj");
Console.WriteLine($" {name}{Path.DirectorySeparatorChar}Program.cs");
Console.WriteLine();
Console.WriteLine("NOTE: The generated .sln assumes this project is a sibling of the Reactor directory:");
Console.WriteLine($" parent/");
Console.WriteLine($" src/Reactor/Reactor.csproj");
Console.WriteLine($" {name}/{name}.csproj");
Console.WriteLine();
Console.WriteLine("To build and run:");
Console.WriteLine($" cd {name}");
Console.WriteLine($" dotnet build {name}.sln");
Console.WriteLine($" dotnet run");
Console.WriteLine();
Console.WriteLine("To develop with an AI agent (MCP) and a VS Code preview panel:");
Console.WriteLine($" mur devtools # prints MCP_ENDPOINT to stdout");
Console.WriteLine($" mur devtools --mcp-port 9000 # pin the port across reloads");
Console.WriteLine($" mur devtools --print-config # emit MCP config for Claude Code / VS Code / Copilot");
return 0;
}
// ---------------------------------------------------------------------------
// Template generators
// ---------------------------------------------------------------------------
string GenerateProgram(string name) =>
$$"""
using System;
using Microsoft.UI.Reactor;
using Microsoft.UI.Reactor.Core;
using Microsoft.UI.Reactor.Layout; // FlexDirection, FlexJustify, FlexAlign
using Microsoft.UI.Xaml; // Thickness, HorizontalAlignment, VerticalAlignment
using Microsoft.UI.Xaml.Controls; // Orientation, InfoBarSeverity, etc.
using static Microsoft.UI.Reactor.Factories;
ReactorApp.Run<App>("{{name}}", width: 800, height: 600
#if DEBUG
// Enables `mur devtools` (agent tools over MCP + VS Code preview panel).
// Run `mur devtools` to launch with component switching, hot reload, and
// an MCP endpoint printed to stdout (MCP_ENDPOINT=http://127.0.0.1:NNNN/mcp).
, devtools: true
#endif
);
class App : Component
{
public override Element Render()
{
var titleBar = TitleBar("{{name}}").Flex(shrink: 0);
var content = Border(
TextBlock("Hello, World!").FontSize(24)
).Padding(24);
return FlexColumn(titleBar, content);
}
}
""";
string GenerateCsproj() =>
"""
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net10.0-windows10.0.22621.0</TargetFramework>
<Platforms>x64;ARM64</Platforms>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<UseWinUI>true</UseWinUI>
<WindowsPackageType>None</WindowsPackageType>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.WindowsAppSDK" Version="2.0.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\src\Reactor\Reactor.csproj" />
</ItemGroup>
</Project>
""";
/// <summary>
/// Generates a .sln that references Reactor via a relative project path.
/// This assumes the new project directory is a sibling of the Reactor/ directory.
/// When Reactor is published as a NuGet package, this should switch to a PackageReference.
/// </summary>
string GenerateSln(string name, string appGuid, string patchGuid)
{
const string csharpGuid = "{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}";
var ag = $"{{{appGuid}}}";
var pg = $"{{{patchGuid}}}";
var t1 = "\t";
var t2 = "\t\t";
return string.Join("\r\n",
"",
"Microsoft Visual Studio Solution File, Format Version 12.00",
"# Visual Studio Version 18",
"VisualStudioVersion = 18.4.11605.240",
"MinimumVisualStudioVersion = 10.0.40219.1",
$"Project(\"{csharpGuid}\") = \"{name}\", \"{name}.csproj\", \"{ag}\"",
"EndProject",
$"Project(\"{csharpGuid}\") = \"Reactor\", \"..\\src\\Reactor\\Reactor.csproj\", \"{pg}\"",
"EndProject",
"Global",
// x64 is listed first so `dotnet build` with no -p:Platform picks x64,
// which matches the vast majority of host machines today. ARM64 hosts
// can still build ARM64 explicitly with -p:Platform=ARM64.
$"{t1}GlobalSection(SolutionConfigurationPlatforms) = preSolution",
$"{t2}Debug|x64 = Debug|x64",
$"{t2}Debug|ARM64 = Debug|ARM64",
$"{t2}Release|x64 = Release|x64",
$"{t2}Release|ARM64 = Release|ARM64",
$"{t1}EndGlobalSection",
$"{t1}GlobalSection(ProjectConfigurationPlatforms) = postSolution",
$"{t2}{ag}.Debug|x64.ActiveCfg = Debug|x64",
$"{t2}{ag}.Debug|x64.Build.0 = Debug|x64",
$"{t2}{ag}.Debug|ARM64.ActiveCfg = Debug|ARM64",
$"{t2}{ag}.Debug|ARM64.Build.0 = Debug|ARM64",
$"{t2}{ag}.Release|x64.ActiveCfg = Release|x64",
$"{t2}{ag}.Release|x64.Build.0 = Release|x64",
$"{t2}{ag}.Release|ARM64.ActiveCfg = Release|ARM64",
$"{t2}{ag}.Release|ARM64.Build.0 = Release|ARM64",
$"{t2}{pg}.Debug|x64.ActiveCfg = Debug|x64",
$"{t2}{pg}.Debug|x64.Build.0 = Debug|x64",
$"{t2}{pg}.Debug|ARM64.ActiveCfg = Debug|ARM64",
$"{t2}{pg}.Debug|ARM64.Build.0 = Debug|ARM64",
$"{t2}{pg}.Release|x64.ActiveCfg = Release|x64",
$"{t2}{pg}.Release|x64.Build.0 = Release|x64",
$"{t2}{pg}.Release|ARM64.ActiveCfg = Release|ARM64",
$"{t2}{pg}.Release|ARM64.Build.0 = Release|ARM64",
$"{t1}EndGlobalSection",
$"{t1}GlobalSection(SolutionProperties) = preSolution",
$"{t2}HideSolutionNode = FALSE",
$"{t1}EndGlobalSection",
"EndGlobal",
"");
}