Repository navigation
Expand file tree
/
Copy pathbuild.fsx
More file actions
387 lines (314 loc) · 13.5 KB
/
Copy pathbuild.fsx
File metadata and controls
387 lines (314 loc) · 13.5 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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#r "nuget: Fun.Build, 1.1.17"
#r "nuget: Fake.DotNet.AssemblyInfoFile"
open Fun.Build
open Fake.IO
open Fake.DotNet
type CliRuntime =
{ Rid: string
NpmDirectory: string
BinaryName: string
NativeLibraryName: string }
let cliRuntimes =
[ { Rid = "win-x64"
NpmDirectory = "clippit-win32-x64"
BinaryName = "clippit.exe"
NativeLibraryName = "libSkiaSharp.dll" }
{ Rid = "osx-x64"
NpmDirectory = "clippit-darwin-x64"
BinaryName = "clippit"
NativeLibraryName = "libSkiaSharp.dylib" }
{ Rid = "osx-arm64"
NpmDirectory = "clippit-darwin-arm64"
BinaryName = "clippit"
NativeLibraryName = "libSkiaSharp.dylib" }
{ Rid = "linux-x64"
NpmDirectory = "clippit-linux-x64"
BinaryName = "clippit"
NativeLibraryName = "libSkiaSharp.so" }
{ Rid = "linux-arm64"
NpmDirectory = "clippit-linux-arm64"
BinaryName = "clippit"
NativeLibraryName = "libSkiaSharp.so" }
{ Rid = "win-arm64"
NpmDirectory = "clippit-win32-arm64"
BinaryName = "clippit.exe"
NativeLibraryName = "libSkiaSharp.dll" } ]
let requestedRids =
let value = System.Environment.GetEnvironmentVariable("CLIPPIT_PUBLISH_RIDS")
if System.String.IsNullOrWhiteSpace(value) then
cliRuntimes
else
value.Split(
',',
System.StringSplitOptions.RemoveEmptyEntries
||| System.StringSplitOptions.TrimEntries
)
|> Array.map (fun rid ->
cliRuntimes
|> List.tryFind (fun r -> r.Rid = rid)
|> Option.defaultWith (fun () -> failwith $"Unsupported CLIPPIT_PUBLISH_RIDS value: {rid}"))
|> Array.toList
let version =
Changelog.GetLastVersion __SOURCE_DIRECTORY__
|> Option.defaultWith (fun () -> failwith "Version is not found")
let allCliRuntimesRequested =
let requested = requestedRids |> List.map _.Rid |> Set.ofList
let supported = cliRuntimes |> List.map _.Rid |> Set.ofList
requested = supported
let isGitHubActions =
System.String.Equals(
System.Environment.GetEnvironmentVariable("GITHUB_ACTIONS"),
"true",
System.StringComparison.OrdinalIgnoreCase
)
let withCiMsBuildLogger (command: string) =
if isGitHubActions then
$"{command} -clp:ErrorsOnly"
else
command
let testFramework = System.Environment.GetEnvironmentVariable("CLIPPIT_TEST_FRAMEWORK")
let testCommands =
// CI runs one framework per matrix job; local builds test both sequentially to avoid shared temp output collisions.
let command =
if isGitHubActions then
"test --project Clippit.Tests/ -c Release --no-build"
else
"test --project Clippit.Tests/"
match testFramework with
| null
| "" -> [ $"{command} -f net10.0"; $"{command} -f net11.0" ]
| framework -> [ $"{command} -f {framework}" ]
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
/// Copy a native binary and its runtime library from publish output into the
/// matching npm platform package directory, making the binary executable on Unix.
let copyNativeAssets (runtime: CliRuntime) =
let rid = runtime.Rid
let npmPkgDir = runtime.NpmDirectory
let binName = runtime.BinaryName
let src = System.IO.Path.Combine(__SOURCE_DIRECTORY__, "bin", "cli", rid, binName)
let destDir = System.IO.Path.Combine(__SOURCE_DIRECTORY__, "npm", npmPkgDir)
let dest = System.IO.Path.Combine(destDir, binName)
let nativeLibName = runtime.NativeLibraryName
let nativeLibSrc = System.IO.Path.Combine(__SOURCE_DIRECTORY__, "bin", "cli", rid, nativeLibName)
let nativeLibDest = System.IO.Path.Combine(destDir, nativeLibName)
if not (System.IO.File.Exists(src)) then
failwith $"Native binary not found for {rid}: {src}"
Shell.copyFile dest src
// Make executable on Unix
if
binName <> "clippit.exe"
&& not (
System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(
System.Runtime.InteropServices.OSPlatform.Windows
)
)
then
let chmod = System.Diagnostics.Process.Start("chmod", $"+x \"{dest}\"")
chmod.WaitForExit()
if chmod.ExitCode <> 0 then
failwith $"chmod failed ({chmod.ExitCode}) for {dest}"
if not (System.IO.File.Exists(nativeLibSrc)) then
failwith $"Native library not found for {rid}: {nativeLibSrc}"
Shell.copyFile nativeLibDest nativeLibSrc
/// Patch package.json version fields for npm pack. Platform packages only need
/// their top-level version; the wrapper also has clippit-* optional dependencies
/// that must match the same published version.
let patchNpmVersion (pkgJsonPath: string) (v: string) =
let content = System.IO.File.ReadAllText(pkgJsonPath)
// Pass 1 — top-level version field
let pass1 =
System.Text.RegularExpressions.Regex.Replace(content, "\"version\":\\s*\"[^\"]+\"", $"\"version\": \"{v}\"")
// Pass 2 — every "pkg-name": "x.y.z" pair anywhere in the file where the
// value looks like a semver string. Scoped to optionalDependencies by the
// fact that other string values in the file are not bare semver strings.
// Pattern: "some-package-name": "digits..."
let pass2 =
System.Text.RegularExpressions.Regex.Replace(
pass1,
"(\"(?:@[^\"/]+/)?clippit(?:-bin)?-[^\"]+\"\\s*:\\s*)\"[^\"]+\"",
$"$1\"{v}\""
)
System.IO.File.WriteAllText(pkgJsonPath, pass2)
let withOriginalFiles (paths: string list) (action: unit -> unit) =
let originals =
paths |> List.map (fun path -> path, System.IO.File.ReadAllText(path))
try
action ()
finally
for path, content in originals do
System.IO.File.WriteAllText(path, content)
let runRequiredCommand (workingDir: string) (fileName: string) (arguments: string) =
let proc =
System.Diagnostics.Process.Start(
System.Diagnostics.ProcessStartInfo(
fileName,
arguments,
WorkingDirectory = workingDir,
UseShellExecute = false
)
)
proc.WaitForExit()
if proc.ExitCode <> 0 then
failwith $"Command failed ({proc.ExitCode}): {fileName} {arguments}"
let publishCliBinaries () =
// NativeAOT is target-toolchain dependent. Set CLIPPIT_PUBLISH_RIDS
// to a comma-separated subset when building locally on a host that cannot
// produce every RID. CI builds each RID on its native runner.
for runtime in requestedRids do
runRequiredCommand
__SOURCE_DIRECTORY__
"dotnet"
(withCiMsBuildLogger
$"publish Clippit.Cli/Clippit.Cli.csproj -f net11.0 -c Release -r {runtime.Rid} --self-contained -p:NativeAot=true -o bin/cli/{runtime.Rid}")
let packNpmPackages () =
if not allCliRuntimesRequested then
failwith
"NPM wrapper package requires all CLI runtimes. Set CLIPPIT_PUBLISH_RIDS=win-x64,win-arm64,osx-x64,osx-arm64,linux-x64,linux-arm64."
let npmDir = System.IO.Path.Combine(__SOURCE_DIRECTORY__, "npm")
let allPackages = [ "clippit" ] @ (cliRuntimes |> List.map _.NpmDirectory)
let packageJsonFiles =
allPackages
|> List.map (fun pkg -> System.IO.Path.Combine(npmDir, pkg, "package.json"))
withOriginalFiles packageJsonFiles (fun () ->
try
// Copy binaries into platform npm packages.
for runtime in requestedRids do
copyNativeAssets runtime
// Patch version in all package.json files only for npm pack.
for pkgJson in packageJsonFiles do
patchNpmVersion pkgJson version.Version
// Pack platform packages, then the wrapper.
Shell.mkdir (System.IO.Path.Combine(__SOURCE_DIRECTORY__, "bin", "npm"))
for pkg in (requestedRids |> List.map _.NpmDirectory) @ [ "clippit" ] do
let pkgDir = System.IO.Path.Combine(npmDir, pkg)
let outDir = System.IO.Path.Combine(__SOURCE_DIRECTORY__, "bin", "npm")
runRequiredCommand pkgDir "npm" $"pack --pack-destination {outDir}"
finally
for runtime in requestedRids do
let generated =
System.IO.Path.Combine(npmDir, runtime.NpmDirectory, runtime.BinaryName)
if System.IO.File.Exists(generated) then
System.IO.File.Delete(generated)
let generatedLibrary =
System.IO.Path.Combine(npmDir, runtime.NpmDirectory, runtime.NativeLibraryName)
if System.IO.File.Exists(generatedLibrary) then
System.IO.File.Delete(generatedLibrary))
// ---------------------------------------------------------------------------
// Build pipeline
// ---------------------------------------------------------------------------
pipeline "build" {
workingDir __SOURCE_DIRECTORY__
runBeforeEachStage (fun ctx ->
if ctx.GetStageLevel() = 0 then
printfn $"::group::{ctx.Name}")
runAfterEachStage (fun ctx ->
if ctx.GetStageLevel() = 0 then
printfn "::endgroup::")
stage "Check environment" {
run "dotnet tool restore"
run "dotnet restore"
}
stage "Check Formatting" { run "dotnet csharpier check ." }
stage "Clean" {
run (fun _ ->
Shell.mkdir "bin"
Shell.cleanDir "bin")
run "dotnet clean"
}
stage "AssemblyInfo" {
run (fun _ ->
let fileName = "Clippit/Properties/AssemblyInfo.g.cs"
AssemblyInfoFile.createCSharp
fileName
[ AssemblyInfo.Title "Clippit"
AssemblyInfo.Product "Clippit"
AssemblyInfo.Description "Fresh PowerTools for OpenXml"
AssemblyInfo.Version version.Version
AssemblyInfo.FileVersion version.Version ]
Shell.copyFile "docs/api/CHANGELOG.md" "CHANGELOG.md")
}
stage "Build" { run (withCiMsBuildLogger "dotnet build Clippit.slnx -c Release") }
stage "RunTests" {
run (fun _ ->
testCommands
|> List.iter (runRequiredCommand __SOURCE_DIRECTORY__ "dotnet"))
}
stage "NuGet" {
run (fun ctx ->
let releaseNotes = version.ReleaseNotes.Trim()
let releaseNotesFile = "bin/RELEASE_NOTES.md"
System.IO.File.WriteAllText(releaseNotesFile, releaseNotes)
let releaseTitle =
match version.DateTime with
| Some d ->
let dateFormat = "MMMM d, yyyy"
$"{version.Version} - {d.ToString(dateFormat, System.Globalization.CultureInfo.InvariantCulture)}"
| None -> version.Version
System.IO.File.WriteAllText("bin/RELEASE_TITLE", releaseTitle)
let targetsPath = "Clippit/Directory.Build.targets"
System.IO.File.WriteAllText(
targetsPath,
$"""<Project>
<PropertyGroup>
<PackageReleaseNotes><![CDATA[{releaseNotes}]]></PackageReleaseNotes>
</PropertyGroup>
</Project>"""
)
try
ctx.RunCommand(
withCiMsBuildLogger
$"dotnet pack Clippit/Clippit.csproj -o bin/ -p:PackageVersion={version.Version}"
)
finally
System.IO.File.Delete(targetsPath))
}
stage "PackCliTool" {
run (fun ctx ->
ctx.RunCommand(
withCiMsBuildLogger
$"dotnet pack Clippit.Cli/Clippit.Cli.csproj -o bin/ -p:PackageVersion={version.Version} -p:CreateRidSpecificToolPackages=false"
))
run (fun ctx ->
ctx.RunCommand(
withCiMsBuildLogger
$"dotnet pack Clippit.Cli/Clippit.Cli.csproj -o bin/ -r any -p:PackageVersion={version.Version}"
))
}
runIfOnlySpecified
}
// ---------------------------------------------------------------------------
// Publish pipeline — produces NativeAOT self-contained binaries + npm packages.
// For CI release packaging from downloaded native artifacts, use the pack-npm
// pipeline instead.
// ---------------------------------------------------------------------------
pipeline "publish" {
workingDir __SOURCE_DIRECTORY__
runBeforeEachStage (fun ctx ->
if ctx.GetStageLevel() = 0 then
printfn $"::group::{ctx.Name}")
runAfterEachStage (fun ctx ->
if ctx.GetStageLevel() = 0 then
printfn "::endgroup::")
stage "PublishCli" { run (fun _ -> publishCliBinaries ()) }
stage "PackNpm" { run (fun _ -> packNpmPackages ()) }
runIfOnlySpecified
}
// ---------------------------------------------------------------------------
// npm packaging pipeline — packs npm packages from binaries already present at
// bin/cli/<rid>/<binary>. Used by GitHub Actions after downloading artifacts
// produced on native OS runners.
// ---------------------------------------------------------------------------
pipeline "pack-npm" {
workingDir __SOURCE_DIRECTORY__
runBeforeEachStage (fun ctx ->
if ctx.GetStageLevel() = 0 then
printfn $"::group::{ctx.Name}")
runAfterEachStage (fun ctx ->
if ctx.GetStageLevel() = 0 then
printfn "::endgroup::")
stage "PackNpm" { run (fun _ -> packNpmPackages ()) }
runIfOnlySpecified
}
tryPrintPipelineCommandHelp ()