-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathbuild.fsx
More file actions
322 lines (267 loc) · 10.5 KB
/
build.fsx
File metadata and controls
322 lines (267 loc) · 10.5 KB
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
// --------------------------------------------------------------------------------------
// FAKE build script
// --------------------------------------------------------------------------------------
#r "nuget: MSBuild.StructuredLogger"
#r "nuget: Fake.Core"
#r "nuget: Fake.Core.Target"
#r "nuget: Fake.Core.Process"
#r "nuget: Fake.DotNet.Cli"
#r "nuget: Fake.Core.ReleaseNotes"
#r "nuget: Fake.DotNet.AssemblyInfoFile"
#r "nuget: Fake.Tools.Git"
#r "nuget: Fake.Core.Environment"
#r "nuget: Fake.Core.UserInput"
#r "nuget: Fake.IO.FileSystem"
#r "nuget: Fake.DotNet.MsBuild"
#r "nuget: Fake.Api.GitHub"
#r "nuget: Fsdk, Version=0.6.0--date20231213-0703.git-d7a5962"
#if FAKE
#load ".fake/build.fsx/intellisense.fsx"
#else
// Boilerplate
System.Environment.GetCommandLineArgs()
|> Array.skip 2 // skip fsi.exe; build.fsx
|> Array.toList
|> Fake.Core.Context.FakeExecutionContext.Create false __SOURCE_FILE__
|> Fake.Core.Context.RuntimeContext.Fake
|> Fake.Core.Context.setExecutionContext
#endif
open Fake.Core
open Fake.DotNet
open Fake.Tools
open Fake.IO
open Fake.IO.FileSystemOperators
open Fake.IO.Globbing.Operators
open Fake.Core.TargetOperators
open Fake.Api
open System
open System.IO
open System.Text.Json.Nodes
Target.initEnvironment()
// --------------------------------------------------------------------------------------
// Information about the project to be used at NuGet and in AssemblyInfo files
// --------------------------------------------------------------------------------------
let project = "FSharpLint"
let solutionFileName = "FSharpLint.slnx"
let authors = "Matthew Mcveigh"
let gitOwner = "fsprojects"
let gitName = "FSharpLint"
let gitHome = $"https://github.com/{gitOwner}"
let gitUrl = $"{gitHome}/{gitName}"
// --------------------------------------------------------------------------------------
// Helpers
// --------------------------------------------------------------------------------------
let isNullOrWhiteSpace = System.String.IsNullOrWhiteSpace
let exec cmd args dir =
let proc =
CreateProcess.fromRawCommandLine cmd args
|> CreateProcess.ensureExitCodeWithMessage $"Error while running '%s{cmd}' with args: %s{args}"
(if isNullOrWhiteSpace dir then proc
else proc |> CreateProcess.withWorkingDirectory dir)
|> Proc.run
|> ignore
let getBuildParam var =
let value = Environment.environVar var
if String.IsNullOrWhiteSpace value then
None
else
Some value
let DoNothing = ignore
// --------------------------------------------------------------------------------------
// Build variables
// --------------------------------------------------------------------------------------
let buildDir = "./build/"
let nugetDir = "./out/"
let docsDir = "./docs/"
let rootDir = __SOURCE_DIRECTORY__ |> DirectoryInfo
System.Environment.CurrentDirectory <- rootDir.FullName
let changelogFilename = "CHANGELOG.md"
let changelog = Changelog.load changelogFilename
let githubRef = Environment.GetEnvironmentVariable "GITHUB_REF"
let tagPrefix = "refs/tags/"
let isTag =
if isNull githubRef then
false
else
githubRef.StartsWith tagPrefix
let nugetVersion =
match (changelog.Unreleased, isTag) with
| (Some _unreleased, true) -> failwith "Shouldn't publish a git tag for changes outside a real release"
| (None, true) ->
changelog.LatestEntry.NuGetVersion
| (_, false) ->
let current = changelog.LatestEntry.NuGetVersion |> SemVer.parse
let bumped = { current with
Patch = current.Patch + 1u
Original = None
PreRelease = None }
let bumpedBaseVersion = string bumped
Fsdk.Network.GetNugetPrereleaseVersionFromBaseVersion bumpedBaseVersion
let PackageReleaseNotes baseProps =
if isTag then
("PackageReleaseNotes", $"%s{gitUrl}/blob/v%s{nugetVersion}/CHANGELOG.md")::baseProps
else
baseProps
// --------------------------------------------------------------------------------------
// Build Targets
// --------------------------------------------------------------------------------------
Target.create "Clean" (fun _ ->
Shell.cleanDirs [buildDir; nugetDir]
)
Target.create "Build" (fun _ ->
DotNet.build id solutionFileName
)
let filterPerformanceTests (p:DotNet.TestOptions) = { p with Filter = Some "\"TestCategory!=Performance\""; Configuration = DotNet.Release }
Target.create "Test" (fun _ ->
DotNet.test filterPerformanceTests "tests/FSharpLint.Core.Tests"
DotNet.test filterPerformanceTests "tests/FSharpLint.Console.Tests"
DotNet.restore id "tests/FSharpLint.FunctionalTest.TestedProject/FSharpLint.FunctionalTest.TestedProject.sln"
DotNet.test filterPerformanceTests "tests/FSharpLint.FunctionalTest"
)
Target.create "Docs" (fun _ ->
exec "dotnet" "fornax build" docsDir
)
// --------------------------------------------------------------------------------------
// Release Targets
// --------------------------------------------------------------------------------------
Target.create "BuildRelease" (fun _ ->
let properties = ("Version", nugetVersion) |> List.singleton |> PackageReleaseNotes
DotNet.build (fun p ->
{ p with
Configuration = DotNet.BuildConfiguration.Release
MSBuildParams = { p.MSBuildParams with Properties = properties }
}
) solutionFileName
)
Target.create "Pack" (fun _ ->
let properties = PackageReleaseNotes ([
("Version", nugetVersion);
("Authors", authors)
("PackageProjectUrl", gitUrl)
("RepositoryType", "git")
("RepositoryUrl", gitUrl)
("PackageLicenseExpression", "MIT")
])
DotNet.pack (fun p ->
{ p with
Configuration = DotNet.BuildConfiguration.Release
OutputPath = Some nugetDir
MSBuildParams = { p.MSBuildParams with Properties = properties }
}
) solutionFileName
)
Target.create "Push" (fun _ ->
let push key =
let distGlob = nugetDir </> "*.nupkg"
distGlob
|> DotNet.nugetPush (fun o -> {
o with
Common = {
o.Common with
CustomParams = Some "--skip-duplicate"
}
PushParams = {
o.PushParams with
Source = Some "https://api.nuget.org/v3/index.json"
ApiKey = Some key
}
})
let key = getBuildParam "nuget-key"
match getBuildParam "GITHUB_EVENT_NAME" with
| None ->
match key with
| None ->
let key = UserInput.getUserPassword "NuGet Key: "
push key
| Some key ->
push key
| Some "push" ->
match key with
| None ->
Console.WriteLine "No nuget-key env var found, skipping..."
| Some key ->
if isTag then
push key
elif getBuildParam "GITHUB_REF_NAME" <> Some "master" then
Console.WriteLine "Not a push to master branch, skipping..."
else
match getBuildParam "GITHUB_SHA" with
| None ->
failwith "GITHUB_SHA should have been populated"
| Some commitHash ->
// NOTE: for this to work, github workflow needs to have `fetch-depth: 0` in its checkout action
let gitArgs = $"describe --exact-match --tags %s{commitHash}"
let proc =
CreateProcess.fromRawCommandLine "git" gitArgs
|> Proc.run
if proc.ExitCode <> 0 then
// commit is not a tag, so go ahead pushing a prerelease
push key
else
Console.WriteLine "Commit mapped to a tag, skipping pushing prerelease..."
| _ ->
Console.WriteLine "Github event name not 'push', skipping..."
)
Target.create "SelfCheck" (fun _ ->
let runLinter () =
let srcDir = Path.Combine(rootDir.FullName, "src") |> DirectoryInfo
let consoleProj = Path.Combine(srcDir.FullName, "FSharpLint.Console", "FSharpLint.Console.fsproj") |> FileInfo
let sol = Path.Combine(rootDir.FullName, solutionFileName) |> FileInfo
exec "dotnet" $"run --framework net9.0 lint %s{sol.FullName}" consoleProj.Directory.FullName
printfn "Running self-check with default rules..."
runLinter ()
let fsharplintJsonDir = Path.Combine("src", "FSharpLint.Core", "fsharplint.json")
let fsharplintJsonText = File.ReadAllText fsharplintJsonDir
let excludedRules =
[
// Formatting rules (maybe mark them as DEPRECATED soon, recommending the use of `fantomas` instead)
"typedItemSpacing"
"unionDefinitionIndentation"
"moduleDeclSpacing"
"classMemberSpacing"
"tupleCommaSpacing"
"tupleIndentation"
"patternMatchClausesOnNewLine"
"patternMatchOrClausesOnNewLine"
"patternMatchClauseIndentation"
"patternMatchExpressionIndentation"
"indentation"
"maxCharactersOnLine"
"trailingNewLineInFile"
"trailingWhitespaceOnLine"
// TODO: we should enable at some point
"typePrefixing"
"unnestedFunctionNames"
"nestedFunctionNames"
"nestedStatements"
// rule is too complex, we can enable it later
"cyclomaticComplexity"
]
let jsonObj = JsonObject.Parse fsharplintJsonText
for pair in jsonObj.AsObject() do
if pair.Value.GetValueKind() = Text.Json.JsonValueKind.Object then
let result, isRule = pair.Value.AsObject().TryGetPropertyValue("enabled")
match result, isRule with
| true, isRule when not (List.contains pair.Key excludedRules) ->
isRule.AsValue().ReplaceWith true
| _ -> ()
File.WriteAllText(fsharplintJsonDir, jsonObj.ToJsonString())
printfn "Now re-running self-check with more rules enabled..."
runLinter ())
// --------------------------------------------------------------------------------------
// Build order
// --------------------------------------------------------------------------------------
Target.create "Default" DoNothing
Target.create "Release" DoNothing
"Clean"
==> "Build"
==> "Test"
==> "Default"
"Clean"
==> "BuildRelease"
==> "Docs"
"Default"
==> "Pack"
==> "Push"
==> "Release"
Target.runOrDefaultWithArguments "Default"