forked from dolittle/Bifrost
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.fsx
476 lines (392 loc) · 19.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
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
#I "Source/Solutions/packages/FAKE/tools/"
#I "Source/Solutions/packages/FAKE/FSharp.Data/lib/net40"
#r "FakeLib.dll"
#r "FSharp.Data.dll"
open Fake
open Fake.RestorePackageHelper
open Fake.Git
open System
open System.IO
open System.Linq
open System.Text.RegularExpressions
open FSharp.Data
open FSharp.Data.JsonExtensions
open FSharp.Data.HttpRequestHeaders
open Fake.FileHelper
open Fake.FileSystemHelper
open Fake.ProcessHelper
open Fake.MSBuildHelper
open AssemblyInfoFile
// https://github.com/krauthaufen/DevILSharp/blob/master/build.fsx
// http://blog.2mas.xyz/take-control-of-your-build-ci-and-deployment-with-fsharp-fake/
let versionRegex = Regex("(\d+).(\d+).(\d+)-*([a-z]+)*[+-]*(\d+)*", RegexOptions.Compiled)
type BuildVersion(major:int, minor:int, patch: int, build:int, preReleaseString:string, release:bool) =
let major = major
let minor = minor
let patch = patch
let preReleaseString = preReleaseString
member this.Major with get() = major
member this.Minor with get() = minor
member this.Patch with get() = patch
member this.Build with get() = build
member this.PreReleaseString with get() = preReleaseString
member this.AsString() : string =
if String.IsNullOrEmpty(preReleaseString) then
if release then
sprintf "%d.%d.%d" major minor patch
else
sprintf "%d.%d.%d-%d" major minor patch build
else
sprintf "%d.%d.%d-%s-%d" major minor patch preReleaseString build
member this.IsPreRelease with get() : bool = preReleaseString.Length > 0
member this.DoesMajorMinorPatchMatch(other:BuildVersion) =
other.Major = major && other.Minor = minor && other.Patch = patch
new (versionAsString:string) =
BuildVersion(versionAsString,0,false)
new (versionAsString:string, build:int, release:bool) =
let versionResult = versionRegex.Match versionAsString
if versionResult.Success then
let major = versionResult.Groups.[1].Value |> int
let minor = versionResult.Groups.[2].Value |> int
let patch = versionResult.Groups.[3].Value |> int
let build = if versionResult.Groups.Count = 6 && versionResult.Groups.[5].Value.Length > 0 then versionResult.Groups.[5].Value |> int else build
if versionResult.Groups.Count >= 5 then
BuildVersion(major,minor,patch,build,versionResult.Groups.[4].Value,release)
else
BuildVersion(major,minor,patch,build,"",release)
else
failwithf "Unable to resolve version from '%s'" versionAsString
BuildVersion(0,0,0,0,"",false)
let spawnProcess (processName:string, arguments:string) =
let startInfo = new System.Diagnostics.ProcessStartInfo(processName)
startInfo.Arguments <- arguments
startInfo.RedirectStandardInput <- true
startInfo.RedirectStandardOutput <- true
startInfo.RedirectStandardError <- true
startInfo.UseShellExecute <- false
startInfo.CreateNoWindow <- true
use proc = new System.Diagnostics.Process(StartInfo = startInfo)
proc.Start() |> ignore
let reader = new System.IO.StreamReader(proc.StandardOutput.BaseStream, System.Text.Encoding.UTF8)
let result = reader.ReadToEnd()
proc.WaitForExit()
if proc.ExitCode <> 0 then
failwith ("Problems spawning ("+processName+") with arguments ("+arguments+"): \r\n" + proc.StandardError.ReadToEnd())
result
let performGitCommand arguments:string =
spawnProcess("git", arguments)
let gitVersion repositoryDir =
let isWindows = System.Environment.OSVersion.Platform = PlatformID.Win32NT
let arguments = sprintf "%s /output json /showvariable SemVer" repositoryDir
let gitVersionExecutable = "Source/Solutions/packages/GitVersion.CommandLine/tools/GitVersion.exe"
let processName = if isWindows then gitVersionExecutable else "mono"
let fullArguments = if isWindows then arguments else sprintf "%s %s" gitVersionExecutable arguments
spawnProcess(processName, fullArguments)
let getCurrentBranch =
performGitCommand("rev-parse --abbrev-ref HEAD").Trim()
let getLatestTag repositoryDir =
//let commitSha = performGitCommand "rev-list --tags --max-count=1"
performGitCommand (sprintf "describe --tag --abbrev=0")
let getVersionFromGitTag(buildNumber:int) =
trace "Get version from Git tag"
let gitVersionTag = gitVersion "./"
tracef "Git tag version : %s" gitVersionTag
new BuildVersion(gitVersionTag, buildNumber, true)
let getLatestNuGetVersion =
trace "Get latest NuGet version"
let jsonAsString = Http.RequestString("https://api.nuget.org/v3/registration1/bifrost/index.json", headers = [ Accept HttpContentTypes.Json ])
let json = JsonValue.Parse(jsonAsString)
let items = json?items.AsArray().[0]?items.AsArray()
let item = items.[items.Length-1]
let catalogEntry = item?catalogEntry
let version = (catalogEntry?version.AsString())
new BuildVersion(version)
let updateProjectJsonFile(file:FileInfo, version:BuildVersion) =
tracef "Update version and dependency versions for '%s'" file.FullName
let json = JsonValue.Load file.FullName
let rec fixVersion json =
match json with
| JsonValue.String _ | JsonValue.Boolean _ | JsonValue.Float _ | JsonValue.Number _ | JsonValue.Null -> json
| JsonValue.Record properties ->
properties
|> Array.map (fun (key, value) -> key,
if key.StartsWith("Bifrost") || key.Equals("version") then
(version.AsString()) |> JsonValue.String
else
fixVersion value
)
|> JsonValue.Record
| JsonValue.Array array ->
array
|> Array.map fixVersion
|> JsonValue.Array
let fixedJson = fixVersion json
File.WriteAllText(file.FullName, sprintf "%O" fixedJson)
//*****************************************************************************
//* Globals
//*****************************************************************************
let company = "Dolittle"
let copyright = "(C) 2008 - 2017 Dolittle"
let trademark = ""
let solutionFile = "./Source/Solutions/Bifrost_All.sln"
let sourceDirectory = sprintf "%s/Source" __SOURCE_DIRECTORY__
let artifactsDirectory = sprintf "%s/artifacts" __SOURCE_DIRECTORY__
let nugetDirectory = sprintf "%s/nuget" artifactsDirectory
let projectDirectories = DirectoryInfo(sourceDirectory).GetDirectories "Bifrost*"
|> Array.filter(fun d -> d.Name.Contains("Spec") = false )
let projectJsonFiles = File.ReadAllLines "projects.txt"
|> Array.map(fun f -> new FileInfo(sprintf "./Source/%s/project.json" f))
let specDirectories = DirectoryInfo(sourceDirectory).GetDirectories "Bifrost*"
|> Array.filter(fun d -> d.Name.Contains("Spec") )
let specProjectJsonFiles = specDirectories
|> Array.map(fun d -> filesInDirMatching "project.json" d)
|> Array.concat
let appveyor = if String.IsNullOrWhiteSpace(System.Environment.GetEnvironmentVariable("APPVEYOR")) then false else true
let currentBranch = getCurrentBranch
// Versioning related
let envBuildNumber = System.Environment.GetEnvironmentVariable("APPVEYOR_BUILD_NUMBER")
let buildNumber = if String.IsNullOrWhiteSpace(envBuildNumber) then 0 else envBuildNumber |> int
let versionFromGitTag = getVersionFromGitTag buildNumber
let lastNuGetVersion = getLatestNuGetVersion
let sameVersion = versionFromGitTag.DoesMajorMinorPatchMatch lastNuGetVersion
// Determine if it is a release build - check if the latest NuGet deployment is a release build matching version number or not.
let isReleaseBuild = sameVersion && (not versionFromGitTag.IsPreRelease && lastNuGetVersion.IsPreRelease)
System.Environment.SetEnvironmentVariable("RELEASE_BUILD",if isReleaseBuild then "true" else "false")
let buildVersion = BuildVersion(versionFromGitTag.Major, versionFromGitTag.Minor, versionFromGitTag.Patch, buildNumber, versionFromGitTag.PreReleaseString,isReleaseBuild)
// Package related
let nugetPath = "./Source/Solutions/.nuget/NuGet.exe"
let nugetUrl = "https://www.nuget.org/api/v2/package"
let mygetUrl = "https://www.myget.org/F/bifrost/api/v2/package"
let nugetKey = System.Environment.GetEnvironmentVariable("NUGET_KEY")
let mygetKey = System.Environment.GetEnvironmentVariable("MYGET_KEY")
// Documentation related
let documentationUser = System.Environment.GetEnvironmentVariable("DOCS_USER")
let documentationUserToken = System.Environment.GetEnvironmentVariable("DOCS_TOKEN")
let documentationSolutionFile = "Source/Solutions/Bifrost_Documentation.sln"
printfn "<----------------------- BUILD DETAILS ----------------------->"
printfn "Git Branch : %s" currentBranch
printfn "Git Version : %s" (versionFromGitTag.AsString())
printfn "Last NuGet version : %s" (lastNuGetVersion.AsString())
printfn "Build version : %s" (buildVersion.AsString())
printfn "Version Same : %b" sameVersion
printfn "Release Build : %b" isReleaseBuild
printfn "Documentation User : %s" documentationUser
printfn "<----------------------- BUILD DETAILS ----------------------->"
//*****************************************************************************
//* Restore Packages
//*****************************************************************************
Target "RestorePackages" (fun _ ->
solutionFile
|> RestoreMSSolutionPackages (fun p ->
{ p with
OutputPath = "./Source/Solutions/packages"
Retries = 4 })
)
//*****************************************************************************
//* Build
//*****************************************************************************
Target "Build" <| fun _ ->
let buildMode = getBuildParamOrDefault "buildMode" "Release"
let setParams defaults =
{ defaults with
Verbosity = Some MSBuildVerbosity.Minimal
Properties =
[
"Optimize", "True"
]
}
build setParams solutionFile
|> DoNothing
//*****************************************************************************
//* Update project json files with correct version
//*****************************************************************************
Target "UpdateVersionOnBuildServer" (fun _ ->
if( appveyor ) then
tracef "Updating build version for AppVeyor to %s" (buildVersion.AsString())
let allArgs = sprintf "UpdateBuild -Version \"%s\"" (buildVersion.AsString())
spawnProcess("appveyor", allArgs) |> ignore
)
//*****************************************************************************
//* Update Assembly Info files with correct information
//*****************************************************************************
Target "UpdateAssemblyInfoFiles" (fun _ ->
let version = sprintf "%d.%d.%d.%d" buildVersion.Major buildVersion.Minor buildVersion.Patch buildVersion.Build
CreateCSharpAssemblyInfoWithConfig "Source/Common/CommonAssemblyInfo.cs" [
Attribute.Company company
Attribute.Copyright copyright
Attribute.Trademark trademark
Attribute.Version version
Attribute.FileVersion version
] <| AssemblyInfoFileConfig(false)
)
//*****************************************************************************
//* Update project.json files with correct version and all Bifrost dependencies
//* to be the same version as well. Since we're in one project, we deploy
//* all the packages in this repository at once
//*****************************************************************************
Target "UpdateProjectJsonFiles" (fun _ ->
for file in projectJsonFiles do
updateProjectJsonFile(file, buildVersion)
)
//*****************************************************************************
//* Build all .NET Core projects
//*****************************************************************************
Target "DotNetCoreBuild" (fun _ ->
for file in projectJsonFiles do
let restoreArgs = sprintf "restore %s" file.FullName
let restoreMessage = sprintf "**** Restoring for : %s *****" restoreArgs
trace restoreMessage
ProcessHelper.Shell.Exec("dotnet", args=restoreArgs) |> ignore
trace "**** RESTORING DONE ****"
let buildArgs = sprintf "build -c Release %s --no-incremental" file.FullName
let message = sprintf "**** BUILDING : %s *****" buildArgs
trace message
ProcessHelper.Shell.Exec("dotnet", args=buildArgs) |> ignore
trace "**** BUILDING DONE ****"
)
//*****************************************************************************
//* Run .NET CLI Test
//*****************************************************************************
Target "DotNetTest" (fun _ ->
for file in specProjectJsonFiles do
let restoreArgs = sprintf "restore %s" file.FullName
let restoreMessage = sprintf "**** Restoring for : %s *****" restoreArgs
trace restoreMessage
ProcessHelper.Shell.Exec("dotnet", args=restoreArgs) |> ignore
trace "**** RESTORING DONE ****"
let testArgs = sprintf "test -f \"netcoreapp1.1\" %s" file.FullName
let testMessage = sprintf "**** Running Specs for : %s *****" testArgs
trace testMessage
ProcessHelper.Shell.Exec("dotnet", args=testArgs) |> ignore
trace "**** Running Specs DONE ****"
)
//*****************************************************************************
//* Package all projects for NuGet
//*****************************************************************************
Target "PackageForNuGet" (fun _ ->
for file in projectJsonFiles do
let allArgs = sprintf "pack --no-build %s --output %s" file.FullName nugetDirectory
ProcessHelper.Shell.Exec("dotnet", args=allArgs) |> ignore
)
//*****************************************************************************
//* Run MSpec Specifications
//*****************************************************************************
Target "MSpec" (fun _ ->
let specFiles = !! ("Source/**/*.Specs.dll")
|> Seq.toArray
|> String.concat " "
let allArgs = sprintf "%s" specFiles
let mspec = if appveyor then "mspec" else "Tools/MSpec/mspec-clr4.exe"
ProcessHelper.Shell.Exec(mspec, args=allArgs) |> ignore
)
//*****************************************************************************
//* Run JavaScript Specifications
//*****************************************************************************
Target "JavaScriptSpecs" (fun _ ->
if Directory.Exists("TestResults") = false then Directory.CreateDirectory("TestResults") |> ignore
let allArgs = sprintf "Forseti.yaml ../TestResults/forseti.testresults.trx BUILD-CI"
let errorCode = ProcessHelper.Shell.Exec("Tools/Forseti/Forseti.Output.exe", args=allArgs, dir="Source")
if errorCode <> 0 then failwith "Running JavaScript specs failed"
)
//*****************************************************************************
//* Generate and publish documentation to site
//*****************************************************************************
Target "GenerateAndPublishDocumentation" (fun _ ->
if String.IsNullOrEmpty(documentationUser) then
trace "Skipping building and publishing documentation - user not set"
else
documentationSolutionFile
|> RestoreMSSolutionPackages (fun p ->
{ p with
OutputPath = "./Source/Solutions/packages"
Retries = 4 })
let buildMode = getBuildParamOrDefault "buildMode" "Release"
let setParams defaults =
{ defaults with
Verbosity = Some MSBuildVerbosity.Minimal
Properties =
[
"Optimize", "True"
]
}
build setParams documentationSolutionFile
|> DoNothing
let siteDir = "dolittle.github.io"
ProcessHelper.Shell.Exec("git" , args="clone https://github.com/dolittle/dolittle.github.io.git") |> ignore
FileHelper.CopyDir "dolittle.github.io/bifrost" "Source/Documentation/_site" (fun f -> true)
ProcessHelper.Shell.Exec("git" , args="add .", dir=siteDir) |> ignore
ProcessHelper.Shell.Exec("git" , args="config --global user.name \"Bifrost Documentation Account\"", dir=siteDir) |> ignore
ProcessHelper.Shell.Exec("git" , args="config --global user.email \"[email protected]\"", dir=siteDir) |> ignore
ProcessHelper.Shell.Exec("git" , args="commit -m \"<-- Autogenerated : documentation updated -->\"", dir=siteDir) |> ignore
let remoteUrl = sprintf "remote set-url origin https://%s:%[email protected]/dolittle/dolittle.github.io.git" documentationUser documentationUserToken
ProcessHelper.Shell.Exec("git" , args=remoteUrl, dir=siteDir) |> ignore
ProcessHelper.Shell.Exec("git" , args="push", dir=siteDir) |> ignore
FileHelper.DeleteDir siteDir
)
//*****************************************************************************
//* Deploy to NuGet if release mode
//*****************************************************************************
Target "DeployNugetPackages" (fun _ ->
let key = if( isReleaseBuild && String.IsNullOrEmpty(nugetKey) = false ) then nugetKey else mygetKey
let source = if( isReleaseBuild && String.IsNullOrEmpty(nugetKey) = false ) then nugetUrl else mygetUrl
if( String.IsNullOrEmpty(key) = false ) then
let packages = !! ("artifacts/nuget/*.nupkg")
|> Seq.toArray
for package in packages do
let allArgs = sprintf "push %s %s -Source %s" package key source
ProcessHelper.Shell.Exec(nugetPath, args=allArgs) |> ignore
else
trace "Not deploying to NuGet - no key set"
)
// ******** Pre Info
// Get Build Number from BuildServer
// Get Version from Git Tag
// Determine if it is a release build - check if the latest NuGet deployment is a release build matching version number or not.
// If tag is not a release tag - Append build number
// ******** BUILD:
// Restore packages
// Create Assembly Version from Tag + Build Number -> Update Assembly Info
// Build
// Run MSpec Specs
// Run JavaScript Specs
//
// If daily or alpha or beta - create nuget packages
// If daily and not alpha or beta -> Deploy to MyGet
// Else deploy to NuGet
// Note: Deploy package only if it is a release build or build parameter saying it should publish package
//
// Clone Documentation Repository
// DocFX for documentation -> Into Documentation repository
// Push changes to Documentation Repository
// Build pipeline
Target "BuildRelease" DoNothing
"UpdateVersionOnBuildServer" ==> "BuildRelease"
"RestorePackages" ==> "BuildRelease"
"Build" ==> "BuildRelease"
// Package pipeline
Target "Package" DoNothing
"UpdateAssemblyInfoFiles" ==> "Package"
"UpdateProjectJsonFiles" ==> "Package"
"DotNetCoreBuild" ==>
"PackageForNuGet" ==> "Package"
// Specifications pipeline
Target "Specifications" DoNothing
"MSpec" ==> "Specifications"
"JavaScriptSpecs" ==> "Specifications"
// Deployment pipeline
Target "Deploy" DoNothing
"DeployNugetPackages" ==> "Deploy"
Target "BuildAndSpecs" DoNothing
"BuildRelease" ==> "BuildAndSpecs"
"Specifications" ==> "BuildAndSpecs"
Target "PackageAndDeploy" DoNothing
"Package" ==> "PackageAndDeploy"
"GenerateAndPublishDocumentation" ==> "PackageAndDeploy"
"Deploy" ==> "PackageAndDeploy"
Target "DotNetCoreBuildAndSpecs" DoNothing
"DotNetCoreBuild" ==> "DotNetCoreBuildAndSpecs"
"DotNetTest" ==> "DotNetCoreBuildAndSpecs"
Target "All" DoNothing
"BuildAndSpecs" ==> "All"
"PackageAndDeploy" =?> ("All", currentBranch.Equals("master") or currentBranch.Equals("HEAD"))
RunTargetOrDefault "All"