forked from fsprojects/fantomas-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.fsx
252 lines (229 loc) · 8.27 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
#r "nuget: Fun.Build, 0.2.9"
#r "nuget: CliWrap, 3.5.0"
#r "nuget: Fake.IO.FileSystem, 5.23.0"
open System
open System.IO
open System.Threading.Tasks
open CliWrap
open CliWrap.Buffered
open Fun.Build
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 pwd = __SOURCE_DIRECTORY__
let fantomasDepDir = pwd </> ".deps" </> "fantomas"
let v6DepDir = pwd </> ".deps" </> "v6.0"
let clientDir = pwd </> "src" </> "client"
let serverDir = __SOURCE_DIRECTORY__ </> "src" </> "server"
let artifactDir = __SOURCE_DIRECTORY__ </> "artifacts"
let git (arguments: string) workingDir =
async {
let! result =
Cli
.Wrap("git")
.WithArguments(arguments)
.WithWorkingDirectory(workingDir)
.ExecuteBufferedAsync()
.Task
|> Async.AwaitTask
return (result.ExitCode, result.StandardOutput)
}
let setEnv name value =
Environment.SetEnvironmentVariable(name, value)
pipeline "Fantomas-Git" {
stage "git" {
paralle
run (fun _ ->
async {
let branch = "main"
if Directory.Exists(fantomasDepDir) then
let! exitCode, _ = git "pull" fantomasDepDir
return exitCode
else
let! exitCode, _ =
git
$"clone -b {branch} --single-branch https://github.com/fsprojects/fantomas.git .deps/fantomas"
__SOURCE_DIRECTORY__
return exitCode
})
// run (fun _ ->
// async {
// let branch = "v6.0"
// if Directory.Exists(v6DepDir) then
// let! exitCode, _ = git "pull" v6DepDir
// return exitCode
// else
// let! exitCode, _ =
// git
// $"clone -b {branch} --single-branch https://github.com/fsprojects/fantomas.git .deps/v6.0"
// __SOURCE_DIRECTORY__
// return exitCode
// })
}
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 v6DepDir
// run "dotnet fsi build.fsx -p Init"
// run "dotnet build src/Fantomas.Core"
// }
}
runIfOnlySpecified true
}
let publishLambda name =
$"dotnet publish -c Release -o {artifactDir </> name} {serverDir}/{name}/{name}.fsproj"
let runLambda name =
$"dotnet watch run --project {serverDir </> name </> name}.fsproj"
let setPerlaEnvVars () =
let mainStageUrl =
"https://arlp8cgo97.execute-api.eu-west-1.amazonaws.com/fantomas-main-stage-1c52a6a"
setEnv "PERLA_AST_BACKEND" $"{mainStageUrl}/ast-viewer"
setEnv "PERLA_OAK_BACKEND" $"{mainStageUrl}/oak-viewer"
setEnv "PERLA_FANTOMAS_V4" $"{mainStageUrl}/fantomas/v4"
setEnv "PERLA_FANTOMAS_V5" $"{mainStageUrl}/fantomas/v5"
setEnv "PERLA_FANTOMAS_MAIN" $"{mainStageUrl}/fantomas/main"
setEnv "PERLA_FANTOMAS_PREVIEW" $"{mainStageUrl}/fantomas/preview"
pipeline "Build" {
workingDir __SOURCE_DIRECTORY__
stage "dotnet install" {
run "dotnet tool restore"
run "dotnet restore"
}
stage "check format F#" { run "dotnet fantomas src infrastructure build.fsx --check" }
stage "clean" {
run (fun _ ->
async {
Shell.rm_rf artifactDir
!!(serverDir + "/*/bin")
++ (serverDir + "/*/obj")
++ (clientDir + "/src/bin")
++ (clientDir + "/build")
|> Seq.iter Shell.rm_rf
return 0
})
}
stage "publish lambdas" {
stage "parallel ones" {
paralle
run (publishLambda "FantomasOnlineV4")
run (publishLambda "FantomasOnlineV5")
run (publishLambda "ASTViewer")
}
run (publishLambda "FantomasOnlineMain")
run (publishLambda "FantomasOnlinePreview")
run (publishLambda "OakViewer")
}
stage "bundle frontend" {
workingDir clientDir
run "dotnet tool restore"
run (fun _ ->
async {
setPerlaEnvVars ()
return 0
})
run "perla build"
run (fun _ ->
async {
File.Create(clientDir </> "dist" </> ".nojekyll").Close()
Shell.cp_r (clientDir </> "dist") (artifactDir </> "client")
return 0
})
}
runIfOnlySpecified false
}
let changedFiles () : Async<string array> =
async {
let! exitCode, stdout = git "status --porcelain" pwd
if exitCode <> 0 then
return failwithf $"could not check the git status: %s{stdout}"
else
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" {
run (fun _ ->
async {
let! files = changedFiles ()
let fantomasArgument = files |> Array.filter isFSharpFile |> String.concat " "
let! fantomasExit =
if String.IsNullOrWhiteSpace fantomasArgument then
async.Return 0
else
Cli
.Wrap("dotnet")
.WithArguments($"fantomas {fantomasArgument}")
.ExecuteAsync()
.Task.ContinueWith(fun (t: Task<CommandResult>) -> t.Result.ExitCode)
|> Async.AwaitTask
// Exit code should be zero
return fantomasExit
}
|> Async.RunSynchronously)
}
runIfOnlySpecified true
}
pipeline "Watch" {
stage "dotnet install" {
run "dotnet tool restore"
run "dotnet restore"
}
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 "PERLA_AST_BACKEND" (localhostBackend astPort "ast-viewer")
setEnv "PERLA_OAK_BACKEND" (localhostBackend oakPort "oak-viewer")
setEnv "PERLA_FANTOMAS_V4" (localhostBackend fantomasV4Port "fantomas/v4")
setEnv "PERLA_FANTOMAS_V5" (localhostBackend fantomasV5Port "fantomas/v5")
setEnv "PERLA_FANTOMAS_MAIN" (localhostBackend fantomasMainPort "fantomas/main")
setEnv "PERLA_FANTOMAS_PREVIEW" (localhostBackend fantomasPreviewPort "fantomas/preview")
return 0
})
}
stage "launch services" {
paralle
run (runLambda "ASTViewer")
run (runLambda "OakViewer")
run (runLambda "FantomasOnlineV4")
run (runLambda "FantomasOnlineV5")
run (runLambda "FantomasOnlineMain")
run (runLambda "FantomasOnlinePreview")
stage "frontend" {
workingDir clientDir
run "dotnet tool restore"
run "dotnet perla serve"
}
}
runIfOnlySpecified true
}