-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHostTemplates.cs
More file actions
354 lines (306 loc) · 12.9 KB
/
Copy pathHostTemplates.cs
File metadata and controls
354 lines (306 loc) · 12.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
using SimpleModule.Cli.Infrastructure;
namespace SimpleModule.Cli.Templates;
public sealed class HostTemplates
{
private const string BaseProjectName = "SimpleModule";
/// <summary>
/// Patterns to strip from the template Host .csproj when generating a new project.
/// Each pattern causes any line containing it to be removed.
/// </summary>
private static readonly string[] CsprojStripPatterns =
[
"modules", // Module ProjectReferences
"ServiceDefaults", // Aspire ServiceDefaults
"InternalsVisibleTo", // Test assembly visibility
"NoWarn",
"SM0011",
"SM0035",
"SM0038", // Suppressed analyzer warnings
"TODO", // Dev comments
"EntityFrameworkCore.Design", // EF migrations tooling
"<Import Project", // MSBuild targets import
@"framework\", // Framework ProjectReferences (replaced by PackageReferences)
"OutputItemType=", // Multi-line Generator ref attributes
"ReferenceOutputAssembly=",
];
/// <summary>
/// Transform the Host .csproj: strip module ProjectReferences, ServiceDefaults,
/// InternalsVisibleTo, NoWarn/SM0011/SM0035/SM0038/TODO lines, EF Core Design package,
/// and the Import Project line. Replace framework ProjectReferences with PackageReferences.
/// Replace SimpleModule with projectName.
/// </summary>
public static string HostCsproj(string projectName)
{
var lines = EmbeddedResourceReader.ReadTemplateLines(
"Templates.Host.SimpleModule.Host.csproj"
);
// Strip lines containing patterns that should not appear in a new project
lines.RemoveAll(line =>
CsprojStripPatterns.Any(p => line.Contains(p, StringComparison.Ordinal))
);
// Remove orphaned multi-line XML fragments (e.g., <ProjectReference\n/> after attribute stripping)
lines.RemoveAll(line =>
{
var trimmed = line.TrimStart();
return trimmed == "/>"
|| (
trimmed.StartsWith("<ProjectReference", StringComparison.Ordinal)
&& !trimmed.Contains("Include=", StringComparison.Ordinal)
);
});
// Remove empty PropertyGroup and ItemGroup elements
lines = RemoveEmptyXmlBlocks(lines, "PropertyGroup");
lines = RemoveEmptyXmlBlocks(lines, "ItemGroup");
lines = TemplateExtractor.CollapseBlankLines(lines);
var content = string.Join(Environment.NewLine, lines);
content = ReplaceProjectName(content, projectName);
// Insert framework PackageReferences before the closing </Project> tag
var packageRefs = """
<ItemGroup>
<PackageReference Include="SimpleModule.Hosting" />
<PackageReference Include="SimpleModule.Generator" OutputItemType="Analyzer" ReferenceOutputAssembly="false" PrivateAssets="all" />
</ItemGroup>
""";
content = content.Replace(
"</Project>",
packageRefs + Environment.NewLine + "</Project>",
StringComparison.Ordinal
);
return content;
}
/// <summary>
/// Copy Program.cs, remove ServiceDefaults, MapDefaultEndpoints, and Storage.Local lines.
/// Framework method names (AddSimpleModule, UseSimpleModule) are kept as-is.
/// </summary>
public static string ProgramCs()
{
var lines = EmbeddedResourceReader.ReadTemplateLines("Templates.Host.Program.cs");
lines.RemoveAll(line =>
line.Contains("ServiceDefaults", StringComparison.Ordinal)
|| line.Contains("MapDefaultEndpoints", StringComparison.Ordinal)
|| line.Contains("Storage.Local", StringComparison.Ordinal)
|| line.Contains("AddLocalStorage", StringComparison.Ordinal)
);
lines = TemplateExtractor.CollapseBlankLines(lines);
return string.Join(Environment.NewLine, lines);
}
/// <summary>
/// Copy index.html as-is.
/// </summary>
public static string IndexHtml()
{
return EmbeddedResourceReader.ReadTemplate("Templates.Host.wwwroot.index.html");
}
/// <summary>
/// Copy favicon.svg as-is. The host's index.html links <c>/favicon.svg</c> and
/// Program.cs maps <c>/favicon.ico</c> to it, so the scaffold must ship the file.
/// </summary>
public static string FaviconSvg()
{
return EmbeddedResourceReader.ReadTemplate("Templates.Host.wwwroot.favicon.svg");
}
/// <summary>
/// Copy app.tsx as-is.
/// </summary>
public static string AppTsx()
{
return EmbeddedResourceReader.ReadTemplate("Templates.Host.ClientApp.app.tsx");
}
/// <summary>
/// Copy vite.config.ts as-is.
/// </summary>
public static string ViteConfig()
{
return EmbeddedResourceReader.ReadTemplate("Templates.Host.ClientApp.vite.config.ts");
}
/// <summary>
/// Copy vite.dev.config.ts as-is.
/// </summary>
public static string ViteDevConfig()
{
return EmbeddedResourceReader.ReadTemplate("Templates.Host.ClientApp.vite.dev.config.ts");
}
/// <summary>
/// Copy validate-pages.mjs as-is.
/// </summary>
public static string ValidatePages()
{
return EmbeddedResourceReader.ReadTemplate("Templates.Host.ClientApp.validate-pages.mjs");
}
/// <summary>
/// Copy ClientApp/package.json, replace @simplemodule/app with @{projectName}/app.
/// </summary>
public static string ClientAppPackageJson(string projectName)
{
var content = EmbeddedResourceReader.ReadTemplate("Templates.Host.ClientApp.package.json");
return content.Replace(
"@simplemodule/app",
$"@{projectName.ToLowerInvariant()}/app",
StringComparison.Ordinal
);
}
/// <summary>
/// Return a clean Styles/app.css with tailwindcss import, theme import,
/// and @source directives for modules.
/// </summary>
/// <remarks>
/// The embedded template lives in <c>template/SimpleModule.Host/Styles/</c> and uses
/// monorepo-relative paths (<c>../../../packages/...</c>, <c>../../../modules/...</c>).
/// In a scaffolded project the layout is <c>src/<Project>.Host/Styles/</c>, so:
/// <list type="bullet">
/// <item>Framework packages live in <c>node_modules/@simplemodule/*</c> at the project root
/// (3 ups from Styles/) — installed via npm.</item>
/// <item>Modules live at <c>src/modules/</c> (2 ups from Styles/).</item>
/// <item><c>@source "./_scan/"</c> is already relative to Styles/ and is kept as-is:
/// SimpleModule.Hosting.targets stages every module's built <c>.pages.js</c> there, and
/// that is the only way Tailwind sees classes from packaged modules (and from module
/// components that live outside <c>Pages/</c>). Stripping it left scaffolded apps
/// missing those utility classes (#290).</item>
/// <item><c>@source "../../../docs/design-system/"</c> is dropped: it is a monorepo-only
/// root with no consumer equivalent (the monorepo tracks it through
/// <c>@(TailwindExtraSourceFiles)</c>), so in a scaffold it would dangle at a
/// directory that never exists — and would silently start scanning unrelated files
/// if one were ever created there.</item>
/// </list>
/// We rewrite each <c>@import</c>/<c>@source</c> directive directly rather than blindly
/// rewriting path prefixes, since the original substrings overlap.
/// </remarks>
public static string AppCss()
{
var lines = EmbeddedResourceReader.ReadTemplateLines("Templates.Host.Styles.app.css");
lines.RemoveAll(line => line.Contains("docs/design-system", StringComparison.Ordinal));
var result = new List<string>(lines.Count);
foreach (var line in lines)
{
var rewritten = line.Replace(
"../../../packages/SimpleModule.Theme.Default/theme.css",
"../../../node_modules/@simplemodule/theme-default/theme.css",
StringComparison.Ordinal
)
.Replace(
"../../../packages/SimpleModule.UI/",
"../../../node_modules/@simplemodule/ui/",
StringComparison.Ordinal
)
.Replace(
"../../../packages/SimpleModule.Client/",
"../../../node_modules/@simplemodule/client/",
StringComparison.Ordinal
)
.Replace("../../../modules/", "../../modules/", StringComparison.Ordinal);
result.Add(rewritten);
}
return string.Join(Environment.NewLine, result);
}
/// <summary>
/// Copy appsettings.json as-is.
/// </summary>
public static string AppSettings()
{
return EmbeddedResourceReader.ReadTemplate("Templates.Host.appsettings.json");
}
/// <summary>
/// Copy appsettings.Development.json as-is.
/// </summary>
public static string AppSettingsDevelopment()
{
return EmbeddedResourceReader.ReadTemplate("Templates.Host.appsettings.Development.json");
}
/// <summary>
/// Copy launchSettings.json, replace SimpleModule with projectName.
/// </summary>
public static string LaunchSettings(string projectName)
{
var content = EmbeddedResourceReader.ReadTemplate(
"Templates.Host.Properties.launchSettings.json"
);
return ReplaceProjectName(content, projectName);
}
// ── Helpers ────────────────────────────────────────────────────────
/// <summary>
/// Replaces "SimpleModule" with projectName in content, preserving framework names
/// (SimpleModule.Hosting, SimpleModule.Generator, SimpleModule.Core,
/// SimpleModule.Database, SimpleModule.DevTools).
/// </summary>
private static string ReplaceProjectName(string content, string projectName)
{
// Framework package/project names that must NOT be renamed
var frameworkNames = new[]
{
"SimpleModule.Hosting",
"SimpleModule.Generator",
"SimpleModule.Core",
"SimpleModule.Database",
"SimpleModule.DevTools",
};
// Protect framework names with placeholders
var placeholders = new (string Original, string Placeholder)[frameworkNames.Length];
for (var i = 0; i < frameworkNames.Length; i++)
{
placeholders[i] = (frameworkNames[i], $"%%FW_{i}%%");
content = content.Replace(
frameworkNames[i],
placeholders[i].Placeholder,
StringComparison.Ordinal
);
}
// Replace the project name
content = content.Replace(BaseProjectName, projectName, StringComparison.Ordinal);
// Restore framework names
for (var i = 0; i < placeholders.Length; i++)
{
content = content.Replace(
placeholders[i].Placeholder,
placeholders[i].Original,
StringComparison.Ordinal
);
}
return content;
}
/// <summary>
/// Removes XML element blocks (e.g., PropertyGroup, ItemGroup) that are empty
/// (contain only whitespace between opening and closing tags).
/// </summary>
private static List<string> RemoveEmptyXmlBlocks(List<string> lines, string elementName)
{
var result = new List<string>();
var openTag = $"<{elementName}";
var closeTag = $"</{elementName}>";
for (var i = 0; i < lines.Count; i++)
{
var trimmed = lines[i].TrimStart();
if (trimmed.StartsWith(openTag, StringComparison.Ordinal))
{
// Look ahead to see if the block is empty
var blockLines = new List<string> { lines[i] };
var j = i + 1;
var isEmpty = true;
while (j < lines.Count)
{
var innerTrimmed = lines[j].TrimStart();
blockLines.Add(lines[j]);
if (innerTrimmed.StartsWith(closeTag, StringComparison.Ordinal))
{
break;
}
if (!string.IsNullOrWhiteSpace(lines[j]))
{
isEmpty = false;
}
j++;
}
if (isEmpty && j < lines.Count)
{
// Skip the entire empty block
i = j;
continue;
}
// Not empty, add all lines normally
result.Add(lines[i]);
continue;
}
result.Add(lines[i]);
}
return result;
}
}