-
Notifications
You must be signed in to change notification settings - Fork 9
/
build.fsx
555 lines (460 loc) · 18.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
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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
// --------------------------------------------------------------------------------------
// FAKE build script
// --------------------------------------------------------------------------------------
#nowarn "0213"
#r "paket: groupref FakeBuild //"
#load "./tools/FSharpLint.fs"
#load "./tools/Web.fs"
#load "./.fake/build.fsx/intellisense.fsx"
open Fake.Core
open Fake.Core.TargetOperators
open Fake.DotNet
open Fake.JavaScript
open Fake.IO
open Fake.IO.FileSystemOperators
open Fake.IO.Globbing.Operators
open Fake.Tools
open Fantomas.FakeHelpers
open Fantomas.FormatConfig
open Tools.Linting
open Tools.Web
open System
open System.IO
// The name of the project
// (used by attributes in AssemblyInfo, name of a NuGet package and directory in 'src')
let project = "Feliz.Recoil"
// Short summary of the project
// (used as description in AssemblyInfo and as a short summary for NuGet package)
let summary = "Fable bindings in Feliz style for Facebook's experimental state management library recoil."
// Author(s) of the project
let author = "Cody Johnson"
// File system information
let solutionFile = "Feliz.Recoil.sln"
// Github repo
let repo = "https://github.com/Shmew/Feliz.Recoil"
// Files to skip Fantomas formatting
let excludeFantomas =
[ __SOURCE_DIRECTORY__ @@ "src/**/*.fs" ]
// Files that have bindings to other languages where name linting needs to be more relaxed.
let relaxedNameLinting =
[ __SOURCE_DIRECTORY__ @@ "src/**/**/*.fs"
__SOURCE_DIRECTORY__ @@ "src/**/*.fs" ]
// Read additional information from the release notes document
let release = ReleaseNotes.load (__SOURCE_DIRECTORY__ @@ "RELEASE_NOTES.md")
// Helper active pattern for project types
let (|Fsproj|Csproj|Vbproj|Shproj|) (projFileName:string) =
match projFileName with
| f when f.EndsWith("fsproj") -> Fsproj
| f when f.EndsWith("csproj") -> Csproj
| f when f.EndsWith("vbproj") -> Vbproj
| f when f.EndsWith("shproj") -> Shproj
| _ -> failwith (sprintf "Project file %s not supported. Unknown project type." projFileName)
let srcGlob = __SOURCE_DIRECTORY__ @@ "src/**/*.??proj"
let fsSrcGlob = __SOURCE_DIRECTORY__ @@ "src/**/*.fs"
let fsTestGlob = __SOURCE_DIRECTORY__ @@ "tests/**/*.fs"
let bin = __SOURCE_DIRECTORY__ @@ "bin"
let temp = __SOURCE_DIRECTORY__ @@ "temp"
let objFolder = __SOURCE_DIRECTORY__ @@ "obj"
let pub = __SOURCE_DIRECTORY__ @@ "public"
let genGlob = __SOURCE_DIRECTORY__ @@ "src/**/*.Generator.*.fsproj"
let libGlob = __SOURCE_DIRECTORY__ @@ "src/**/*.fsproj"
let foldExcludeGlobs (g: IGlobbingPattern) (d: string) = g -- d
let foldIncludeGlobs (g: IGlobbingPattern) (d: string) = g ++ d
let fsSrcAndTest =
!! fsSrcGlob
++ fsTestGlob
-- (__SOURCE_DIRECTORY__ @@ "src/**/obj/**")
-- (__SOURCE_DIRECTORY__ @@ "tests/**/obj/**")
-- (__SOURCE_DIRECTORY__ @@ "src/**/AssemblyInfo.*")
-- (__SOURCE_DIRECTORY__ @@ "src/**/**/AssemblyInfo.*")
let fsRelaxedNameLinting =
let baseGlob s =
!! s
-- (__SOURCE_DIRECTORY__ @@ "src/**/AssemblyInfo.*")
-- (__SOURCE_DIRECTORY__ @@ "src/**/obj/**")
-- (__SOURCE_DIRECTORY__ @@ "tests/**/obj/**")
match relaxedNameLinting with
| [h] when relaxedNameLinting.Length = 1 -> baseGlob h |> Some
| h::t -> List.fold foldIncludeGlobs (baseGlob h) t |> Some
| _ -> None
let setCmd f args =
match Environment.isWindows with
| true -> Command.RawCommand(f, Arguments.OfArgs args)
| false -> Command.RawCommand("mono", Arguments.OfArgs (f::args))
let configuration() =
FakeVar.getOrDefault "configuration" "Release"
let getEnvFromAllOrNone (s: string) =
let envOpt (envVar: string) =
if String.isNullOrEmpty envVar then None
else Some(envVar)
let procVar = Environment.GetEnvironmentVariable(s) |> envOpt
let userVar = Environment.GetEnvironmentVariable(s, EnvironmentVariableTarget.User) |> envOpt
let machVar = Environment.GetEnvironmentVariable(s, EnvironmentVariableTarget.Machine) |> envOpt
match procVar,userVar,machVar with
| Some(v), _, _
| _, Some(v), _
| _, _, Some(v)
-> Some(v)
| _ -> None
// Set default
FakeVar.set "configuration" "Release"
// --------------------------------------------------------------------------------------
// Set configuration mode based on target
Target.create "ConfigDebug" <| fun _ ->
FakeVar.set "configuration" "Debug"
Target.create "ConfigRelease" <| fun _ ->
FakeVar.set "configuration" "Release"
// --------------------------------------------------------------------------------------
// Generate assembly info files with the right version & up-to-date information
Target.create "AssemblyInfo" <| fun _ ->
let getAssemblyInfoAttributes projectName =
[ AssemblyInfo.Title (projectName)
AssemblyInfo.Product project
AssemblyInfo.Description summary
AssemblyInfo.Version release.AssemblyVersion
AssemblyInfo.FileVersion release.AssemblyVersion
AssemblyInfo.Configuration <| configuration()
AssemblyInfo.InternalsVisibleTo (sprintf "%s.Tests" projectName) ]
let getProjectDetails projectPath =
let projectName = Path.GetFileNameWithoutExtension(projectPath)
( projectPath,
projectName,
Path.GetDirectoryName(projectPath),
(getAssemblyInfoAttributes projectName)
)
!! srcGlob
|> Seq.map getProjectDetails
|> Seq.iter (fun (projFileName, _, folderName, attributes) ->
match projFileName with
| Fsproj -> AssemblyInfoFile.createFSharp (folderName </> "AssemblyInfo.fs") attributes
| Csproj -> AssemblyInfoFile.createCSharp ((folderName </> "Properties") </> "AssemblyInfo.cs") attributes
| Vbproj -> AssemblyInfoFile.createVisualBasic ((folderName </> "My Project") </> "AssemblyInfo.vb") attributes
| Shproj -> () )
// --------------------------------------------------------------------------------------
// Copies binaries from default VS location to expected bin folder
// But keeps a subdirectory structure for each project in the
// src folder to support multiple project outputs
Target.create "CopyBinaries" <| fun _ ->
!! libGlob
-- genGlob
-- (__SOURCE_DIRECTORY__ @@ "src/**/*.shproj")
|> Seq.map (fun f -> ((Path.getDirectory f) @@ "bin" @@ configuration(), "bin" @@ (Path.GetFileNameWithoutExtension f)))
|> Seq.iter (fun (fromDir, toDir) -> Shell.copyDir toDir fromDir (fun _ -> true))
// --------------------------------------------------------------------------------------
// Clean tasks
Target.create "Clean" <| fun _ ->
let clean() =
!! (__SOURCE_DIRECTORY__ @@ "tests/**/bin")
++ (__SOURCE_DIRECTORY__ @@ "tests/**/obj")
++ (__SOURCE_DIRECTORY__ @@ "tools/bin")
++ (__SOURCE_DIRECTORY__ @@ "tools/obj")
++ (__SOURCE_DIRECTORY__ @@ "src/**/bin")
++ (__SOURCE_DIRECTORY__ @@ "src/**/obj")
|> Seq.toList
|> List.append [bin; temp; objFolder]
|> Shell.cleanDirs
TaskRunner.runWithRetries clean 10
Target.create "CleanDocs" <| fun _ ->
let clean() =
!! (pub @@ "*.md")
++ (pub @@ "*bundle.*")
++ (pub @@ "**/README.md")
++ (pub @@ "**/RELEASE_NOTES.md")
++ (pub @@ "index.html")
|> List.ofSeq
|> List.iter Shell.rm
TaskRunner.runWithRetries clean 10
Target.create "CopyDocFiles" <| fun _ ->
[ pub @@ "Recoil/README.md", __SOURCE_DIRECTORY__ @@ "README.md"
pub @@ "Recoil/RELEASE_NOTES.md", __SOURCE_DIRECTORY__ @@ "RELEASE_NOTES.md"
pub @@ "index.html", __SOURCE_DIRECTORY__ @@ "docs/index.html" ]
|> List.iter (fun (target, source) -> Shell.copyFile target source)
Target.create "PrepDocs" ignore
Target.create "PostBuildClean" <| fun _ ->
let clean() =
!! srcGlob
-- (__SOURCE_DIRECTORY__ @@ "src/**/*.shproj")
|> Seq.map (
(fun f -> (Path.getDirectory f) @@ "bin" @@ configuration())
>> (fun f -> Directory.EnumerateDirectories(f) |> Seq.toList )
>> (fun fL -> fL |> List.map (fun f -> Directory.EnumerateDirectories(f) |> Seq.toList)))
|> (Seq.concat >> Seq.concat)
|> Seq.iter Directory.delete
TaskRunner.runWithRetries clean 10
Target.create "PostPublishClean" <| fun _ ->
let clean() =
!! (__SOURCE_DIRECTORY__ @@ "src/**/bin" @@ configuration() @@ "/**/publish")
|> Seq.iter Directory.delete
TaskRunner.runWithRetries clean 10
// --------------------------------------------------------------------------------------
// Restore tasks
let restoreSolution () =
solutionFile
|> DotNet.restore id
Target.create "Restore" <| fun _ ->
TaskRunner.runWithRetries restoreSolution 5
Target.create "YarnInstall" <| fun _ ->
let setParams (defaults:Yarn.YarnParams) =
{ defaults with
Yarn.YarnParams.YarnFilePath = (__SOURCE_DIRECTORY__ @@ "packages/tooling/Yarnpkg.Yarn/content/bin/yarn.cmd")
}
Yarn.install setParams
// --------------------------------------------------------------------------------------
// Build tasks
Target.create "Build" <| fun _ ->
let setParams (defaults:MSBuildParams) =
{ defaults with
Verbosity = Some(Quiet)
Targets = ["Build"]
Properties =
[
"Optimize", "True"
"DebugSymbols", "True"
"Configuration", configuration()
"Version", release.AssemblyVersion
"GenerateDocumentationFile", "true"
"DependsOnNETStandard", "true"
]
}
restoreSolution()
!! libGlob
-- genGlob
|> List.ofSeq
|> List.iter (MSBuild.build setParams)
// --------------------------------------------------------------------------------------
// Publish net core applications
Target.create "PublishDotNet" <| fun _ ->
let runPublish (project: string) (framework: string) =
let setParams (defaults:MSBuildParams) =
{ defaults with
Verbosity = Some(Quiet)
Targets = ["Publish"]
Properties =
[
"Optimize", "True"
"DebugSymbols", "True"
"Configuration", configuration()
"Version", release.AssemblyVersion
"GenerateDocumentationFile", "true"
"TargetFramework", framework
]
}
MSBuild.build setParams project
!! libGlob
-- genGlob
|> Seq.map
((fun f -> (((Path.getDirectory f) @@ "bin" @@ configuration()), f) )
>>
(fun f ->
Directory.EnumerateDirectories(fst f)
|> Seq.filter (fun frFolder -> frFolder.Contains("netcoreapp"))
|> Seq.map (fun frFolder -> DirectoryInfo(frFolder).Name), snd f))
|> Seq.iter (fun (l,p) -> l |> Seq.iter (runPublish p))
// --------------------------------------------------------------------------------------
// Lint and format source code to ensure consistency
Target.create "Format" <| fun _ ->
let config =
{ FormatConfig.Default with
PageWidth = 120
SpaceBeforeColon = false }
fsSrcAndTest
|> (fun src -> List.fold foldExcludeGlobs src excludeFantomas)
|> List.ofSeq
|> formatCode config
|> Async.RunSynchronously
|> printfn "Formatted files: %A"
Target.create "Lint" <| fun _ ->
fsSrcAndTest
-- (__SOURCE_DIRECTORY__ @@ "src/**/AssemblyInfo.*")
|> (fun src -> List.fold foldExcludeGlobs src relaxedNameLinting)
|> (fun fGlob ->
match fsRelaxedNameLinting with
| Some(glob) ->
[(false, fGlob); (true, glob)]
| None -> [(false, fGlob)])
|> Seq.map (fun (b,glob) -> (b,glob |> List.ofSeq))
|> List.ofSeq
|> FSharpLinter.lintFiles
// --------------------------------------------------------------------------------------
// Run the unit test binaries
Target.create "RunTests" <| fun _ ->
!! ("tests/**/bin" @@ configuration() @@ "**" @@ "*Tests.exe")
|> Seq.iter (fun f ->
CreateProcess.fromCommand(setCmd f [])
|> CreateProcess.withTimeout (TimeSpan.MaxValue)
|> CreateProcess.ensureExitCodeWithMessage "Tests failed."
|> Proc.run
|> ignore)
// --------------------------------------------------------------------------------------
// Generate Paket load scripts
Target.create "LoadScripts" <| fun _ ->
let frameworks =
__SOURCE_DIRECTORY__ @@ "bin"
|> Directory.EnumerateDirectories
|> Seq.map (fun d ->
Directory.EnumerateDirectories d
|> Seq.map (fun f -> DirectoryInfo(f).Name)
|> List.ofSeq)
|> List.ofSeq
|> List.reduce List.append
|> List.distinct
|> List.reduce (fun acc elem -> sprintf "%s --framework %s" elem acc)
|> function
| e when e.Length > 0 ->
Some (sprintf "--framework %s" e)
| _ -> None
let arguments =
[Some("generate-load-scripts"); frameworks]
|> List.choose id
|> List.reduce (fun acc elem -> sprintf "%s %s" acc elem)
arguments
|> CreateProcess.fromRawCommandLine ((__SOURCE_DIRECTORY__ @@ ".paket") @@ "paket.exe")
|> CreateProcess.withTimeout (TimeSpan.MaxValue)
|> CreateProcess.ensureExitCodeWithMessage "Failed to generate paket load scripts."
|> Proc.run
|> ignore
// --------------------------------------------------------------------------------------
// Update package.json version & name
Target.create "PackageJson" <| fun _ ->
let setValues (current: Json.JsonPackage) =
{ current with
Name = Str.toKebabCase project |> Some
Version = release.NugetVersion |> Some
Description = summary |> Some
Homepage = repo |> Some
Repository =
{ Json.RepositoryValue.Type = "git" |> Some
Json.RepositoryValue.Url = repo |> Some
Json.RepositoryValue.Directory = None }
|> Some
Bugs =
{ Json.BugsValue.Url =
@"https://github.com/Shmew/Feliz.Recoil/issues/new/choose" |> Some } |> Some
License = "MIT" |> Some
Author = author |> Some
Private = true |> Some }
Json.setJsonPkg setValues
// --------------------------------------------------------------------------------------
// Documentation targets
Target.createFinal "KillProcess" <| fun _ ->
Process.killAllByName "node.exe"
Process.killAllByName "Node.js"
Target.create "Start" <| fun _ ->
let buildApp = async { Yarn.exec "start" id }
let launchBrowser =
let url = "http://localhost:8080"
async {
do! Async.Sleep 15000
try
if Environment.isLinux then
Shell.Exec(
sprintf "URL=\"%s\"; xdg-open $URL ||\
sensible-browser $URL || x-www-browser $URL || gnome-open $URL" url)
|> ignore
else Shell.Exec("open", args = url) |> ignore
with _ -> failwith "Opening browser failed."
}
Target.activateFinal "KillProcess"
[ buildApp; launchBrowser ]
|> Async.Parallel
|> Async.RunSynchronously
|> ignore
Target.create "DemoRaw" <| fun _ ->
Yarn.exec "compile-demo-raw" id
Target.create "PublishPages" <| fun _ ->
Yarn.exec "publish-docs" id
// --------------------------------------------------------------------------------------
// Build and release NuGet targets
Target.create "NuGet" <| fun _ ->
Paket.pack(fun p ->
{ p with
OutputPath = bin
Version = release.NugetVersion
ReleaseNotes = Fake.Core.String.toLines release.Notes
ProjectUrl = repo
MinimumFromLockFile = true
IncludeReferencedProjects = true })
Target.create "NuGetPublish" <| fun _ ->
Paket.push(fun p ->
{ p with
ApiKey =
match getEnvFromAllOrNone "NUGET_KEY" with
| Some key -> key
| None -> failwith "The NuGet API key must be set in a NUGET_KEY environment variable"
WorkingDir = bin })
// --------------------------------------------------------------------------------------
// Release Scripts
let gitPush msg =
Git.Staging.stageAll ""
Git.Commit.exec "" msg
Git.Branches.push ""
Target.create "GitPush" <| fun p ->
p.Context.Arguments
|> List.choose (fun s ->
match s.StartsWith("--Msg=") with
| true -> Some(s.Substring 6)
| false -> None)
|> List.tryHead
|> function
| Some(s) -> s
| None -> (sprintf "Bump version to %s" release.NugetVersion)
|> gitPush
Target.create "GitTag" <| fun _ ->
Git.Branches.tag "" release.NugetVersion
Git.Branches.pushTag "" "origin" release.NugetVersion
Target.create "PublishDocs" <| fun _ ->
gitPush "Publishing docs"
// --------------------------------------------------------------------------------------
// Run all targets by default. Invoke 'build -t <Target>' to override
Target.create "All" ignore
Target.create "Dev" ignore
Target.create "Release" ignore
Target.create "Publish" ignore
"Clean"
==> "AssemblyInfo"
==> "Restore"
==> "PackageJson"
==> "YarnInstall"
==> "Build"
==> "PostBuildClean"
==> "CopyBinaries"
"Build" ==> "RunTests"
"Build"
==> "PostBuildClean"
==> "PublishDotNet"
==> "PostPublishClean"
==> "CopyBinaries"
"Restore" ==> "Lint"
"Restore" ==> "Format"
"Format"
?=> "Lint"
?=> "Build"
?=> "RunTests"
?=> "CleanDocs"
"Restore" ==> "LoadScripts"
"All"
==> "GitPush"
?=> "GitTag"
"All" <== ["Lint"; "RunTests"; "CopyBinaries" ]
"CleanDocs"
==> "CopyDocFiles"
==> "PrepDocs"
"All"
==> "NuGet"
==> "NuGetPublish"
"PrepDocs"
==> "PublishPages"
==> "PublishDocs"
"All"
==> "PrepDocs"
==> "DemoRaw"
"All"
==> "PrepDocs"
==> "Start"
"All" ==> "PublishPages"
"ConfigDebug" ?=> "Clean"
"ConfigRelease" ?=> "Clean"
"Dev" <== ["All"; "ConfigDebug"; "Start"]
"Release" <== ["All"; "NuGet"; "ConfigRelease"]
"Publish" <== ["Release"; "ConfigRelease"; "NuGetPublish"; "PublishDocs"; "GitTag"; "GitPush" ]
Target.runOrDefaultWithArguments "Dev"