-
Notifications
You must be signed in to change notification settings - Fork 585
/
NugetVersion.fs
116 lines (107 loc) · 4.25 KB
/
NugetVersion.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
[<System.Obsolete("Use Fake.DotNet.NuGet.Version instead")>]
module Fake.NuGetVersion
#nowarn "44"
open System
open System.Net
open Newtonsoft.Json
open Fake.SemVerHelper
open System.Xml
open System.Xml.Linq
[<System.Obsolete("Use Fake.DotNet.NuGet.Version instead")>]
type NuGetSearchItemResult =
{ Id:string
Version:string
Published:DateTime }
[<System.Obsolete("Use Fake.DotNet.NuGet.Version instead")>]
type NuGetSearchResult =
{ results:NuGetSearchItemResult list }
[<System.Obsolete("Use Fake.DotNet.NuGet.Version instead")>]
type NuGetSearchResponse =
{ d:NuGetSearchResult }
[<System.Obsolete("Use Fake.DotNet.NuGet.Version instead")>]
type NuGetVersionIncrement = SemVerInfo -> SemVerInfo
/// Increment patch version
[<System.Obsolete("Use Fake.DotNet.NuGet.Version instead")>]
let IncPatch:NuGetVersionIncrement =
fun (v:SemVerInfo) ->
{ v with Build=""; Patch=(v.Patch+1) }
/// Increment minor version
[<System.Obsolete("Use Fake.DotNet.NuGet.Version instead")>]
let IncMinor:NuGetVersionIncrement =
fun (v:SemVerInfo) ->
{ v with Build=""; Patch=0; Minor=(v.Minor+1) }
/// Increment major version
[<System.Obsolete("Use Fake.DotNet.NuGet.Version instead")>]
let IncMajor:NuGetVersionIncrement =
fun (v:SemVerInfo) ->
{ v with Build=""; Patch=0; Minor=0; Major=(v.Major+1) }
/// Arguments for the next NuGet version number computing
[<System.Obsolete("Use Fake.DotNet.NuGet.Version instead")>]
type NuGetVersionArg =
{ Server:string
PackageName:string
Increment:NuGetVersionIncrement
DefaultVersion:string }
/// Default arguments to compute next NuGet version number
static member Default() =
{ Server="https://www.nuget.org/api/v2"
PackageName=""
Increment=IncMinor
DefaultVersion="1.0" }
/// Retrieve current NuGet version number
[<System.Obsolete("Use Fake.DotNet.NuGet.Version instead")>]
let getLastNuGetVersion server (packageName:string) =
let escape = Uri.EscapeDataString
let url =
sprintf "%s/Search()?$filter=IsLatestVersion&searchTerm='%s'&includePrerelease=false"
server packageName
let client = new WebClient()
client.Headers.Add("Accept", "application/json, application/xml")
let text = client.DownloadString url
let hasContentType = client.ResponseHeaders.AllKeys |> Seq.contains "Content-Type"
let version =
if hasContentType && client.ResponseHeaders.Item("Content-Type").Contains "application/json"
then
let json = JsonConvert.DeserializeObject<NuGetSearchResponse>(text)
json.d.results
|> Seq.filter (fun i -> i.Id = packageName)
|> Seq.sortByDescending (fun i -> i.Published)
|> Seq.tryHead
|> fun i ->
match i with
| Some v -> Some (SemVerHelper.parse v.Version)
| None -> None
else
let xml = XDocument.Parse text
let xmlns = "http://www.w3.org/2005/Atom"
let xmlnsd="http://schemas.microsoft.com/ado/2007/08/dataservices"
let xmlnsm="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"
xml.Descendants(XName.Get("entry", xmlns))
|> Seq.filter (
fun entry ->
entry.Elements(XName.Get("title", xmlns))
|> Seq.exists (
fun t ->
t.Attribute(XName.Get "type").Value = "text"
&& t.Value = packageName
)
)
|> Seq.tryHead
|> function
| Some e ->
e.Descendants(XName.Get ("properties", xmlnsm))
|> fun props ->
props.Elements(XName.Get ("Version", xmlnsd))
|> Seq.tryHead
|> function
| Some n -> Some (SemVerHelper.parse n.Value)
| None -> None
| None -> None
version
/// Compute next NuGet version number
[<System.Obsolete("Use Fake.DotNet.NuGet.Version instead")>]
let nextVersion (f : NuGetVersionArg -> NuGetVersionArg) =
let arg = f (NuGetVersionArg.Default())
match getLastNuGetVersion arg.Server arg.PackageName with
| Some v -> (arg.Increment v).ToString()
| None -> arg.DefaultVersion