-
Notifications
You must be signed in to change notification settings - Fork 43
/
Build.fsx
187 lines (153 loc) · 8.92 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
#r @"build/tools/FAKE.Core/tools/FakeLib.dll"
open Fake
open Fake.AppVeyor
open Fake.DotNetCli
open System.Text.RegularExpressions
let buildDir = getBuildParamOrDefault "BuildDir" "build"
let buildToolsDir = buildDir </> "tools"
let nuGetOutputFolder = buildDir </> "NuGetPackages"
let nuGetPackages = !! (nuGetOutputFolder </> "*.nupkg" )
// Skip symbol packages because NuGet publish symbols automatically when package is published.
-- (nuGetOutputFolder </> "*.symbols.nupkg")
let solutionToBuild = "Src/Fare.sln"
let testProjectDir = "Src/Fare.IntegrationTests"
let configuration = getBuildParamOrDefault "BuildConfiguration" "Release"
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 runDotNet command configuration properties =
let stringProps = properties
@ [ "AssemblyVersion", buildVersion.assemblyVersion
"FileVersion", buildVersion.fileVersion
"InformationalVersion", buildVersion.infoVersion
"PackageVersion", buildVersion.nugetVersion ]
|> Seq.map(fun (name, value) -> sprintf "/p:%s=%s" name value )
|> String.concat " "
DotNetCli.RunCommand id
(sprintf "%s %s --configuration %s %s" command solutionToBuild configuration stringProps)
Target "Verify" (fun _ -> runDotNet "build" configuration [])
Target "Build" (fun _ -> runDotNet "build" configuration [])
Target "Test" (fun _ ->
DotNetCli.RunCommand (fun p -> { p with WorkingDir = testProjectDir })
(sprintf "test --configuration %s" configuration)
)
Target "CleanNuGetPackages" (fun _ ->
CleanDir nuGetOutputFolder
)
Target "NuGetPack" (fun _ ->
// Pack projects using MSBuild.
runDotNet "pack" configuration [ "IncludeSource", "true"
"IncludeSymbols", "true"
"PackageOutputPath", FullName nuGetOutputFolder ]
)
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 = nuGetOutputFolder
PublishUrl = packageFeed
AccessKey = accessKey
SymbolPublishUrl = symbolFeed
SymbolAccessKey = accessKey
WorkingDir = nuGetOutputFolder
ToolPath = buildToolsDir </> "nuget.exe" }))
Target "PublishNuGetPublic" (fun _ ->
let feed = "https://www.nuget.org/api/v2/package"
let key = getBuildParam "NuGetPublicKey"
publishPackagesWithSymbols feed "" key
)
Target "CompleteBuild" DoNothing
"CleanNuGetPackages" ==> "NuGetPack"
"Verify" ==> "NuGetPack"
"Test" ==> "NuGetPack"
"NuGetPack" ==> "CompleteBuild"
"NuGetPack" ==> "PublishNuGetPublic"
// ==============================================
// ================== AppVeyor ==================
// ==============================================
// Add a 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 -> "CompleteBuild"
// ========= ENTRY POINT =========
RunTargetOrDefault "CompleteBuild"