/
ChocoHelper.fs
830 lines (727 loc) · 39.6 KB
/
ChocoHelper.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
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
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
namespace Fake
#nowarn "44"
/// Contains tasks which allow to call [Chocolatey](https://chocolatey.org)
[<System.Obsolete("Use Fake.Windows.Choco instead")>]
module Choco =
open System
open System.Diagnostics
open System.Text;
open System.IO
open System.Xml.Linq
[<System.Obsolete("Use Fake.Windows.Choco instead")>]
type ChocolateyInstallerType =
| Zip
| Exe
| Msi
| SelfContained
[<System.Obsolete("Use Fake.Windows.Choco instead")>]
type ChocolateyChecksumType =
| Md5
| Sha1
| Sha256
| Sha512
[<System.Obsolete("Use Fake.Windows.Choco instead")>]
/// The choco install parameter type.
[<CLIMutable>]
type ChocoInstallParams = {
/// Version of the package
/// Equivalent to the `--version <version>` option.
Version: string
/// Include prerelease. Default `false`.
/// Equivalent to the `--pre` option.
Prerelease: bool
/// Parameters to pass to the package.
/// Equivalent to the `--params <params>` option.
PackageParameters: string
/// The source to find the package(s) to install.
// Special sources include: ruby, webpi, cygwin, windowsfeatures, and python.
/// Equivalent to the `--source <source>` option.
Source: string
/// Force x86 (32bit) installation on 64 bit systems. Default `false`.
/// Equivalent to the `--forcex86` option.
ForceX86: bool
/// Install Arguments to pass to the native installer in the package.
/// Equivalent to the `--installargs <args>` option.
InstallArgs: string
/// Should install arguments be used exclusively without appending to current package passed arguments? Default `false`.
/// Equivalent to the `--overrideargs` option.
OverrideArgs: bool
/// Skip Powershell - Do not run chocolateyInstall.ps1. Default `false`.
/// Equivalent to the `--skippowershell` option.
SkipPowershell: bool
/// User - used with authenticated feeds.
/// Equivalent to the `--user <user>` option.
User: string
/// Password - the user's password to the source.
/// Equivalent to the `--password <password>` option.
Password: string
/// The choco execution timeout.
Timeout:TimeSpan
/// The location of the choco executable. Automatically found if null or empty.
ToolPath: string
/// A character string containing additional arguments to give to choco.
AdditionalArgs: string
/// Do not prompt for user input or confirmations. Default `true`.
/// Equivalent to the `-y` option.
NonInteractive: bool
}
[<System.Obsolete("Use Fake.Windows.Choco instead")>]
/// The choco pack parameter type.
[<CLIMutable>]
type ChocoPackParams = {
/// The version you would like to insert into the package.
/// Equivalent to the `--version <version>` option.
Version: string
/// The choco execution timeout.
Timeout:TimeSpan
/// The location of the choco executable. Automatically found if null or empty.
ToolPath: string
/// A character string containing additional arguments to give to choco.
AdditionalArgs: string
/// Do not prompt for user input or confirmations. Default `true`.
/// Equivalent to the `-y` option.
NonInteractive: bool
/// Authors of the package.
/// Used for the nuspec creation.
Authors: string list
/// Owners of the package.
/// Used for the nuspec creation.
Owners: string list
/// Id of the package. Should be lowercase, not contains weird chars and use dash (-) instead of spaces.
/// Used for the nuspec creation.
PackageId: string
/// Title of the package.
/// Used for the nuspec creation.
Title: string
/// Summary of the package.
/// Used for the nuspec, chocolateyInstall.ps1 and chocolateyUninstall.ps1 creation.
Summary: string
/// Description of the package.
/// Used for the nuspec creation.
Description: string
/// Tags of the package.
/// Used for the nuspec creation.
Tags: string list
/// Release notes of the package.
/// Used for the nuspec creation.
ReleaseNotes: string
/// Copyright of the package.
/// Used for the nuspec creation.
Copyright: string
/// Output directory for the files (nuspec, chocolateyInstall.ps1 and chocolateyUninstall.ps1) creation.
OutputDir: string
/// Dependencies of the package.
/// Used for the nuspec creation.
Dependencies: NugetDependencies
/// Dependencies by framework of the package.
/// Used for the nuspec creation.
DependenciesByFramework: NugetFrameworkDependencies list
/// References of the package.
/// Used for the nuspec creation.
References: NugetReferences
/// References by framework of the package.
/// Used for the nuspec creation.
ReferencesByFramework: NugetFrameworkReferences list
/// Framework assemblies of the package.
/// Used for the nuspec creation.
FrameworkAssemblies: NugetFrameworkAssemblyReferences list
/// Files of the package.
/// Used for the nuspec creation.
Files: list<string*string option*string option>
/// Url to the software.
/// Used for the nuspec creation.
ProjectUrl: string
/// Url to the icon of the package.
/// Used for the nuspec creation.
IconUrl: string
/// Url to the license of the software.
/// Used for the nuspec creation.
LicenseUrl: string
/// True if the software needs license acceptance. Default: false.
/// Used for the nuspec creation.
RequireLicenseAcceptance: bool
/// Url to the chocolatey package repository, not the software (unless they are the same).
/// Used for the nuspec creation.
PackageSourceUrl: string
/// Url pointing to the location of the wiki or docs of the software.
/// Used for the nuspec creation.
DocsUrl: string
/// Url pointing to the forum or email list group for the software.
/// Used for the nuspec creation.
MailingListUrl: string
/// Url pointing to the location where issues and tickets can be accessed.
/// Used for the nuspec creation.
BugTrackerUrl: string
/// Url pointing to the location of the underlying software source.
/// Used for the nuspec creation.
ProjectSourceUrl: string
/// Boolean specifying whether the package will be marked as a [development-only dependency](https://docs.nuget.org/Release-Notes/NuGet-2.7#development-only-dependencies). Default: false.
/// Used for the nuspec creation.
DevelopmentDependency: bool
/// Url pointing to the installer (exe, msi, zip) of the package.
/// Used to create chocolateyInstall.ps1 if it doesn't exists.
PackageDownloadUrl: string
/// Url pointing to the installer (exe, msi, zip) of the 64 bits version of the package.
/// Used to create chocolateyInstall.ps1 if it doesn't exists.
PackageDownload64Url: string
/// Silent args for the installer.
/// Used to create chocolateyInstall.ps1 and/or chocolateyUninstall.ps1 if it doesn't exists.
SilentArgs: string
/// Unzip location for zip package. Default: Chocolatey install folder.
/// Used to create chocolateyInstall.ps1 if it doesn't exists.
UnzipLocation: string
/// Installer type. Default: Zip.
/// Used to create chocolateyInstall.ps1 and/or chocolateyUninstall.ps1 if it doesn't exists.
InstallerType: ChocolateyInstallerType
/// Either:
///
/// - For zip: the zip filename originally installed
/// - For exe or msi: the full path to the native uninstaller to run
UninstallPath: string
/// The checksum hash value of the PackageDownloadUrl resource
/// This allows a checksum to be validated for files that are not local. The checksum type is covered by ChecksumType.
/// Equivalent to the `--checksum <string>` option of Install-Chocolatey[Zip]Package functions.
/// Used to create chocolateyInstall.ps1 if it doesn't exists.
Checksum: string
/// The checksum hash value of the PackageDownload64Url resource
/// This allows a checksum to be validated for files that are not local. The checksum type is covered by ChecksumType64.
/// Equivalent to the `--checksum <string>` option of Install-Chocolatey[Zip]Package functions.
/// Used to create chocolateyInstall.ps1 if it doesn't exists.
Checksum64: string
/// The type of checksum that the file is validated with. Default: Sha256
/// Used to create chocolateyInstall.ps1 if it doesn't exists.
ChecksumType : ChocolateyChecksumType
/// The type of checksum that the file is validated with. Default: Sha256
/// Used to create chocolateyInstall.ps1 if it doesn't exists.
Checksum64Type : ChocolateyChecksumType
}
[<System.Obsolete("Use Fake.Windows.Choco instead")>]
/// The choco push parameter type.
[<CLIMutable>]
type ChocoPushParams = {
/// The source we are pushing the package to. Default: "https://chocolatey.org/"
/// Equivalent to the `--source <source>` option.
Source: string
/// The api key for the source. If not specified (and not local file source), does a lookup.
/// If not specified and one is not found for an https source, push will fail.
/// Equivalent to the `--apikey <apikey>` option.
ApiKey: string
/// The choco execution timeout.
Timeout:TimeSpan
/// The location of the choco executable. Automatically found if null or empty.
ToolPath: string
/// A character string containing additional arguments to give to choco.
AdditionalArgs: string
/// Do not prompt for user input or confirmations. Default `true`.
/// Equivalent to the `-y` option.
NonInteractive: bool
}
[<System.Obsolete("Use Fake.Windows.Choco instead")>]
type private NuspecData = {
Version: string
Title: string
Authors: string
Owners: string
PackageId: string
Summary: string
Description: string
Tags: string
ReleaseNotes: string
Copyright: string
ProjectUrl: string
IconUrl: string
LicenseUrl: string
RequireLicenseAcceptance: string
PackageSourceUrl: string
DocsUrl: string
MailingListUrl: string
BugTrackerUrl: string
ProjectSourceUrl: string
DevelopmentDependency: string
DependenciesXml: string
ReferencesXml: string
FrameworkAssembliesXml: string
FilesXml: string
}
[<System.Obsolete("Use Fake.Windows.Choco instead")>]
/// The default option set given to choco install.
let ChocoInstallDefaults = {
Timeout = TimeSpan.FromMinutes 5.
NonInteractive = true
Prerelease = false
ForceX86 = false
OverrideArgs = false
SkipPowershell = false
Version = null
PackageParameters = null
Source = null
InstallArgs = null
User = null
Password = null
ToolPath = null
AdditionalArgs = null
}
[<System.Obsolete("Use Fake.Windows.Choco instead")>]
/// The default option set given to choco pack.
let ChocoPackDefaults: ChocoPackParams = {
Timeout = TimeSpan.FromMinutes 5.
NonInteractive = true
Version = null
ToolPath = null
AdditionalArgs = null
Authors = []
Owners = []
PackageId = ""
Title = ""
Summary = null
Description = null
Tags = []
ReleaseNotes = null
Copyright = null
OutputDir = "./Chocolatey"
Dependencies = []
DependenciesByFramework = []
References = []
ReferencesByFramework = []
FrameworkAssemblies = []
Files = []
ProjectUrl = null
IconUrl = null
LicenseUrl = null
RequireLicenseAcceptance = false
PackageSourceUrl = null
DocsUrl = null
MailingListUrl = null
BugTrackerUrl = null
ProjectSourceUrl = null
PackageDownloadUrl = null
PackageDownload64Url = null
SilentArgs = ""
UnzipLocation = "$(Split-Path -parent $MyInvocation.MyCommand.Definition)"
InstallerType = ChocolateyInstallerType.Zip
UninstallPath = null
DevelopmentDependency = false
Checksum = null
Checksum64 = null
ChecksumType = ChocolateyChecksumType.Sha256
Checksum64Type = ChocolateyChecksumType.Sha256
}
[<System.Obsolete("Use Fake.Windows.Choco instead")>]
/// The default option set given to choco push.
let ChocoPushDefaults = {
Timeout = TimeSpan.FromMinutes 5.
NonInteractive = true
Source = "https://chocolatey.org/"
ApiKey = null
ToolPath = null
AdditionalArgs = null
}
[<System.Obsolete("Use Fake.Windows.Choco instead")>]
let private getPaths =
let programDataPath = environVar "ProgramData"
if programDataPath |> isNotNullOrEmpty
then
[
Seq.singleton (programDataPath @@ "chocolatey" @@ "bin")
pathDirectories
]
else
[
pathDirectories
]
[<System.Obsolete("Use Fake.Windows.Choco instead")>]
/// [omit]
/// Tries to find the specified choco executable:
///
/// 1. In the `<ProgramData>\chocolatey\bin` directory
/// 2. In the `PATH` environment variable.
let FindExe =
getPaths |> Seq.concat
|> Seq.map (fun directory -> directory @@ "choco.exe")
|> Seq.tryFind fileExists
[<System.Obsolete("Use Fake.Windows.Choco instead")>]
/// [omit]
/// Invokes chocolatey with the specified arguments
/// ## Parameters
/// - `exePath` - The location of choco executable. Automatically found if null or empty.
/// - `args` - The arguments given to the executable.
/// - `timeout` - The choco execution timeout
let private callChoco exePath args timeout =
// Try to find the choco executable if not specified by the user.
let chocoExe =
if not <| isNullOrEmpty exePath then exePath else
let found = FindExe
if found <> None then found.Value else failwith "Cannot find the choco executable."
use __ = traceStartTaskUsing "choco" args
let setInfo (info:ProcessStartInfo) =
info.FileName <- chocoExe
info.Arguments <- args
let result = ExecProcess (setInfo) timeout
if result <> 0 then failwithf "choco failed with exit code %i." result
let private getTempFolder =
let tempFolder = directoryInfo (Path.GetTempPath() @@ "FakeChocolateyPack")
if tempFolder.Exists
then tempFolder.Delete(true)
tempFolder.Create()
CreateDir (tempFolder.FullName @@ "tools")
tempFolder.FullName
let private appendLine line builder =
Printf.bprintf builder "%s%s" line Environment.NewLine
builder
let private appendFormattedLine format value builder =
appendLine (sprintf format value) builder
let private appendFormattedLineIfNotNull format value builder =
if isNull value then builder
else appendFormattedLine format value builder
let private getNuspecData parameters =
let getFrameworkGroup (frameworkTags : (string * string) seq) =
frameworkTags
|> Seq.map (fun (frameworkVersion, tags) ->
if isNullOrEmpty frameworkVersion then sprintf "<group>%s</group>" tags
else sprintf "<group targetFramework=\"%s\">%s</group>" frameworkVersion tags)
|> toLines
let getGroup items toTags =
if List.isEmpty items then null
else sprintf "<group>%s</group>" (items |> toTags)
let getReferencesTags references =
references
|> Seq.map (fun assembly -> sprintf "<reference file=\"%s\" />" assembly)
|> toLines
let references = getGroup parameters.References getReferencesTags
let referencesByFramework =
parameters.ReferencesByFramework
|> Seq.map (fun x -> (x.FrameworkVersion, getReferencesTags x.References))
|> getFrameworkGroup
let referencesXml = match (references + referencesByFramework) with | null -> null | "" -> null | r -> sprintf "<references>%s</references>" r
let getFrameworkAssemblyTags references =
references
|> Seq.map (fun x ->
if List.isEmpty x.FrameworkVersions then sprintf "<frameworkAssembly assemblyName=\"%s\" />" x.AssemblyName
else sprintf "<frameworkAssembly assemblyName=\"%s\" targetFramework=\"%s\" />" x.AssemblyName (x.FrameworkVersions |> separated ", "))
|> toLines
let frameworkAssembliesXml =
if List.isEmpty parameters.FrameworkAssemblies then null
else sprintf "<frameworkAssemblies>%s</frameworkAssemblies>" (parameters.FrameworkAssemblies |> getFrameworkAssemblyTags)
let getDependenciesTags dependencies =
dependencies
|> Seq.map (fun (package, version) -> sprintf "<dependency id=\"%s\" version=\"%s\" />" package version)
|> toLines
let dependencies = getGroup parameters.Dependencies getDependenciesTags
let dependenciesByFramework =
parameters.DependenciesByFramework
|> Seq.map (fun x -> (x.FrameworkVersion, getDependenciesTags x.Dependencies))
|> getFrameworkGroup
let dependenciesXml = match (dependencies + dependenciesByFramework) with | null -> null | "" -> null | d -> sprintf "<dependencies>%s</dependencies>" d
let filesTags =
parameters.Files
|> Seq.map (fun (source, target, exclude) ->
let excludeStr =
if exclude.IsSome then sprintf " exclude=\"%s\"" exclude.Value
else String.Empty
let targetStr =
if target.IsSome then sprintf " target=\"%s\"" target.Value
else String.Empty
sprintf "<file src=\"%s\"%s%s />" source targetStr excludeStr)
|> toLines
let filesXml = match filesTags.Length with | 0 -> null | _ -> sprintf "<files>%s</files>" filesTags
let xmlEncode (notEncodedText : string) =
if String.IsNullOrWhiteSpace notEncodedText then null
else XText(notEncodedText).ToString().Replace("ß","ß")
let toSingleLine (text:string) =
match text with
| null -> null
| _ -> text.Replace("\r", "").Replace("\n", "").Replace(" ", " ")
{ Version = parameters.Version |> xmlEncode
Title = parameters.Title |> xmlEncode
Authors = parameters.Authors |> separated ", " |> xmlEncode
Owners = parameters.Owners |> separated ", " |> xmlEncode
PackageId = parameters.PackageId |> xmlEncode
Summary = parameters.Summary |> toSingleLine |> xmlEncode
Description = parameters.Description |> toSingleLine |> xmlEncode
Tags = parameters.Tags |> separated " " |> xmlEncode
ReleaseNotes = parameters.ReleaseNotes |> xmlEncode
Copyright = parameters.Copyright |> xmlEncode
ProjectUrl = parameters.ProjectUrl |> xmlEncode
IconUrl = parameters.IconUrl |> xmlEncode
LicenseUrl = parameters.LicenseUrl |> xmlEncode
RequireLicenseAcceptance = parameters.RequireLicenseAcceptance.ToString().ToLower()
PackageSourceUrl = parameters.PackageSourceUrl |> xmlEncode
DocsUrl = parameters.DocsUrl |> xmlEncode
MailingListUrl = parameters.MailingListUrl |> xmlEncode
BugTrackerUrl = parameters.BugTrackerUrl |> xmlEncode
ProjectSourceUrl = parameters.ProjectSourceUrl |> xmlEncode
DevelopmentDependency = match parameters.DevelopmentDependency with | false -> null | true -> "true"
DependenciesXml = dependenciesXml
ReferencesXml = referencesXml
FrameworkAssembliesXml = frameworkAssembliesXml
FilesXml = filesXml }
/// [omit]
/// Create nuspec from template
let private createNuSpecFromTemplate (parameters:ChocoPackParams) (templateNuSpec:FileInfo) outputDir =
let specFile = outputDir @@ (templateNuSpec.Name.Replace("nuspec", "") + parameters.Version + ".nuspec")
|> FullName
tracefn "Creating .nuspec file at %s" specFile
templateNuSpec.CopyTo(specFile, true) |> ignore
let nuspecData = getNuspecData parameters
let replacements =
[ "@build.number@", nuspecData.Version
"@title@", nuspecData.Title
"@authors@", nuspecData.Authors
"@owners@", nuspecData.Owners
"@project@", nuspecData.PackageId
"@summary@", nuspecData.Summary
"@description@", nuspecData.Description
"@tags@", nuspecData.Tags
"@releaseNotes@", nuspecData.ReleaseNotes
"@copyright@", nuspecData.Copyright
"@projectUrl@", nuspecData.ProjectUrl
"@iconUrl@", nuspecData.IconUrl
"@licenseUrl@", nuspecData.LicenseUrl
"@requireLicenseAcceptance@", nuspecData.RequireLicenseAcceptance
"@packageSourceUrl@", parameters.PackageSourceUrl
"@docsUrl@", nuspecData.DocsUrl
"@mailingListUrl@", nuspecData.MailingListUrl
"@bugTrackerUrl@", nuspecData.BugTrackerUrl
"@projectSourceUrl@", nuspecData.ProjectSourceUrl
"@developmentDependency@", nuspecData.DevelopmentDependency
"@dependencies@", nuspecData.DependenciesXml
"@references@", nuspecData.ReferencesXml
"@frameworkAssemblies@", nuspecData.FrameworkAssembliesXml
"@files@", nuspecData.FilesXml ]
processTemplates replacements [ specFile ]
tracefn "Created nuspec file %s" specFile
specFile
/// [omit]
/// Create nuspec from data
let private createNuSpec (parameters:ChocoPackParams) outputDir =
let specFile = outputDir @@ parameters.PackageId + "." + parameters.Version + ".nuspec"
|> FullName
tracefn "Creating .nuspec file at %s" specFile
let nuspecData = getNuspecData parameters
let nuspecContent = new StringBuilder()
|> appendLine "<?xml version=\"1.0\"?>"
|> appendLine "<package xmlns=\"http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd\">"
|> appendLine " <metadata>"
|> appendFormattedLine " <id>%s</id>" nuspecData.PackageId
|> appendFormattedLine " <version>%s</version>" nuspecData.Version
|> appendFormattedLineIfNotNull " <title>%s</title>" nuspecData.Title
|> appendFormattedLine " <authors>%s</authors>" nuspecData.Authors
|> appendFormattedLineIfNotNull " <owners>%s</owners>" nuspecData.Owners
|> appendFormattedLine " <description>%s</description>" nuspecData.Description
|> appendFormattedLineIfNotNull " <releaseNotes>%s</releaseNotes>" nuspecData.ReleaseNotes
|> appendFormattedLineIfNotNull " <summary>%s</summary>" nuspecData.Summary
|> appendFormattedLineIfNotNull " <projectUrl>%s</projectUrl>" nuspecData.ProjectUrl
|> appendFormattedLineIfNotNull " <iconUrl>%s</iconUrl>" nuspecData.IconUrl
|> appendFormattedLineIfNotNull " <licenseUrl>%s</licenseUrl>" nuspecData.LicenseUrl
|> appendFormattedLineIfNotNull " <copyright>%s</copyright>" nuspecData.Copyright
|> appendFormattedLineIfNotNull " <requireLicenseAcceptance>%s</requireLicenseAcceptance>" nuspecData.RequireLicenseAcceptance
|> appendFormattedLineIfNotNull " <tags>%s</tags>" nuspecData.Tags
|> appendFormattedLineIfNotNull " <packageSourceUrl>%s</packageSourceUrl>" nuspecData.PackageSourceUrl
|> appendFormattedLineIfNotNull " <docsUrl>%s</docsUrl>" nuspecData.DocsUrl
|> appendFormattedLineIfNotNull " <mailingListUrl>%s</mailingListUrl>" nuspecData.MailingListUrl
|> appendFormattedLineIfNotNull " <bugTrackerUrl>%s</bugTrackerUrl>" nuspecData.BugTrackerUrl
|> appendFormattedLineIfNotNull " <projectSourceUrl>%s</projectSourceUrl>" nuspecData.ProjectSourceUrl
|> appendFormattedLineIfNotNull " <developmentDependency>%s</developmentDependency>" nuspecData.DevelopmentDependency
|> appendFormattedLineIfNotNull " %s" nuspecData.DependenciesXml
|> appendFormattedLineIfNotNull " %s" nuspecData.ReferencesXml
|> appendFormattedLineIfNotNull " %s" nuspecData.FrameworkAssembliesXml
|> appendLine " </metadata>"
|> appendFormattedLineIfNotNull " %s" nuspecData.FilesXml
|> appendWithoutQuotes "</package>"
|> toText
WriteStringToFile false specFile nuspecContent
tracefn "Created nuspec file %s" specFile
specFile
let private installerTypeToString x =
match Microsoft.FSharp.Reflection.FSharpValue.GetUnionFields(x, typeof<ChocolateyInstallerType>) with
| case, _ -> case.Name.ToLower()
let private checksumTypeToString x =
match Microsoft.FSharp.Reflection.FSharpValue.GetUnionFields(x, typeof<ChocolateyChecksumType>) with
| case, _ -> case.Name.ToLower()
let private createChocolateyInstallPs1 (parameters:ChocoPackParams) outputDir =
let outputPath = outputDir @@ "tools" @@ "chocolateyInstall.ps1" |> FullName
tracefn "Create chocolateyInstall.ps1 at %s" outputPath
if isNullOrWhiteSpace parameters.Title || isNullOrWhiteSpace parameters.PackageDownloadUrl
then failwith "chocolateyInstall.ps1 need at least Title and PackageDownloadUrl to be created."
let installContent = new StringBuilder()
|> appendFormattedLine "$packageName = '%s'" parameters.Title
|> match parameters.InstallerType with ChocolateyInstallerType.Zip -> id | _ -> appendFormattedLine "$installerType = '%s'" (installerTypeToString parameters.InstallerType)
|> appendFormattedLine "$url = '%s'" parameters.PackageDownloadUrl
|> appendFormattedLineIfNotNull "$url64 = '%s'" parameters.PackageDownload64Url
|> match parameters.InstallerType with ChocolateyInstallerType.Zip -> id | _ -> appendFormattedLine "$silentArgs = '%s'" parameters.SilentArgs
|> match parameters.InstallerType with ChocolateyInstallerType.Zip -> appendFormattedLine "$unzipLocation = \"%s\"" parameters.UnzipLocation | _ -> id
|> appendLine String.Empty
|> match parameters.InstallerType with
| ChocolateyInstallerType.Zip -> appendWithoutQuotes "Install-ChocolateyZipPackage $packageName $url $unzipLocation"
| _ -> appendWithoutQuotes "Install-ChocolateyPackage $packageName $installerType $silentArgs $url"
|> appendIfTrue (isNotNullOrEmpty parameters.PackageDownload64Url) " $url64"
|> appendIfTrueWithoutQuotes (isNotNullOrEmpty parameters.Checksum) ("-Checksum " + parameters.Checksum)
|> appendIfTrueWithoutQuotes (isNotNullOrEmpty parameters.Checksum) ("-ChecksumType " + checksumTypeToString parameters.ChecksumType)
|> appendIfTrueWithoutQuotes (isNotNullOrEmpty parameters.Checksum64) ("-Checksum64 " + parameters.Checksum64)
|> appendIfTrueWithoutQuotes (isNotNullOrEmpty parameters.Checksum64) ("-Checksum64Type " + checksumTypeToString parameters.Checksum64Type)
|> toText
WriteStringToFile false outputPath installContent
tracefn "Created chocolateyInstall.ps1 at %s" outputPath
let private createChocolateyInstallPs1FromTemplate (parameters:ChocoPackParams) templatePath outputDir =
let outputPath = outputDir @@ "tools" @@ "chocolateyInstall.ps1" |> FullName
tracefn "Create chocolateyInstall.ps1 at %s from template %s" outputPath templatePath
templatePath |> CopyFile outputPath
let replacements =
[ "@packageName@", parameters.Title
"@url@", parameters.PackageDownloadUrl
"@url64@", parameters.PackageDownload64Url
"@silentArgs@", parameters.SilentArgs
"@unzipLocation@", parameters.UnzipLocation
"@installerType@", (installerTypeToString parameters.InstallerType)
"@checksum@", parameters.Checksum
"@checksumType@", (checksumTypeToString parameters.ChecksumType)
"@checksum64@", parameters.Checksum64
"@checksum64Type@", (checksumTypeToString parameters.Checksum64Type)
]
processTemplates replacements [ outputPath ]
tracefn "Created chocolateyInstall.ps1 at %sfrom template %s" outputPath templatePath
let private createChocolateyUninstallPs1 (parameters: ChocoPackParams) outputDir =
if not (isNullOrWhiteSpace parameters.Title) && not (isNullOrWhiteSpace parameters.UninstallPath)
then
let outputPath = outputDir @@ "tools" @@ "chocolateyUninstall.ps1" |> FullName
tracefn "Create chocolateyUninstall.ps1 at %s" outputPath
let uninstallContent = new StringBuilder()
|> appendFormattedLine "$packageName = '%s'" parameters.Title
|> match parameters.InstallerType with ChocolateyInstallerType.Zip -> id | _ -> appendFormattedLine "$installerType = '%s'" (installerTypeToString parameters.InstallerType)
|> appendFormattedLine "$file = \"%s\"" parameters.UninstallPath
|> match parameters.InstallerType with ChocolateyInstallerType.Zip -> id | _ -> appendFormattedLine "$silentArgs = '%s'" parameters.SilentArgs
|> appendLine String.Empty
|> match parameters.InstallerType with
| ChocolateyInstallerType.Zip -> appendWithoutQuotes "Uninstall-ChocolateyZipPackage $packageName $file"
| _ -> appendWithoutQuotes "Uninstall-ChocolateyPackage $packageName $installerType $silentArgs $file"
|> toText
WriteStringToFile false outputPath uninstallContent
tracefn "Created chocolateyUninstall.ps1 at %s" outputPath
let private createChocolateyUninstallPs1FromTemplate (parameters:ChocoPackParams) templatePath outputDir =
let outputPath = outputDir @@ "tools" @@ "chocolateyUninstall.ps1" |> FullName
tracefn "Create chocolateyUninstall.ps1 at %s from template %s" outputPath templatePath
templatePath |> CopyFile outputPath
let replacements =
[ "@packageName@", parameters.Title
"@silentArgs@", parameters.SilentArgs
"@installerType@", (installerTypeToString parameters.InstallerType)
"@uninstallPath@", parameters.UninstallPath
]
processTemplates replacements [ outputPath ]
tracefn "Created chocolateyUninstall.ps1 at %sfrom template %s" outputPath templatePath
let private callChocoPack nuspecFile (parameters: ChocoPackParams) =
let args = new StringBuilder()
|> appendWithoutQuotes "pack"
|> append nuspecFile
|> appendWithoutQuotesIfNotNull parameters.Version "--version "
|> appendIfTrueWithoutQuotes parameters.NonInteractive "-y"
|> appendWithoutQuotesIfNotNull parameters.AdditionalArgs parameters.AdditionalArgs
|> toText
callChoco parameters.ToolPath args parameters.Timeout
/// True if choco is available (only on windows)
/// ## Sample usage
/// "Build" =?> ("ChocoInstall", Choco.IsAvailable)
[<System.Obsolete("Use Fake.Windows.Choco instead")>]
let IsAvailable = not isUnix && FindExe <> None
/// Call choco to [install](https://github.com/chocolatey/choco/wiki/CommandsInstall) a package
/// ## Parameters
/// - `setParams` - Function used to manipulate the default choco parameters. See `ChocoInstallParams`
/// - `packages` - Names of packages, path to packages.config, .nuspec or .nupkg to install
/// ## Sample usage
///
/// Target "ChocoInstall" (fun _ ->
/// "pretzel" |> Choco.Install (fun p -> { p with Version = "0.4.0" })
/// )
[<System.Obsolete("Use Fake.Windows.Choco instead")>]
let Install (setParams: (ChocoInstallParams -> ChocoInstallParams)) (packages: string) =
if packages |> isNullOrEmpty then failwith "'packages' must not be empty."
let parameters = setParams ChocoInstallDefaults
let args = new StringBuilder()
|> appendWithoutQuotes "install"
|> append packages
|> appendWithoutQuotesIfNotNull parameters.Version "--version "
|> appendIfTrueWithoutQuotes parameters.Prerelease "--pre"
|> appendWithoutQuotesIfNotNull parameters.PackageParameters "--params "
|> appendWithoutQuotesIfNotNull parameters.Source "--source "
|> appendIfTrueWithoutQuotes parameters.ForceX86 "--forcex86"
|> appendWithoutQuotesIfNotNull parameters.InstallArgs "--installargs "
|> appendIfTrueWithoutQuotes parameters.OverrideArgs "--overrideargs"
|> appendIfTrueWithoutQuotes parameters.SkipPowershell "--skippowershell"
|> appendWithoutQuotesIfNotNull parameters.User "--user "
|> appendWithoutQuotesIfNotNull parameters.Password "--password "
|> appendIfTrueWithoutQuotes parameters.NonInteractive "-y"
|> appendIfNotNullOrEmpty parameters.AdditionalArgs ""
|> toText
callChoco parameters.ToolPath args parameters.Timeout
/// Call choco to [pack](https://github.com/chocolatey/choco/wiki/CommandsPack) a package and create .nuspec, chocolateyInstall.ps1 and chocolateyUninstall.ps1 if informations are specified
/// ## Parameters
/// - `setParams` - Function used to manipulate the default choco parameters. See `ChocoPackParams`
/// ## Sample usage
///
/// Target "ChocoPack" (fun _ ->
/// Choco.Pack (fun p -> { p with Version = "0.5.0"; ... })
/// )
[<System.Obsolete("Use Fake.Windows.Choco instead")>]
let Pack setParams =
let parameters = setParams ChocoPackDefaults
if Directory.Exists parameters.OutputDir |> not then
failwithf "OutputDir %s does not exist." parameters.OutputDir
let tempFolder = getTempFolder
let nuspecFile = createNuSpec parameters tempFolder
if parameters.InstallerType <> ChocolateyInstallerType.SelfContained then
createChocolateyInstallPs1 parameters tempFolder
createChocolateyUninstallPs1 parameters tempFolder
callChocoPack nuspecFile parameters
parameters.PackageId + "." + parameters.Version + ".nupkg" |> FileHelper.MoveFile parameters.OutputDir
/// Call choco to [pack](https://github.com/chocolatey/choco/wiki/CommandsPack) a package
/// ## Parameters
/// - `setParams` - Function used to manipulate the default choco parameters. See `ChocoPackParams`
/// - `nuspecPath` - path to the .nuspec to pack
/// ## Sample usage
///
/// Target "ChocoPack" (fun _ ->
/// "pretzel.nuspec" |> Choco.Pack (fun p -> { p with Version = "0.5.0" })
/// )
[<System.Obsolete("Use Fake.Windows.Choco instead")>]
let PackFromTemplate setParams nuspecPath =
if nuspecPath |> isNullOrEmpty then failwith "'nuspecPath' must not be empty."
let parameters = setParams ChocoPackDefaults
if Directory.Exists parameters.OutputDir |> not then
failwithf "OutputDir %s does not exist." parameters.OutputDir
let tempFolder = getTempFolder
let nuspecFile = createNuSpecFromTemplate parameters (fileInfo nuspecPath) tempFolder
let rootFolder = (Directory.GetParent nuspecPath).FullName
let chocoInstallPath = rootFolder @@ "tools" @@ "chocolateyInstall.ps1"
if fileExists chocoInstallPath
then createChocolateyInstallPs1FromTemplate parameters chocoInstallPath tempFolder
elif parameters.InstallerType <> ChocolateyInstallerType.SelfContained
then createChocolateyInstallPs1 parameters tempFolder
let chocoUninstallPath = rootFolder @@ "tools" @@ "chocolateyUninstall.ps1"
if fileExists chocoUninstallPath
then createChocolateyUninstallPs1FromTemplate parameters chocoUninstallPath tempFolder
elif parameters.InstallerType <> ChocolateyInstallerType.SelfContained
then createChocolateyUninstallPs1 parameters tempFolder
callChocoPack nuspecFile parameters
parameters.PackageId + "." + parameters.Version + ".nupkg" |> FileHelper.MoveFile parameters.OutputDir
/// Call choco to [push](https://github.com/chocolatey/choco/wiki/CommandsPush) a package
/// ## Parameters
/// - `setParams` - Function used to manipulate the default choco parameters. See `ChocoPushParams`
/// - `nupkgPath` - path to the .nupkg to push
/// ## Sample usage
///
/// Target "ChocoPush" (fun _ ->
/// "pretzel.0.5.0.nupkg" |> Choco.Push (fun p -> { p with ApiKey = "123-123123-123" })
/// )
[<System.Obsolete("Use Fake.Windows.Choco instead")>]
let Push setParams nupkgPath =
if nupkgPath |> isNullOrEmpty then failwith "'nupkgPath' must not be empty."
let parameters = setParams ChocoPushDefaults
let args = new StringBuilder()
|> appendWithoutQuotes "push"
|> append nupkgPath
|> appendWithoutQuotesIfNotNull parameters.Source "--source "
|> appendWithoutQuotesIfNotNull parameters.ApiKey "--apikey "
|> appendIfTrueWithoutQuotes parameters.NonInteractive "-y"
|> appendWithoutQuotesIfNotNull parameters.AdditionalArgs parameters.AdditionalArgs
|> toText
callChoco parameters.ToolPath args parameters.Timeout