forked from fable-compiler/Fable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.fsx
511 lines (423 loc) · 18.7 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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
#r "packages/FAKE/tools/FakeLib.dll"
open System
open System.IO
open System.Text.RegularExpressions
open Fake
open Fake.AssemblyInfoFile
module Util =
open System.Net
let join pathParts =
Path.Combine(Array.ofSeq pathParts)
let run workingDir fileName args =
printfn "CWD: %s" workingDir
let fileName, args =
if EnvironmentHelper.isUnix
then fileName, args else "cmd", ("/C " + fileName + " " + args)
let ok =
execProcess (fun info ->
info.FileName <- fileName
info.WorkingDirectory <- workingDir
info.Arguments <- args) TimeSpan.MaxValue
if not ok then failwith (sprintf "'%s> %s %s' task failed" workingDir fileName args)
let runAndReturn workingDir fileName args =
printfn "CWD: %s" workingDir
let fileName, args =
if EnvironmentHelper.isUnix
then fileName, args else "cmd", ("/C " + args)
ExecProcessAndReturnMessages (fun info ->
info.FileName <- fileName
info.WorkingDirectory <- workingDir
info.Arguments <- args) TimeSpan.MaxValue
|> fun p -> p.Messages |> String.concat "\n"
let downloadArtifact path (url: string) =
let tempFile = Path.ChangeExtension(Path.GetTempFileName(), ".zip")
use client = new WebClient()
use stream = client.OpenRead(url)
use writer = new StreamWriter(tempFile)
stream.CopyTo(writer.BaseStream)
FileUtils.mkdir path
CleanDir path
run path "unzip" (sprintf "-q %s" tempFile)
File.Delete tempFile
let rmdir dir =
if EnvironmentHelper.isUnix
then FileUtils.rm_rf dir
// Use this in Windows to prevent conflicts with paths too long
else run "." "cmd" ("/C rmdir /s /q " + Path.GetFullPath dir)
let visitFile (visitor: string->string) (fileName : string) =
File.ReadAllLines(fileName)
|> Array.map (visitor)
|> fun lines -> File.WriteAllLines(fileName, lines)
// This code is supposed to prevent OutOfMemory exceptions but it outputs wrong BOM
// use reader = new StreamReader(fileName, encoding)
// let tempFileName = Path.GetTempFileName()
// use writer = new StreamWriter(tempFileName, false, encoding)
// while not reader.EndOfStream do
// reader.ReadLine() |> visitor |> writer.WriteLine
// reader.Close()
// writer.Close()
// File.Delete(fileName)
// File.Move(tempFileName, fileName)
let compileScript symbols outDir fsxPath =
let dllFile = Path.ChangeExtension(Path.GetFileName fsxPath, ".dll")
let opts = [
yield FscHelper.Out (Path.Combine(outDir, dllFile))
yield FscHelper.Target FscHelper.TargetType.Library
yield! symbols |> List.map FscHelper.Define
]
FscHelper.compile opts [fsxPath]
|> function 0 -> () | _ -> failwithf "Cannot compile %s" fsxPath
let normalizeVersion (version: string) =
let i = version.IndexOf("-")
if i > 0 then version.Substring(0, i) else version
let assemblyInfo projectDir version extra =
let version = normalizeVersion version
let asmInfoPath = projectDir </> "AssemblyInfo.fs"
(Attribute.Version version)::extra
|> CreateFSharpAssemblyInfo asmInfoPath
let loadReleaseNotes pkg =
Lazy<_>(fun () ->
sprintf "RELEASE_NOTES_%s.md" pkg
|> ReleaseNotesHelper.LoadReleaseNotes)
module Npm =
let script workingDir script args =
sprintf "run %s -- %s" script (String.concat " " args)
|> Util.run workingDir "npm"
let install workingDir modules =
sprintf "install %s" (String.concat " " modules)
|> Util.run workingDir "npm"
let command workingDir command args =
sprintf "%s %s" command (String.concat " " args)
|> Util.run workingDir "npm"
let commandAndReturn workingDir command args =
sprintf "%s %s" command (String.concat " " args)
|> Util.runAndReturn workingDir "npm"
let getLatestVersion package tag =
let package =
match tag with
| Some tag -> package + "@" + tag
| None -> package
commandAndReturn "." "show" [package; "version"]
let updatePackageKeyValue f pkgDir keys =
let pkgJson = Path.Combine(pkgDir, "package.json")
let reg =
String.concat "|" keys
|> sprintf "\"(%s)\"\\s*:\\s*\"(.*?)\""
|> Regex
let lines =
File.ReadAllLines pkgJson
|> Array.map (fun line ->
let m = reg.Match(line)
if m.Success then
match f(m.Groups.[1].Value, m.Groups.[2].Value) with
| Some(k,v) -> reg.Replace(line, sprintf "\"%s\": \"%s\"" k v)
| None -> line
else line)
File.WriteAllLines(pkgJson, lines)
module Node =
let run workingDir script args =
let args = sprintf "%s %s" script (String.concat " " args)
Util.run workingDir "node" args
module Fake =
let fakePath = "packages" </> "docs" </> "FAKE" </> "tools" </> "FAKE.exe"
let fakeStartInfo script workingDirectory args fsiargs environmentVars =
(fun (info: System.Diagnostics.ProcessStartInfo) ->
info.FileName <- System.IO.Path.GetFullPath fakePath
info.Arguments <- sprintf "%s --fsiargs -d:FAKE %s \"%s\"" args fsiargs script
info.WorkingDirectory <- workingDirectory
let setVar k v = info.EnvironmentVariables.[k] <- v
for (k, v) in environmentVars do setVar k v
setVar "MSBuild" msBuildExe
setVar "GIT" Git.CommandHelper.gitPath
setVar "FSI" fsiPath)
/// Run the given buildscript with FAKE.exe
let executeFAKEWithOutput workingDirectory script fsiargs envArgs =
let exitCode =
ExecProcessWithLambdas
(fakeStartInfo script workingDirectory "" fsiargs envArgs)
TimeSpan.MaxValue false ignore ignore
System.Threading.Thread.Sleep 1000
exitCode
// version info
let releaseCompiler = Util.loadReleaseNotes "COMPILER"
let releaseCore = Util.loadReleaseNotes "CORE"
// Targets
Target "Clean" (fun _ ->
// Don't delete node_modules for faster builds
!! "build/fable/bin" ++ "src/fable/*/obj/"
|> CleanDirs
!! "build/fable/**/*.*" -- "build/fable/node_modules/**/*.*"
|> Seq.iter FileUtils.rm
!! "build/tests/**/*.*" -- "build/tests/node_modules/**/*.*"
|> Seq.iter FileUtils.rm
)
Target "FableSuaveRelease" (fun _ ->
Util.assemblyInfo "src/fable/Fable.Core" releaseCore.Value.NugetVersion []
Util.assemblyInfo "src/fable/Fable.Compiler" releaseCompiler.Value.NugetVersion []
Util.assemblyInfo "src/fable/Fable.Client.Suave" releaseCompiler.Value.NugetVersion [
Attribute.Metadata ("fableCoreVersion", Util.normalizeVersion releaseCore.Value.NugetVersion)
]
let buildDir = "build/fable"
[ "src/fable/Fable.Core/Fable.Core.fsproj"
"src/fable/Fable.Compiler/Fable.Compiler.fsproj"
"src/fable/Fable.Client.Suave/Fable.Client.Suave.fsproj" ]
|> MSBuildRelease (buildDir + "/bin") "Build"
|> Log "Fable-Compiler-Release-Output: "
// For some reason, ProjectCracker targets are not working after updating the package
!! "packages/FSharp.Compiler.Service.ProjectCracker/utilities/net45/FSharp.Compiler.Service.ProjectCrackerTool.exe*"
|> Seq.iter (fun x -> FileUtils.cp x "build/fable/bin")
)
Target "FableCompilerRelease" (fun _ ->
Util.assemblyInfo "src/fable/Fable.Core" releaseCore.Value.NugetVersion []
Util.assemblyInfo "src/fable/Fable.Compiler" releaseCompiler.Value.NugetVersion []
Util.assemblyInfo "src/fable/Fable.Client.Node" releaseCompiler.Value.NugetVersion [
Attribute.Metadata ("fableCoreVersion", Util.normalizeVersion releaseCore.Value.NugetVersion)
]
let buildDir = "build/fable"
[ "src/fable/Fable.Core/Fable.Core.fsproj"
"src/fable/Fable.Compiler/Fable.Compiler.fsproj"
"src/fable/Fable.Client.Node/Fable.Client.Node.fsproj" ]
|> MSBuildRelease (buildDir + "/bin") "Build"
|> Log "Fable-Compiler-Release-Output: "
// For some reason, ProjectCracker targets are not working after updating the package
!! "packages/FSharp.Compiler.Service.ProjectCracker/utilities/net45/FSharp.Compiler.Service.ProjectCrackerTool.exe*"
|> Seq.iter (fun x -> FileUtils.cp x "build/fable/bin")
FileUtils.cp_r "src/fable/Fable.Client.Node/js" buildDir
FileUtils.cp "README.md" buildDir
Npm.command buildDir "version" [releaseCompiler.Value.NugetVersion]
Npm.install buildDir []
)
Target "FableCompilerDebug" (fun _ ->
let buildDir = "build/fable"
[ "src/fable/Fable.Core/Fable.Core.fsproj"
"src/fable/Fable.Compiler/Fable.Compiler.fsproj"
"src/fable/Fable.Client.Node/Fable.Client.Node.fsproj" ]
|> MSBuildDebug (buildDir + "/bin") "Build"
|> Log "Fable-Compiler-Debug-Output: "
FileUtils.cp_r "src/fable/Fable.Client.Node/js" buildDir
Npm.command buildDir "version" [releaseCompiler.Value.NugetVersion]
Npm.install buildDir []
)
Target "FableCompilerNetcore" (fun _ ->
try
// Copy JS files
let srcDir, buildDir = "src/netcore/Fable.Client.Node", "build/fable"
FileUtils.cp_r "src/fable/Fable.Client.Node/js" buildDir
FileUtils.cp "README.md" buildDir
Npm.command buildDir "version" [releaseCompiler.Value.NugetVersion]
Npm.install buildDir []
// Edit package.json for NetCore
(buildDir, ["name"; "fable"])
||> Npm.updatePackageKeyValue (function
| "name", _ -> Some("name", "fable-compiler-netcore")
| "fable", v -> Some("fable-netcore", v)
| _ -> None)
// Restore packages
[ "src/netcore/Forge.Core"; "src/netcore/Fable.Core"; "src/netcore/Fable.Compiler"; srcDir ]
|> Seq.iter (fun dir -> Util.run dir "dotnet" "restore")
// Publish Fable NetCore
Util.run srcDir "dotnet" "publish -c Release"
FileUtils.cp_r (srcDir + "/bin/Release/netcoreapp1.0/publish") (buildDir + "/bin")
// Put FSharp.Core.optdata/sigdata next to FSharp.Core.dll
FileUtils.cp (buildDir + "/bin/runtimes/any/native/FSharp.Core.optdata") (buildDir + "/bin")
FileUtils.cp (buildDir + "/bin/runtimes/any/native/FSharp.Core.sigdata") (buildDir + "/bin")
// Compile NUnit plugin
let pluginDir = "src/plugins/nunit"
Util.run pluginDir "dotnet" "restore"
Util.run pluginDir "dotnet" "build -c Release"
// Compile tests
Node.run "." buildDir ["src/tests --target netcore"]
let testsBuildDir = "build/tests"
FileUtils.cp "src/tests/package.json" testsBuildDir
Npm.install testsBuildDir []
// Copy the development version of fable-core.js
let fableCoreNpmDir = "src/fable/Fable.Core/npm"
Npm.install fableCoreNpmDir []
Npm.script fableCoreNpmDir "tsc" ["fable-core.ts --target ES2015 --declaration"]
setEnvironVar "BABEL_ENV" "target-umd"
Npm.script fableCoreNpmDir "babel" ["fable-core.js -o fable-core.js --compact=false"]
FileUtils.cp "src/fable/Fable.Core/npm/fable-core.js" "build/tests/node_modules/fable-core/"
// Run tests
Npm.script testsBuildDir "test" []
with
| ex ->
printfn "Target FableCompilerNetcore didn't work, make sure of the following:"
printfn "- You have NetCore SDK installed"
printfn "- You cloned FSharp.Compiler.Service on same level as Fable"
printfn "- FSharp.Compiler.Service > build All.NetCore run successfully"
raise ex
)
Target "CompileFableImportTests" (fun _ ->
let buildDir = "build/imports/bin"
CleanDir buildDir
[ "import/Fable.Import.Test.fsproj" ]
|> MSBuildDebug buildDir "Build"
|> Log "Fable-Import-Test-Output: "
)
// Target "FableSuave" (fun _ ->
// let buildDir = "build/suave"
// !! "src/fable-client-suave/Fable.Client.Suave.fsproj"
// |> MSBuildDebug buildDir "Build"
// |> Log "Debug-Output: "
// // Copy Fable.Core.dll to buildDir so it can be referenced by F# code
// FileUtils.cp "import/core/Fable.Core.dll" buildDir
// )
Target "NUnitTest" (fun _ ->
let testsBuildDir = "build/tests"
!! "src/tests/Fable.Tests.fsproj"
|> MSBuildRelease testsBuildDir "Build"
|> ignore
[Path.Combine(testsBuildDir, "Fable.Tests.dll")]
|> Testing.NUnit3.NUnit3 id
)
let compileAndRunMochaTests es2015 =
let testsBuildDir = "build/tests"
let testCompileArgs = if es2015 then ["--ecma es2015"] else []
MSBuildDebug "src/tests/DllRef/bin" "Build" ["src/tests/DllRef/Fable.Tests.DllRef.fsproj"] |> ignore
Node.run "." "build/fable" ["src/tests/DllRef"]
Node.run "." "build/fable" ["src/tests/Other"]
Node.run "." "build/fable" ("src/tests/"::testCompileArgs)
FileUtils.cp "src/tests/package.json" testsBuildDir
Npm.install testsBuildDir []
// Copy the development version of fable-core.js
FileUtils.cp "src/fable/Fable.Core/npm/fable-core.js" "build/tests/node_modules/fable-core/"
Npm.script testsBuildDir "test" []
Target "MochaTest" (fun _ ->
compileAndRunMochaTests false
)
Target "ES6MochaTest" (fun _ ->
compileAndRunMochaTests true
)
let quickTest _ =
FileUtils.mkdir "src/tools/temp/node_modules/fable-core/"
FileUtils.cp "src/fable/Fable.Core/npm/package.json" "src/tools/temp/node_modules/fable-core/"
FileUtils.cp "src/fable/Fable.Core/npm/fable-core.js" "src/tools/temp/node_modules/fable-core/"
Node.run "." "build/Fable" ["src/tools/QuickTest.fsx -o temp -m commonjs"]
Node.run "." "src/tools/temp/QuickTest.js" []
Target "QuickTest" quickTest
Target "QuickFableCompilerTest" quickTest
Target "QuickFableCoreTest" quickTest
Target "Plugins" (fun _ ->
!! "src/plugins/**/*.fsx"
|> Seq.iter (fun fsx -> Util.compileScript [] (Path.GetDirectoryName fsx) fsx)
)
Target "Providers" (fun _ ->
!! "src/providers/**/*.fsx"
|> Seq.filter (fun path -> path.Contains("test") |> not)
|> Seq.iter (fun fsxPath ->
let buildDir = Path.GetDirectoryName(Path.GetDirectoryName(fsxPath))
Util.compileScript ["NO_GENERATIVE"] buildDir fsxPath)
)
Target "MakeArtifactLighter" (fun _ ->
Util.rmdir "build/fable/node_modules"
!! "build/fable/bin/*.pdb" ++ "build/fable/bin/*.xml"
|> Seq.iter FileUtils.rm
)
Target "PublishCompiler" (fun _ ->
let applyTag = function
| Some tag -> ["--tag"; tag]
| None -> []
// Check if version is prerelease or not
let fableCompilerTag =
if releaseCompiler.Value.NugetVersion.IndexOf("-") > 0 then Some "next" else None
let workingDir = "temp/build"
let url = "https://ci.appveyor.com/api/projects/alfonsogarciacaro/fable/artifacts/build/fable.zip"
Util.downloadArtifact workingDir url
applyTag fableCompilerTag |> Npm.command workingDir "publish"
)
Target "PublishCore" (fun _ ->
// Check if version is prerelease or not
if releaseCore.Value.NugetVersion.IndexOf("-") > 0 then ["--tag next"] else []
|> Npm.command "src/fable/Fable.Core/npm" "publish"
)
Target "PublishCompilerNetcore" (fun _ ->
// Check if version is prerelease or not
if releaseCompiler.Value.NugetVersion.IndexOf("-") > 0 then ["--tag next"] else []
|> Npm.command "build/fable" "publish"
)
Target "FableCoreRelease" (fun _ ->
let fableCoreNpmDir = "src/fable/Fable.Core/npm"
// Update fable-core npm version
(fableCoreNpmDir, ["version"])
||> Npm.updatePackageKeyValue (fun (_,v) ->
if v <> releaseCore.Value.NugetVersion
then Some("version", releaseCore.Value.NugetVersion)
else None)
// Update Fable.Core version
Util.assemblyInfo "src/fable/Fable.Core/" releaseCore.Value.NugetVersion []
!! "src/fable/Fable.Core/Fable.Core.fsproj"
|> MSBuild fableCoreNpmDir "Build" [
"Configuration","Release"
"DefineConstants","IMPORT"
"DocumentationFile","npm/Fable.Core.xml"]
|> ignore // Log outputs all files in node_modules
Npm.install fableCoreNpmDir []
// Compile TypeScript
Npm.script fableCoreNpmDir "tsc" ["fable-core.ts --target ES2015 --declaration"]
// Compile Es2015 syntax to ES5 with different module targets
setEnvironVar "BABEL_ENV" "target-commonjs"
Npm.script fableCoreNpmDir "babel" ["fable-core.js -o commonjs.js --compact=false"]
setEnvironVar "BABEL_ENV" "target-es2015"
Npm.script fableCoreNpmDir "babel" ["fable-core.js -o es2015.js --compact=false"]
setEnvironVar "BABEL_ENV" "target-umd"
Npm.script fableCoreNpmDir "babel" ["fable-core.js -o fable-core.js --compact=false"]
// Minimize
Npm.script fableCoreNpmDir "uglifyjs" ["fable-core.js -c -m -o fable-core.min.js"]
)
Target "FableCoreDebug" (fun _ ->
let fableCoreNpmDir = "src/fable/Fable.Core/npm"
Npm.script fableCoreNpmDir "tsc" ["fable-core.ts --target ES2015 --declaration"]
setEnvironVar "BABEL_ENV" "target-umd"
Npm.script fableCoreNpmDir "babel" ["fable-core.js -o fable-core.js --compact=false"]
)
Target "UpdateSampleRequirements" (fun _ ->
let fableVersion = "^" + releaseCompiler.Value.NugetVersion
let fableCoreVersion = "^" + releaseCore.Value.NugetVersion
!! "samples/**/package.json"
|> Seq.iter (fun path ->
(Path.GetDirectoryName path, ["fable"; "fable-core"])
||> Npm.updatePackageKeyValue (fun (k,v) ->
match k with
| "fable" when v <> fableVersion -> Some(k, fableVersion)
| "fable-core" when v <> fableCoreVersion -> Some(k, fableCoreVersion)
| _ -> None
))
)
Target "BrowseDocs" (fun _ ->
let exit = Fake.executeFAKEWithOutput "docs" "docs.fsx" "" ["target", "BrowseDocs"]
if exit <> 0 then failwith "Browsing documentation failed"
)
Target "GenerateDocs" (fun _ ->
let exit = Fake.executeFAKEWithOutput "docs" "docs.fsx" "" ["target", "GenerateDocs"]
if exit <> 0 then failwith "Generating documentation failed"
)
Target "PublishDocs" (fun _ ->
let exit = Fake.executeFAKEWithOutput "docs" "docs.fsx" "" ["target", "PublishDocs"]
if exit <> 0 then failwith "Publishing documentation failed"
)
Target "All" ignore
// Build order
"Clean"
==> "FableCoreRelease"
==> "FableCompilerRelease"
==> "CompileFableImportTests"
==> "Plugins"
==> "MochaTest"
=?> ("MakeArtifactLighter", environVar "APPVEYOR" = "True")
==> "All"
"FableCoreRelease"
==> "PublishCore"
"Clean"
==> "FableSuaveRelease"
"Clean"
==> "FableCompilerNetcore"
"FableCompilerNetcore"
==> "PublishCompilerNetcore"
"Plugins"
==> "ES6MochaTest"
"FableCompilerDebug"
==> "QuickFableCompilerTest"
"FableCoreDebug"
==> "QuickFableCoreTest"
// Start build
RunTargetOrDefault "All"