-
Notifications
You must be signed in to change notification settings - Fork 2
/
DefaultSetup.fsx
576 lines (463 loc) · 23.5 KB
/
DefaultSetup.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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
#load "loadall.fsx"
#load "AdditionalSources.fsx"
#load "AssemblyResources.fsx"
//#load ".paket/load/netstandard2.0/build/build.group.fsx"
namespace Aardvark.Fake
open System
open System.IO
open System.Diagnostics
open System.Text.RegularExpressions
open Aardvark.Fake
open Argu
open Fake.IO
open Fake.IO.Globbing.Operators
open Fake.Tools.Git
open Fake.Core
open Fake.Core.TargetOperators
open Fake.DotNet
[<AutoOpen>]
module Startup =
let notes = ReleaseNotes.load "RELEASE_NOTES.md"
let getVersion() =
notes.NugetVersion
let getReleaseNotes() =
notes.Notes |> String.concat "^"
let createTag (newVersion : string) =
if CommandHelper.directRunGitCommand "." (sprintf "tag -a %s -m \"%s\"" newVersion newVersion) then
Trace.logfn "created tag %A" newVersion
try
try
Branches.pushTag "." "origin" newVersion
with e ->
Trace.traceError "failed to push tag %A to origin (please push yourself)"
raise e
with e ->
Branches.deleteTag "." newVersion
Trace.logfn "deleted tag %A" newVersion
raise e
else
failwithf "could not create tag: %A" newVersion
type private Arguments =
| Debug
| Verbose
| Pre
interface IArgParserTemplate with
member s.Usage =
match s with
| Debug -> "debug build"
| Verbose -> "verbose mode"
| Pre -> "prerelease package"
let private argParser = ArgumentParser.Create<Arguments>()
type Config =
{
debug : bool
prerelease : bool
verbose : bool
target : string
args : list<string>
}
let mutable config = { debug = false; prerelease = false; verbose = false; target = "Default"; args = [] }
let entry() =
Environment.SetEnvironmentVariable("Platform", "Any CPU")
let argv = Environment.GetCommandLineArgs() |> Array.skip 2 // yeah really
let res = argParser.Parse(argv, ignoreUnrecognized = true)
let debug = res.Contains <@ Debug @>
let verbose = res.Contains <@ Verbose @>
let prerelease = res.Contains <@ Pre @>
printfn "parsed options: debug=%b, verbose=%b prerelease=%b" debug verbose prerelease
let argv = argv |> Array.filter (fun str -> not (str.StartsWith "-")) |> Array.toList
let target, args =
match argv with
| [] -> "Default", []
| t::rest -> t, rest
//Paket.Logging.verbose <- verbose
config <- { debug = debug; prerelease = prerelease; verbose = verbose; target = target; args = args }
//Environment.SetEnvironmentVariable("Target", target)
let target =
try ignore (Target.get target); target
with _ -> "Help"
Target.run 1 target []
module NugetInfo =
let defaultValue (fallback : 'a) (o : Option<'a>) =
match o with
| Some o -> o
| None -> fallback
let private adjust (v : PreRelease) =
let o =
let number = v.Values |> List.tryPick (function PreReleaseSegment.Numeric n -> Some n | _ -> None)
match number with
| Some n -> sprintf "%s%04d" v.Name (int n)
| None -> v.Name
{ v with
Origin = o
Values = [AlphaNumeric o]
}
let nextVersion (major : bool) (prerelease : bool) (v : string) =
let v : SemVerInfo = SemVer.parse v
let version =
match v.PreRelease with
| Some _ when prerelease -> { v with Original = None }
| Some _ -> { v with PreRelease = None; Original = None }
| _ ->
match major with
| false -> { v with Patch = v.Patch + 1u; Original = None }
| true -> { v with Minor = v.Minor + 1u; Patch = 0u; Original = None }
if prerelease then
let incrementPreRelease (s : PreReleaseSegment) =
let prefix = "prerelease"
let increment (number : string) =
match Int32.TryParse number with
| true, n -> Some <| bigint (n + 1)
| _ -> None
match s with
| Numeric n -> Numeric (n + bigint 1)
| AlphaNumeric str as o ->
if str.StartsWith prefix then
increment (str.Substring prefix.Length)
|> Option.map Numeric
|> Option.defaultValue o
else
o
let pre =
version.PreRelease |> Option.map (fun p ->
{ p with Values = p.Values |> List.map incrementPreRelease }
)
let def =
{
Origin = "prerelease1"
Name = "prerelease"
Values = [ AlphaNumeric "prerelease"; Numeric (bigint 1) ]
}
{ version with PreRelease = pre |> defaultValue def |> adjust |> Some }.ToString()
else
{ version with PreRelease = None}.ToString()
let assemblyVersion (vstr : string) =
let v : SemVerInfo = SemVer.parse vstr
sprintf "%d.%d.0.0" v.Major v.Minor
module DefaultSetup =
let mutable verbosity = Some Fake.DotNet.MSBuildVerbosity.Minimal
let packageNameRx = Regex @"^(?<name>[a-zA-Z_0-9\.-]+?)\.(?<version>([0-9]+\.)*[0-9]+)(.*?)\.nupkg$"
let getUserConsentForPush (oldVersion : string) (newVersion : string) =
printfn ""
printfn "Package version (current): %s" oldVersion
printfn "Package version (new) : %s" newVersion
printf "Create and push new version to deploy targets (Y|_N_)? "
match Console.ReadLine() with
| "y"
| "Y" -> ()
| _ -> printfn "aborting"
Environment.Exit(0) |> ignore
let push targets =
for (target, keyName) in targets do
let packages = !!"bin/*.nupkg"
let packageNameRx = Regex @"^(?<name>[a-zA-Z_0-9\.-]+?)\.(?<version>([0-9]+\.)*[0-9]+)(.*?)\.nupkg$"
let myPackages =
packages
|> Seq.choose (fun p ->
let m = packageNameRx.Match (Path.GetFileName p)
if m.Success then
Some(m.Groups.["name"].Value)
else
None
)
|> Set.ofSeq
let accessKey =
let accessKeyPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile),".ssh")
let readKey dir =
let accessKeyPath = Path.Combine(dir, keyName)
if File.Exists accessKeyPath then
let r = Some (File.ReadAllText accessKeyPath)
Trace.logfn "key: %A" r.Value
r
else printfn "bad:%s" accessKeyPath; None
match readKey accessKeyPath with
| Some key -> Some key
| _ -> readKey (Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),"aardvark-keys"))
let tag = getVersion()
match accessKey with
| Some accessKey ->
try
for id in myPackages do
let names = [ sprintf "bin/%s.%s.nupkg" id tag ]
for packageName in names do
Trace.logfn "pushing: %s" packageName
//Paket.Dependencies.Push(packageName, apiKey = accessKey, url = target)
let command = sprintf "push %s --api-key %s --url %s" packageName accessKey target
AdditionalSources.shellExecutePaket None command
with e ->
Trace.traceError (string e)
| None ->
Trace.traceError (sprintf "Could not find nuget access key")
createTag tag
let install(solutionNames : seq<string>) =
let core = Seq.head solutionNames
//let vsVersion =
// Fake.DotNet.MSBuild.
// match MSBuildHelper.MSBuildDefaults.Properties |> List.tryPick (fun (n,v) -> if n = "VisualStudioVersion" then Some v else None) with
// | Some vsVersion -> vsVersion
// | None ->
// let versionRx = System.Text.RegularExpressions.Regex @"\\(?<version>[0-9]+\.[0-9]+)\\bin\\msbuild\.exe$"
// let m = versionRx.Match (MSBuildHelper.msBuildExe.ToLower())
// if m.Success then
// m.Groups.["version"].Value
// else
// "15.0"
// //failwith "could not determine Visual Studio Version"
Target.create "Install" (fun _ ->
//AdditionalSources.paketDependencies.Install(false)
AdditionalSources.shellExecutePaket (Some core) "install"
AdditionalSources.installSources()
)
Target.create "Restore" (fun _ ->
if not (File.Exists "paket.lock") then
AdditionalSources.shellExecutePaket None "install"
core |> Fake.DotNet.DotNet.restore (fun o ->
{ o with MSBuildParams = { o.MSBuildParams with DisableInternalBinLog = true }}
)
//Fake.DotNet.MSBuild.run o "./bin" "Restore" [] [core] |> ignore
AdditionalSources.installSources ()
)
Target.create "Update" (fun _ ->
match config.args with
| [] ->
//AdditionalSources.paketDependencies.Update(false)
AdditionalSources.shellExecutePaket (Some core) "update"
| xs ->
let filter = xs |> List.map (sprintf "(%s)") |> String.concat "|" |> sprintf "(%s)"
//AdditionalSources.paketDependencies.UpdateFilteredPackages(Some "Main",filter,None,false,false,false,false,false,Paket.SemVerUpdateMode.NoRestriction,false)
let command = sprintf "update --group Main --filter %s" filter;
AdditionalSources.shellExecutePaket (Some core) command
AdditionalSources.installSources ()
)
Target.create "AddSource" (fun _ ->
AdditionalSources.addSources core config.args
)
Target.create "RemoveSource" (fun _ ->
AdditionalSources.removeSources core config.args
)
Target.create "Clean" (fun _ ->
Shell.deleteDir (Path.Combine("bin", "Release"))
Shell.deleteDir (Path.Combine("bin", "Debug"))
)
Target.create "Compile" (fun _ ->
let cfg = if config.debug then "Debug" else "Release"
let tag =
try
let tag = getVersion()
let assemblyVersion = NugetInfo.assemblyVersion tag
Some (tag, assemblyVersion)
with _ -> None
let props =
[
yield "Configuration", cfg
match tag with
| Some (tag, assemblyVersion) ->
yield "AssemblyVersion", assemblyVersion
yield "AssemblyFileVersion", assemblyVersion
yield "InformationalVersion", assemblyVersion
yield "ProductVersion", assemblyVersion
yield "PackageVersion", tag
| _ -> ()
]
core |> Fake.DotNet.DotNet.build (fun o ->
{ o with
NoRestore = true
Configuration = if config.debug then Fake.DotNet.DotNet.BuildConfiguration.Debug else Fake.DotNet.DotNet.BuildConfiguration.Release
MSBuildParams =
{ o.MSBuildParams with
Properties = props
DisableInternalBinLog = true
Verbosity = verbosity
}
}
)
)
Target.create "UpdateBuildScript" (fun _ ->
//AdditionalSources.paketDependencies.UpdateGroup("Build",false,false,false,false,false,Paket.SemVerUpdateMode.NoRestriction,false)
AdditionalSources.shellExecutePaket None "update --group Build"
)
Target.create "CreatePackage" (fun _ ->
let tag = getVersion()
//AdditionalSources.paketDependencies.Pack("bin", version = tag, releaseNotes = releaseNotes, buildPlatform = "AnyCPU")
let command = sprintf "pack bin --interproject-references fix --build-platform AnyCPU --version %s --release-notes \"%s\"" tag (getReleaseNotes())
let command =
if config.debug then
command + " --build-config Debug"
else
command
AdditionalSources.shellExecutePaket None command
)
Target.create "Push" (fun _ ->
if IncrediblyUglyHackfulNugetOverride.isHackActive () then
Trace.traceImportant "there are hacked packages in your global nuget folder. If you continue you are really hateful. Press any key to destroy all packages and deal with method not founds all the way!"
System.Console.ReadLine() |> ignore
let rx = Regex @"(?<url>[^ ]+)[ \t]*(?<keyfile>[^ ]+)"
let targets = "deploy.targets"
let targets =
if File.Exists targets then
File.ReadAllLines targets
|> Array.choose (fun str ->
let m = rx.Match str
if m.Success then
Some (m.Groups.["url"].Value, m.Groups.["keyfile"].Value)
else
Trace.traceImportant (sprintf "could not parse target: %A" str)
None
)
else
[||]
push targets
)
Target.create "AddNativeResources" (fun _ ->
let dir =
if Directory.Exists "libs/Native" then Some "libs/Native"
elif Directory.Exists "lib/Native" then Some "lib/Native"
else None
let dirs (dir : string) (pat : string) (o : SearchOption) =
if Directory.Exists dir then
let rx = System.Text.RegularExpressions.Regex pat
Directory.GetDirectories(dir, "*", o)
|> Array.filter (Path.GetFileName >> rx.IsMatch)
|> Array.map Path.GetFullPath
else
[||]
let files (dir : string) (pat : string) (o : SearchOption) =
if Directory.Exists dir then
let rx = System.Text.RegularExpressions.Regex pat
Directory.GetFiles(dir, "*", o)
|> Array.filter (Path.GetFileName >> rx.IsMatch)
|> Array.map Path.GetFullPath
else
[||]
let binDirs =
(
dirs "bin" "(^netcoreapp.*$)|(^net4.*$)|(^net5.0$)|^Debug$|^Release$" SearchOption.AllDirectories
|> Array.toList
)
match dir with
| Some dir ->
for d in Directory.GetDirectories dir do
let n = Path.GetFileName d
let d = d |> Path.GetFullPath
let paths =
Array.concat [
files "bin/Release" (@"^.*\.(dll|exe)$") SearchOption.AllDirectories
files "bin/Debug" (@"^.*\.(dll|exe)$") SearchOption.AllDirectories
]
|> Array.filter (fun p ->
Path.GetFileNameWithoutExtension(p).ToLower() = n.ToLower()
)
AssemblyResources.copyDependencies d binDirs
for p in paths do
if File.Exists p then
try
Trace.logfn "adding folder %A to %A p" d p
AssemblyResources.addFolder d p
with e ->
Trace.logfn "could not add folder %A to assembly %A with %A, retrying without symbols" d p e
AssemblyResources.addFolder' d p false
| None ->
()
)
Target.create "OverrideGlobalPackages" (fun _ ->
IncrediblyUglyHackfulNugetOverride.copyToGlobal getVersion false
)
Target.create "RevertGlobalPackages" (fun _ ->
IncrediblyUglyHackfulNugetOverride.copyToGlobal getVersion true
)
Target.create "Help" (fun _ ->
let defColor = Console.ForegroundColor
let highlightColor = ConsoleColor.Yellow
printfn "aardvark build script"
printfn " syntax: build [Target] [Options] where target is one of the following"
printfn " with Options = --verbose | --debug |--pre"
printfn " please note, that for reasons, debug builds are also built into bin/Release"
Console.ForegroundColor<-highlightColor
printfn " Default (which is executed when no target is given)"
Console.ForegroundColor<-defColor
printfn " same like compile but also copying native dependencies (from libs/Native/PROJECTNAME)"
printfn " to bin/Release and injecting them as resource into the resulting dll/exe"
Console.ForegroundColor<-highlightColor
printfn " Compile"
Console.ForegroundColor<-defColor
printfn " builds the project's solution to bin/Release"
Console.ForegroundColor<-highlightColor
printfn " Clean"
Console.ForegroundColor<-defColor
printfn " deletes all output files in bin/Debug and bin/Release"
Console.ForegroundColor<-highlightColor
printfn " CreatePackage"
Console.ForegroundColor<-defColor
printfn " creates packages for all projects having a paket.template using the current"
printfn " git tag as version (note that the tag needs to have a comment)."
printfn " the resulting packages are stored in bin/*.nupkg"
Console.ForegroundColor<-highlightColor
printfn " Push"
Console.ForegroundColor<-defColor
printfn " creates packages (see CreatePackage) and deploys to all feeds specified in deploy.targets"
Console.ForegroundColor<-highlightColor
printfn " PushMinor [--pre]"
Console.ForegroundColor<-defColor
printfn " increments current version and creates and deploys package(s) (Push), e.g."
printfn " 1.2.3 -> 1.2.4"
printfn " 1.2.3 -> 1.2.4-prelease1 (when using --pre)"
Console.ForegroundColor<-highlightColor
printfn " PushMajor [--pre]"
Console.ForegroundColor<-defColor
printfn " increments current version and creates and deploys package(s) (Push), e.g."
printfn " 1.2.3 -> 1.3.0"
printfn " 1.2.3 -> 1.3.0-prelease1 (when using --pre)"
Console.ForegroundColor<-highlightColor
printfn " PushPre"
Console.ForegroundColor<-defColor
printfn " creates and deploys prelease package(s), e.g."
printfn " 1.2.3 -> 1.2.4-prelease1"
printfn " 1.2.3-prelease1 -> 1.2.3-prelease2"
Console.ForegroundColor<-highlightColor
printfn " Restore"
Console.ForegroundColor<-defColor
printfn " ensures that all packages given in paket.lock are installed"
printfn " with their respective version."
Console.ForegroundColor<-highlightColor
printfn " Install"
Console.ForegroundColor<-defColor
printfn " installs all packages specified in paket.dependencies and"
printfn " adjusts project files according to paket.references (next to the project)"
printfn " may also perform a new resolution when versions in paket.dependencies have changed."
Console.ForegroundColor<-highlightColor
printfn " Update [regex]"
Console.ForegroundColor<-defColor
printfn " searches for newer compatible version (according to paket.dependencies)"
printfn " and installs them if possible. this target also adjusts the project files"
Console.ForegroundColor<-highlightColor
printfn " UpdateBuildScript"
Console.ForegroundColor<-defColor
printfn " updates the build script and its dependencies."
printfn ""
printfn " advanced features"
Console.ForegroundColor<-highlightColor
printfn " AddSource <folder>"
Console.ForegroundColor<-defColor
printfn " adds the repository located in <folder> as source dependecy causing all packages"
printfn " referenced from that repository to be overriden by a locally built variant."
printfn " Note that these overrides are not version-aware and will override all packages"
printfn " without any compatibility checks."
printfn " Furthermore these source packages \"survive\" Install/Update/Restore and"
printfn " are rebuilt (upon restore/install/update) when files are modified in the source folder"
Console.ForegroundColor<-highlightColor
printfn " RemoveSource <folder>"
Console.ForegroundColor<-defColor
printfn " adds the repository located in <folder> from the source dependencies and restores"
printfn " the original version from its respective package source."
)
Target.create "Default" ignore
"Restore" ==> "Compile" |> ignore
"Compile" ==> "AddNativeResources" |> ignore
"AddNativeResources" ==> "CreatePackage" |> ignore
"Compile" ==> "CreatePackage" |> ignore
"Compile" ==> "AddNativeResources" ==> "Default" |> ignore
"CreatePackage" ==> "OverrideGlobalPackages" |> ignore
"CreatePackage" ==> "Push" |> ignore
//"PushMinor" |> ignore
//"PushMajor" |> ignore
//"PushPre" |> ignore