-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.fs
218 lines (184 loc) · 5.76 KB
/
run.fs
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
open System
open System.IO
open Fake.Core
open Fake.IO
open RunHelpers
open RunHelpers.Shortcuts
open RunHelpers.Templates
open RunHelpers.Watch
module Process =
let create cmd args = CreateProcess.fromRawCommand cmd args
[<AutoOpen>]
module Shortcuts =
let sass args =
Process.create "sass" args
|> Job.fromCreateProcess
type BuildMode =
| Debug
| Release
type TargetBrowser =
| FireFox
| Chrome
module Task =
let restore () =
let modules =
Directory.EnumerateFiles("modules", "*.fsproj", SearchOption.AllDirectories)
job {
DotNet.toolRestore ()
Pnpm.install ()
for modul in modules do
DotNet.restore modul
}
let femto () =
let projects =
[ "lib"; "modules" ]
|> List.map (fun folder -> Directory.EnumerateFiles(folder, "*.fsproj", SearchOption.AllDirectories))
|> List.reduce Seq.append
job {
for project in projects do
dotnet [ "femto"; project ]
}
let buildSass mode =
let path = "dist/css/"
printfn "Clean %s" path
Shell.cleanDir path
sass [
if mode = Release then "--no-source-map"
"-I"
"node_modules/bulma"
$"sass/:{path}"
]
let buildModule mode targetBrowser modul =
job {
let folder = Path.GetDirectoryName(modul: string)
let fableTarget = $"%s{folder}/dist/"
printfn "- Clean %s" fableTarget
Shell.cleanDir fableTarget
// Build F#/JavaScript
dotnet [
"fable"
folder
"-e"
".fs.js"
if mode = Debug then
"--define"
"DEBUG"
if targetBrowser = Chrome then
"--define"
"CHROME"
"-o"
$"%s{fableTarget}"
]
Process.create
"pnpm"
[ "exec"
"webpack-cli"
"--mode"
match mode with
| Debug -> "development"
| Release -> "production"
"-c"
$"%s{folder}/webpack.config.js" ]
|> CreateProcess.withWorkingDirectory folder
|> Job.fromCreateProcess
}
let watch () =
let setupWatcher =
WatcherOptions.create ()
|> WatcherOptions.excludeFolder "dist"
|> setupWatcher
// Register watchers
let mode = Debug
use _ = setupWatcher [ "sass/" ] (fun () -> buildSass Debug)
use _ =
Directory.EnumerateFiles("modules", "*.fsproj", SearchOption.AllDirectories)
|> Seq.toList
|> List.map (fun modul ->
setupWatcher [ "lib/"; Path.GetDirectoryName modul ] (fun () -> buildModule mode FireFox modul))
|> FileSystemWatcherList.combine
printfn "Waiting for changes... (enter to exit)"
Console.ReadLine() |> ignore
Job.ok
let build mode targetBrowser =
let modules =
Directory.EnumerateFiles("modules", "*.fsproj", SearchOption.AllDirectories)
job {
buildSass mode
for modul in modules do
buildModule mode targetBrowser modul
}
let pack () =
let folder = "./out"
job {
printfn "Clean %s" folder
Shell.cleanDir folder
printfn "Copy files to %s..." folder
// We need the manifest, the assets and the scripts
Shell.copy folder [ "manifest.json" ]
[ "assets"; "dist" ]
|> List.iter (fun dir -> Shell.copyDir $"%s{folder}/%s{dir}" dir (fun _ -> true))
// We remove the content_security_policy, which is only needed for development
printfn "Remove csp line from manifest.json"
File.ReadAllLines $"{folder}/manifest.json"
|> Array.filter (fun line -> not (line.Contains("\"content_security_policy\"")))
|> (fun content -> File.WriteAllLines($"{folder}/manifest.json", content))
// At first we lint the source
pnpm [
"exec"
"web-ext"
"lint"
"-s"
folder
]
// Now we can bundle stuff together
pnpm [
"exec"
"web-ext"
"build"
"-s"
folder
"-a"
folder
]
}
[<EntryPoint>]
let main args =
args
|> List.ofArray
|> function
| [ "restore" ] -> Task.restore ()
| [ "subbuild" ]
| [ "subbuild-firefox " ] -> Task.build Debug FireFox
| [ "subbuild-chrome" ] -> Task.build Debug Chrome
| []
| [ "build" ]
| [ "build-firefox" ] ->
job {
Task.restore ()
Task.femto ()
Task.build Debug FireFox
}
| [ "build-chrome" ] ->
job {
Task.restore ()
Task.femto ()
Task.build Debug Chrome
}
| [ "subwatch" ] -> Task.watch ()
| [ "watch" ] ->
job {
Task.restore ()
Task.femto ()
Task.build Debug FireFox
Task.watch ()
}
| [ "bundle" ]
| [ "pack" ] ->
job {
Task.restore ()
Task.femto ()
Task.build Release FireFox
Task.pack ()
}
| _ -> Job.error [ "Invalid input" ]
|> Job.execute