forked from ai-traders/liget
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.fsx
280 lines (236 loc) · 9.33 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
// --------------------------------------------------------------------------------------
// FAKE build script
// --------------------------------------------------------------------------------------
#r "paket: groupref Tools //"
open System.IO
#load "./.fake/build.fsx/intellisense.fsx"
open Fake.IO
open Fake.IO.FileSystemOperators
open Fake.IO.Globbing.Operators
open Fake.DotNet
open Fake.Core
open Fake.Tools.Git.CommandHelper
open Fake.Tools.Git
open Fake.Api
open Fake.DotNet.Testing.XUnit2
open Fake.Testing.Common
open System.Linq
let project = "LiGet"
let gitOwner = "ai-traders"
// Pattern specifying assemblies to be tested using XUnit
let testAssemblies = !! "tests/*.Tests/bin/Release/*/publish/*.Tests.dll"
// Read additional information from the release notes document
let changelogFile = "CHANGELOG.md"
let release = File.read "CHANGELOG.md" |> ReleaseNotes.parse
let changelogVersion = release.AssemblyVersion
// --------------------------------------------------------------------------------------
// Build projects
Target.create "Build" (fun _ ->
let msbuild target =
let setParams (defaults:MSBuildParams) =
{ defaults with
Verbosity = Some(Quiet)
Targets = [target]
Properties =
[
"Version", changelogVersion
"Configuration", "Release"
]
}
MSBuild.build setParams ""
msbuild "Restore"
msbuild "Publish"
)
// Build SPA
let spaRoot = "src" </> "LiGet.UI"
let spaPublishDir = "src" </> "LiGet" </> "bin" </> "Release" </> "netcoreapp2.1" </> "publish" </> "wwwroot"
let runSpaProcess exe args timeout =
let result =
Process.execWithResult (fun info ->
{ info with
FileName = exe
WorkingDirectory = spaRoot
Arguments = args}) timeout
if result.ExitCode <> 0 then
let errors = System.String.Join(System.Environment.NewLine,result.Errors)
Trace.traceError <| System.String.Join(System.Environment.NewLine,result.Messages)
failwithf "%s process exited with %d: %s" exe result.ExitCode errors
Trace.trace <| System.String.Join(System.Environment.NewLine,result.Messages)
Target.create "SpaRestore" (fun _ ->
runSpaProcess "yarn" "install" (System.TimeSpan.FromMinutes(10.0))
)
Target.create "SpaBuild" (fun _ ->
runSpaProcess "npm" "run build" (System.TimeSpan.FromMinutes(2.0))
)
Target.create "SpaPublish" (fun _ ->
Directory.delete spaPublishDir
Directory.create spaPublishDir
!! "src/LiGet.UI/dist/**/*" |> Shell.copy spaPublishDir
)
// --------------------------------------------------------------------------------------
// Run the unit tests
let runXunit setParams assemblies =
// A bit of patching to use dotnet core runner
let discoverNoAppDomainExists parameters =
let helpText =
Process.execWithResult ((fun info ->
{ info with FileName = parameters.ToolPath}) >> Process.withFramework) (System.TimeSpan.FromMinutes 1.)
let canSetNoAppDomain = helpText.Messages.Any(fun msg -> msg.Contains("-noappdomain"))
{parameters with NoAppDomain = canSetNoAppDomain}
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 -> Trace.traceImportant
| _ -> (fun m -> raise(FailedTestsException m))
let failBuildIfXUnitReportedError errorLevel =
buildErrorMessage
>> Option.iter (failBuildWithMessage errorLevel)
let details = String.separated ", " assemblies
use __ = Trace.traceTask "xUnit2" details
let parametersFirst = setParams XUnit2Defaults
let parameters =
if parametersFirst.NoAppDomain
then discoverNoAppDomainExists parametersFirst
else parametersFirst
let xunitRunner = "packages/tools/xunit.runner.console/tools/netcoreapp2.0/xunit.console.dll"
let args = xunitRunner + " " + buildArgs parameters assemblies
let result =
Process.execSimple ((fun info ->
{ info with
FileName = "dotnet"
WorkingDirectory = defaultArg parameters.WorkingDir "."
Arguments = args}) >> Process.withFramework) parameters.TimeOut
failBuildIfXUnitReportedError parameters.ErrorLevel result
__.MarkSuccess()
let runProcess file args workDir =
let result =
Process.execSimple ((fun info ->
{ info with
FileName = file
WorkingDirectory = workDir
Arguments = args})) (System.TimeSpan.FromMinutes(2.0))
match result with
| error when result <> 0 -> failwithf "Process exited with non-zero"
| _ -> ()
let runDotnet options command args =
let result = DotNet.exec options command args
if result.ExitCode <> 0 then
let errors = System.String.Join(System.Environment.NewLine,result.Errors)
Trace.traceError <| System.String.Join(System.Environment.NewLine,result.Messages)
failwithf "dotnet process exited with %d: %s" result.ExitCode errors
Target.create "RunUnitTests" (fun _ ->
testAssemblies
|> runXunit (fun p -> {
p with
ExcludeTraits=[ ("Category","integration") ]
TimeOut=System.TimeSpan.FromMinutes(5.0)
XmlOutputPath=Some "UnitTestsResults.xml"
HtmlOutputPath=Some "UnitTestResults.html"
})
)
let createNuPkg name versions =
let projectDir = "e2e" </> "input" </> name
Directory.delete projectDir
Directory.create projectDir
runDotnet (fun o -> {o with WorkingDirectory = projectDir }) "new" "classlib"
versions |> Seq.iter(fun version ->
runDotnet (fun o -> {o with WorkingDirectory = projectDir }) "pack" <| sprintf "/p:Version=%s" version)
Target.create "ExampleNuGets" (fun _ ->
let packFile = "e2e" </> "input" </> "example3" </> "pack.sh"
createNuPkg "liget-test1" ["1.0.0"]
createNuPkg "liget-two" ["1.0.0"; "2.1.0"]
runProcess packFile "" ("e2e" </> "input" </> "example3")
)
Target.create "RunIntegrationTests" (fun _ ->
testAssemblies
|> runXunit (fun p -> {
p with
IncludeTraits=[ ("Category","integration") ]
})
)
Target.create "RunTests" (fun _ ->
testAssemblies
|> runXunit (fun p -> {
p with
TimeOut=System.TimeSpan.FromMinutes(10.0)
XmlOutputPath=Some "TestsResults.xml"
HtmlOutputPath=Some "TestResults.html"
})
)
// --------------------------------------------------------------------------------------
// Releases
let changelogUnreleased =
let changeLogString = File.readAsString changelogFile
changeLogString.Contains("Unreleased")
let gitDir = (findGitDir System.Environment.CurrentDirectory).FullName
let currentSha = gitDir |> Information.getCurrentSHA1
let tagVersion =
if changelogUnreleased then
sprintf "%s-%s" release.AssemblyVersion (currentSha.Substring(0, 8))
else
release.AssemblyVersion
let isCurrentCommitTagged =
let ok,msg,error = runGitCommand gitDir <| sprintf "describe --contains %s" currentSha
if ok then
Trace.tracefn "Current SHA %s is tagged" currentSha
else
Trace.tracefn "Current SHA %s is not tagged" currentSha
ok
let zipFile = sprintf @"pkg/liget-%s.zip" tagVersion
Target.create "Zip" (fun _ ->
Directory.create "pkg"
!! "src/LiGet/bin/Release/netcoreapp2.1/publish/**/*"
|> Zip.zip "src/LiGet/bin/Release/netcoreapp2.1/publish" zipFile
)
Target.create "GitTag" (fun _ ->
if isCurrentCommitTagged then
failwithf "Already tagged"
if changelogUnreleased then
Trace.tracefn "Changelog is set as Unreleased - this is a preview release"
Trace.tracefn "Executing git tag version=%s" tagVersion
Branches.tag gitDir tagVersion
Branches.pushTag gitDir "origin" tagVersion
Trace.tracefn "Released code version=%s" tagVersion
)
Target.create "GitHubRelease" (fun _ ->
let token =
match Environment.environVarOrDefault "GITHUB_TOKEN" "" with
| s when not (System.String.IsNullOrWhiteSpace s) -> s
| _ -> failwith "please set the GITHUB_TOKEN environment variable to a github personal access token with repro access."
let notes =
if changelogUnreleased then
[
"*This is a preview release, which has passed all end-to-end tests, but may not contain all final features.*\n"
sprintf "Try docker image `tomzo/liget:%s`\n" tagVersion
]
else
(sprintf "Docker image `tomzo/liget:%s`\n" tagVersion)::release.Notes
GitHub.createClientWithToken token
|> GitHub.draftNewRelease gitOwner project tagVersion changelogUnreleased notes
|> GitHub.uploadFiles [zipFile]
|> GitHub.publishDraft
|> Async.RunSynchronously)
open Fake.Core.TargetOperators
Target.create "All" ignore
"GitTag" ==> "GitHubRelease"
"Zip" ==> "GitHubRelease"
"SpaRestore"
==> "SpaBuild"
==> "SpaPublish"
==> "All"
"ExampleNuGets"
==> "RunIntegrationTests"
"SpaPublish" ==> "Build"
"Build" ==> "RunTests"
"Build" ==> "RunIntegrationTests"
"ExampleNuGets" ==> "RunTests"
"Build"
==> "RunTests"
==> "All"
// start build
Target.runOrDefault "All"