generated from petabridge/Petabridge.App
-
Notifications
You must be signed in to change notification settings - Fork 2
/
build.fsx
445 lines (356 loc) · 16.6 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
#I @"tools/FAKE/tools"
#r "FakeLib.dll"
open System
open System.IO
open System.Text
open Fake
open Fake.DotNetCli
open Fake.DocFxHelper
// Information about the project for Nuget and Assembly info files
let configuration = "Release"
// Metadata used when signing packages and DLLs
let signingName = "My Library"
let signingDescription = "My REALLY COOL Library"
let signingUrl = "https://signing.is.cool/"
// Read release notes and version
let solutionFile = FindFirstMatchingFile "*.sln" __SOURCE_DIRECTORY__ // dynamically look up the solution
let buildNumber = environVarOrDefault "BUILD_NUMBER" "0"
let hasTeamCity = (not (buildNumber = "0")) // check if we have the TeamCity environment variable for build # set
let preReleaseVersionSuffix = "beta" + (if (not (buildNumber = "0")) then (buildNumber) else DateTime.UtcNow.Ticks.ToString())
let releaseNotes =
File.ReadLines (__SOURCE_DIRECTORY__ @@ "RELEASE_NOTES.md")
|> ReleaseNotesHelper.parseReleaseNotes
let versionFromReleaseNotes =
match releaseNotes.SemVer.PreRelease with
| Some r -> r.Origin
| None -> ""
let versionSuffix =
match (getBuildParam "nugetprerelease") with
| "dev" -> preReleaseVersionSuffix
| "" -> versionFromReleaseNotes
| str -> str
// Directories
let toolsDir = __SOURCE_DIRECTORY__ @@ "tools"
let output = __SOURCE_DIRECTORY__ @@ "bin"
let outputTests = __SOURCE_DIRECTORY__ @@ "TestResults"
let outputPerfTests = __SOURCE_DIRECTORY__ @@ "PerfResults"
let outputNuGet = output @@ "nuget"
Target "Clean" (fun _ ->
ActivateFinalTarget "KillCreatedProcesses"
CleanDir output
CleanDir outputTests
CleanDir outputPerfTests
CleanDir outputNuGet
CleanDir "docs/_site"
)
Target "AssemblyInfo" (fun _ ->
XmlPokeInnerText "./src/Directory.Build.props" "//Project/PropertyGroup/VersionPrefix" releaseNotes.AssemblyVersion
XmlPokeInnerText "./src/Directory.Build.props" "//Project/PropertyGroup/PackageReleaseNotes" (releaseNotes.Notes |> String.concat "\n")
)
Target "Build" (fun _ ->
DotNetCli.Build
(fun p ->
{ p with
Project = solutionFile
Configuration = configuration }) // "Rebuild"
)
//--------------------------------------------------------------------------------
// Tests targets
//--------------------------------------------------------------------------------
module internal ResultHandling =
let (|OK|Failure|) = function
| 0 -> OK
| x -> Failure x
let buildErrorMessage = function
| OK -> None
| Failure errorCode ->
Some (sprintf "xUnit2 reported an error (Error Code %d)" errorCode)
let failBuildWithMessage = function
| DontFailBuild -> traceError
| _ -> (fun m -> raise(FailedTestsException m))
let failBuildIfXUnitReportedError errorLevel =
buildErrorMessage
>> Option.iter (failBuildWithMessage errorLevel)
Target "RunTests" (fun _ ->
let projects =
match (isWindows) with
| true -> !! "./src/**/*.Tests.csproj"
| _ -> !! "./src/**/*.Tests.csproj" // if you need to filter specs for Linux vs. Windows, do it here
let runSingleProject project =
let arguments =
match (hasTeamCity) with
| true -> (sprintf "test -c Release --no-build --logger:trx --logger:\"console;verbosity=normal\" --results-directory %s -- -parallel none -teamcity" (outputTests))
| false -> (sprintf "test -c Release --no-build --logger:trx --logger:\"console;verbosity=normal\" --results-directory %s -- -parallel none" (outputTests))
let result = ExecProcess(fun info ->
info.FileName <- "dotnet"
info.WorkingDirectory <- (Directory.GetParent project).FullName
info.Arguments <- arguments) (TimeSpan.FromMinutes 30.0)
ResultHandling.failBuildIfXUnitReportedError TestRunnerErrorLevel.Error result
projects |> Seq.iter (log)
projects |> Seq.iter (runSingleProject)
)
Target "NBench" <| fun _ ->
let projects =
match (isWindows) with
| true -> !! "./src/**/*.Tests.Performance.csproj"
| _ -> !! "./src/**/*.Tests.Performance.csproj" // if you need to filter specs for Linux vs. Windows, do it here
let runSingleProject project =
let arguments =
match (hasTeamCity) with
| true -> (sprintf "nbench --nobuild --teamcity --concurrent true --trace true --output %s" (outputPerfTests))
| false -> (sprintf "nbench --nobuild --concurrent true --trace true --output %s" (outputPerfTests))
let result = ExecProcess(fun info ->
info.FileName <- "dotnet"
info.WorkingDirectory <- (Directory.GetParent project).FullName
info.Arguments <- arguments) (TimeSpan.FromMinutes 30.0)
ResultHandling.failBuildIfXUnitReportedError TestRunnerErrorLevel.Error result
projects |> Seq.iter runSingleProject
//--------------------------------------------------------------------------------
// Serialization
//--------------------------------------------------------------------------------
Target "Protobuf" <| fun _ ->
let protocPath =
if isWindows then findToolInSubPath "protoc.exe" "tools/Google.Protobuf.Tools/tools/windows_x64"
elif isMacOS then findToolInSubPath "protoc" "tools/Google.Protobuf.Tools/tools/macosx_x64"
else findToolInSubPath "protoc" "tools/Google.Protobuf.Tools/tools/linux_x64"
// SAMPLE PROTOFILES - replace with your own
let protoFiles = [
("Akka.Cqrs.proto", "/src/Akka.CQRS/Serialization/Proto/")
("Akka.Cqrs.Pricing.proto", "/src/Akka.CQRS.Pricing/Serialization/Proto") ]
printfn "Using proto.exe: %s" protocPath
let runProtobuf assembly =
let protoName, destinationPath = assembly
let args = StringBuilder()
|> append (sprintf "-I=%s" (__SOURCE_DIRECTORY__ @@ "/src/protobuf/") )
|> append (sprintf "--csharp_out=internal_access:%s" (__SOURCE_DIRECTORY__ @@ destinationPath))
|> append "--csharp_opt=file_extension=.g.cs"
|> append (__SOURCE_DIRECTORY__ @@ "/src/protobuf" @@ protoName)
|> toText
let result = ExecProcess(fun info ->
info.FileName <- protocPath
info.WorkingDirectory <- (Path.GetDirectoryName (FullName protocPath):string)
info.Arguments <- args) (System.TimeSpan.FromMinutes 45.0) (* Reasonably long-running task. *)
if result <> 0 then failwithf "protoc failed. %s %s" protocPath args
protoFiles |> Seq.iter (runProtobuf)
//--------------------------------------------------------------------------------
// Code signing targets
//--------------------------------------------------------------------------------
Target "SignPackages" (fun _ ->
let canSign = hasBuildParam "SignClientSecret" && hasBuildParam "SignClientUser"
if(canSign) then
log "Signing information is available."
let assemblies = !! (outputNuGet @@ "*.nupkg")
let signPath =
let globalTool = tryFindFileOnPath "SignClient.exe"
match globalTool with
| Some t -> t
| None -> if isWindows then findToolInSubPath "SignClient.exe" "tools/signclient"
elif isMacOS then findToolInSubPath "SignClient" "tools/signclient"
else findToolInSubPath "SignClient" "tools/signclient"
let signAssembly assembly =
let args = StringBuilder()
|> append "sign"
|> append "--config"
|> append (__SOURCE_DIRECTORY__ @@ "appsettings.json")
|> append "-i"
|> append assembly
|> append "-r"
|> append (getBuildParam "SignClientUser")
|> append "-s"
|> append (getBuildParam "SignClientSecret")
|> append "-n"
|> append signingName
|> append "-d"
|> append signingDescription
|> append "-u"
|> append signingUrl
|> toText
let result = ExecProcess(fun info ->
info.FileName <- signPath
info.WorkingDirectory <- __SOURCE_DIRECTORY__
info.Arguments <- args) (System.TimeSpan.FromMinutes 5.0) (* Reasonably long-running task. *)
if result <> 0 then failwithf "SignClient failed.%s" args
assemblies |> Seq.iter (signAssembly)
else
log "SignClientSecret not available. Skipping signing"
)
//--------------------------------------------------------------------------------
// Nuget targets
//--------------------------------------------------------------------------------
let overrideVersionSuffix (project:string) =
match project with
| _ -> versionSuffix // add additional matches to publish different versions for different projects in solution
Target "CreateNuget" (fun _ ->
let projects = !! "src/**/*.csproj"
-- "src/**/*Tests.csproj" // Don't publish unit tests
-- "src/**/*Tests*.csproj"
let runSingleProject project =
DotNetCli.Pack
(fun p ->
{ p with
Project = project
Configuration = configuration
AdditionalArgs = ["--include-symbols --no-build"]
VersionSuffix = overrideVersionSuffix project
OutputPath = outputNuGet })
projects |> Seq.iter (runSingleProject)
)
Target "PublishNuget" (fun _ ->
let projects = !! "./bin/nuget/*.nupkg" -- "./bin/nuget/*.symbols.nupkg"
let apiKey = getBuildParamOrDefault "nugetkey" ""
let source = getBuildParamOrDefault "nugetpublishurl" ""
let symbolSource = getBuildParamOrDefault "symbolspublishurl" ""
let shouldPublishSymbolsPackages = not (symbolSource = "")
if (not (source = "") && not (apiKey = "") && shouldPublishSymbolsPackages) then
let runSingleProject project =
DotNetCli.RunCommand
(fun p ->
{ p with
TimeOut = TimeSpan.FromMinutes 10. })
(sprintf "nuget push %s --api-key %s --source %s --symbol-source %s" project apiKey source symbolSource)
projects |> Seq.iter (runSingleProject)
else if (not (source = "") && not (apiKey = "") && not shouldPublishSymbolsPackages) then
let runSingleProject project =
DotNetCli.RunCommand
(fun p ->
{ p with
TimeOut = TimeSpan.FromMinutes 10. })
(sprintf "nuget push %s --api-key %s --source %s" project apiKey source)
projects |> Seq.iter (runSingleProject)
)
//--------------------------------------------------------------------------------
// Docker images
//--------------------------------------------------------------------------------
let GetDockerProjects =
let dockerFiles = !! "src/**/Dockerfile" // folders with Dockerfiles in it
let projects = dockerFiles
|> Seq.map (fun dFile -> Path.GetDirectoryName(dFile))
|> Seq.map (fun folder -> !! (folder + "/*.csproj"))
|> Seq.concat
projects
Target "PublishCode" (fun _ ->
ActivateFinalTarget "KillCreatedProcesses"
let projects = GetDockerProjects
let runSingleProject project =
DotNetCli.Publish
(fun p ->
{ p with
Project = project
Configuration = configuration
})
projects|> Seq.iter (runSingleProject)
)
let mapDockerImageName (projectName:string) =
match projectName with
| str -> Some(str.ToLowerInvariant())
Target "BuildDockerImages" (fun _ ->
let projects = GetDockerProjects
let remoteRegistryUrl = getBuildParamOrDefault "remoteRegistry" ""
let composedGetFileNameWithoutExtension (p:string) =
System.IO.Path.GetFileNameWithoutExtension p
let buildDockerImage imageName projectPath =
let args =
if(hasBuildParam "remoteRegistry") then
StringBuilder()
|> append "build"
|> append "-t"
|> append (imageName + ":" + releaseNotes.AssemblyVersion)
|> append "-t"
|> append (imageName + ":latest")
|> append "-t"
|> append (remoteRegistryUrl + "/" + imageName + ":" + releaseNotes.AssemblyVersion)
|> append "-t"
|> append (remoteRegistryUrl + "/" + imageName + ":latest")
|> append "."
|> toText
else
StringBuilder()
|> append "build"
|> append "-t"
|> append (imageName + ":" + releaseNotes.AssemblyVersion)
|> append "-t"
|> append (imageName + ":latest")
|> append "."
|> toText
let composedGetDirName (p:string) =
System.IO.Path.GetDirectoryName p
ExecProcess(fun info ->
info.FileName <- "docker"
info.WorkingDirectory <- composedGetDirName projectPath
info.Arguments <- args) (System.TimeSpan.FromMinutes 5.0) (* Reasonably long-running task. *)
let runSingleProject project =
let projectName = composedGetFileNameWithoutExtension project
let imageName = mapDockerImageName projectName
let result = match imageName with
| None -> 0
| Some(name) -> buildDockerImage name project
if result <> 0 then failwithf "docker build failed. %s" project
projects |> Seq.iter (runSingleProject)
)
//--------------------------------------------------------------------------------
// Documentation
//--------------------------------------------------------------------------------
Target "DocFx" (fun _ ->
DotNetCli.Restore (fun p -> { p with Project = solutionFile })
DotNetCli.Build (fun p -> { p with Project = solutionFile; Configuration = configuration })
let docsPath = "./docs"
DocFx (fun p ->
{ p with
Timeout = TimeSpan.FromMinutes 30.0;
WorkingDirectory = docsPath;
DocFxJson = docsPath @@ "docfx.json" })
)
//--------------------------------------------------------------------------------
// Cleanup
//--------------------------------------------------------------------------------
FinalTarget "KillCreatedProcesses" (fun _ ->
log "Shutting down dotnet build-server"
let result = ExecProcess(fun info ->
info.FileName <- "dotnet"
info.WorkingDirectory <- __SOURCE_DIRECTORY__
info.Arguments <- "build-server shutdown") (System.TimeSpan.FromMinutes 2.0)
if result <> 0 then failwithf "dotnet build-server shutdown failed"
)
//--------------------------------------------------------------------------------
// Help
//--------------------------------------------------------------------------------
Target "Help" <| fun _ ->
List.iter printfn [
"usage:"
"./build.ps1 [target]"
""
" Targets for building:"
" * Build Builds"
" * Nuget Create and optionally publish nugets packages"
" * SignPackages Signs all NuGet packages, provided that the following arguments are passed into the script: SignClientSecret={secret} and SignClientUser={username}"
" * RunTests Runs tests"
" * All Builds, run tests, creates and optionally publish nuget packages"
" * DocFx Creates a DocFx-based website for this solution"
""
" Other Targets"
" * Help Display this help"
""]
//--------------------------------------------------------------------------------
// Target dependencies
//--------------------------------------------------------------------------------
Target "BuildRelease" DoNothing
Target "All" DoNothing
Target "Docker" DoNothing
Target "Nuget" DoNothing
// build dependencies
"Clean" ==> "AssemblyInfo" ==> "Build" ==> "BuildRelease"
// tests dependencies
"Build" ==> "RunTests"
// nuget dependencies
"Clean" ==> "Build" ==> "CreateNuget"
"CreateNuget" ==> "SignPackages" ==> "PublishNuget" ==> "Nuget"
// docs
"Clean" ==> "BuildRelease" ==> "Docfx"
// Docker
"BuildRelease" ==> "PublishCode" ==> "BuildDockerImages" ==> "Docker"
// all
"BuildRelease" ==> "All"
"RunTests" ==> "All"
"NBench" ==> "All"
"Nuget" ==> "All"
RunTargetOrDefault "Help"