-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.fsx
118 lines (98 loc) · 4.16 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
#r @"packages\FAKE\tools\FakeLib.dll"
open System
open System.IO
open System.Text
open Fake
open Fake.FileUtils
let chocolateyKeyVar = "CHOCO_KEY"
let chocolateyFeed = "https://chocolatey.org/api/v2/"
open System.Diagnostics
let private callChoco args timeout =
// Try to find the choco executable if not specified by the user.
let chocoExe =
let found = Choco.FindExe
if found <> None then found.Value else failwith "Cannot find the choco executable."
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 callChocoPack (setParams: (Choco.ChocoPackParams -> Choco.ChocoPackParams)) =
let parameters = setParams Choco.ChocoPackDefaults
let args = StringBuilder()
|> appendWithoutQuotes "pack"
|> appendWithoutQuotesIfNotNull parameters.AdditionalArgs parameters.AdditionalArgs
|> toText
callChoco args parameters.Timeout
Target "BuildChocoPackages" (fun _ ->
trace "Building nupkg from nuspec"
!! "**/*.nuspec"
|> Seq.iter (fun nuspec ->
let directory = Path.GetDirectoryName nuspec
let nuspecName = Path.GetFileName nuspec
trace (sprintf "building %s in %s" nuspecName directory)
pushd <| Path.GetDirectoryName nuspec
callChocoPack (fun p -> { p with OutputDir = directory })
popd()
)
)
let getPackageVersionAndStatus (repoUrl:string) packageName version =
let webClient = new System.Net.WebClient();
let url : string = repoUrl.TrimEnd('/') + "/Packages(Id='" + packageName + "',Version='" + version + "')"
let resp = webClient.DownloadString(url)
let doc = XMLDoc resp
let entry = doc.["entry"]
let properties = entry.["m:properties"]
let property name =
let p = properties.["d:" + name]
if p = null || p.IsEmpty then "" else p.InnerText
let boolProperty name = (property name).ToLower() = "true"
(property "Version", boolProperty "IsApproved")
let existingPackage packageId version =
try
Some (getPackageVersionAndStatus chocolateyFeed packageId version)
with
:? Net.WebException as exc -> if exc.Message.Contains("404") then None else failwith "Error"
| _ -> failwith "Error"
let latestVersion packageId =
try
Some (packageId |> getLatestPackage chocolateyFeed)
with
:? ArgumentException -> None
| _ -> failwith "Error"
let shouldPushNewPackage pkg =
let metaInfo = GetMetaDataFromPackageFile pkg
let version = metaInfo.Version
let packageId = metaInfo.Id
trace (sprintf "Verify package %s %s" packageId version)
match existingPackage packageId version with
None -> trace "Such version does not exit"
let lastPackage = packageId |> latestVersion
match lastPackage with
Some pkg -> let repoVersion = Version(pkg.Version)
let localVersion = Version(version)
if localVersion > repoVersion then
true // push new version
else
trace "Higher version already exists"
false
| None -> true // push new package
| Some (_, approved) ->
trace "This version already exists"
if not approved then
trace "But it is not approved"
not approved
Target "PublishArtifacts" (fun _ ->
!! "**/*.nupkg"
-- "/packages/**"
|> Seq.filter shouldPushNewPackage
|> Seq.iter (fun pkg ->
trace (sprintf "Pushing package %s" pkg)
ProcessHelper.enableProcessTracing <- false
pkg |> Choco.Push (fun p -> { p with ApiKey = environVar chocolateyKeyVar })
ProcessHelper.enableProcessTracing <- true
)
)
"BuildChocoPackages"
==> "PublishArtifacts"
RunTargetOrDefault "PublishArtifacts"