Skip to content

Commit

Permalink
Accept and normalize versions like 6.0.1302.0-Preview - fixes #364
Browse files Browse the repository at this point in the history
  • Loading branch information
forki committed Nov 17, 2014
1 parent 5b3348c commit fbc18c5
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 45 deletions.
3 changes: 3 additions & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#### 0.14.3 - 17.11.2014
* BUGFIX: Accept and normalize versions like 6.0.1302.0-Preview - https://github.com/fsprojects/Paket/issues/364

#### 0.14.2 - 16.11.2014
* BUGFIX: handling of package dependencies containing string "nuget" - https://github.com/fsprojects/Paket/pull/363

Expand Down
73 changes: 31 additions & 42 deletions src/Paket.Core/SemVer.fs
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,27 @@ type SemVerInfo =
PreRelease : PreRelease option
/// The optional build no.
Build : string
/// The optional prerelease build no.
PreReleaseBuild : string
// The original version text
Original : string option }

member x.Normalize() =
let build =
if String.IsNullOrEmpty x.Build |> not then "." + x.Build
if String.IsNullOrEmpty x.Build |> not && x.Build <> "0" then "." + x.Build
else ""

let preReleaseBuild =
if String.IsNullOrEmpty x.PreReleaseBuild |> not && x.PreReleaseBuild <> "0" then "." + x.PreReleaseBuild
else ""

let pre =
match x.PreRelease, (String.IsNullOrEmpty x.Build |> not && x.Build <> "0") with
| Some preRelease, _ -> "-" + preRelease.Name + build
| None, true -> build
match x.PreRelease, (String.IsNullOrEmpty x.PreReleaseBuild |> not) with
| Some preRelease, _ -> "-" + preRelease.Name + preReleaseBuild
| None, true -> preReleaseBuild
| _ -> ""

sprintf "%d.%d.%d" x.Major x.Minor x.Patch + pre
sprintf "%d.%d.%d" x.Major x.Minor x.Patch + build + pre

member x.AsString = x.ToString()

Expand All @@ -76,8 +82,7 @@ type SemVerInfo =
override x.Equals(yobj) =
match yobj with
| :? SemVerInfo as y ->
x.Major = y.Major && x.Minor = y.Minor && x.Patch = y.Patch && x.PreRelease = y.PreRelease
&& (x.Build = y.Build || (x.Build = "0" && y.Build = "") || (y.Build = "0" && x.Build = ""))
x.Major = y.Major && x.Minor = y.Minor && x.Patch = y.Patch && x.PreRelease = y.PreRelease && x.Build = y.Build && x.PreReleaseBuild = y.PreReleaseBuild
| _ -> false

override x.GetHashCode() = hash (x.Minor, x.Minor, x.Patch, x.PreRelease, x.Build)
Expand All @@ -88,14 +93,18 @@ type SemVerInfo =
if x.Major <> y.Major then compare x.Major y.Major
else if x.Minor <> y.Minor then compare x.Minor y.Minor
else if x.Patch <> y.Patch then compare x.Patch y.Patch
else if x.PreRelease = y.PreRelease && x.Build = y.Build then 0
else if x.PreRelease.IsNone && not y.PreRelease.IsNone && x.Build = "" then 1
else if y.PreRelease.IsNone && not x.PreRelease.IsNone && y.Build = "" then -1
else if x.PreRelease <> y.PreRelease then compare x.PreRelease y.PreRelease
else if x.Build <> y.Build then
match Int32.TryParse x.Build, Int32.TryParse y.Build with
| (true, b1), (true, b2) -> compare b1 b2
| _ -> compare x.Build y.Build
else if x.PreRelease = y.PreRelease && x.PreReleaseBuild = y.PreReleaseBuild then 0
else if x.PreRelease.IsNone && not y.PreRelease.IsNone && x.PreReleaseBuild = "0" then 1
else if y.PreRelease.IsNone && not x.PreRelease.IsNone && y.PreReleaseBuild = "0" then -1
else if x.PreRelease <> y.PreRelease then compare x.PreRelease y.PreRelease
else if x.PreReleaseBuild <> y.PreReleaseBuild then
match Int32.TryParse x.PreReleaseBuild, Int32.TryParse y.PreReleaseBuild with
| (true, b1), (true, b2) -> compare b1 b2
| _ -> compare x.PreReleaseBuild y.PreReleaseBuild
else 0
| _ -> invalidArg "yobj" "cannot compare values of different types"

Expand All @@ -111,36 +120,16 @@ module SemVer =
/// parse "1.2.3-alpha002" > parse "1.2.3-alpha1" // true
/// parse "1.5.0-beta.2" > parse "1.5.0-rc.1" // false
let Parse(version : string) =
let splitted = version.Split '.'
let dashSplitted = version.Split '-'
let splitted = dashSplitted.[0].Split '.'
let l = splitted.Length

