This repository has been archived by the owner on Nov 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
/
build.fsx
384 lines (313 loc) · 12 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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#r "paket: groupref netcorebuild //"
#load ".fake/build.fsx/intellisense.fsx"
#if !FAKE
#r "./packages/netcorebuild/NETStandard.Library.NETFramework/build/net461/lib/netstandard.dll"
#endif
#nowarn "52"
open System
open System.IO
open System.Text.RegularExpressions
open Fake.Core
open Fake.Core.TargetOperators
open Fake.DotNet
open Fake.IO
open Fake.IO.Globbing.Operators
open Fake.IO.FileSystemOperators
open Fake.Tools.Git
open Fake.JavaScript
let versionFromGlobalJson : DotNet.CliInstallOptions -> DotNet.CliInstallOptions = (fun o ->
{ o with Version = DotNet.Version (DotNet.getSDKVersionFromGlobalJson()) }
)
let dotnetSdk = lazy DotNet.install versionFromGlobalJson
let inline dtntWorkDir wd =
DotNet.Options.lift dotnetSdk.Value
>> DotNet.Options.withWorkingDirectory wd
let inline yarnWorkDir (ws : string) (yarnParams : Yarn.YarnParams) =
{ yarnParams with WorkingDirectory = ws }
let srcFiles =
!! "./src/Thoth.Json/Thoth.Json.fsproj"
++ "./src/Thoth.Json.Net/Thoth.Json.Net.fsproj"
++ "./src/Thoth.Json.Giraffe/Thoth.Json.Giraffe.fsproj"
++ "./src/Thoth.Elmish/Thoth.Elmish.fsproj"
++ "./src/Thoth.Elmish.Toast/Thoth.Elmish.Toast.fsproj"
++ "./src/Thoth.Elmish.FormBuilder/Thoth.Elmish.FormBuilder.fsproj"
++ "./src/Thoth.Elmish.FormBuilder.BasicFields/Thoth.Elmish.FormBuilder.BasicFields.fsproj"
let fableTestsGlob = "tests/fable/**/*.fsproj"
let dotnetTestsGlob = "tests/dotnet/**/*.fsproj"
let docFile = "./docs/Docs.fsproj"
let root = __SOURCE_DIRECTORY__
module Util =
let visitFile (visitor: string -> string) (fileName : string) =
File.ReadAllLines(fileName)
|> Array.map (visitor)
|> fun lines -> File.WriteAllLines(fileName, lines)
let replaceLines (replacer: string -> Match -> string option) (reg: Regex) (fileName: string) =
fileName |> visitFile (fun line ->
let m = reg.Match(line)
if not m.Success
then line
else
match replacer line m with
| None -> line
| Some newLine -> newLine)
// Module to print colored message in the console
module Logger =
let consoleColor (fc : ConsoleColor) =
let current = Console.ForegroundColor
Console.ForegroundColor <- fc
{ new IDisposable with
member x.Dispose() = Console.ForegroundColor <- current }
let warn str = Printf.kprintf (fun s -> use c = consoleColor ConsoleColor.DarkYellow in printf "%s" s) str
let warnfn str = Printf.kprintf (fun s -> use c = consoleColor ConsoleColor.DarkYellow in printfn "%s" s) str
let error str = Printf.kprintf (fun s -> use c = consoleColor ConsoleColor.Red in printf "%s" s) str
let errorfn str = Printf.kprintf (fun s -> use c = consoleColor ConsoleColor.Red in printfn "%s" s) str
let run (cmd:string) dir args =
Command.RawCommand(cmd, Arguments.OfArgs args)
|> CreateProcess.fromCommand
|> CreateProcess.withWorkingDirectory dir
|> Proc.run
|> ignore
// if Process.execSimple (fun info ->
// { info with
// FileName = cmd
// WorkingDirectory =
// if not (String.IsNullOrWhiteSpace dir) then dir else info.WorkingDirectory
// Arguments = args
// }
// ) TimeSpan.MaxValue <> 0 then
// failwithf "Error while running '%s' with args: %s " cmd args
let mono workingDir args =
Command.RawCommand("mono", Arguments.OfArgs args)
|> CreateProcess.fromCommand
|> CreateProcess.withWorkingDirectory workingDir
|> Proc.run
|> ignore
Target.create "Clean" (fun _ ->
!! "src/**/bin"
++ "src/**/obj"
++ "tests/**/bin"
++ "tests/**/obj"
++ "docs/**/bin"
++ "docs/**/obj"
++ "docs/**/build"
++ "docs/scss/extra"
++ "docs/public"
++ "demos/Thoth.Elmish.Demo/bin"
++ "demos/Thoth.Elmish.Demo/obj"
++ "demos/Thoth.Elmish.Demo/output"
|> Shell.cleanDirs
)
Target.create "YarnInstall"(fun _ ->
Yarn.install id
Yarn.install (fun o -> { o with WorkingDirectory = "./docs/" })
)
Target.create "DotnetRestore" (fun _ ->
srcFiles
++ dotnetTestsGlob
++ docFile
|> Seq.iter (fun proj ->
DotNet.restore id proj
))
// let dotnet workingDir command args =
// let result =
// DotNet.exec (fun p ->
// { p with WorkingDirectory = workingDir
// DotNetCliPath = "dotnet" } )
// command
// args
// if not result.OK then failwithf "dotnet failed with code %i" result.ExitCode
let build project framework =
DotNet.build (fun p ->
{ p with Framework = Some framework } ) project
Target.create "MochaTest" (fun _ ->
!! fableTestsGlob
|> Seq.iter(fun proj ->
let projDir = proj |> Path.getDirectory
let configFile = projDir </> "splitter.config.js"
//Compile to JS
Yarn.exec ("fable-splitter -c " + configFile) id
//Run mocha tests
//TODO: Seems latest fable-splitter outputs tests Main file to "tests" subfolder, bug?
let projDirOutput = projDir </> "bin/tests"
Yarn.exec ("run mocha " + projDirOutput) id
)
)
let testNetFrameworkDir = root </> "tests" </> "dotnet" </> "bin" </> "Release" </> "net461"
let testNetCoreDir = root </> "tests" </> "dotnet" </> "bin" </> "Release" </> "netcoreapp2.0"
Target.create "ExpectoTest" (fun _ ->
build "tests/dotnet/Thoth.Tests.fsproj" "netcoreapp2.0"
build "tests/dotnet/Thoth.Tests.fsproj" "net461"
if Environment.isUnix then
mono testNetFrameworkDir [ "Thoth.Tests.exe" ]
else
run (testNetFrameworkDir </> "Thoth.Tests.exe") root []
let result = DotNet.exec (dtntWorkDir testNetCoreDir) "" "Thoth.Tests.dll"
if not result.OK then failwithf "Expecto for netcore tests failed."
)
let docs = root </> "docs"
let docsContent = docs </> "src" </> "Content"
let buildMain = docs </> "build" </> "src" </> "Main.js"
let buildSass _ =
Yarn.exec "run node-sass --output-style compressed --output docs/public/ docs/scss/main.scss" id
let applyAutoPrefixer _ =
Yarn.exec "run postcss docs/public/main.css --use autoprefixer -o docs/public/main.css" id
Target.create "Docs.Watch" (fun _ ->
use watcher = new FileSystemWatcher(docsContent, "*.md")
watcher.IncludeSubdirectories <- true
watcher.EnableRaisingEvents <- true
watcher.Changed.Add(fun _ ->
Command.RawCommand("node", Arguments.OfArgs [ buildMain ])
|> CreateProcess.fromCommand
|> Proc.run
|> ignore
)
// Make sure the style is generated
// Watch mode of node-sass don't trigger a first build
buildSass ()
!! docFile
|> Seq.iter (fun proj ->
let projDir = proj |> Path.getDirectory
[ async {
Yarn.exec
"fable-splitter --port free -c docs/splitter.config.js -w"
(yarnWorkDir projDir)
}
async {
Yarn.exec "run node-sass --output-style compressed --watch --output docs/public/ docs/scss/main.scss" id
}
async {
Yarn.exec "run http-server -c-1 docs/public" id
}
]
|> Async.Parallel
|> Async.RunSynchronously
|> ignore
)
)
Target.create "Docs.Setup" (fun _ ->
// Make sure directories exist
Directory.ensure "./docs/scss/extra/highlight.js/"
Directory.ensure "./docs/public/demos/"
// Copy files from node_modules allow us to manage them via yarn
Shell.copyDir "./docs/public/fonts" "./node_modules/@fortawesome/fontawesome-free/webfonts" (fun _ -> true)
// Copy demos file
Shell.copyDir "./docs/public/demos" "./demos/Thoth.Elmish.Demo/output/" (fun _ -> true)
DotNet.restore id docFile
)
Target.create "Docs.Build" (fun _ ->
!! docFile
|> Seq.iter (fun proj ->
let projDir = proj |> Path.getDirectory
Yarn.exec
"fable-splitter --port free -c docs/splitter.config.js -p"
(yarnWorkDir projDir)
buildSass ()
applyAutoPrefixer ()
)
)
Target.create "Watch" (fun _ ->
!! fableTestsGlob
|> Seq.iter(fun proj ->
let projDir = proj |> Path.getDirectory
let configFile = projDir </> "splitter.config.js"
//Compile to JS
Yarn.exec ("fable-splitter -w -c " + configFile) id
)
)
Target.create "Build.Demos" (fun _ ->
Yarn.install (fun o -> { o with WorkingDirectory = "./demos/Thoth.Elmish.Demo/" })
Yarn.exec
"webpack -p"
(yarnWorkDir (root </> "demos" </> "Thoth.Elmish.Demo"))
)
Target.create "Watch.Demos" (fun _ ->
Yarn.install (fun o -> { o with WorkingDirectory = "./demos/Thoth.Elmish.Demo/" })
Yarn.exec
"webpack-dev-server"
(yarnWorkDir (root </> "demos" </> "Thoth.Elmish.Demo"))
)
let needsPublishing (versionRegex: Regex) (releaseNotes: ReleaseNotes.ReleaseNotes) projFile =
printfn "Project: %s" projFile
if releaseNotes.NugetVersion.ToUpper().EndsWith("NEXT")
then
Logger.warnfn "Version in Release Notes ends with NEXT, don't publish yet."
false
else
File.ReadLines(projFile)
|> Seq.tryPick (fun line ->
let m = versionRegex.Match(line)
if m.Success then Some m else None)
|> function
| None -> failwith "Couldn't find version in project file"
| Some m ->
let sameVersion = m.Groups.[1].Value = releaseNotes.NugetVersion
if sameVersion then
Logger.warnfn "Already version %s, no need to publish." releaseNotes.NugetVersion
not sameVersion
let pushNuget (releaseNotes: ReleaseNotes.ReleaseNotes) (projFile: string) =
let versionRegex = Regex("<Version>(.*?)</Version>", RegexOptions.IgnoreCase)
if needsPublishing versionRegex releaseNotes projFile then
let projDir = Path.GetDirectoryName(projFile)
let nugetKey =
match Environment.environVarOrNone "NUGET_KEY" with
| Some nugetKey -> nugetKey
| None -> failwith "The Nuget API key must be set in a NUGET_KEY environmental variable"
(versionRegex, projFile) ||> Util.replaceLines (fun line _ ->
versionRegex.Replace(line, "<Version>" + releaseNotes.NugetVersion + "</Version>") |> Some)
DotNet.pack (fun p ->
{ p with
Configuration = DotNet.Release
Common = { p.Common with DotNetCliPath = "dotnet" } } )
projFile
let files =
Directory.GetFiles(projDir </> "bin" </> "Release", "*.nupkg")
|> Array.find (fun nupkg -> nupkg.Contains(releaseNotes.NugetVersion))
|> fun x -> [x]
Paket.pushFiles (fun o ->
{ o with ApiKey = nugetKey
PublishUrl = "https://www.nuget.org/api/v2/package" })
files
Target.create "Publish" (fun _ ->
srcFiles
|> Seq.iter(fun s ->
let projFile = s
let projDir = IO.Path.GetDirectoryName(projFile)
let release = projDir </> "RELEASE_NOTES.md" |> ReleaseNotes.load
pushNuget release projFile
)
)
// Where to push generated documentation
let githubLink = "[email protected]:MangelMaxime/Thoth.git"
let publishBranch = "gh-pages"
let repoRoot = __SOURCE_DIRECTORY__
let temp = repoRoot </> "temp"
Target.create "Docs.Publish" (fun _ ->
// Clean the repo before cloning this avoid potential conflicts
Shell.cleanDir temp
Repository.cloneSingleBranch "" githubLink publishBranch temp
// Copy new files
Shell.copyRecursive "docs/public" temp true |> printfn "%A"
// Deploy the new site
Staging.stageAll temp
Commit.exec temp (sprintf "Update site (%s)" (DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")))
Branches.push temp
)
"Clean"
==> "YarnInstall"
==> "DotnetRestore"
==> "MochaTest"
==> "ExpectoTest"
==> "Publish"
"DotnetRestore"
==> "Watch"
"Docs.Setup"
<== [ "DotnetRestore" ]
"Build.Demos"
==> "Docs.Setup"
"Docs.Build"
<== [ "Docs.Setup" ]
"Docs.Watch"
<== [ "Docs.Setup" ]
"Docs.Build"
==> "Docs.Publish"
Target.runOrDefault "ExpectoTest"