-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbuild.fsx
316 lines (257 loc) · 11.3 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
open System.IO
#r "paket: groupref build //"
#load "./.fake/build.fsx/intellisense.fsx"
#if !FAKE
#r "netstandard"
#r "Facades/netstandard" // https://github.com/ionide/ionide-vscode-fsharp/issues/839#issuecomment-396296095
#endif
open System
open Fake.Core
open Fake.DotNet
open Fake.IO
open Fake.IO.FileSystemOperators
open Fake.IO.Globbing.Operators
let serverPath = Path.getFullName "./src/Server"
let piServerPath = Path.getFullName "./src/PiServer"
let clientPath = Path.getFullName "./src/Client"
let deployDir = Path.getFullName "./deploy"
let piDeployDir = Path.getFullName "./pideploy"
let firmwareDeployDir = Path.getFullName "./firmware"
let dockerUser = Environment.environVarOrDefault "DockerUser" String.Empty
let dockerPassword = Environment.environVarOrDefault "DockerPassword" String.Empty
let dockerLoginServer = Environment.environVarOrDefault "DockerLoginServer" String.Empty
let dockerImageName = Environment.environVarOrDefault "DockerImageName" String.Empty
let releases =
"RELEASE_NOTES.md"
|> System.IO.File.ReadAllLines
let release = ReleaseNotes.load "RELEASE_NOTES.md"
let currentFirmware = firmwareDeployDir </> (sprintf "PiFirmware.%s.zip" release.NugetVersion)
let platformTool tool winTool =
let tool = if Environment.isUnix then tool else winTool
match ProcessUtils.tryFindFileOnPath tool with
| Some t -> t
| _ ->
let errorMsg =
tool + " was not found in path. " +
"Please install it and make sure it's available from your path. " +
"See https://safe-stack.github.io/docs/quickstart/#install-pre-requisites for more info"
failwith errorMsg
let nodeTool = platformTool "node" "node.exe"
let yarnTool = platformTool "yarn" "yarn.cmd"
let install = lazy DotNet.install DotNet.Versions.FromGlobalJson
let inline withWorkDir wd =
DotNet.Options.lift install.Value
>> DotNet.Options.withWorkingDirectory wd
let runTool cmd args workingDir =
let result =
Process.execSimple (fun info ->
{ info with
FileName = cmd
WorkingDirectory = workingDir
Arguments = args })
TimeSpan.MaxValue
if result <> 0 then failwithf "'%s %s' failed" cmd args
let runDotNet cmd workingDir =
let result =
DotNet.exec (withWorkDir workingDir) cmd ""
if result.ExitCode <> 0 then failwithf "'dotnet %s' failed in %s" cmd workingDir
let openBrowser url =
let result =
//https://github.com/dotnet/corefx/issues/10361
Process.execSimple (fun info ->
{ info with
FileName = url
UseShellExecute = true })
TimeSpan.MaxValue
if result <> 0 then failwithf "opening browser failed"
Target.create "Clean" (fun _ ->
Shell.cleanDirs [deployDir; piDeployDir; firmwareDeployDir]
)
Target.create "InstallClient" (fun _ ->
printfn "Node version:"
runTool nodeTool "--version" __SOURCE_DIRECTORY__
printfn "Yarn version:"
runTool yarnTool "--version" __SOURCE_DIRECTORY__
runTool yarnTool "install --frozen-lockfile" __SOURCE_DIRECTORY__
runDotNet "restore" clientPath
)
Target.create "RestoreServer" (fun _ ->
runDotNet "restore" serverPath
runDotNet "restore" piServerPath
)
Target.create "Build" (fun _ ->
runDotNet "build" serverPath
runTool yarnTool "webpack-cli --config src/Client/webpack.config.js -p" clientPath
)
Target.create "Run" (fun _ ->
let server = async {
runDotNet "watch run" serverPath
}
let fablewatch = async { runTool yarnTool "webpack-dev-server --config src/Client/webpack.config.js --port 8080" clientPath }
let browser = async {
do! Async.Sleep 5000
openBrowser "http://localhost:8080"
}
[ server; fablewatch; browser ]
|> Async.Parallel
|> Async.RunSynchronously
|> ignore
)
Target.create "SetReleaseNotes" (fun _ ->
let lines = [
"module internal ReleaseNotes"
""
(sprintf "let Version = \"%s\"" release.NugetVersion)
""
(sprintf "let IsPrerelease = %b" (release.SemVer.PreRelease <> None))
""
"let Notes = \"\"\""] @ Array.toList releases @ ["\"\"\""]
System.IO.File.WriteAllLines("src/Client/ReleaseNotes.fs",lines)
)
let copyInLinuxFormat target (files: seq<string>) =
files
|> Seq.iter (fun file ->
let fi = FileInfo file
let content = File.ReadAllText file
let content = content.Replace("\r\n","\n").Replace("\r","\n")
let targetFile = target </> fi.Name
File.WriteAllText(targetFile,content))
Target.create "CreateFirmware" (fun _ ->
let dotnetOpts = install.Value (DotNet.Options.Create())
let publish = piDeployDir </> "bin"
Shell.cleanDirs [publish]
let result =
Process.execSimple (fun info ->
{ info with
FileName = dotnetOpts.DotNetCliPath
WorkingDirectory = piServerPath
Arguments = "publish -c Release -r linux-arm --self-contained -o \"" + Path.getFullName publish + "\"" }) TimeSpan.MaxValue
if result <> 0 then failwith "Publish PiServer failed"
[ piServerPath </> "PiServer"
"./README.md"
piServerPath </> "PiServer.defaults"
] |> copyInLinuxFormat piDeployDir
!! (piServerPath </> "*.sh") |> copyInLinuxFormat piDeployDir
!! (piServerPath </> "*.js") |> copyInLinuxFormat publish
System.IO.Compression.ZipFile.CreateFromDirectory(piDeployDir, currentFirmware)
)
Target.create "BundleClient" (fun _ ->
let dotnetOpts = install.Value (DotNet.Options.Create())
let result =
Process.execSimple (fun info ->
{ info with
FileName = dotnetOpts.DotNetCliPath
WorkingDirectory = serverPath
Arguments = "publish -c Release -o \"" + Path.getFullName deployDir + "\"" }) TimeSpan.MaxValue
if result <> 0 then failwith "Publish Server failed"
let clientDir = deployDir </> "client"
let publicDir = clientDir </> "public"
let jsDir = clientDir </> "js"
let imageDir = clientDir </> "Images"
!! "src/Client/public/**/*.*" |> Shell.copyFiles publicDir
!! "src/Client/js/**/*.*" |> Shell.copyFiles jsDir
!! "src/Client/Images/**/*.*" |> Shell.copyFiles imageDir
!! "src/Client/*.css" |> Shell.copyFiles clientDir
"src/Client/index.html" |> Shell.copyFile clientDir
let indexFile = System.IO.FileInfo(clientDir </> "index.html")
let content = System.IO.File.ReadAllText(indexFile.FullName)
let newContent =
content
.Replace("""<script src="./public/main.js"></script>""",sprintf """<script src="./public/main.%s.js"></script>""" release.NugetVersion)
.Replace("""<script src="./public/vendors.js"></script>""",sprintf """<script src="./public/vendors.%s.js"></script>""" release.NugetVersion)
.Replace("""<link rel="stylesheet" type="text/css" href="landing.css">""",sprintf """<link rel="stylesheet" type="text/css" href="landing.%s.css">""" release.NugetVersion)
System.IO.File.WriteAllText(indexFile.FullName,newContent)
let bundleFile = System.IO.FileInfo(publicDir </> "main.js")
let newBundleFile = System.IO.FileInfo(publicDir </> sprintf "main.%s.js" release.NugetVersion)
System.IO.File.Move (bundleFile.FullName,newBundleFile.FullName)
let vendorFile = System.IO.FileInfo(publicDir </> "vendors.js")
let newVendorFile = System.IO.FileInfo(publicDir </> sprintf "vendors.%s.js" release.NugetVersion)
System.IO.File.Move (vendorFile.FullName,newVendorFile.FullName)
let cssFile = System.IO.FileInfo(clientDir </> "landing.css")
let newcssFile = System.IO.FileInfo(clientDir </> sprintf "landing.%s.css" release.NugetVersion)
System.IO.File.Move (cssFile.FullName,newcssFile.FullName)
)
Target.create "CreateDockerImage" (fun _ ->
if String.IsNullOrEmpty dockerUser then
failwithf "docker username not given."
if String.IsNullOrEmpty dockerImageName then
failwithf "docker image name not given."
let result =
Process.execSimple (fun info ->
{ info with
FileName = "docker"
UseShellExecute = false
Arguments = sprintf "build -t %s/%s ." dockerUser dockerImageName }) TimeSpan.MaxValue
if result <> 0 then failwith "Docker build failed"
)
Target.create "PrepareRelease" (fun _ ->
Fake.Tools.Git.Branches.checkout "" false "master"
Fake.Tools.Git.CommandHelper.directRunGitCommand "" "fetch origin" |> ignore
Fake.Tools.Git.CommandHelper.directRunGitCommand "" "fetch origin --tags" |> ignore
Fake.Tools.Git.Staging.stageAll ""
Fake.Tools.Git.Commit.exec "" (sprintf "Bumping version to %O" release.NugetVersion)
Fake.Tools.Git.Branches.pushBranch "" "origin" "master"
let tagName = string release.NugetVersion
Fake.Tools.Git.Branches.tag "" tagName
Fake.Tools.Git.Branches.pushTag "" "origin" tagName
let result =
Process.execSimple (fun info ->
{ info with
FileName = "docker"
Arguments = sprintf "tag %s/%s %s/%s:%s" dockerUser dockerImageName dockerUser dockerImageName release.NugetVersion}) TimeSpan.MaxValue
if result <> 0 then failwith "Docker tag failed"
let result =
Process.execSimple (fun info ->
{ info with
FileName = "docker"
Arguments = sprintf "tag %s/%s %s/%s:latest" dockerUser dockerImageName dockerUser dockerImageName}) TimeSpan.MaxValue
if result <> 0 then failwith "Docker tag latest failed"
)
#load "paket-files/build/fsharp/FAKE/modules/Octokit/Octokit.fsx"
open Octokit
Target.create "Deploy" (fun _ ->
let user = Environment.environVarOrDefault "GithubUser" String.Empty
let pw = Environment.environVarOrDefault "GithubPassword" String.Empty
// release on github
createClient user pw
|> createDraft "forki" "Audio" release.NugetVersion (release.SemVer.PreRelease <> None) release.Notes
|> uploadFile currentFirmware
|> releaseDraft
|> Async.RunSynchronously
let result =
Process.execSimple (fun info ->
{ info with
FileName = "docker"
WorkingDirectory = deployDir
Arguments = sprintf "login %s --username \"%s\" --password \"%s\"" dockerLoginServer dockerUser dockerPassword }) TimeSpan.MaxValue
if result <> 0 then failwith "Docker login failed"
let result =
Process.execSimple (fun info ->
{ info with
FileName = "docker"
WorkingDirectory = deployDir
Arguments = sprintf "push %s/%s:%s" dockerUser dockerImageName release.NugetVersion }) TimeSpan.MaxValue
if result <> 0 then failwith "Docker push failed"
let result =
Process.execSimple (fun info ->
{ info with
FileName = "docker"
WorkingDirectory = deployDir
Arguments = sprintf "push %s/%s:latest" dockerUser dockerImageName }) TimeSpan.MaxValue
if result <> 0 then failwith "Docker push latest failed"
)
open Fake.Core.TargetOperators
"Clean"
==> "InstallClient"
==> "SetReleaseNotes"
==> "Build"
==> "CreateFirmware"
==> "BundleClient"
==> "CreateDockerImage"
==> "PrepareRelease"
==> "Deploy"
"Clean"
==> "InstallClient"
==> "RestoreServer"
==> "Run"
Target.runOrDefaultWithArguments "Build"