forked from fsprojects/fantomas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.fsx
358 lines (304 loc) · 14 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
#r "paket:
nuget Fake.BuildServer.AppVeyor
nuget Fake.Core.ReleaseNotes
nuget Fake.Core.Xml
nuget Fake.DotNet.Cli
nuget Fake.DotNet.Paket
nuget Fake.Tools.Git
nuget Fake.Core.Process
nuget Fake.Core.Target //"
#load "./.fake/build.fsx/intellisense.fsx"
#if !FAKE
#r "netstandard"
#endif
open Fake.Core
open Fake.IO
open Fake.Core.TargetOperators
open Fake.BuildServer
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 = "Copyright \169 2019"
let iconUrl = "https://raw.githubusercontent.com/fsprojects/fantomas/master/fantomas_logo.png"
let licenceUrl = "https://github.com/fsprojects/fantomas/blob/master/LICENSE.md"
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 = [ "Anh-Dung Phan"; "Gustavo Guerra" ]
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 = Fake.Core.ReleaseNotes.parse (IO.File.ReadAllLines "RELEASE_NOTES.md")
// 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/netcoreapp3.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
)
let buildNumber =
Environment.environVarOrNone "BUILD_NUMBER" |> Option.map (System.Int32.Parse)
Target.create "ProjectVersion" (fun _ ->
let version =
match buildNumber with
| Some n ->
sprintf "%s.%i" release.NugetVersion n
| None ->
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.Tests"
)
// --------------------------------------------------------------------------------------
// 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"
)
// --------------------------------------------------------------------------------------
// Build a NuGet package
Target.create "Pack" (fun _ ->
let nugetVersion =
match buildNumber with
| Some n ->
sprintf "%s-alpha-%03d" release.NugetVersion n
| None ->
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
"PackageIconUrl", iconUrl
"PackageLicenseUrl", licenceUrl
] }
DotNet.pack (fun p ->
{ p with
NoBuild = true
Configuration = configuration
OutputPath = Some "./bin"
MSBuildParams = args
}) projectPath
pack "Fantomas"
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 =
let paketFile = Path.Combine(".", ".paket", (if Environment.isWindows then "paket.exe" else "paket"))
IO.Directory.EnumerateFiles("bin", "*.nupkg", SearchOption.TopDirectoryOnly)
|> Seq.iter (fun nupkg ->
let args =
[ yield "push";
yield! additionalArguments
yield nupkg ]
CreateProcess.fromRawCommand paketFile args
|> CreateProcess.disableTraceCommand
|> CreateProcess.redirectOutput
|> CreateProcess.withOutputEventsNotNull Trace.trace Trace.traceError
|> CreateProcess.ensureExitCode
|> Proc.run
|> ignore
)
Target.create "Push" (fun _ -> pushPackage [])
// Paket.push (fun p -> { p with WorkingDir = "bin" }))
Target.create "MyGet" (fun _ ->
let apiKey = Environment.environVarOrDefault "MYGET_KEY" "key-missing"
let args = ["--url"; "https://www.myget.org/F/fantomas/api/v2/package"; "--api-key"; apiKey ]
pushPackage args
)
// --------------------------------------------------------------------------------------
// Run all targets by default. Invoke 'build <Target>' to override
Target.create "All" ignore
"Clean"
==> "ProjectVersion"
==> "Build"
==> "UnitTests"
==> "Pack"
==> "All"
==> "Push"
"Build"
==> "TestExternalProjects"
"Pack"
==> "MyGet"
Target.runOrDefault "All"