forked from fsprojects/FSharp.Data.Adaptive
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.fsx
More file actions
511 lines (406 loc) · 16.6 KB
/
build.fsx
File metadata and controls
511 lines (406 loc) · 16.6 KB
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
#nowarn "213"
#r "paket: groupref Build //"
#load ".fake/build.fsx/intellisense.fsx"
open System
open System.IO
open Fake.Core
open Fake.Core.TargetOperators
open Fake.DotNet
open Fake.IO
open Fake.IO.Globbing.Operators
open Fake.Tools
open System.Text.RegularExpressions
do Environment.CurrentDirectory <- __SOURCE_DIRECTORY__
let notes = ReleaseNotes.load "RELEASE_NOTES.md"
let isWindows =
Environment.OSVersion.Platform <> PlatformID.Unix && Environment.OSVersion.Platform <> PlatformID.MacOSX
type GitDescription =
{
tag : string
commitsSince : int
dirty : bool
objectName : string
}
static member TryParse(description : string) =
let rx = Regex @"^(.*?)-([0-9]+)-g(.*?)(-dirty)?$"
let m = rx.Match description
if m.Success then
let tag = m.Groups.[1].Value
let commitsSince = m.Groups.[2].Value |> int
let objectName = m.Groups.[3].Value
let dirty = m.Groups.[4].Success
Some { tag = tag; commitsSince = commitsSince; dirty = dirty; objectName = objectName }
else
None
static member TryGet (repoDir : string) =
let success, lines, err = Git.CommandHelper.runGitCommand repoDir "describe --tags --long --always --dirty"
match lines with
| d :: _ when success ->
match GitDescription.TryParse d with
| Some desc -> Ok desc
| None -> Error err
| _ ->
Error err
static member Get (repoDir : string) =
match GitDescription.TryGet repoDir with
| Ok desc -> desc
| Error err ->
Trace.traceErrorfn "%s" err
failwithf "git could not get information: %s" err
type GitRemoteStatus =
{
branch : string
remote : string
local : string
mergeBase : string
}
member x.RemoteAhead = not x.IsSync && x.mergeBase = x.local
member x.LocalAhead = not x.IsSync && x.mergeBase = x.remote
member x.IsSync = x.local = x.remote
static member TryGet (repoDir : string) =
Git.CommandHelper.directRunGitCommandAndFail repoDir "fetch"
let branch = Git.Information.getBranchName repoDir
match Git.CommandHelper.runGitCommand repoDir (sprintf "rev-parse %s" branch) with
| true, [localName], _ ->
match Git.CommandHelper.runGitCommand repoDir (sprintf "rev-parse origin/%s" branch) with
| true, [remoteName], _ ->
match Git.CommandHelper.runGitCommand repoDir (sprintf "merge-base %s origin/%s" branch branch) with
| true, [mergeBase], _ ->
Ok {
branch = branch
remote = remoteName
local = localName
mergeBase = mergeBase
}
| _, _, err ->
Error err
| _, _, _ ->
Ok {
branch = branch
remote = "empty"
local = localName
mergeBase = "empty"
}
| _, _, err ->
Error err
static member Get(repoDir : string) =
match GitRemoteStatus.TryGet repoDir with
| Ok status -> status
| Error err -> failwithf "git could not get remote status: %s" err
Target.create "Clean" (fun _ ->
if Directory.Exists "bin/Debug" then
Trace.trace "deleting bin/Debug"
Directory.delete "bin/Debug"
if Directory.Exists "bin/Release" then
Trace.trace "deleting bin/Release"
Directory.delete "bin/Release"
let pkgs = !!"bin/*.nupkg" |> Seq.toList
if not (List.isEmpty pkgs) then
Trace.tracefn "deleting packages: %s" (pkgs |> Seq.map Path.GetFileNameWithoutExtension |> String.concat ", ")
File.deleteAll pkgs
let dirs = Directory.EnumerateDirectories("src", "obj", SearchOption.AllDirectories) |> Seq.toList
if not (List.isEmpty dirs) then
for d in dirs do
let parent = Path.GetDirectoryName d
let proj = Directory.GetFiles(parent, "*.fsproj") |> Seq.isEmpty |> not
if proj then
Trace.tracefn "deleting %s" d
Directory.delete d
)
Target.create "Compile" (fun _ ->
let options (o : DotNet.BuildOptions) =
let v = sprintf "%d.%d.%d.%s" notes.SemVer.Major notes.SemVer.Minor notes.SemVer.Patch (string notes.SemVer.Build)
{ o with
Configuration = DotNet.BuildConfiguration.Release
MSBuildParams =
{ o.MSBuildParams with
DisableInternalBinLog = true
Properties =
[
"GenerateAssemblyInfo", "true"
"AssemblyVersion", v
"FileVersion", v
"AssemblyFileVersion", v
"ProductVersion", v
"InformationalVersion", v
]
}
}
DotNet.build options "FSharp.Data.Adaptive.sln"
)
Target.create "NpmInstall" (fun _ ->
let modules = "node_modules" |> Path.GetFullPath
if not (Directory.Exists modules) then
Trace.trace "running `npm install`"
let npm =
if isWindows then CreateProcess.fromRawCommand "cmd" ["/C"; "npm"; "install"; "--dev"]
else CreateProcess.fromRawCommand "npm" ["install"]
use s = new MemoryStream()
npm
|> CreateProcess.withWorkingDirectory Environment.CurrentDirectory
|> CreateProcess.withStandardError (StreamSpecification.UseStream(true, s))
|> CreateProcess.withStandardOutput (StreamSpecification.UseStream(true, s))
|> Proc.run
|> ignore
)
Target.create "CompileFable" (fun _ ->
let npx = "node_modules/npx/index.js" |> Path.GetFullPath
CreateProcess.fromRawCommand "node" [npx; "fable-splitter"; "-c"; "splitter-config.js"]
|> CreateProcess.withWorkingDirectory Environment.CurrentDirectory
|> CreateProcess.withStandardError StreamSpecification.Inherit
|> CreateProcess.withStandardOutput StreamSpecification.Inherit
|> CreateProcess.ensureExitCode
|> Proc.run
|> ignore
)
Target.create "WatchFable" (fun _ ->
let wpds = "node_modules/webpack-dev-server/bin/webpack-dev-server.js" |> Path.GetFullPath
//let proj = "src/FSharp.Data.Adaptive/FSharp.Data.Adaptive.fsproj" |> Path.GetFullPath
//let old = Environment.CurrentDirectory
//Environment.CurrentDirectory <- Path.GetDirectoryName proj
CreateProcess.fromRawCommand "node" [wpds]
|> CreateProcess.withWorkingDirectory Environment.CurrentDirectory
|> CreateProcess.withStandardError StreamSpecification.Inherit
|> CreateProcess.withStandardOutput StreamSpecification.Inherit
|> CreateProcess.ensureExitCode
|> Proc.run
|> ignore
)
Target.create "Pack" (fun _ ->
Paket.pack (fun o ->
{ o with
WorkingDir = Environment.CurrentDirectory
OutputPath = "bin"
PinProjectReferences = true
ProjectUrl = "https://github.com/fsprojects/FSharp.Data.Adaptive"
Version = notes.NugetVersion
ReleaseNotes = String.concat "\n" notes.Notes
}
)
)
Target.create "CheckPush" (fun _ ->
let desc = GitDescription.Get "."
let newTag = notes.NugetVersion
if desc.dirty then
Git.Information.showStatus "."
failwith "repo not clean (please commit your changes)"
let status = GitRemoteStatus.Get "."
if status.RemoteAhead then
failwithf "remote branch %s contains new commits (please pull)" status.branch
elif status.LocalAhead then
Trace.traceImportantfn "local branch %s contains new commits. would you like to push it? (y|N)" status.branch
let answer = Console.ReadLine().Trim().ToLower()
match answer with
| "y" -> Git.CommandHelper.directRunGitCommandAndFail "." (sprintf "push origin %s" status.branch)
| _ -> failwithf "local branch %s contains new commits." status.branch
if desc.tag = newTag && desc.commitsSince > 0 then
failwithf "cannot push package version %s since current head is %d commits ahead of the tag" newTag desc.commitsSince
)
let releaseGithub() =
let packageNameRx = Regex @"^(?<name>[a-zA-Z_0-9\.-]+?)\.(?<version>([0-9]+\.)*[0-9]+.*?)\.nupkg$"
let packages =
!!"bin/*.nupkg"
|> Seq.filter (fun path ->
let name = Path.GetFileName path
let m = packageNameRx.Match name
if m.Success then
m.Groups.["version"].Value = notes.NugetVersion
else
false
)
|> Seq.toList
let token =
let path =
Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile),
".ssh",
"github.token"
)
if File.Exists path then File.ReadAllText path |> Some
else None
match token with
| Some token ->
Fake.Api.GitHub.createClientWithToken token
|> Fake.Api.GitHub.createRelease "fsprojects" "FSharp.Data.Adaptive" notes.NugetVersion (fun p ->
{ p with
Draft = true
Prerelease = notes.SemVer.PreRelease |> Option.isSome
Body = String.concat "\r\n" notes.Notes
}
)
|> Fake.Api.GitHub.uploadFiles packages
|> Fake.Api.GitHub.publishDraft
|> Async.RunSynchronously
| None ->
()
Target.create "Push" (fun _ ->
let packageNameRx = Regex @"^(?<name>[a-zA-Z_0-9\.-]+?)\.(?<version>([0-9]+\.)*[0-9]+.*?)\.nupkg$"
let packages =
!!"bin/*.nupkg"
|> Seq.filter (fun path ->
let name = Path.GetFileName path
let m = packageNameRx.Match name
if m.Success then
m.Groups.["version"].Value = notes.NugetVersion
else
false
)
|> Seq.toList
let targetsAndKeys =
(if File.Exists "deploy.targets" then File.ReadAllLines "deploy.targets" else [||])
|> Array.map (fun l -> l.Split(' '))
|> Array.choose (function [|dst; key|] -> Some (dst, key) | _ -> None)
|> Array.choose (fun (dst, key) ->
let path =
Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile),
".ssh",
key
)
if File.exists path then
let key = File.ReadAllText(path).Trim()
Some (dst, key)
else
None
)
|> Map.ofArray
if List.isEmpty packages then
Trace.traceImportant "no packages produced"
elif Map.isEmpty targetsAndKeys then
Trace.traceImportant "no deploy targets"
else
Git.CommandHelper.directRunGitCommandAndFail "." "fetch --tags"
try Git.Branches.tag "." notes.NugetVersion
with _ -> Trace.traceImportantfn "tag %s already exists" notes.NugetVersion
let status = GitRemoteStatus.Get "."
let desc = GitDescription.Get "."
if desc.dirty then
Git.Information.showStatus "."
failwith "repo not clean (please commit your changes)"
if desc.commitsSince > 0 then
failwithf "cannot push package version %s since current head is %d commits ahead of the tag" desc.tag desc.commitsSince
if status.RemoteAhead then
failwithf "remote branch %s contains new commits (please pull)" status.branch
if status.LocalAhead then
failwithf "local branch %s contains new commits." status.branch
if not desc.dirty && desc.commitsSince = 0 && status.IsSync then
let failed = ref Set.empty
for (dst, key) in Map.toSeq targetsAndKeys do
Trace.tracefn "pushing to %s" dst
let options (o : Paket.PaketPushParams) =
{ o with
PublishUrl = dst
ApiKey = key
WorkingDir = "bin"
}
try
Paket.pushFiles options packages
with _ ->
failed := Set.add dst !failed
let allFailed = targetsAndKeys |> Map.forall (fun dst _ -> Set.contains dst !failed)
if allFailed then
Trace.traceErrorfn "could not push any packages (deleting tag)"
Git.Branches.deleteTag "." notes.NugetVersion
else
if not (Set.isEmpty !failed) then
for f in !failed do
Trace.traceErrorfn "could not push to %s (please push manually)" f
try Git.Branches.pushTag "." "origin" notes.NugetVersion
with _ ->
Trace.traceErrorfn "could not push tag %s: PLEASE PUSH MANUALLY" notes.NugetVersion
reraise()
releaseGithub()
)
Target.create "RunTest" (fun _ ->
let options (o : DotNet.TestOptions) =
{ (o.WithRedirectOutput false) with
NoBuild = true
NoRestore = true
MSBuildParams = { o.MSBuildParams with DisableInternalBinLog = true }
Configuration = DotNet.BuildConfiguration.Release
Logger = Some "console;verbosity=normal"
}
DotNet.test options "FSharp.Data.Adaptive.sln"
)
Target.create "Default" ignore
Target.create "Docs" (fun _ ->
let path = Path.Combine(__SOURCE_DIRECTORY__, "packages/docs/FSharp.Compiler.Tools/tools/fsi.exe")
let workingDir = "docs/tools"
let args = "generate.fsx"
let command, args =
if false (* EnvironmentHelper.isMono *) then "mono", sprintf "'%s' %s" path args
else path, args
if Shell.Exec(command, args, workingDir) <> 0 then
failwith "failed to generate docs"
)
Target.create "GenerateDocs" (fun _ ->
let path = Path.Combine(__SOURCE_DIRECTORY__, "packages/docs/FSharp.Compiler.Tools/tools/fsi.exe")
let workingDir = "docs/tools"
let args = "--define:RELEASE generate.fsx"
let command, args =
if false (* EnvironmentHelper.isMono *) then "mono", sprintf "'%s' %s" path args
else path, args
if Shell.Exec(command, args, workingDir) <> 0 then
failwith "failed to generate docs"
)
let cleanDir (dir : string) =
let info = DirectoryInfo(dir)
if info.Exists then
let children = info.GetFileSystemInfos()
for c in children do
Trace.tracefn "delete %s" c.Name
match c with
| :? DirectoryInfo as d ->
if d.Name <> ".git" then d.Delete(true)
| _ -> c.Delete()
let rec copyRecursive (src : string) (dst : string) =
let info = DirectoryInfo(src)
if info.Exists && not (info.Name.StartsWith ".") then
Directory.ensure dst
let children = info.GetFileSystemInfos()
for c in children do
Trace.tracefn "copy %s" c.Name
match c with
| :? DirectoryInfo as d -> copyRecursive d.FullName (Path.Combine(dst, d.Name))
| :? FileInfo as f -> f.CopyTo(Path.Combine(dst, f.Name)) |> ignore
| _ -> ()
let gitOwner = "fsprojects"
let gitHome = "https://github.com/" + gitOwner
let gitName = "FSharp.Data.Adaptive"
Target.create "ReleaseDocs" (fun _ ->
let name = Guid.NewGuid() |> string
let tempDocsDir = "temp/" + name
let outputDir = "docs/output"
Git.Repository.cloneSingleBranch "" (gitHome + "/" + gitName + ".git") "gh-pages" tempDocsDir
cleanDir tempDocsDir
Directory.ensure outputDir
copyRecursive outputDir tempDocsDir
Git.Staging.stageAll tempDocsDir
let info = Git.Information.getCurrentSHA1 __SOURCE_DIRECTORY__
Git.Commit.exec tempDocsDir (sprintf "Update generated documentation for version %s/%s" notes.NugetVersion info)
Git.Branches.push tempDocsDir
let rec reallyDelete (iter : int) =
if iter >= 0 then
try Directory.Delete(tempDocsDir, true)
with _ -> reallyDelete (iter - 1)
reallyDelete 5
)
"NpmInstall" ==> "CompileFable"
"NpmInstall" ==> "WatchFable"
"Compile" ==>
"Docs"
"Compile" ==>
"GenerateDocs" ==>
"ReleaseDocs"
"CheckPush" ?=> "Compile"
"Compile" ?=> "RunTest"
"RunTest" ?=> "Pack"
"Compile" ?=> "CompileFable"
"Compile" ==> "Pack"
"CompileFable" ==> "Pack"
"Pack" ==> "Push"
"RunTest" ==> "Push"
"CheckPush" ==> "Push"
"Compile" ==> "Default"
"RunTest" ==> "Default"
Target.runOrDefault "Default"