forked from fabulous-dev/Fabulous
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.fsx
451 lines (380 loc) · 15.4 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
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
#r "paket: groupref fakebuild //"
#load "./.fake/build.fsx/intellisense.fsx"
open Fake.Api
open Fake.Core
open Fake.DotNet
open Fake.DotNet.NuGet
open Fake.IO
open Fake.IO.Globbing.Operators
open System.IO
open Newtonsoft.Json
open Newtonsoft.Json.Linq
let repositoryOwner = "fsprojects"
let repositoryName = "Fabulous"
let release = ReleaseNotes.load "RELEASE_NOTES.md"
let buildDir = Path.getFullName "./build_output"
let composeOutputPath subDir projectPath =
let projectName = Path.GetFileNameWithoutExtension projectPath
sprintf "%s/%s/%s" buildDir subDir projectName
let removeIncompatiblePlatformProjects pattern =
if Environment.isMacOS then
pattern
-- "**/*.WPF.fsproj"
-- "**/*.UWP.fsproj"
elif Environment.isWindows then
pattern
-- "**/*.macOS.fsproj"
-- "**/*.iOS.fsproj"
else
pattern
-- "**/*.macOS.fsproj"
-- "**/*.iOS.fsproj"
-- "**/*.WPF.fsproj"
-- "**/*.UWP.fsproj"
-- "**/*.Droid.fsproj"
// JavaSdkDirectory is a temporary fix for the Windows 2019 build agent
// It's currently failing to build Xamarin.Android : https://github.com/Microsoft/azure-pipelines-image-generation/blob/master/images/win/Vs2019-Server2019-Readme.md
let addJDK properties =
match Environment.environVarOrDefault "JAVA_HOME_8_X64" "" with
| javaHome when not (System.String.IsNullOrWhiteSpace javaHome && Environment.isWindows) -> ("JavaSdkDirectory", javaHome) :: properties
| _ -> properties
let dotnetBuild paths =
for projectPath in paths do
DotNet.build (fun opt ->
{ opt with
Configuration = DotNet.BuildConfiguration.Release }) projectPath
let computeBounds (semVer: SemVerInfo) =
match semVer.PreRelease with
| Some _ ->
sprintf "[%s]" semVer.AsString
| None when semVer.Major = 0u ->
sprintf "[0.%i%%2C0.%i)" semVer.Minor (semVer.Minor + 1u)
| None ->
sprintf "[%i.0%%2C%i.0)" semVer.Major (semVer.Major + 1u)
let dotnetPack paths =
for projectPath in paths do
let args =
sprintf "-p:IncludeSourceLink=True -p:IsPacking=true -p:VersionBounds=\"%s\" -p:RestoreAdditionalProjectSources=\"%s\""
(computeBounds release.SemVer)
(Path.GetFullPath(buildDir))
DotNet.pack (fun opt ->
{ opt with
Common = { opt.Common with CustomParams = Some args }
Configuration = DotNet.BuildConfiguration.Release
OutputPath = Some buildDir }) projectPath
let dotnetTest outputSubDir paths =
for projectPath in paths do
let outputPath = composeOutputPath outputSubDir projectPath
DotNet.test (fun opt ->
{ opt with
Logger = Some "trx"
ResultsDirectory = Some outputPath }) projectPath
let msbuild paths =
for projectPath in paths do
let projectName = Path.GetFileNameWithoutExtension projectPath
let properties = [ ("Configuration", "Release") ] |> addJDK
MSBuild.run id "" "Build" properties [projectPath] |> Trace.logItems (projectName + "-Build-Output: ")
let nugetPack paths =
for nuspecPath in paths do
NuGet.NuGetPack (fun opt ->
{ opt with
WorkingDir = Path.GetDirectoryName nuspecPath
OutputPath = buildDir
Version = release.NugetVersion
ReleaseNotes = (String.toLines release.Notes) }) nuspecPath
/// Replaces the value of attribute in an xml node in the XML document specified by a XPath expression.
let replaceXPathAttributeNSIfExists xpath (attribute:string) value (namespaces : #seq<string * string>) (doc : System.Xml.XmlDocument) =
let nsmgr = System.Xml.XmlNamespaceManager(doc.NameTable)
namespaces |> Seq.iter nsmgr.AddNamespace
let node = doc.SelectSingleNode(xpath, nsmgr)
if not (isNull node) then
let attributeValue = node.Attributes.[attribute]
if not (isNull attributeValue) then
attributeValue.Value <- value
doc
Target.create "Clean" (fun _ ->
Shell.cleanDir buildDir
)
Target.create "Restore" (fun _ ->
// Restore all projects (except templates) compatible with the current platform
!! "**/*.fsproj" ++ "**/*.csproj"
-- "**/templates/**/*.fsproj" -- "**/templates/**/*.csproj"
|> removeIncompatiblePlatformProjects
|> (fun projects ->
for project in projects do
DotNet.restore id project
)
)
Target.create "FormatBindings" (fun _ ->
let bindingFiles =
!! "Fabulous.XamarinForms/Fabulous.XamarinForms.Controls/Xamarin.Forms.Core.fs"
for file in bindingFiles do
File.ReadAllText file
|> JToken.Parse
|> (fun token -> token.ToString(Formatting.Indented))
|> (fun json -> File.WriteAllText(file, json))
)
Target.create "UpdateVersion" (fun _ ->
// Updates Directory.Build.props
let props = "./Directory.Build.props"
Xml.loadDoc props
|> Xml.replaceXPath "//Version/text()" release.AssemblyVersion
|> Xml.replaceXPath "//PackageReleaseNotes/text()" (String.toLines release.Notes)
|> Xml.replaceXPath "//PackageVersion/text()" release.NugetVersion
|> Xml.saveDoc props
// Updates template.json
let templates = !! "**/templates/**/.template.config/template.json"
for template in templates do
File.readAsString template
|> JObject.Parse
|> (fun o ->
let prop = o.["name"] :?> JValue
let name =
let currentValue = prop.Value.ToString()
let realName = currentValue.Remove(currentValue.LastIndexOf(" "))
sprintf "%s v%s" realName release.NugetVersion
prop.Value <- name
o
)
|> (fun o ->
let prop = o.["symbols"].["FabulousPkgsVersion"].["defaultValue"] :?> JValue
prop.Value <- release.NugetVersion
o
)
|> (fun o -> JsonConvert.SerializeObject(o, Formatting.Indented))
|> File.writeString false template
)
Target.create "BuildTools" (fun _ ->
!! "tools/**/*.fsproj"
|> dotnetBuild
)
Target.create "BuildFabulous" (fun _ ->
!! "src/**/*.fsproj"
|> dotnetBuild
)
Target.create "RunFabulousTests" (fun _ ->
!! "tests/**/*.fsproj"
|> dotnetTest "Fabulous/TestResults"
)
Target.create "BuildFabulousCodeGen" (fun _ ->
!! "Fabulous.CodeGen/src/**/*.fsproj"
|> dotnetBuild
)
Target.create "RunFabulousCodeGenTests" (fun _ ->
!! "Fabulous.CodeGen/tests/**/*.fsproj"
|> dotnetTest "Fabulous.CodeGen/TestResults"
)
Target.create "BuildFabulousXamarinFormsTools" (fun _ ->
!! "Fabulous.XamarinForms/tools/**/*.fsproj"
|> dotnetBuild
)
Target.create "BuildFabulousXamarinFormsDependencies" (fun _ ->
!! "Fabulous.XamarinForms/src/**/*.fsproj"
-- "Fabulous.XamarinForms/src/Fabulous.XamarinForms/Fabulous.XamarinForms.fsproj" // This one needs to run the generator beforehand
|> dotnetBuild
)
Target.create "RunGeneratorForFabulousXamarinForms" (fun _ ->
let generatorPath = "Fabulous.XamarinForms/tools/Fabulous.XamarinForms.Generator/bin/Release/netcoreapp3.1/Fabulous.XamarinForms.Generator.dll"
let mappingFilePath = "Fabulous.XamarinForms/src/Fabulous.XamarinForms/Xamarin.Forms.Core.json"
let attributesOutputFilePath = "Fabulous.XamarinForms/src/Fabulous.XamarinForms/Xamarin.Forms.Core.Attributes.fs"
let buildersOutputFilePath = "Fabulous.XamarinForms/src/Fabulous.XamarinForms/Xamarin.Forms.Core.fs"
DotNet.exec id generatorPath (sprintf "-m %s -a %s -b %s" mappingFilePath attributesOutputFilePath buildersOutputFilePath)
|> (fun x ->
match x.OK with
| true -> ()
| false -> failwith "The generator stopped due to an exception"
)
)
Target.create "BuildFabulousXamarinForms" (fun _ ->
!! "Fabulous.XamarinForms/src/Fabulous.XamarinForms/Fabulous.XamarinForms.fsproj"
|> dotnetBuild
)
Target.create "RunFabulousXamarinFormsTests" (fun _ ->
!! "Fabulous.XamarinForms/tests/**/*.fsproj"
|> dotnetTest "Fabulous.XamarinForms/TestResults"
)
Target.create "RunGeneratorForFabulousXamarinFormsExtensions" (fun _ ->
let generatorPath = "Fabulous.XamarinForms/tools/Fabulous.XamarinForms.Generator/bin/Release/netcoreapp3.1/Fabulous.XamarinForms.Generator.dll"
let files =
!! "Fabulous.XamarinForms/extensions/**/*.json"
-- "Fabulous.XamarinForms/extensions/**/obj/*.json"
for mappingFile in files do
let attributesOutputFile = mappingFile.Replace(".json", ".Attributes.fs")
let buildersOutputFile = mappingFile.Replace(".json", ".fs")
DotNet.exec id generatorPath (sprintf "-m %s -a %s -b %s" mappingFile attributesOutputFile buildersOutputFile)
|> (fun x ->
match x.OK with
| true -> ()
| false -> failwith "The generator stopped due to an exception"
)
)
Target.create "BuildFabulousXamarinFormsExtensions" (fun _ ->
!! "Fabulous.XamarinForms/extensions/**/*.fsproj"
|> dotnetBuild
)
Target.create "BuildFabulousStaticView" (fun _ ->
!! "Fabulous.StaticView/src/**/*.fsproj"
|> dotnetBuild
)
Target.create "PackFabulous" (fun _ ->
// Pack Fabulous first since it is used by other packages
!! "src/Fabulous/Fabulous.fsproj"
|> dotnetPack
!! "src/**/*.fsproj"
-- "src/Fabulous/Fabulous.fsproj"
|> dotnetPack
)
Target.create "PackFabulousCodeGen" (fun _ ->
!! "Fabulous.CodeGen/src/**/*.fsproj"
|> dotnetPack
)
Target.create "PackFabulousStaticView" (fun _ ->
!! "Fabulous.StaticView/src/**/*.fsproj"
|> dotnetPack
)
Target.create "PackFabulousXamarinForms" (fun _ ->
!! "Fabulous.XamarinForms/src/Fabulous.XamarinForms/*.fsproj"
|> dotnetPack
!! "Fabulous.XamarinForms/src/Fabulous.XamarinForms.LiveUpdate/*.fsproj"
|> dotnetPack
)
Target.create "PackFabulousXamarinFormsTemplates" (fun _ ->
!! "Fabulous.XamarinForms/templates/*.nuspec"
|> nugetPack
)
Target.create "PackFabulousXamarinFormsExtensions" (fun _ ->
!! "Fabulous.XamarinForms/extensions/**/*.fsproj"
|> dotnetPack
)
Target.create "BuildFabulousXamarinFormsSamples" (fun _ ->
!! "Fabulous.XamarinForms/samples/**/*.fsproj"
|> removeIncompatiblePlatformProjects
|> msbuild
)
Target.create "RunFabulousXamarinFormsSamplesTests" (fun _ ->
!! "Fabulous.XamarinForms/samples/**/*.Tests.fsproj"
|> dotnetTest "Fabulous.XamarinForms/samples/TestResults"
)
Target.create "BuildFabulousStaticViewSamples" (fun _ ->
!! "Fabulous.StaticView/samples/**/*.fsproj"
|> removeIncompatiblePlatformProjects
|> msbuild
)
Target.create "TestTemplatesNuGet" (fun _ ->
let ticks = let now = System.DateTime.Now in now.Ticks // Prevents warning FS0052
let testAppName = "testapp2" + string (abs (hash ticks) % 100)
// Globally install the templates from the template nuget package we just built
DotNet.exec id "new" "-u Fabulous.XamarinForms.Templates" |> ignore
DotNet.exec id "new" (sprintf "-i %s/Fabulous.XamarinForms.Templates.%s.nupkg" buildDir release.NugetVersion) |> ignore
// Instantiate the template.
Shell.cleanDir testAppName
let extraArgs =
if Environment.isWindows then " --WPF --UWP"
elif Environment.isMacOS then " --macOS"
elif Environment.isLinux then " --Android=false --iOS=false"
else ""
DotNet.exec id "new fabulous-xf-app" (sprintf "-n %s -lang F# --allow-scripts yes --GTK%s" testAppName extraArgs) |> ignore
// Restore NuGet packages
let pkgs = Path.GetFullPath(buildDir)
let sln = sprintf "%s/%s.sln" testAppName testAppName
let args = sprintf "restore %s -source https://api.nuget.org/v3/index.json -source %s" sln pkgs
if Environment.isWindows then
Shell.Exec(".\\.nuget\\NuGet.exe", args) |> ignore
else
Shell.Exec("mono", sprintf ".nuget/NuGet.exe %s" args) |> ignore
// Build for all combinations
for c in ["Debug"; "Release"] do
for p in ["Any CPU"; "iPhoneSimulator"] do
let properties = [("Platform", p); ("Configuration", c)] |> addJDK
MSBuild.run id "" "Build" properties [sln] |> Trace.logItems ("Build-Output: ")
)
Target.create "CreateGitHubRelease" (fun _ ->
let token =
match Environment.environVarOrDefault "GITHUB_TOKEN" "" with
| s when not (System.String.IsNullOrWhiteSpace s) -> s
| _ -> failwith "Please set the GITHUB_TOKEN environment variable to a github personal access token with repo access."
try
GitHub.createClientWithToken token
|> GitHub.draftNewRelease repositoryOwner repositoryName release.NugetVersion release.SemVer.PreRelease.IsSome (release.Notes |> List.map (sprintf "- %s"))
// |> GitHub.uploadFiles !!(buildDir + "/*.nupkg") // Randomly failing to upload for some reasons...
|> GitHub.publishDraft
|> Async.RunSynchronously
with
| ex -> printfn "GitHub release skipped. Reason: %A" ex
)
Target.create "PublishNuGetPackages" (fun _ ->
let nugetApiKey =
match Environment.environVarOrDefault "NUGET_APIKEY" "" with
| s when not (System.String.IsNullOrWhiteSpace s) -> s
| _ -> failwith "Please set the NUGET_APIKEY environment variable to a NuGet API key with write access to the Fabulous packages."
// dotnet publishes both nupkg and snupkg by default
let path = System.IO.Path.Combine(buildDir, "*.nupkg")
DotNet.exec id "nuget" (sprintf "push %s -k %s --skip-duplicate -s https://api.nuget.org/v3/index.json" path nugetApiKey) |> ignore
)
Target.create "Prepare" ignore
Target.create "Fabulous" ignore
Target.create "Fabulous.CodeGen" ignore
Target.create "Fabulous.StaticView" ignore
Target.create "StartFabulousXamarinForms" ignore
Target.create "Fabulous.XamarinForms" ignore
Target.create "Fabulous.XamarinForms.Extensions" ignore
Target.create "Build" ignore
Target.create "Pack" ignore
Target.create "TestSamples" ignore
Target.create "Test" ignore
Target.create "Release" ignore
open Fake.Core.TargetOperators
"Clean"
==> "Restore"
==> "FormatBindings"
==> "UpdateVersion"
==> "BuildTools"
==> "Prepare"
"Prepare"
==> "BuildFabulous"
==> "RunFabulousTests"
==> "Fabulous"
==> "StartFabulousXamarinForms"
"Prepare"
==> "BuildFabulousCodeGen"
==> "RunFabulousCodeGenTests"
==> "Fabulous.CodeGen"
==> "StartFabulousXamarinForms"
"Prepare"
==> "BuildFabulousStaticView"
==> "Fabulous.StaticView"
==> "Build"
"StartFabulousXamarinForms"
==> "BuildFabulousXamarinFormsTools"
==> "BuildFabulousXamarinFormsDependencies"
==> "RunGeneratorForFabulousXamarinForms"
==> "BuildFabulousXamarinForms"
==> "RunFabulousXamarinFormsTests"
==> "Fabulous.XamarinForms"
"Fabulous.XamarinForms"
==> "RunGeneratorForFabulousXamarinFormsExtensions"
==> "BuildFabulousXamarinFormsExtensions"
==> "Fabulous.XamarinForms.Extensions"
==> "Build"
"Build"
==> "PackFabulous"
==> "PackFabulousCodeGen"
==> "PackFabulousStaticView"
==> "PackFabulousXamarinForms"
==> "PackFabulousXamarinFormsTemplates"
==> "PackFabulousXamarinFormsExtensions"
==> "Pack"
"Build"
==> "BuildFabulousXamarinFormsSamples"
==> "RunFabulousXamarinFormsSamplesTests"
==> "BuildFabulousStaticViewSamples"
==> "TestSamples"
==> "Test"
"Pack"
==> "TestTemplatesNuGet"
==> "Test"
"Test"
==> "CreateGitHubRelease"
==> "PublishNuGetPackages"
==> "Release"
Target.runOrDefault "Build"