-
Notifications
You must be signed in to change notification settings - Fork 17
/
build.fsx
381 lines (307 loc) · 12 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
// --------------------------------------------------------------------------------------
// 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 Tools.Linting
open Tools.Web
open System
// The name of the project
// (used by attributes in AssemblyInfo, name of a NuGet package and directory in 'src')
let project = "Fable.SignalR"
// Short summary of the project
// (used as description in AssemblyInfo and as a short summary for NuGet package)
let summary = "Fable and server bindings for SignalR."
// Author(s) of the project
let author = "Cody Johnson"
// File system information
let solutionFile = "Fable.SignalR.sln"
// Github repo
let repo = "https://github.com/Shmew/Fable.SignalR"
// Files that have bindings to other languages where name linting needs to be more relaxed.
let relaxedNameLinting =
[ __SOURCE_DIRECTORY__ @@ "src/Fable.SignalR/*.fs"
__SOURCE_DIRECTORY__ @@ "src/Fable.SignalR.Elmish/*.fs"
__SOURCE_DIRECTORY__ @@ "src/Fable.SignalR.Feliz/*.fs"
__SOURCE_DIRECTORY__ @@ "src/Fable.SignalR.Shared/*.fs"
__SOURCE_DIRECTORY__ @@ "tests/**/*.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 docs = __SOURCE_DIRECTORY__ @@ "docs"
let temp = __SOURCE_DIRECTORY__ @@ "temp"
let objFolder = __SOURCE_DIRECTORY__ @@ "obj"
let dist = __SOURCE_DIRECTORY__ @@ "dist"
let libGlob = __SOURCE_DIRECTORY__ @@ "src" @@ "**" @@ "*.fsproj"
let demoGlob = __SOURCE_DIRECTORY__ @@ "demo" @@ "**" @@ "*.fsproj"
let dotnetTestGlob = __SOURCE_DIRECTORY__ @@ "tests" @@ "*DotNet*" @@ "*.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 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"
let killProcs () =
Process.killAllCreatedProcesses()
Process.killAllByName "node"
Process.killAllByName "MSBuild"
Target.createFinal "KillProcess" <| fun _ ->
killProcs()
// --------------------------------------------------------------------------------------
// Set configuration mode based on target
Target.create "ConfigDebug" <| fun _ ->
FakeVar.set "configuration" "Debug"
Target.create "ConfigRelease" <| fun _ ->
FakeVar.set "configuration" "Release"
// --------------------------------------------------------------------------------------
// 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; dist]
|> Shell.cleanDirs
TaskRunner.runWithRetries clean 10
Target.create "CleanDocs" <| fun _ ->
let clean() =
!! (docs @@ "RELEASE_NOTES.md")
|> List.ofSeq
|> List.iter Shell.rm
TaskRunner.runWithRetries clean 10
Target.create "CopyDocFiles" <| fun _ ->
[ docs @@ "RELEASE_NOTES.md", __SOURCE_DIRECTORY__ @@ "RELEASE_NOTES.md" ]
|> List.iter (fun (target, source) -> Shell.copyFile target source)
Target.create "PrepDocs" ignore
// --------------------------------------------------------------------------------------
// Restore tasks
let restoreSolution () =
solutionFile
|> DotNet.restore id
Target.create "Restore" <| fun _ ->
TaskRunner.runWithRetries restoreSolution 5
Target.create "YarnInstall" <| fun _ ->
if Environment.isWindows then
let setParams (defaults:Yarn.YarnParams) =
{ defaults with
Yarn.YarnParams.YarnFilePath = (__SOURCE_DIRECTORY__ @@ "packages/tooling/Yarnpkg.Yarn/content/bin/yarn.cmd")
}
Yarn.install setParams
else Yarn.install id
Target.create "RebuildSass" <| fun _ ->
Target.activateFinal "KillProcess"
TaskRunner.runWithRetries (fun () -> Npm.exec "rebuild node-sass" id) 5
// --------------------------------------------------------------------------------------
// 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"
]
}
Target.activateFinal "KillProcess"
restoreSolution()
!! libGlob
++ demoGlob
++ dotnetTestGlob
|> List.ofSeq
|> List.iter (MSBuild.build setParams)
// --------------------------------------------------------------------------------------
// Lint source code
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 tests
Target.create "RunTests" <| fun _ ->
Target.activateFinal "KillProcess"
!! (__SOURCE_DIRECTORY__ @@ "tests" @@ "**" @@ "bin" @@ configuration() @@ "**" @@ "*Tests.exe")
|> Seq.iter (fun f ->
killProcs()
Command.RawCommand(f, Arguments.Empty)
|> CreateProcess.fromCommand
|> CreateProcess.withTimeout (System.TimeSpan.MaxValue)
|> CreateProcess.ensureExitCodeWithMessage "Tests failed."
|> 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/Fable.SignalR/issues/new/choose" |> Some } |> Some
License = "MIT" |> Some
Author = author |> Some
Private = true |> Some }
Json.setJsonPkg setValues
Target.create "Start" <| fun _ ->
Yarn.exec "start" 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
Target.create "CI" ignore
"Clean"
==> "Restore"
==> "PackageJson"
==> "YarnInstall"
==> "Lint"
==> "Build"
==> "RebuildSass"
==> "RunTests"
"All"
==> "GitPush"
?=> "GitTag"
"All" <== ["Lint"; "RunTests"]
"CleanDocs"
==> "CopyDocFiles"
==> "PrepDocs"
"All"
==> "NuGet"
?=> "NuGetPublish"
"PrepDocs"
==> "PublishPages"
==> "PublishDocs"
"All"
==> "PrepDocs"
"All"
==> "PrepDocs"
==> "Start"
"All" ==> "PublishPages"
"ConfigDebug" ?=> "Clean"
"ConfigRelease" ?=> "Clean"
"Dev" <== ["All"; "ConfigDebug"; "Start"]
"Release" <== ["All"; "ConfigRelease"; "NuGet"]
"Publish" <== ["Release"; "NuGetPublish"; "PublishDocs"; "GitTag"; "GitPush" ]
"CI" <== ["RunTests"]
Target.runOrDefaultWithArguments "Dev"