Skip to content

Instantly share code, notes, and snippets.

@rflechner
Created January 8, 2016 15:41
Show Gist options
  • Save rflechner/d75f5424f0bb2d901250 to your computer and use it in GitHub Desktop.
Save rflechner/d75f5424f0bb2d901250 to your computer and use it in GitHub Desktop.
#r "packages/Newtonsoft.Json.7.0.1/lib/net45/Newtonsoft.Json.dll"
open System
open System.Net
open Newtonsoft.Json
type NugetSearchItemResult =
{ Version:string
Published:DateTime }
type NugetSearchResult =
{ results:NugetSearchItemResult list }
type NugetSearchResponse =
{ d:NugetSearchResult }
type NugetVersionIncrement = string -> Version
let private positive i = Math.Max(0, i)
/// Increment build number of a version
let IncBuild:NugetVersionIncrement =
fun (version:string) ->
let v = Version version
sprintf "%d.%d.%d" (positive v.Major) (positive v.Minor) (positive v.Build+1)
|> Version
/// Increment minor version
let IncMinor:NugetVersionIncrement =
fun (version:string) ->
let v = Version version
let n = sprintf "%d.%d.0" (positive v.Major) (positive v.Minor+1)
Version n
/// Increment major version
let IncMajor:NugetVersionIncrement =
fun (version:string) ->
let v = Version version
sprintf "%d.0.0" (positive v.Major+1)
|> Version
/// Arguments for the next nuget version number computing
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=IncBuild
DefaultVersion="1.0" }
/// Retrieve current nuget version number
let getlastNugetVersion server (packageName:string) =
let escape = Uri.EscapeDataString
let url =
sprintf "%s/Packages()?$filter=%s%s%s&$orderby=%s"
server
(escape "Id eq '")
packageName
(escape "'")
(escape "IsLatestVersion desc")
let client = new WebClient()
client.Headers.Add("Accept", "application/json")
let text = client.DownloadString url
let json = JsonConvert.DeserializeObject<NugetSearchResponse>(text)
json.d.results
|> Seq.sortByDescending (fun i -> i.Published)
|> Seq.tryHead
|> fun i -> match i with | Some v -> Some v.Version | None -> None
/// Compute next nuget version number
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
@rflechner
Copy link
Author

There is a small module I use to auto increment my nugets packages versions.

For example, we can get current published version of FAKE nuget with:

getlastNugetVersion (NugetVersionArg.Default().Server) "FAKE"
//val it : string option = Some "4.12.0"

To auto increment a nuget, we can do this:

NuGet (fun p -> 
            {p with
                 // Other settings truncated ....
                Project = "MyProject"
                Title = "MyProject"
                Version = nextVersion (fun arg -> { arg with PackageName="MyProject" })
             })
            "MyProject.nuspec"

We can choose increment method with:

nextVersion (fun arg -> { arg with PackageName="FAKE"; Increment=IncBuild })
//val it : string = "4.12.1"

nextVersion (fun arg -> { arg with PackageName="FAKE"; Increment=IncMinor })
//val it : string = "4.13.0"

nextVersion (fun arg -> { arg with PackageName="FAKE"; Increment=IncMajor })
//val it : string = "5.12.0"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment