-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.fsx
240 lines (181 loc) · 6.68 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
#r "nuget: Fun.Build, 0.5.2"
#r "nuget: Humanizer.Core"
open System
open System.IO
open System.Xml.Linq
open Fun.Build
open Humanizer
let (</>) a b = Path.Combine (a, b)
let restoreStage =
stage "restore" {
run "dotnet tool restore"
run "dotnet restore --locked-mode"
}
let buildStage =
stage "build" { run "dotnet build -c Release --no-restore -maxCpuCount" }
pipeline "Build" {
restoreStage
stage "lint" { run "dotnet fantomas . --check" }
buildStage
stage "test" { run "dotnet test -c Release --no-build" }
stage "docs" {
run "dotnet fsdocs build --parameters fsdocs-collection-name \"G-Research F# Analyzers\" --noapidocs --eval"
}
runIfOnlySpecified false
}
pipeline "EnsureTrailingNewline" {
stage "TestData" {
run (fun _ ->
Directory.EnumerateFiles (
Path.Combine (__SOURCE_DIRECTORY__, "tests", "FSharp.Analyzers.Tests", "data"),
"*.*",
SearchOption.AllDirectories
)
|> Seq.iter (fun filePath ->
let contents = File.ReadAllText filePath
let contents = contents.Replace ("\r", "")
let contents =
if contents.EndsWith ("\n", StringComparison.Ordinal) then
contents
else
String.Concat (contents, "\n")
File.WriteAllText (filePath, contents)
)
)
}
runIfOnlySpecified true
}
pipeline "Docs" {
stage "Docs" {
restoreStage
buildStage
run
"dotnet fsdocs watch --parameters fsdocs-collection-name \"G-Research F# Analyzers\" --noapidocs --eval --port 5000"
}
runIfOnlySpecified true
}
let getLastCompileItem (fsproj : string) =
let xml = File.ReadAllText fsproj
let doc = XDocument.Parse xml
Seq.last (doc.Descendants (XName.Get "Compile"))
pipeline "NewAnalyzer" {
stage "Scaffold" {
run (fun _ctx ->
Console.Write "Enter analyzer name:"
let analyzerName = Console.ReadLine().Trim ()
let analyzerName =
if analyzerName.EndsWith ("Analyzer", StringComparison.Ordinal) then
analyzerName
else
$"%s{analyzerName}Analyzer"
let camelCasedName = analyzerName.Camelize ()
let analyzerFilePath =
__SOURCE_DIRECTORY__ </> $"src/FSharp.Analyzers/%s{analyzerName}.fs"
let analyzerContent =
$"""module GR.FSharp.Analyzers.%s{analyzerName}
open System
open FSharp.Analyzers.SDK
open FSharp.Analyzers.SDK.TASTCollecting
open FSharp.Compiler.Symbols
open FSharp.Compiler.Syntax
open FSharp.Compiler.Text
[<CliAnalyzer("%s{analyzerName}",
"", // TODO: add description.
"https://g-research.github.io/fsharp-analyzers/analyzers/%s{analyzerName}.html")>]
let %s{camelCasedName} : Analyzer<CliContext> =
fun (ctx : CliContext) -> async {{ return List.empty<Message> }}
"""
File.WriteAllText (analyzerFilePath, analyzerContent)
printfn "Created %s" analyzerFilePath
let addCompileItem relativeFsProj filenameWithoutExtension =
let fsproj = __SOURCE_DIRECTORY__ </> relativeFsProj
let sibling = getLastCompileItem fsproj
if
sibling.Attribute(XName.Get ("Include")).Value
<> $"%s{filenameWithoutExtension}.fs"
then
sibling.Parent.Add (XElement.Parse ($"<Compile Include=\"%s{filenameWithoutExtension}.fs\" />"))
sibling.Document.Save fsproj
addCompileItem "src/FSharp.Analyzers/FSharp.Analyzers.fsproj" analyzerName
let testFolderName = analyzerName.Replace("Analyzer", String.Empty).Camelize ()
let analyzerTestsFilePath =
__SOURCE_DIRECTORY__
</> $"tests/FSharp.Analyzers.Tests/%s{analyzerName}Tests.fs"
let analyzerTestsContent =
$"""module GR.FSharp.Analyzers.Tests.%s{analyzerName}Tests
open System.Collections
open System.IO
open NUnit.Framework
open FSharp.Compiler.CodeAnalysis
open FSharp.Analyzers.SDK.Testing
open GR.FSharp.Analyzers
open GR.FSharp.Analyzers.Tests.Common
let mutable projectOptions : FSharpProjectOptions = FSharpProjectOptions.zero
[<SetUp>]
let Setup () =
task {{
let! options = mkOptionsFromProject "net7.0" []
projectOptions <- options
}}
type TestCases() =
interface IEnumerable with
member _.GetEnumerator () : IEnumerator =
constructTestCaseEnumerator [| "%s{testFolderName}" |]
[<TestCaseSource(typeof<TestCases>)>]
let %s{analyzerName}Tests (fileName : string) =
task {{
let fileName = Path.Combine (dataFolder, fileName)
let! messages =
File.ReadAllText fileName
|> getContext projectOptions
|> %s{analyzerName}.%s{camelCasedName}
do! assertExpected fileName messages
}}
type NegativeTestCases() =
interface IEnumerable with
member _.GetEnumerator () : IEnumerator =
constructTestCaseEnumerator [| "%s{testFolderName}" ; "negative" |]
[<TestCaseSource(typeof<NegativeTestCases>)>]
let NegativeTests (fileName : string) =
task {{
let fileName = Path.Combine (dataFolder, fileName)
let! messages =
File.ReadAllText fileName
|> getContext projectOptions
|> %s{analyzerName}.%s{camelCasedName}
Assert.That (messages, Is.Empty)
}}
"""
File.WriteAllText (analyzerTestsFilePath, analyzerTestsContent)
addCompileItem "tests/FSharp.Analyzers.Tests/FSharp.Analyzers.Tests.fsproj" $"%s{analyzerName}Tests"
printfn "Created %s" analyzerTestsFilePath
let testFolder =
DirectoryInfo (__SOURCE_DIRECTORY__ </> "tests/FSharp.Analyzers.Tests/data" </> testFolderName)
testFolder.Create ()
let sampleFilePath = testFolder.FullName </> "Sample.fs"
File.WriteAllText (sampleFilePath, "module Sample\n\n")
printfn "Created %s" sampleFilePath
let documentationFilePath =
__SOURCE_DIRECTORY__ </> $"docs/analyzers/%s{analyzerName}.md"
let title = testFolderName.Pascalize ()
let documentationContent =
$"""---
title: %s{title} Analyzer
category: analyzers
categoryindex: 1
index:
---
# %s{title} Analyzer
## Problem
```fsharp
```
## Fix
```fsharp
```
"""
File.WriteAllText (documentationFilePath, documentationContent)
)
}
runIfOnlySpecified true
}
tryPrintPipelineCommandHelp ()