-
Notifications
You must be signed in to change notification settings - Fork 7
/
build.fsx
348 lines (313 loc) · 12.2 KB
/
build.fsx
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
#r "nuget: Fun.Build"
#r "nuget: Fake.IO.FileSystem, 6.0.0"
#r "nuget: NuGet.Packaging"
#r "nuget: NuGet.Protocol"
#load "docs.fsx"
open System
open System.IO
open System.Threading
open Fun.Build
open Fun.Build.Github
open Fun.Result
open Fake.IO
open Fake.IO.FileSystemOperators
open Fake.IO.Globbing.Operators
open NuGet.Common
open NuGet.Protocol
open NuGet.Protocol.Core.Types
let options = {|
NUGET_API_KEY = EnvArg.Create("NUGET_API_KEY", description = "NUGET api key")
WASM = CmdArg.Create(longName = "--wasm", description = "Run in blazor wasm mode, By default will run in server mode.")
|}
let getBindingInfos () =
Directory.GetDirectories("Bindings")
|> Seq.map (fun x -> x </> Path.GetFileName x + ".fsproj")
|> Seq.filter File.exists
|> Seq.map (fun file ->
let fileContent = File.readAsString file
let version =
let startIndex = fileContent.IndexOf("Version=") + 9
let endIndex = fileContent.IndexOf("\"", startIndex)
fileContent.Substring(startIndex, endIndex - startIndex)
let package =
let endIndex = fileContent.IndexOf("Version=") - 2
let startIndex = fileContent.LastIndexOf("Include=", endIndex) + 9
fileContent.Substring(startIndex, endIndex - startIndex)
{|
name = Path.GetFileNameWithoutExtension file
package = package
version = version
|}
)
let getNugetPackageLatestVersion (package: string) = task {
let logger = NullLogger.Instance
let cancellationToken = CancellationToken.None
use cache = new SourceCacheContext()
let repository = Repository.Factory.GetCoreV3("https://api.nuget.org/v3/index.json")
let! resource = repository.GetResourceAsync<FindPackageByIdResource>()
let! versions = resource.GetAllVersionsAsync(package, cache, logger, cancellationToken)
return
versions
|> Seq.filter (fun x -> not x.IsPrerelease)
|> Seq.tryLast
|> Option.defaultWith (fun _ -> failwith $"No version found for {package}")
}
let stage_checkEnv =
stage "Check envs" {
stage "generate Directory.Build.props for version control" {
run (fun _ ->
!! "./*/CHANGELOG.md"
|> Seq.iter (fun file ->
printfn "%s %s" file (Path.getDirectory file)
let version =
Path.getDirectory file
|> Changelog.GetLastVersion
|> Option.defaultWith (fun _ -> failwith "No version available")
$"""<!-- auto generated -->
<Project>
<PropertyGroup>
<Version>{version.Version}</Version>
</PropertyGroup>
</Project>"""
|> File.writeString false (Path.getDirectory file </> "Directory.Build.props")
)
)
}
run "dotnet restore"
run "dotnet build"
run (ignore >> Docs.DocBuilder.build)
}
let stage_test =
stage "Test" {
run "dotnet build"
run "dotnet test --no-build"
}
let stage_packNuget projDir =
stage $"Pack Nuget for {projDir}" {
workingDir projDir
run $"dotnet pack -c Release -o {__SOURCE_DIRECTORY__}"
}
let stage_generateBindingProjects name package nsp assemblyName patch targets =
let projectName = "Fun.Blazor." + name
let projectDir = "Bindings" </> projectName
stage name {
workingDir projectDir
whenAny {
whenNot { cmdArg "--package" }
whenCmd {
longName "--package"
description "Generate when specified"
acceptValues [ projectName ]
}
}
run (fun _ -> task {
let! nugetVersion = getNugetPackageLatestVersion package
let version = nugetVersion.OriginalVersion
printfn $"Found verion {version} for package {package}"
let bindingVersion =
if String.IsNullOrEmpty patch then version else version + "." + patch
Directory.ensure projectDir
File.write false (projectDir </> projectName + ".fsproj") [
$"""
<Project Sdk="Microsoft.NET.Sdk.Razor">
<PropertyGroup>
<TargetFrameworks>{targets}</TargetFrameworks>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<TrimMode>link</TrimMode>
<IsTrimmable>true</IsTrimmable>
<Version>{bindingVersion}</Version>
</PropertyGroup>
<ItemGroup>
<PackageReference FunBlazor="" FunBlazorNamespace="{nsp}" FunBlazorAssemblyName="{defaultArg assemblyName package}" Include="{package}" Version="{version}" />
</ItemGroup>
<ItemGroup>
<Compile Include="*.fs" />
</ItemGroup>
<ItemGroup>
<PackageReference Update="FSharp.Core" Version="6.0.0" />
<PackageReference Include="Fun.Blazor" Version="4.1.0" />
</ItemGroup>
</Project>"""
]
})
run $"dotnet run --project ../../Fun.Blazor.Cli/Fun.Blazor.Cli.fsproj --framework net8.0 -- generate {projectName}.fsproj"
run "dotnet build"
}
let stage_pack =
stage "Pack" {
stage "core projects" {
whenNot { cmdArg "--bindings" }
stage_packNuget "Fun.Blazor"
stage_packNuget "Fun.Blazor.CustomElements"
stage_packNuget "Fun.Blazor.Cli"
stage_packNuget "Fun.Blazor.Elmish"
stage_packNuget "Fun.Blazor.Generator"
stage_packNuget "Fun.Blazor.HotReload"
stage_packNuget "Fun.Blazor.HtmlTemplate"
stage_packNuget "Fun.Blazor.Reactive"
stage_packNuget "Fun.Blazor.Server"
stage_packNuget "Fun.Blazor.Wasm"
stage_packNuget "Fun.Htmx"
}
stage "bindings projects" {
whenCmdArg "--bindings"
run (fun ctx -> asyncResult {
let files =
Directory.GetDirectories("Bindings")
|> Seq.map (fun x -> x </> Path.GetFileName x + ".fsproj")
|> Seq.filter File.exists
for file in files do
do! ctx.RunCommand $"dotnet pack {file} -c Release -o {__SOURCE_DIRECTORY__}"
})
}
}
pipeline "dev" {
description "Start develop with Fun.Blazor.Docs"
stage_checkEnv
stage "docs" {
paralle
stage "wasm" {
workingDir "./Fun.Blazor.Docs.Wasm"
whenCmdArg options.WASM
run "dotnet run"
}
stage "server" {
workingDir "./Fun.Blazor.Docs.Server"
whenNot { cmdArg options.WASM }
run "dotnet run"
}
//stage "hot-reload" {
// workingDir "./Fun.Blazor.Cli"
// whenCmdArg "--hot-reload"
// run "dotnet run --framework net8.0 -- watch ../Fun.Blazor.Docs.Wasm/Fun.Blazor.Docs.Wasm.fsproj"
//}
}
runIfOnlySpecified
}
pipeline "docs" {
description "Publish docs to github"
collapseGithubActionLogs
stage_checkEnv
stage "Publish docs" {
run "dotnet publish Fun.Blazor.Docs.Wasm/Fun.Blazor.Docs.Wasm.fsproj -c Release -o Fun.Blazor.Docs.Wasm.Release --nologo"
stage "Clean HEADOUTLET because it prevent app start" {
run (fun _ ->
!!("Fun.Blazor.Docs.Wasm.Release" </> "**" </> "index.html")
|> Seq.iter (
File.applyReplace (fun x ->
let startIndex = x.IndexOf("<!-- %%-PRERENDERING-HEADOUTLET-BEGIN-%% -->")
let endIndex = x.IndexOf("<!-- %%-PRERENDERING-HEADOUTLET-END-%% -->") + 44
if startIndex > -1 then x.Remove(startIndex, endIndex - startIndex) else x
)
)
)
}
stage "Tune" {
whenBranch "master"
whenEnvVar "GITHUB_ENV"
run (fun _ ->
!!("Fun.Blazor.Docs.Wasm.Release" </> "**" </> "index.html")
|> Seq.iter (File.applyReplace (fun x -> x.Replace("""<base href="/"/>""", """<base href="/Fun.Blazor.Docs/" /> """)))
)
run "cp Fun.Blazor.Docs.Wasm.Release/wwwroot/index.html Fun.Blazor.Docs.Wasm.Release/wwwroot/404.html"
run "touch Fun.Blazor.Docs.Wasm.Release/wwwroot/.nojekyll"
}
}
runIfOnlySpecified
}
pipeline "packages" {
description "Push packages to nuget"
collapseGithubActionLogs
stage_checkEnv
stage_test
stage_pack
stage "Push to nuget" {
failIfIgnored
whenBranch "master"
whenGithubAction
whenEnvVar options.NUGET_API_KEY
run (fun ctx ->
let key = ctx.GetEnvVar options.NUGET_API_KEY.Name
ctx.RunSensitiveCommand $"dotnet nuget push *.nupkg -s https://api.nuget.org/v3/index.json --skip-duplicate -k {key}"
)
}
runIfOnlySpecified
}
pipeline "bindings" {
description "Generate bindings project"
collapseGithubActionLogs
stage "generate" {
// paralle
continueStepsOnFailure
continueStageOnFailure
stage_generateBindingProjects "Microsoft.Web" "Microsoft.AspNetCore.Components.Web" "Microsoft.AspNetCore.Components" None "" "net9.0"
stage_generateBindingProjects
"Microsoft.Authorization"
"Microsoft.AspNetCore.Components.Authorization"
"Microsoft.AspNetCore.Components.Authorization"
None
""
"net9.0"
stage_generateBindingProjects "Microsoft.QuickGrid" "Microsoft.AspNetCore.Components.QuickGrid" "Microsoft.AspNetCore.Components.QuickGrid" None "" "net9.0"
stage_generateBindingProjects "Microsoft.FluentUI" "Microsoft.FluentUI.AspNetCore.Components" "Microsoft.FluentUI.AspNetCore.Components" None "" "net8.0"
stage_generateBindingProjects "AntDesign" "AntDesign" "AntDesign" None "" "net6.0;net8.0"
stage_generateBindingProjects "MudBlazor" "MudBlazor" "MudBlazor" None "" "net7.0;net8.0"
stage_generateBindingProjects "ApexCharts" "Blazor-ApexCharts" "ApexCharts" None "" "net6.0;net8.0"
stage_generateBindingProjects "BlazorMonaco" "BlazorMonaco" "BlazorMonaco" None "" "net6.0;net8.0"
stage_generateBindingProjects "Diagrams" "Z.Blazor.Diagrams" "Blazor.Diagrams" (Some "Blazor.Diagrams") "" "net6.0;net8.0"
stage_generateBindingProjects "Radzen" "Radzen.Blazor" "Radzen.Blazor" None "" "net6.0;net8.0;net9.0"
}
stage "pack for binding projects" {
run (fun _ ->
printfn "Update binding docs"
File.write false ("Docs" </> "17 Bindings" </> "README.md") [
"# Bindings"
""
"Below is auto generated bindings, if the version does not match your requirements you can use the Fun.Blazor.Cli to generate your own."
""
"The bindings will be updated every week."
""
"```bash"
for info in getBindingInfos () do
$"dotnet add package {info.name} --version {info.version}"
"```"
]
)
}
runIfOnlySpecified
}
pipeline "bindings-check" {
description "Check if there is new version for the binding project"
collapseGithubActionLogs
stage "check" {
run (fun _ -> task {
let mutable hasErrors = false
for info in getBindingInfos () do
printfn $"Check for {info.package}, current version: {info.version}"
let! nugetVersion = getNugetPackageLatestVersion info.package
let latestVersion = nugetVersion.OriginalVersion
if latestVersion <> info.version then
hasErrors <- true
printfn $"::error::Package {info.package} should be updated from {info.version} to {latestVersion}"
if hasErrors then
raise (PipelineFailedException("Some packages' version changed"))
})
}
runIfOnlySpecified
}
pipeline "test" {
description "Test related functions"
collapseGithubActionLogs
stage_checkEnv
stage_test
runIfOnlySpecified
}
pipeline "benchmark" {
description "Benchmark rendering performance"
stage "run" {
workingDir "Benchmark/BenchmarkV2"
run "dotnet run -c Release"
}
runIfOnlySpecified
}
tryPrintPipelineCommandHelp ()