-
Notifications
You must be signed in to change notification settings - Fork 18
/
build.fsx
210 lines (171 loc) · 9.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
#r @"build/tools/FAKE.Core/tools/FakeLib.dll"
open Fake
open Fake.AppVeyor
open System.Text.RegularExpressions
let sourceDir = "Src"
let solutionToBuild = sourceDir </> "Albedo.sln" |> FullName
let testProjectDir = sourceDir </> "Albedo.UnitTests" |> FullName
let tmpBuildDir = "build"
let configuration = getBuildParamOrDefault "BuildConfiguration" "Release"
let nugetOutputDir = tmpBuildDir </> "NuGetPackages" |> FullName
let nuGetPackages = !! (nugetOutputDir </> "*.nupkg" )
// Skip symbol packages because NuGet publish symbols automatically when package is published
-- (nugetOutputDir </> "*.symbols.nupkg")
type BuildVersionCalculationSource = { major: int; minor: int; revision: int; preSuffix: string;
commitsNum: int; sha: string; buildNumber: int }
let getVersionSourceFromGit buildNumber =
// The --fist-parent flag is required to correctly work for vNext branch.
// Example of output for a release tag: v3.50.2-288-g64fd5c5b, for a prerelease tag: v3.50.2-alpha1-288-g64fd5c5b
let desc = Git.CommandHelper.runSimpleGitCommand "" "describe --tags --long --abbrev=40 --first-parent --match=v*"
// Previously repository contained a few broken tags like "v.3.21.1". They were removed, but could still exist
// in forks. We handle them as well to not fail on such repositories.
let result = Regex.Match(desc,
@"^v(\.)?(?<maj>\d+)\.(?<min>\d+)\.(?<rev>\d+)(?<pre>-\w+\d*)?-(?<num>\d+)-g(?<sha>[a-z0-9]+)$",
RegexOptions.IgnoreCase)
.Groups
let getMatch (name:string) = result.[name].Value
{ major = getMatch "maj" |> int
minor = getMatch "min" |> int
revision = getMatch "rev" |> int
preSuffix = getMatch "pre"
commitsNum = getMatch "num" |> int
sha = getMatch "sha"
buildNumber = buildNumber
}
type BuildVersionInfo = { assemblyVersion:string; fileVersion:string; infoVersion:string; nugetVersion:string;
source: Option<BuildVersionCalculationSource> }
let calculateVersion source =
let s = source
let (major, minor, revision, preReleaseSuffix, commitsNum, sha, buildNumber) =
(s.major, s.minor, s.revision, s.preSuffix, s.commitsNum, s.sha, s.buildNumber)
let assemblyVersion = sprintf "%d.%d.0.0" major minor
let fileVersion = sprintf "%d.%d.%d.%d" major minor revision buildNumber
// If number of commits since last tag is greater than zero, we append another identifier with number of commits.
// The produced version is larger than the last tag version.
// If we are on a tag, we use version without modification.
// Examples of output: 3.50.2.1, 3.50.2.215, 3.50.1-rc1.3, 3.50.1-rc3.35
let nugetVersion = match commitsNum with
| 0 -> sprintf "%d.%d.%d%s" major minor revision preReleaseSuffix
| _ -> sprintf "%d.%d.%d%s.%d" major minor revision preReleaseSuffix commitsNum
let infoVersion = match commitsNum with
| 0 -> nugetVersion
| _ -> sprintf "%s-%s" nugetVersion sha
{ assemblyVersion=assemblyVersion; fileVersion=fileVersion; infoVersion=infoVersion; nugetVersion=nugetVersion;
source = Some source }
// Calculate version that should be used for the build. Define globally as data might be required by multiple targets.
// Please never name the build parameter with version as "Version" - it might be consumed by the MSBuild, override
// the defined properties and break some tasks (e.g. NuGet restore).
let buildVersion = match getBuildParamOrDefault "BuildVersion" "git" with
| "git" -> getBuildParamOrDefault "BuildNumber" "0"
|> int
|> getVersionSourceFromGit
|> calculateVersion
| assemblyVer -> { assemblyVersion = assemblyVer
fileVersion = getBuildParamOrDefault "BuildFileVersion" assemblyVer
infoVersion = getBuildParamOrDefault "BuildInfoVersion" assemblyVer
nugetVersion = getBuildParamOrDefault "BuildNugetVersion" assemblyVer
source = None }
let runMsBuild target configuration properties =
let verbosity = match getBuildParam "BuildVerbosity" |> toLower with
| "quiet" | "q" -> Quiet
| "minimal" | "m" -> Minimal
| "normal" | "n" -> Normal
| "detailed" | "d" -> Detailed
| "diagnostic" | "diag" -> Diagnostic
| _ -> Minimal
let configProperty = match configuration with
| Some c -> [ "Configuration", c ]
| _ -> []
let properties = configProperty @ properties
@ [ "AssemblyVersion", buildVersion.assemblyVersion
"FileVersion", buildVersion.fileVersion
"InformationalVersion", buildVersion.infoVersion
"PackageVersion", buildVersion.nugetVersion ]
solutionToBuild
|> build (fun p -> { p with MaxCpuCount = Some None
Verbosity = Some verbosity
Targets = [ target ]
Properties = properties })
Target "Restore" (fun _ ->
runMsBuild "Restore" None []
)
Target "Clean" (fun _ ->
CleanDir nugetOutputDir
)
Target "Verify" (fun _ ->
runMsBuild "Rebuild" (Some "Verify") []
)
Target "Build" (fun _ ->
runMsBuild "Rebuild" (Some configuration) []
)
Target "Test" (fun _ ->
DotNetCli.Test (fun p -> {p with Configuration = configuration
WorkingDir = testProjectDir
AdditionalArgs = [ "--no-build" ]})
)
Target "Pack" (fun _ ->
runMsBuild "Pack" (Some configuration) [ "IncludeSource", "true"
"IncludeSymbols", "true"
"PackageOutputPath", FullName nugetOutputDir
"NoBuild", "true" ]
)
let publishPackagesWithSymbols packageFeed symbolFeed accessKey =
nuGetPackages
|> Seq.map (fun pkg ->
let meta = GetMetaDataFromPackageFile pkg
meta.Id, meta.Version
)
|> Seq.iter (fun (id, version) -> NuGetPublish (fun p -> { p with Project = id
Version = version
OutputPath = nugetOutputDir
PublishUrl = packageFeed
AccessKey = accessKey
SymbolPublishUrl = symbolFeed
SymbolAccessKey = accessKey
WorkingDir = nugetOutputDir }))
Target "PublishNuGetPublic" (fun _ ->
let feed = "https://www.nuget.org/api/v2/package"
let key = getBuildParam "NuGetPublicKey"
publishPackagesWithSymbols feed "" key
)
"Restore"
==> "Clean"
==> "Verify"
==> "Build"
==> "Test"
==> "Pack"
==> "PublishNuGetPublic"
// ==============================================
// ================== AppVeyor ==================
// ==============================================
// Add helper to identify whether current trigger is PR
type AppVeyorEnvironment with
static member IsPullRequest = isNotNullOrEmpty AppVeyorEnvironment.PullRequestNumber
type AppVeyorTrigger = SemVerTag | CustomTag | PR | Unknown
let anAppVeyorTrigger =
let tag = if AppVeyorEnvironment.RepoTag then Some AppVeyorEnvironment.RepoTagName else None
let isPR = AppVeyorEnvironment.IsPullRequest
let branch = if isNotNullOrEmpty AppVeyorEnvironment.RepoBranch then Some AppVeyorEnvironment.RepoBranch else None
match tag, isPR, branch with
| Some t, _, _ when "v\d+.*" >** t -> SemVerTag
| Some _, _, _ -> CustomTag
| _, true, _ -> PR
| _ -> Unknown
// Print state info at the very beginning
if buildServer = BuildServer.AppVeyor
then logfn "[AppVeyor state] Is tag: %b, tag name: '%s', is PR: %b, branch name: '%s', trigger: %A"
AppVeyorEnvironment.RepoTag
AppVeyorEnvironment.RepoTagName
AppVeyorEnvironment.IsPullRequest
AppVeyorEnvironment.RepoBranch
anAppVeyorTrigger
Target "AppVeyor" (fun _ ->
//Artifacts might be deployable, so we update build version to find them later by file version
if not AppVeyorEnvironment.IsPullRequest then UpdateBuildVersion buildVersion.fileVersion
)
// Add logic to resolve action based on current trigger info
dependency "AppVeyor" <| match anAppVeyorTrigger with
| SemVerTag -> "PublishNuGetPublic"
| PR | CustomTag | Unknown -> "Pack"
// ========= ENTRY POINT =========
RunTargetOrDefault "Pack"