-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
build.fsx
314 lines (281 loc) · 10.1 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
#r "nuget: Fun.Build, 1.0.4"
#r "nuget: Fake.IO.FileSystem, 5.23.0"
open System
open System.IO
open Fun.Build
open Fun.Build.Internal
open Fake.IO
open Fake.IO.FileSystemOperators
open Fake.IO.Globbing.Operators
let astPort = 7412
let oakPort = 8904
let fantomasMainPort = 11084
let fantomasPreviewPort = 12007
let fantomasV4Port = 10707
let fantomasV5Port = 11009
let fantomasV6Port = 13042
let pwd = __SOURCE_DIRECTORY__
let fantomasDepDir = pwd </> ".deps" </> "fantomas"
let previewBranch = "v7.0"
let previewDepDir = pwd </> ".deps" </> previewBranch
let clientDir = pwd </> "src" </> "client"
let serverDir = __SOURCE_DIRECTORY__ </> "src" </> "server"
let artifactDir = __SOURCE_DIRECTORY__ </> "artifacts"
let alwaysOk = async { return Ok() }
let mapResultToCode =
function
| Ok _ -> 0
| Error _ -> 1
let git (ctx: StageContext) (workingDir: string) (arguments: string) : Async<Result<unit, string>> =
ctx.RunCommand($"git %s{arguments}", workingDir = workingDir)
let setEnv name value =
Environment.SetEnvironmentVariable(name, value)
pipeline "Fantomas-Git" {
stage "git" {
paralle
run (fun ctx ->
async {
let branch = "main"
if Directory.Exists(fantomasDepDir) then
let! result = git ctx fantomasDepDir "pull"
return mapResultToCode result
else
let! result =
git
ctx
__SOURCE_DIRECTORY__
$"clone -b {branch} --single-branch https://github.com/fsprojects/fantomas.git .deps/fantomas"
return mapResultToCode result
})
run (fun ctx ->
async {
if Directory.Exists(previewDepDir) then
let! result = git ctx previewDepDir "pull"
return mapResultToCode result
else
let! result =
git
ctx
__SOURCE_DIRECTORY__
$"clone -b {previewBranch} --single-branch https://github.com/fsprojects/fantomas.git .deps/{previewBranch}"
return mapResultToCode result
})
}
stage "build" {
paralle
stage "build fantomas main" {
workingDir fantomasDepDir
run "dotnet fsi build.fsx -p Init"
run "dotnet build src/Fantomas.Core"
}
stage "build fantomas preview" {
workingDir previewDepDir
run "dotnet fsi build.fsx -p Init"
run "dotnet build src/Fantomas.Core"
}
}
runIfOnlySpecified true
}
let publishLambda name =
$"dotnet publish --tl -c Release {serverDir}/{name}/{name}.fsproj"
let runLambda name =
$"dotnet watch run --project {serverDir </> name </> name}.fsproj --tl"
let setViteToProduction () =
setEnv "NODE_ENV" "production"
let mainStageUrl =
"https://arlp8cgo97.execute-api.eu-west-1.amazonaws.com/fantomas-main-stage-1c52a6a"
setEnv "VITE_AST_BACKEND" $"{mainStageUrl}/ast-viewer"
setEnv "VITE_OAK_BACKEND" $"{mainStageUrl}/oak-viewer"
setEnv "VITE_FANTOMAS_V4" $"{mainStageUrl}/fantomas/v4"
setEnv "VITE_FANTOMAS_V5" $"{mainStageUrl}/fantomas/v5"
setEnv "VITE_FANTOMAS_V6" $"{mainStageUrl}/fantomas/v6"
setEnv "VITE_FANTOMAS_MAIN" $"{mainStageUrl}/fantomas/main"
setEnv "VITE_FANTOMAS_PREVIEW" $"{mainStageUrl}/fantomas/preview"
let bunInstall =
stage "bun install" {
workingDir clientDir
run "bun i"
}
let dotnetInstall =
stage "dotnet install" {
run "dotnet tool restore"
run "dotnet restore --tl"
}
pipeline "Build" {
workingDir __SOURCE_DIRECTORY__
bunInstall
dotnetInstall
stage "check format F#" { run "dotnet fantomas src infrastructure build.fsx --check" }
stage "check format JS" {
workingDir clientDir
run "bun run lint"
}
stage "clean" {
run (fun _ ->
async {
Shell.rm_rf artifactDir
Shell.rm_rf (clientDir + "/build")
return 0
})
}
stage "publish lambdas" {
stage "parallel ones" {
paralle
run (publishLambda "FantomasOnlineV4")
run (publishLambda "FantomasOnlineV5")
run (publishLambda "FantomasOnlineV6")
run (publishLambda "ASTViewer")
}
run (publishLambda "FantomasOnlineMain")
run (publishLambda "FantomasOnlinePreview")
run (publishLambda "OakViewer")
}
stage "bundle frontend" {
workingDir clientDir
run (fun _ ->
async {
setViteToProduction ()
return 0
})
run "bun run build"
run (fun _ ->
async {
File.Create(clientDir </> "build" </> ".nojekyll").Close()
Shell.cp_r (clientDir </> "build") (artifactDir </> "client")
return 0
})
}
runIfOnlySpecified false
}
let changedFiles (ctx: StageContext) : Async<string array> =
async {
let! result = ctx.RunCommandCaptureOutput "git status --porcelain"
match result with
| Error _ -> return failwithf "Could not run git status"
| Ok stdout ->
return
stdout.Split('\n')
|> Array.choose (fun line ->
let line = line.Trim()
if (line.StartsWith("AM") || line.StartsWith("M")) then
Some(line.Replace("AM ", "").Replace("M ", ""))
else
None)
}
let fsharpExtensions = set [| ".fs"; ".fsi"; ".fsx" |]
let jsExtensions = set [| ".js"; ".jsx" |]
let isFSharpFile path =
FileInfo(path).Extension |> fsharpExtensions.Contains
let isJSFile path =
FileInfo(path).Extension |> jsExtensions.Contains
pipeline "FormatChanged" {
workingDir __SOURCE_DIRECTORY__
stage "Format code" {
run (fun ctx ->
async {
let! files = changedFiles ctx
let fantomasArgument = files |> Array.filter isFSharpFile |> String.concat " "
printfn "%s" fantomasArgument
let! fsharpResult =
if String.IsNullOrWhiteSpace fantomasArgument then
alwaysOk
else
ctx.RunCommand $"dotnet fantomas %s{fantomasArgument}"
let! files = changedFiles ctx
let prettierArgument =
files
|> Array.choose (fun path ->
if isJSFile path then
Some(path.Replace("src/client/", ""))
else
None)
|> String.concat " "
let! prettierResult =
if String.IsNullOrWhiteSpace prettierArgument then
alwaysOk
else
ctx.RunCommand(
$"bun x prettier --write {prettierArgument}",
workingDir = (__SOURCE_DIRECTORY__ </> "src" </> "client")
)
return (mapResultToCode fsharpResult + mapResultToCode prettierResult)
})
}
runIfOnlySpecified true
}
let prepareEnvironmentVariables =
stage "prepare environment variables" {
run (fun _ ->
async {
let localhostBackend port subPath =
let gitpodEnv = Environment.GetEnvironmentVariable("GITPOD_WORKSPACE_URL")
if String.IsNullOrWhiteSpace(gitpodEnv) then
sprintf "http://localhost:%i/%s" port subPath
else
let gitpodEnv = gitpodEnv.Replace("https://", "")
sprintf "https://%i-%s/%s" port gitpodEnv subPath
setEnv "NODE_ENV" "development"
setEnv "VITE_AST_BACKEND" (localhostBackend astPort "ast-viewer")
setEnv "VITE_OAK_BACKEND" (localhostBackend oakPort "oak-viewer")
setEnv "VITE_FANTOMAS_V4" (localhostBackend fantomasV4Port "fantomas/v4")
setEnv "VITE_FANTOMAS_V5" (localhostBackend fantomasV5Port "fantomas/v5")
setEnv "VITE_FANTOMAS_V6" (localhostBackend fantomasV6Port "fantomas/v6")
setEnv "VITE_FANTOMAS_MAIN" (localhostBackend fantomasMainPort "fantomas/main")
setEnv "VITE_FANTOMAS_PREVIEW" (localhostBackend fantomasPreviewPort "fantomas/preview")
return 0
})
}
pipeline "Watch" {
bunInstall
dotnetInstall
prepareEnvironmentVariables
stage "launch services" {
paralle
run (runLambda "ASTViewer")
run (runLambda "OakViewer")
run (runLambda "FantomasOnlineV4")
run (runLambda "FantomasOnlineV5")
run (runLambda "FantomasOnlineV6")
run (runLambda "FantomasOnlineMain")
run (runLambda "FantomasOnlinePreview")
stage "frontend" {
workingDir clientDir
run "bunx --bun vite"
}
}
runIfOnlySpecified true
}
let runPublishedLambda name =
let binary =
__SOURCE_DIRECTORY__
</> "artifacts"
</> "publish"
</> name
</> "debug"
</> $"%s{name}.dll"
stage $"Run %s{name}" {
run $"dotnet publish --nologo -c Debug -tl {serverDir </> name </> name}.fsproj"
run $"dotnet %s{binary}"
}
pipeline "Start" {
bunInstall
dotnetInstall
prepareEnvironmentVariables
stage "launch services" {
paralle
runPublishedLambda "ASTViewer"
runPublishedLambda "OakViewer"
runPublishedLambda "FantomasOnlineV4"
runPublishedLambda "FantomasOnlineV5"
runPublishedLambda "FantomasOnlineV6"
runPublishedLambda "FantomasOnlineMain"
runPublishedLambda "FantomasOnlinePreview"
stage "frontend" {
workingDir clientDir
run "bun run build"
run "bun run serve"
}
}
runIfOnlySpecified true
}
tryPrintPipelineCommandHelp ()