forked from nblockchain/fantomless
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.fsx
513 lines (427 loc) · 18.5 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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
#r "paket: groupref build //"
#load "./.fake/build.fsx/intellisense.fsx"
open Fake.Core
open Fake.IO
open Fake.IO.FileSystemOperators
open Fake.Core.TargetOperators
open Fake.IO.Globbing.Operators
open System
open System.IO
open Fake.DotNet
// Git configuration (used for publishing documentation in gh-pages branch)
// The profile where the project is posted
let gitHome = "https://github.com/fsprojects"
// The name of the project on GitHub
let gitName = "fantomas"
// The name of the project
// (used by attributes in AssemblyInfo, name of a NuGet package and directory in 'src')
let project = "Fantomas"
let projectUrl = sprintf "%s/%s" gitHome gitName
// Short summary of the project
// (used as description in AssemblyInfo and as a short summary for NuGet package)
let summary = "Source code formatter for F#"
let copyright =
sprintf "Copyright © %d" DateTime.UtcNow.Year
let configuration = DotNet.BuildConfiguration.Release
// Longer description of the project
// (used as a description for NuGet package; line breaks are automatically cleaned up)
let description =
"""This library aims at formatting F# source files based on a given configuration.
Fantomas will ensure correct indentation and consistent spacing between elements in the source files.
Some common use cases include
(1) Reformatting a code base to conform a universal page width
(2) Converting legacy code from verbose syntax to light syntax
(3) Formatting auto-generated F# signatures."""
// List of author names (for NuGet package)
let authors =
[ "Florian Verdonck"
"Jindřich Ivánek" ]
let owner = "Anh-Dung Phan"
// Tags for your project (for NuGet package)
let tags =
"F# fsharp formatting beautifier indentation indenter"
// (<solutionFile>.sln is built during the building process)
let solutionFile = "fantomas"
//// Environment.CurrentDirectory <- __SOURCE_DIRECTORY__
let release =
ReleaseNotes.parse (File.ReadAllLines "RELEASE_NOTES.md")
// Files to format
let sourceFiles =
!! "src/**/*.fs" ++ "src/**/*.fsi" ++ "build.fsx"
-- "src/**/obj/**/*.fs"
// Types and helper functions for building external projects (see the TestExternalProjects target below)
type ProcessStartInfo =
{ ProcessName: string
Arguments: string list }
[<NoComparison>]
[<NoEquality>]
type ExternalProjectInfo =
{ GitUrl: string
DirectoryName: string
Tag: string
SourceSubDirectory: string
BuildConfigurationFn: string -> ProcessStartInfo }
// Construct the commands/arguments for running an external project build script for both windows and linux
// For linux we run this by invoking sh explicitly and passing the build.sh script as an argument as some
// projects generated on windows don't have the executable permission set for .sh scripts. On windows we
// treat .cmd files as executable
let configureBuildCommandFromFakeBuildScripts scriptPrefix argument pathToProject =
if Environment.isWindows then
{ ProcessName = Path.combine pathToProject (sprintf "%s.cmd" scriptPrefix)
Arguments = [ argument ] }
else
{ ProcessName = "sh"
Arguments =
[ sprintf "%s/%s.sh" pathToProject scriptPrefix
argument ] }
let configureBuildCommandFromDefaultFakeBuildScripts pathToProject =
configureBuildCommandFromFakeBuildScripts "build" "Build" pathToProject
let configureBuildCommandDotnetBuild pathToProject =
{ ProcessName = "dotnet"
Arguments = [ "build"; pathToProject ] }
// Construct the path of the fantomas executable to use for external project tests
let fantomasExecutableForExternalTests projectdir =
let configuration =
match configuration with
| DotNet.BuildConfiguration.Debug -> "Debug"
| DotNet.BuildConfiguration.Release -> "Release"
| DotNet.BuildConfiguration.Custom s -> s
{ ProcessName = "dotnet"
Arguments = [ sprintf "%s/src/Fantomas.CoreGlobalTool/bin/%s/net5.0/fantomas-tool.dll" projectdir configuration ] }
let externalProjectsToTest =
[
// { GitUrl = @"https://github.com/fsprojects/Argu"
// DirectoryName = "Argu"
// Tag = "5.1.0"
// SourceSubDirectory = "src"
// BuildConfigurationFn = configureBuildCommandFromDefaultFakeBuildScripts }
{ GitUrl = @"https://github.com/jack-pappas/ExtCore"
DirectoryName = "ExtCore"
Tag = "master"
SourceSubDirectory = "."
BuildConfigurationFn = configureBuildCommandDotnetBuild } ]
let externalProjectsToTestFailing =
[ { GitUrl = @"https://github.com/fsprojects/Chessie"
DirectoryName = "Chessie"
Tag = "master"
SourceSubDirectory = "src"
BuildConfigurationFn = configureBuildCommandFromDefaultFakeBuildScripts }
{ GitUrl = @"https://github.com/fscheck/FsCheck"
DirectoryName = "FsCheck"
Tag = "master"
SourceSubDirectory = "src"
BuildConfigurationFn = configureBuildCommandFromDefaultFakeBuildScripts }
{ GitUrl = @"https://github.com/fsprojects/fantomas"
DirectoryName = "Fantomas"
Tag = "v2.9.0"
SourceSubDirectory = "src"
BuildConfigurationFn = configureBuildCommandFromDefaultFakeBuildScripts }
{ GitUrl = @"https://github.com/SAFE-Stack/SAFE-BookStore"
DirectoryName = "SAFE-BookStore"
Tag = "master"
SourceSubDirectory = "src"
BuildConfigurationFn = configureBuildCommandFromDefaultFakeBuildScripts }
{ GitUrl = @"https://github.com/fsprojects/Paket"
DirectoryName = "Paket"
Tag = "5.181.1"
SourceSubDirectory = "src"
BuildConfigurationFn = configureBuildCommandFromDefaultFakeBuildScripts }
{ GitUrl = @"https://github.com/fsprojects/FSharpPlus"
DirectoryName = "FSharpPlus"
Tag = "v1.0.0"
SourceSubDirectory = "src"
BuildConfigurationFn = configureBuildCommandFromDefaultFakeBuildScripts }
{ GitUrl = @"https://github.com/MangelMaxime/fulma-demo"
DirectoryName = "fulma-demo"
Tag = "master"
SourceSubDirectory = "src"
BuildConfigurationFn = configureBuildCommandFromFakeBuildScripts "fake" "build" } ]
// --------------------------------------------------------------------------------------
// Clean build results & restore NuGet packages
Target.create
"Clean"
(fun _ ->
[ "bin"
"src/Fantomas/bin"
"src/Fantomas/obj"
"src/Fantomas.CoreGlobalTool/bin"
"src/Fantomas.CoreGlobalTool/obj" ]
|> List.iter Shell.cleanDir)
Target.create
"ProjectVersion"
(fun _ ->
let version = release.NugetVersion
let setProjectVersion project =
let file =
sprintf "src/%s/%s.fsproj" project project
Xml.poke file "Project/PropertyGroup/Version/text()" version
setProjectVersion "Fantomas"
setProjectVersion "Fantomas.CoreGlobalTool"
setProjectVersion "Fantomas.CoreGlobalTool.Tests"
setProjectVersion "Fantomas.Tests"
setProjectVersion "Fantomas.Extras")
// --------------------------------------------------------------------------------------
// Build library & test project
Target.create
"Build"
(fun _ ->
let sln = sprintf "%s.sln" solutionFile
DotNet.build (fun p -> { p with Configuration = configuration }) sln)
Target.create
"UnitTests"
(fun _ ->
DotNet.test
(fun p ->
{ p with
Configuration = configuration
NoRestore = true
NoBuild = true
// TestAdapterPath = Some "."
// Logger = Some "nunit;LogFilePath=../../TestResults.xml"
// Current there is an issue with NUnit reporter, https://github.com/nunit/nunit3-vs-adapter/issues/589
})
"src/Fantomas.Tests/Fantomas.Tests.fsproj"
DotNet.test
(fun p ->
{ p with
Configuration = configuration
NoRestore = true
NoBuild = true
// TestAdapterPath = Some "."
// Logger = Some "nunit;LogFilePath=../../TestResults.xml"
// Current there is an issue with NUnit reporter, https://github.com/nunit/nunit3-vs-adapter/issues/589
})
"src/Fantomas.CoreGlobalTool.Tests/Fantomas.CoreGlobalTool.Tests.fsproj")
// --------------------------------------------------------------------------------------
// Build a NuGet package
Target.create
"Pack"
(fun _ ->
let nugetVersion = release.NugetVersion
let pack project =
let projectPath =
sprintf "src/%s/%s.fsproj" project project
let args =
let defaultArgs = MSBuild.CliArguments.Create()
{ defaultArgs with
Properties =
[ "Title", project
"PackageVersion", nugetVersion
"Authors", (String.Join(" ", authors))
"Owners", owner
"PackageRequireLicenseAcceptance", "false"
"Description", description
"Summary", summary
"PackageReleaseNotes", ((String.toLines release.Notes).Replace(",", ""))
"Copyright", copyright
"PackageTags", tags
"PackageProjectUrl", projectUrl ] }
DotNet.pack
(fun p ->
{ p with
NoRestore = true
Configuration = configuration
OutputPath = Some "./bin"
MSBuildParams = args })
projectPath
pack "Fantomas"
pack "Fantomas.Extras"
pack "Fantomas.CoreGlobalTool")
// This takes the list of external projects defined above, does a git checkout of the specified repo and tag,
// tries to build the project, then reformats with fantomas and tries to build the project again. If this fails
// then there was a regression in fantomas that mangles the source code
let testExternalProjects externalProjectsToTest =
let externalBuildErrors =
let project = Environment.environVar "project"
externalProjectsToTest
|> if String.IsNullOrWhiteSpace(project) then
id
else
List.filter (fun p -> p.DirectoryName = project)
|> List.map
(fun project ->
let relativeProjectDir =
sprintf "external-project-tests/%s" project.DirectoryName
Shell.cleanDir relativeProjectDir
// Use "shallow" clone by setting depth to 1 to only check out the one commit we want to build
Fake.Tools.Git.CommandHelper.gitCommand
"."
(sprintf "clone --branch %s --depth 1 %s %s" project.Tag project.GitUrl relativeProjectDir)
let fullProjectPath =
sprintf "%s/%s" __SOURCE_DIRECTORY__ relativeProjectDir
let buildStartInfo =
project.BuildConfigurationFn fullProjectPath
let buildExternalProject () =
buildStartInfo.Arguments
|> CreateProcess.fromRawCommand buildStartInfo.ProcessName
|> CreateProcess.withWorkingDirectory relativeProjectDir
|> CreateProcess.withTimeout (TimeSpan.FromMinutes 5.0)
|> Proc.run
let cleanResult = buildExternalProject ()
if cleanResult.ExitCode <> 0 then
failwithf
"Initial build of external project %s returned with a non-zero exit code"
project.DirectoryName
let fantomasStartInfo =
fantomasExecutableForExternalTests __SOURCE_DIRECTORY__
let arguments =
fantomasStartInfo.Arguments
@ [ "--recurse"
project.SourceSubDirectory ]
let invokeFantomas () =
CreateProcess.fromRawCommand fantomasStartInfo.ProcessName arguments
|> CreateProcess.withWorkingDirectory (sprintf "%s/%s" __SOURCE_DIRECTORY__ relativeProjectDir)
|> CreateProcess.withTimeout (TimeSpan.FromMinutes 5.0)
|> Proc.run
let fantomasResult = invokeFantomas ()
if fantomasResult.ExitCode <> 0 then
Some
<| sprintf "Fantomas invokation for %s returned with a non-zero exit code" project.DirectoryName
else
let formattedResult = buildExternalProject ()
if formattedResult.ExitCode <> 0 then
Some
<| sprintf
"Build of external project after fantomas formatting failed for project %s"
project.DirectoryName
else
printfn "Successfully built %s after reformatting" project.DirectoryName
None)
|> List.choose id
if not (List.isEmpty externalBuildErrors) then
failwith (String.Join("\n", externalBuildErrors))
Target.create "TestExternalProjects" (fun _ -> testExternalProjects externalProjectsToTest)
Target.create "TestExternalProjectsFailing" (fun _ -> testExternalProjects externalProjectsToTestFailing)
// Workaround for https://github.com/fsharp/FAKE/issues/2242
let pushPackage additionalArguments =
Directory.EnumerateFiles("bin", "*.nupkg", SearchOption.TopDirectoryOnly)
|> Seq.iter
(fun nupkg ->
let args =
[ yield "push"
yield! additionalArguments
yield nupkg ]
CreateProcess.fromRawCommand "dotnet" ("paket" :: args)
|> CreateProcess.disableTraceCommand
|> CreateProcess.redirectOutput
|> CreateProcess.withOutputEventsNotNull Trace.trace Trace.traceError
|> CreateProcess.ensureExitCode
|> Proc.run
|> ignore)
Target.create "Push" (fun _ -> pushPackage [])
let git command =
CreateProcess.fromRawCommandLine "git" command
|> CreateProcess.redirectOutput
|> Proc.run
|> fun p -> p.Result.Output.Trim()
open Microsoft.Azure.Cosmos.Table
Target.create
"Benchmark"
(fun _ ->
DotNet.exec
id
("src"
</> "Fantomas.Benchmarks"
</> "bin"
</> "Release"
</> "net5.0"
</> "Fantomas.Benchmarks.dll")
""
|> ignore
match Environment.environVarOrNone "TABLE_STORAGE_CONNECTION_STRING" with
| Some conn ->
let branchName = git "rev-parse --abbrev-ref HEAD"
let commit = git "rev-parse HEAD"
let operatingSystem = Environment.environVar "RUNNER_OS"
let results =
File.ReadLines(
"./BenchmarkDotNet.Artifacts/results/Fantomas.Benchmarks.Runners.CodePrinterTest-report.csv"
)
|> Seq.map (fun line -> line.Split(',') |> Array.toList)
|> Seq.toList
|> fun lineGroups ->
match lineGroups with
| [ header; values ] ->
let csvValues = List.zip header values
let metaData =
[ "Branch", branchName
"Commit", commit
"Operating System", operatingSystem ]
[ yield! csvValues; yield! metaData ]
| _ -> []
let storageAccount = CloudStorageAccount.Parse(conn)
let tableClient = storageAccount.CreateCloudTableClient()
let table =
tableClient.GetTableReference("FantomasBenchmarks")
let entry = DynamicTableEntity()
entry.PartitionKey <- "GithubActions"
entry.RowKey <-
(sprintf "%s|%s|%s" branchName commit operatingSystem)
.ToLower()
results
|> List.iter
(fun (k, v) ->
let key = k.Replace(' ', '_')
if not (isNull v) then
entry.Properties.Add(key, EntityProperty.CreateEntityPropertyFromObject(v)))
let tableOperation = TableOperation.InsertOrReplace(entry)
table.Execute(tableOperation) |> printfn "%O"
| None -> printfn "Not saving benchmark results to the cloud")
Target.create
"Format"
(fun _ ->
let result =
sourceFiles
|> Seq.map (sprintf "\"%s\"")
|> String.concat " "
|> DotNet.exec id "fantomas"
if not result.OK then
printfn "Errors while formatting all files: %A" result.Messages)
Target.create
"FormatChanged"
(fun _ ->
let result =
Fake.Tools.Git.FileStatus.getChangedFilesInWorkingCopy "." "HEAD"
|> Seq.choose
(fun (_, file) ->
let ext = Path.GetExtension(file)
if
file.StartsWith("src")
&& (ext = ".fs" || ext = ".fsi")
then
Some(sprintf "\"%s\"" file)
else
None)
|> String.concat " "
|> DotNet.exec id "fantomas"
if not result.OK then
printfn "Problem when formatting changed files:\n\n%A" result.Errors)
Target.create
"CheckFormat"
(fun _ ->
let result =
sourceFiles
|> Seq.map (sprintf "\"%s\"")
|> String.concat " "
|> sprintf "%s --check"
|> DotNet.exec id "fantomas"
if result.ExitCode = 0 then
Trace.log "No files need formatting"
elif result.ExitCode = 99 then
failwith "Some files need formatting, run `dotnet fake build -t Format` to format them"
else
Trace.logf "Errors while formatting: %A" result.Errors)
// --------------------------------------------------------------------------------------
// Run all targets by default. Invoke 'build <Target>' to override
Target.create "All" ignore
"Clean"
==> "ProjectVersion"
==> "CheckFormat"
==> "Build"
==> "UnitTests"
==> "Benchmark"
==> "Pack"
==> "All"
==> "Push"
"Build" ==> "TestExternalProjects"
Target.runOrDefault "All"