-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFrontend.cs
More file actions
213 lines (178 loc) · 8.31 KB
/
Frontend.cs
File metadata and controls
213 lines (178 loc) · 8.31 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
// Frontend command, decomposed:
// FrontendEnvironment owns scaffolding (build-frontend.sh template,
// CRLF→LF normalization, .gitattributes patching) and the
// (AutoYes × JsonMode × required) → (Create | Decline | Blocked) matrix.
// FrontendBuild owns the docker invocation; OS-shell wrapping is the
// process-runner port's concern.
// The CLI composition layer translates outcomes into StepResult fields.
using Spectre.Console;
namespace Dev;
internal interface IFrontendEnvironment
{
ScaffoldOutcome Prepare(string workspacePath, ScaffoldOptions options);
}
internal interface IFrontendBuild
{
BuildOutcome Run(string workspacePath, BuildOptions options);
}
internal interface IConfirmationPrompt
{
bool Confirm(string question);
}
internal sealed class AnsiConsolePrompter : IConfirmationPrompt
{
public bool Confirm(string question) => AnsiConsole.Prompt(new ConfirmationPrompt(question));
}
internal enum ScaffoldState { Ready, BlockedByPrompt, UserDeclined, Warning }
internal sealed record Mutation(string File, string Action, string? Detail = null);
internal sealed record ScaffoldOutcome(
ScaffoldState State,
IReadOnlyList<Mutation> Mutations,
IReadOnlyList<string> Warnings,
string? BlockingCode,
string? BlockingMessage);
internal sealed record ScaffoldOptions(
bool AutoYes,
bool JsonMode,
bool DryRun,
IConfirmationPrompt Prompter,
IFileSystem Fs,
Action<string> Log);
internal sealed record BuildOutcome(
string Image,
int ExitCode,
IReadOnlyList<string> StdoutTail,
IReadOnlyList<string> StderrTail);
internal sealed record BuildOptions(string Image, IProcessRunner Runner, RunContext Ctx);
internal sealed class FrontendEnvironment : IFrontendEnvironment
{
public ScaffoldOutcome Prepare(string workspacePath, ScaffoldOptions options)
{
var mutations = new List<Mutation>();
var warnings = new List<string>();
var scriptOutcome = EnsureBuildScript(workspacePath, options, mutations);
if (scriptOutcome is not null)
return scriptOutcome;
var attrsState = EnsureGitAttributes(workspacePath, options, mutations, warnings);
return new ScaffoldOutcome(attrsState, mutations, warnings, null, null);
}
private static ScaffoldOutcome? EnsureBuildScript(
string workspacePath, ScaffoldOptions opts, List<Mutation> mutations)
{
var buildFile = Path.Combine(workspacePath, "build-frontend.sh");
if (opts.Fs.FileExists(buildFile))
{
var content = opts.Fs.ReadAllText(buildFile);
if (content.Contains("\r\n"))
{
opts.Log($"[yellow]Converting[/] {Path.GetFileName(buildFile)} [yellow]to LF line endings...[/]");
if (!opts.DryRun) opts.Fs.WriteAllText(buildFile, content.Replace("\r\n", "\n"));
mutations.Add(new Mutation(buildFile, "crlf_to_lf"));
}
return null;
}
opts.Log($"[red]No {Path.GetFileName(buildFile)} file found in the current directory.[/]");
switch (ResolveCreate(opts, required: true))
{
case CreateDecision.Blocked:
return new ScaffoldOutcome(
ScaffoldState.BlockedByPrompt,
mutations, Array.Empty<string>(),
"interaction_required",
"build-frontend.sh is missing; re-run with --yes to create it.");
case CreateDecision.Decline:
opts.Log("[red]Aborting.[/]");
return new ScaffoldOutcome(
ScaffoldState.UserDeclined,
mutations, Array.Empty<string>(),
"user_aborted",
"User declined to create build-frontend.sh.");
case CreateDecision.Create:
default:
opts.Log($"[green]Creating[/] {Path.GetFileName(buildFile)} [green]file in the current directory...[/]");
var trimmed = workspacePath.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);
var folderName = Path.GetFileName(trimmed);
if (!opts.DryRun) opts.Fs.WriteAllText(buildFile, BuildScriptTemplate(folderName));
mutations.Add(new Mutation(buildFile, "created"));
return null;
}
}
private static ScaffoldState EnsureGitAttributes(
string workspacePath, ScaffoldOptions opts, List<Mutation> mutations, List<string> warnings)
{
var attributesFile = Path.Combine(workspacePath, ".gitattributes");
if (opts.Fs.FileExists(attributesFile))
{
var content = opts.Fs.ReadAllText(attributesFile);
if (!content.Contains("*.sh text eol=lf"))
{
opts.Log($"[yellow]Adding LF line endings for bash files to {Path.GetFileName(attributesFile)}...[/]");
var separator = content.Length == 0 || content.EndsWith('\n') ? string.Empty : "\n";
if (!opts.DryRun) opts.Fs.WriteAllText(attributesFile, content + separator + "*.sh text eol=lf\n");
mutations.Add(new Mutation(attributesFile, "appended", "*.sh text eol=lf"));
}
return ScaffoldState.Ready;
}
opts.Log($"[red]No {Path.GetFileName(attributesFile)} file found in the current directory.[/]");
switch (ResolveCreate(opts, required: false))
{
case CreateDecision.Create:
opts.Log($"[green]Creating[/] {Path.GetFileName(attributesFile)} [green]file in the current directory...[/]");
if (!opts.DryRun) opts.Fs.WriteAllText(attributesFile, GitAttributesTemplate());
mutations.Add(new Mutation(attributesFile, "created"));
return ScaffoldState.Ready;
default: // Decline (interactive decline or json without --yes)
opts.Log("[yellow]Ignoring.[/]");
warnings.Add("gitattributes_missing");
return ScaffoldState.Warning;
}
}
private enum CreateDecision { Create, Decline, Blocked }
private static CreateDecision ResolveCreate(ScaffoldOptions opts, bool required)
{
if (opts.AutoYes) return CreateDecision.Create;
if (opts.JsonMode) return required ? CreateDecision.Blocked : CreateDecision.Decline;
return opts.Prompter.Confirm("Do you want to create it?")
? CreateDecision.Create
: CreateDecision.Decline;
}
private static string BuildScriptTemplate(string folderName)
{
var script = $$"""
#!/usr/bin/env bash
set -euo pipefail
# enter your frontend folder, if any
cd "{{folderName}}/"
# install dependencies
npm ci
# compile Sass → CSS
find wwwroot -type f -name "*.scss" -print -execdir sh -c 'sass "{}:${1%.scss}.css"' _ {} \;
# transpile TypeScript
tsc --project ./tsconfig.json
# run any additional build steps
npm run build
""";
return script.Replace("\r\n", "\n");
}
private static string GitAttributesTemplate()
=> "# Set default behavior to automatically normalize line endings.\n* text=auto\n# Explicitly declare text files we want to always be normalized and converted to native line endings on checkout.\n*.sh text eol=lf";
}
internal sealed class FrontendBuild : IFrontendBuild
{
public BuildOutcome Run(string workspacePath, BuildOptions options)
{
// Invoke docker directly with an argv list rather than building a shell command:
// workspacePath comes from the caller's working directory and could contain
// quotes, $(…), or backticks that would otherwise break out of a `bash -c "…"`
// wrapper. ArgumentList delivers each token verbatim to the docker process.
var argv = new[]
{
"run", "--rm",
"-v", $"{workspacePath}:/src",
"-w", "/src",
options.Image,
};
var result = options.Runner.Run(ProcSpec.ExecArgs("docker", argv), options.Ctx);
return new BuildOutcome(options.Image, result.ExitCode, result.StdoutTail, result.StderrTail);
}
}