let patch, preRelease =
match l with
| 0 -> 0, ""
| 1 ->
let splitted' = splitted.[0].Split '-'
0,
if splitted'.Length > 1 then splitted'.[1]
else ""
| 2 ->
let splitted' = splitted.[1].Split '-'
0,
if splitted'.Length > 1 then splitted'.[1]
else ""
| _ ->
let splitted' = splitted.[2].Split '-'
Int32.Parse splitted'.[0],
if splitted'.Length > 1 then splitted'.[1]
else ""
{ Major =
if l > 0 then Int32.Parse (splitted.[0].Split('-').[0])
else 0
Minor =
if l > 1 then Int32.Parse (splitted.[1].Split('-').[0])
else 0
Patch = patch
PreRelease = PreRelease.TryParse preRelease
Build =
if l > 3 then splitted.[3]
else String.Empty

let prereleaseBuild = if dashSplitted.Length > 1 && dashSplitted.[1].Split('.').Length > 1 then dashSplitted.[1].Split('.').[1] else "0"

{ Major = if l > 0 then Int32.Parse(splitted.[0]) else 0
Minor = if l > 1 then Int32.Parse(splitted.[1]) else 0
Patch = if l > 2 then Int32.Parse(splitted.[2]) else 0
PreRelease = PreRelease.TryParse(if dashSplitted.Length > 1 then dashSplitted.[1].Split('.').[0] else String.Empty)
Build = if l > 3 then splitted.[3] else "0"
PreReleaseBuild = prereleaseBuild
Original = Some version }
2 changes: 1 addition & 1 deletion src/Paket/Paket.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<StartProgram>paket.exe</StartProgram>
<StartWorkingDirectory>
</StartWorkingDirectory>
<StartWorkingDirectory>D:\code\paket</StartWorkingDirectory>
<StartWorkingDirectory>D:\code\PaketKopie</StartWorkingDirectory>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
Expand Down
20 changes: 18 additions & 2 deletions tests/Paket.Tests/SemVerSpecs.fs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ let ``can parse semver strings and print the result``() =
(SemVer.Parse "1.0.0-alpha.beta").ToString() |> shouldEqual "1.0.0-alpha.beta"
(SemVer.Parse "1.0.0-rc.1").ToString() |> shouldEqual "1.0.0-rc.1"
(SemVer.Parse "1.2.3-foo").ToString() |> shouldEqual "1.2.3-foo"
(SemVer.Parse "6.0.1302.0-Preview").PreRelease |> shouldEqual (PreRelease.TryParse "Preview")
(SemVer.Parse "1.2.3").ToString() |> shouldEqual "1.2.3"
(SemVer.Parse "1.2.3.0").ToString() |> shouldEqual "1.2.3.0"
(SemVer.Parse "1.2.3.0").Patch |> shouldEqual 3
(SemVer.Parse "1.2.3").Patch |> shouldEqual 3
(SemVer.Parse "1.2.3.0").Build |> shouldEqual "0"
(SemVer.Parse "1.2.3").Build |> shouldEqual "0"
(SemVer.Parse "3.1.1.1").Build |> shouldEqual "1"
(SemVer.Parse "1.0.0-rc.3").PreReleaseBuild |> shouldEqual "3"
(SemVer.Parse "1.0.0-rc.1").PreReleaseBuild |> shouldEqual "1"

[<Test>]
let ``can parse semver strings``() =
Expand All @@ -24,7 +34,7 @@ let ``can parse semver strings``() =
semVer.PreRelease |> shouldEqual (Some { Origin = "alpha"
Name = "alpha"
Number = None })
semVer.Build |> shouldEqual "beta"
semVer.PreReleaseBuild |> shouldEqual "beta"

[<Test>]
let ``can compare semvers``() =
Expand Down Expand Up @@ -69,6 +79,7 @@ let ``can parse FSharp.Data versions``() =
let ``can normalize versions``() =
(SemVer.Parse "2.3") |> shouldEqual (SemVer.Parse "2.3.0")
(SemVer.Parse "2.3").Normalize() |> shouldEqual ((SemVer.Parse "2.3.0").ToString())
(SemVer.Parse "3.1.1.1").Normalize() |> shouldEqual "3.1.1.1"
(SemVer.Parse "3.1.1.1").Normalize() |> shouldEqual ((SemVer.Parse "3.1.1.1").ToString())
(SemVer.Parse "1.2.3").Normalize() |> shouldEqual ((SemVer.Parse "1.2.3").ToString())
(SemVer.Parse "1.0.0-rc.3").Normalize() |> shouldEqual ((SemVer.Parse "1.0.0-rc.3").ToString())
Expand All @@ -78,4 +89,9 @@ let ``can normalize versions``() =

[<Test>]
let ``can normalize build zeros``() =
(SemVer.Parse "2.0.30506.0").Normalize() |> shouldEqual ((SemVer.Parse "2.0.30506").ToString())
(SemVer.Parse "2.0.30506.0").Normalize() |> shouldEqual ((SemVer.Parse "2.0.30506").ToString())


[<Test>]
let ``can normalize build zeros in prerelease``() =
(SemVer.Parse "6.0.1302.0-Preview").Normalize() |> shouldEqual "6.0.1302-Preview"

0 comments on commit fbc18c5

Please sign in to comment.