-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.fsx
209 lines (169 loc) · 5.97 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
#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 Fake.Core
open Fake.DotNet
open Fake.IO
open Fake.IO.FileSystemOperators
open Fake.IO.Globbing.Operators
Target.initEnvironment ()
let serverPath = Path.getFullName "./src/Server"
let clientPath = Path.getFullName "./src/Client"
let wcatCliPath = Path.getFullName "./src/wcat"
let pywcatPath = Path.getFullName "./src/Python/pywcat"
let clientDeployPath = Path.combine clientPath "deploy"
let deployDir = Path.getFullName "./deploy"
let release = ReleaseNotes.load "RELEASE_NOTES.md"
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 gitTool = platformTool "git" "git.exe"
let runTool cmd args workingDir =
let arguments = args |> Arguments.OfArgs
Command.RawCommand (cmd, arguments)
|> CreateProcess.fromCommand
|> CreateProcess.withWorkingDirectory workingDir
|> CreateProcess.ensureExitCode
|> Proc.run
|> ignore
let runDotNet cmd workingDir =
let result =
DotNet.exec (DotNet.Options.withWorkingDirectory workingDir) cmd ""
if result.ExitCode <> 0 then failwithf "'dotnet %s' failed in %s" cmd workingDir
let openBrowser url =
//https://github.com/dotnet/corefx/issues/10361
Command.ShellCommand url
|> CreateProcess.fromCommand
|> CreateProcess.ensureExitCodeWithMessage "opening browser failed"
|> Proc.run
|> ignore
Target.create "Clean" (fun _ ->
[ deployDir
clientDeployPath ]
|> Shell.cleanDirs
)
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__
)
Target.create "ReplaceVersions" (fun _ ->
Shell.regexReplaceInFileWithEncoding
"let app = \".+\""
("let app = \"" + release.NugetVersion + "\"")
System.Text.Encoding.UTF8
(Path.combine clientPath "Version.fs")
Shell.regexReplaceInFileWithEncoding
"var version = \".+\""
("var version = \"" + release.NugetVersion + "\"")
System.Text.Encoding.UTF8
(Path.combine wcatCliPath "version.go")
Shell.regexReplaceInFileWithEncoding
"__version__ = \".+\""
("__version__ = \"" + release.NugetVersion + "\"")
System.Text.Encoding.UTF8
(Path.combine pywcatPath "__init__.py")
)
Target.create "Build" (fun _ ->
runDotNet "build" serverPath
runTool yarnTool [ "webpack-cli"; "-p" ] __SOURCE_DIRECTORY__
)
Target.create "Run" (fun _ ->
let goTool = platformTool "go" "go.exe"
let server = async {
runDotNet "watch run" serverPath
}
let client = async {
runTool yarnTool [ "webpack-dev-server" ] __SOURCE_DIRECTORY__
}
let browser = async {
do! Async.Sleep 5000
openBrowser "http://localhost:8084"
}
let gowatcher = async {
//install package and watch for changes
let goInstall _ =
try
runTool goTool [ "install" ] wcatCliPath
Trace.tracefn "Successfully ran go install"
with err ->
Trace.traceErrorfn "Failed to run go install: %s" err.Message
!! (wcatCliPath @@ "**/*.go")
|> ChangeWatcher.run goInstall
|> ignore
goInstall ()
}
let vsCodeSession = Environment.hasEnvironVar "vsCodeSession"
let safeClientOnly = Environment.hasEnvironVar "safeClientOnly"
let skipWcatCLI = Environment.hasEnvironVar "skipWcatCLI"
let tasks =
[ if not safeClientOnly then yield server
if not safeClientOnly && not skipWcatCLI then yield gowatcher
yield client
if not vsCodeSession then yield browser ]
tasks
|> Async.Parallel
|> Async.RunSynchronously
|> ignore
)
let gitCommitRelease message =
runTool gitTool [
"add"
"RELEASE_NOTES.md"
"src/Client/Version.fs"
"src/wcat/version.go"
"src/Python/pywcat/__init__.py"
] __SOURCE_DIRECTORY__
Trace.tracefn "Git added release files"
runTool gitTool [ "commit"; "-m"; message ] __SOURCE_DIRECTORY__
Trace.tracefn "Created git commit: %s" message
let gitTag tag =
let args = [ "tag"; "-a"; tag; "-m"; tag ]
runTool gitTool args __SOURCE_DIRECTORY__
Trace.tracefn "Created git tag: %s" tag
let gitPush _ =
use trace = Trace.traceTask "git push" "Push commit and tag with git"
let args = [ "push"; "--follow-tags" ]
runTool gitTool args __SOURCE_DIRECTORY__
trace.MarkSuccess()
Target.create "Bundle" (fun _ ->
let serverDir = Path.combine deployDir "Server"
let clientDir = Path.combine deployDir "Client"
let publicDir = Path.combine clientDir "public"
let publishArgs = sprintf "publish -c Release -o \"%s\"" serverDir
runDotNet publishArgs serverPath
Shell.copyDir publicDir clientDeployPath FileFilter.allFiles
)
Target.create "Release" (fun _ ->
let tag = sprintf "v%s" release.NugetVersion
let commitMessage = sprintf "Release %s" release.NugetVersion
gitCommitRelease commitMessage
gitTag tag
gitPush()
)
open Fake.Core.TargetOperators
"Clean"
==> "InstallClient"
==> "ReplaceVersions"
==> "Build"
==> "Bundle"
"ReplaceVersions"
==> "Release"
"Clean"
==> "InstallClient"
==> "Run"
Target.runOrDefaultWithArguments "Build"