-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbuild.fsx
298 lines (246 loc) · 8.88 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
#r @"packages/build/FAKE/tools/FakeLib.dll"
open Fake
open Fake.Git
open Fake.AssemblyInfoFile
open Fake.ReleaseNotesHelper
open Fake.UserInputHelper
open System
let release = LoadReleaseNotes "RELEASE_NOTES.md"
let productName = "Suave.SerilogExtensions"
let sln = "Suave.SerilogExtensions.sln"
let srcGlob =__SOURCE_DIRECTORY__ @@ "src/**/*.??proj"
let testsGlob = __SOURCE_DIRECTORY__ @@ "tests/**/*.??proj"
let distDir = __SOURCE_DIRECTORY__ @@ "dist"
let distGlob = distDir @@ "*.nupkg"
let toolsDir = __SOURCE_DIRECTORY__ @@ "tools"
let gitOwner = "zaid-ajaj"
let gitRepoName = "Suave.SerilogExtensions"
let configuration =
EnvironmentHelper.environVarOrDefault "CONFIGURATION" "Release"
let isRelease () =
Fake.TargetHelper.CurrentTargetOrder
|> Seq.collect id
|> Seq.exists ((=)"Release")
Target "Clean" (fun _ ->
["bin"; "temp" ; distDir]
|> CleanDirs
!! srcGlob
++ testsGlob
|> Seq.collect(fun p ->
["bin";"obj"]
|> Seq.map(fun sp ->
IO.Path.GetDirectoryName p @@ sp)
)
|> CleanDirs
)
Target "DotnetRestore" (fun _ ->
[sln ; toolsDir]
|> Seq.iter(fun dir ->
DotNetCli.Restore (fun c ->
{ c with
Project = dir
//This makes sure that Proj2 references the correct version of Proj1
AdditionalArgs = [sprintf "/p:PackageVersion=%s" release.NugetVersion]
}))
)
Target "DotnetBuild" (fun _ ->
DotNetCli.Build (fun c ->
{ c with
Project = sln
Configuration = configuration
//This makes sure that Proj2 references the correct version of Proj1
AdditionalArgs =
[
sprintf "/p:PackageVersion=%s" release.NugetVersion
sprintf "/p:SourceLinkCreate=%b" (isRelease ())
"--no-restore"
]
}))
let invoke f = f ()
let invokeAsync f = async { f () }
type TargetFramework =
| Full of string
| Core of string
let (|StartsWith|_|) (prefix: string) (s: string) =
if s.StartsWith prefix then Some () else None
let getTargetFramework tf = Core tf
let getTargetFrameworksFromProjectFile (projFile : string)=
let doc = Xml.XmlDocument()
doc.Load(projFile)
doc.GetElementsByTagName("TargetFramework").[0].InnerText.Split(';')
|> Seq.map getTargetFramework
|> Seq.toList
let selectRunnerForFramework tf =
let runMono = sprintf "mono -f %s -c %s --loggerlevel Warn"
let runCore = sprintf "run -f %s -c %s"
match tf with
| Full t when isMono-> runMono t configuration
| Full t -> runCore t configuration
| Core t -> runCore t configuration
let addLogNameParamToArgs tf args =
let frameworkName =
match tf with
| Full t -> t
| Core t -> t
sprintf "%s -- --log-name Expecto.%s" args frameworkName
let runTests modifyArgs =
!! testsGlob
|> Seq.map(fun proj -> proj, getTargetFrameworksFromProjectFile proj)
|> Seq.collect(fun (proj, targetFrameworks) ->
targetFrameworks
|> Seq.map (fun tf -> fun () ->
DotNetCli.RunCommand (fun c ->
{ c with
WorkingDir = IO.Path.GetDirectoryName proj
}) (tf |> selectRunnerForFramework |> modifyArgs |> addLogNameParamToArgs tf))
)
Target "DotnetTest" (fun _ ->
runTests (sprintf "%s --no-build")
|> Seq.iter invoke
)
let execProcAndReturnMessages filename args =
let args' = args |> String.concat " "
ProcessHelper.ExecProcessAndReturnMessages
(fun psi ->
psi.FileName <- filename
psi.Arguments <-args'
) (TimeSpan.FromMinutes(1.))
let pkill args =
execProcAndReturnMessages "pkill" args
let killParentsAndChildren processId=
pkill [sprintf "-P %d" processId]
Target "WatchTests" (fun _ ->
runTests (sprintf "watch %s")
|> Seq.iter (invokeAsync >> Async.Catch >> Async.Ignore >> Async.Start)
printfn "Press Ctrl+C (or Ctrl+Break) to stop..."
let cancelEvent = Console.CancelKeyPress |> Async.AwaitEvent |> Async.RunSynchronously
cancelEvent.Cancel <- true
if isWindows |> not then
startedProcesses
|> Seq.iter(fst >> killParentsAndChildren >> ignore )
else
//Hope windows handles this right?
()
)
Target "AssemblyInfo" (fun _ ->
let releaseChannel =
match release.SemVer.PreRelease with
| Some pr -> pr.Name
| _ -> "release"
let getAssemblyInfoAttributes projectName =
[ Attribute.Title (projectName)
Attribute.Product productName
Attribute.Version release.AssemblyVersion
Attribute.Metadata("ReleaseDate", release.Date.Value.ToString("o"))
Attribute.FileVersion release.AssemblyVersion
Attribute.InformationalVersion release.AssemblyVersion
Attribute.Metadata("ReleaseChannel", releaseChannel)
Attribute.Metadata("GitHash", Information.getCurrentSHA1(null))
]
let getProjectDetails (projectPath: string) =
let projectName = System.IO.Path.GetFileNameWithoutExtension(projectPath)
( projectPath,
projectName,
System.IO.Path.GetDirectoryName(projectPath),
(getAssemblyInfoAttributes projectName)
)
!! srcGlob
++ testsGlob
|> Seq.map getProjectDetails
|> Seq.iter (fun (projFileName, projectName, folderName, attributes) ->
match projFileName with
| Fsproj -> CreateFSharpAssemblyInfo (folderName @@ "AssemblyInfo.fs") attributes
| Csproj -> CreateCSharpAssemblyInfo ((folderName @@ "Properties") @@ "AssemblyInfo.cs") attributes
| Vbproj -> CreateVisualBasicAssemblyInfo ((folderName @@ "My Project") @@ "AssemblyInfo.vb") attributes
| _ -> ()
)
)
Target "DotnetPack" (fun _ ->
!! srcGlob
|> Seq.iter (fun proj ->
DotNetCli.Pack (fun c ->
{ c with
Project = proj
Configuration = configuration
OutputPath = distDir
AdditionalArgs =
[
sprintf "/p:PackageVersion=%s" release.NugetVersion
sprintf "/p:PackageReleaseNotes=\"%s\"" (String.Join("\n",release.Notes))
sprintf "/p:SourceLinkCreate=%b" (isRelease ())
]
})
)
)
Target "SourcelinkTest" (fun _ ->
!! distGlob
|> Seq.iter (fun nupkg ->
DotNetCli.RunCommand
(fun p -> { p with WorkingDir = toolsDir} )
(sprintf "sourcelink test %s" nupkg)
)
)
let isReleaseBranchCheck () =
let releaseBranch = "master"
if Git.Information.getBranchName "" <> releaseBranch then failwithf "Not on %s. If you want to release please switch to this branch." releaseBranch
Target "Publish" (fun _ ->
isReleaseBranchCheck ()
Paket.Push(fun c ->
{ c with
PublishUrl = "https://www.nuget.org"
WorkingDir = "dist"
}
)
)
Target "GitRelease" (fun _ ->
isReleaseBranchCheck ()
let releaseNotesGitCommitFormat = ("",release.Notes |> Seq.map(sprintf "* %s\n")) |> String.Join
StageAll ""
Git.Commit.Commit "" (sprintf "Bump version to %s \n%s" release.NugetVersion releaseNotesGitCommitFormat)
Branches.push ""
Branches.tag "" release.NugetVersion
Branches.pushTag "" "origin" release.NugetVersion
)
#load "paket-files/build/fsharp/FAKE/modules/Octokit/Octokit.fsx"
open Octokit
Target "GitHubRelease" (fun _ ->
let client =
match Environment.GetEnvironmentVariable "GITHUB_TOKEN" with
| null ->
let user =
match getBuildParam "github-user" with
| s when not (String.IsNullOrWhiteSpace s) -> s
| _ -> getUserInput "Username: "
let pw =
match getBuildParam "github-pw" with
| s when not (String.IsNullOrWhiteSpace s) -> s
| _ -> getUserPassword "Password: "
createClient user pw
| token -> createClientWithToken token
client
|> createDraft gitOwner gitRepoName release.NugetVersion (release.SemVer.PreRelease <> None) release.Notes
|> fun draft ->
!! distGlob
|> Seq.fold (fun draft pkg -> draft |> uploadFile pkg) draft
|> releaseDraft
|> Async.RunSynchronously
)
Target "Release" DoNothing
// Only call Clean if DotnetPack was in the call chain
// Ensure Clean is called before DotnetRestore
"Clean" ==> "DotnetRestore"
"Clean" ==> "DotnetPack"
// Only call AssemblyInfo if Publish was in the call chain
// Ensure AssemblyInfo is called after DotnetRestore and before DotnetBuild
"DotnetRestore" ?=> "AssemblyInfo"
"AssemblyInfo" ?=> "DotnetBuild"
"AssemblyInfo" ==> "Publish"
"DotnetRestore"
==> "DotnetBuild"
==> "DotnetTest"
==> "DotnetPack"
==> "Publish"
==> "Release"
"DotnetRestore"
==> "WatchTests"
RunTargetOrDefault "DotnetPack"