Skip to content

Commit

Permalink
Allow to format PackageRequirements in Nuget syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
forki committed Sep 29, 2015
1 parent c3dc0b0 commit 99d2a42
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 57 deletions.
3 changes: 3 additions & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#### 2.4.10 - 29.09.2015
* Állow to format VersionRequirements in NuGet syntax

#### 2.4.9 - 29.09.2015
* BUGFIX: Fix KeyNotFoundException when project is net4.0-client - https://github.com/fsprojects/Paket/issues/1095

Expand Down
13 changes: 2 additions & 11 deletions src/Paket.Core/NupkgWriter.fs
Original file line number Diff line number Diff line change
Expand Up @@ -80,19 +80,10 @@ let nuspecDoc (info:CompleteInfo) =
frameworkAssembliesList |> List.iter (buildFrameworkReferencesNode >> d.Add)
metadataNode.Add d

let buildDependencyNode (Id, (VersionRequirement(range, prerelease))) =
let buildDependencyNode (Id, requirement:VersionRequirement) =
let dep = XElement(ns + "dependency")
dep.SetAttributeValue(XName.Get "id", Id)

let pre =
match prerelease with
| No -> ""
| Concrete [x] -> "-" + x
| _ -> "-prerelease"

match range.FormatInNuGetSyntax() + pre with
| "0" -> ()
| versionStr -> dep.SetAttributeValue(XName.Get "version", versionStr)
dep.SetAttributeValue(XName.Get "version", requirement.FormatInNuGetSyntax())
dep

let buildDependenciesNode dependencyList =
Expand Down
66 changes: 41 additions & 25 deletions src/Paket.Core/VersionRange.fs
Original file line number Diff line number Diff line change
Expand Up @@ -67,31 +67,6 @@ type VersionRange =

from + " " + _to

/// formats a VersionRange in NuGet syntax
member this.FormatInNuGetSyntax() =
match this with
| Minimum(version) ->
match version.Normalize() with
| "0.0.0" -> ""
| x -> x
| GreaterThan(version) -> sprintf "(%s,)" (version.Normalize())
| Maximum(version) -> sprintf "(,%s]" (version.Normalize())
| LessThan(version) -> sprintf "(,%s)" (version.Normalize())
| Specific(version) -> sprintf "[%s]" (version.Normalize())
| OverrideAll(version) -> sprintf "[%s]" (version.Normalize())
| Range(fromB, from,_to,_toB) ->
let getMinDelimiter (v:VersionRangeBound) =
match v with
| VersionRangeBound.Including -> "["
| VersionRangeBound.Excluding -> "("

let getMaxDelimiter (v:VersionRangeBound) =
match v with
| VersionRangeBound.Including -> "]"
| VersionRangeBound.Excluding -> ")"

sprintf "%s%s,%s%s" (getMinDelimiter fromB) (from.Normalize()) (_to.Normalize()) (getMaxDelimiter _toB)

type VersionRequirement =
| VersionRequirement of VersionRange * PreReleaseStatus
/// Checks wether the given version is in the version range
Expand Down Expand Up @@ -203,6 +178,47 @@ type VersionRequirement =

VersionRequirement(range,!prereleases)

/// Formats a VersionRequirement in NuGet syntax
member this.FormatInNuGetSyntax() =
match this with
| VersionRequirement(range,prerelease) ->
let pre =
match prerelease with
| No -> ""
| Concrete [x] -> "-" + x
| _ -> "-prerelease"

let normalize (v:SemVerInfo) = v.Normalize() + pre

let str =
match range with
| Minimum(version) ->
match normalize version with
| "0.0.0" -> ""
| x -> x
| GreaterThan(version) -> sprintf "(%s,)" (normalize version)
| Maximum(version) -> sprintf "(,%s]" (normalize version)
| LessThan(version) -> sprintf "(,%s)" (normalize version)
| Specific(version) -> sprintf "[%s]" (normalize version)
| OverrideAll(version) -> sprintf "[%s]" (normalize version)
| Range(fromB, from,_to,_toB) ->
let getMinDelimiter (v:VersionRangeBound) =
match v with
| VersionRangeBound.Including -> "["
| VersionRangeBound.Excluding -> "("

let getMaxDelimiter (v:VersionRangeBound) =
match v with
| VersionRangeBound.Including -> "]"
| VersionRangeBound.Excluding -> ")"

sprintf "%s%s,%s%s" (getMinDelimiter fromB) (normalize from) (normalize _to) (getMaxDelimiter _toB)


match str with
| "0" -> ""
| versionStr -> versionStr

/// Represents a resolver strategy.
[<RequireQualifiedAccess>]
type ResolverStrategy =
Expand Down
4 changes: 2 additions & 2 deletions src/Paket/Paket.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@
<StartWorkingDirectory>c:\code\Paket09x</StartWorkingDirectory>
<StartWorkingDirectory>D:\code\Pakettest</StartWorkingDirectory>
<StartWorkingDirectory>D:\code\Paketkopie</StartWorkingDirectory>
<StartArguments>update</StartArguments>
<StartArguments>update nuget Vetserve.Appointments.Interop -v</StartArguments>
<StartAction>Project</StartAction>
<StartProgram>paket.exe</StartProgram>
<StartWorkingDirectory>c:\code\Paketkopie</StartWorkingDirectory>
<StartWorkingDirectory>d:\code\PaketKopie</StartWorkingDirectory>
<StartWorkingDirectory>d:\code\Paketrepro</StartWorkingDirectory>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
Expand Down
13 changes: 13 additions & 0 deletions tests/Paket.Tests/DependenciesFile/VersionRequirementSpecs.fs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,19 @@ let ``can order twiddle-wakka``() =
parse "~> 2.2" |> shouldBeSmallerThan (parse "~> 2.3")
parse "~> 1.2" |> shouldBeSmallerThan (parse "~> 2.3")

[<Test>]
let ``can parse twiddle-wakka with prerelease``() =
let req = DependenciesFileParser.parseVersionRequirement "~> 6.0.0 prerelease"

req
|> shouldEqual
(VersionRequirement.VersionRequirement(
VersionRange.Range(VersionRangeBound.Including,SemVer.Parse("6.0.0"),SemVer.Parse("6.1.0"),VersionRangeBound.Excluding),
PreReleaseStatus.All))

req.FormatInNuGetSyntax()
|> shouldEqual "[6.0.0-prerelease,6.1.0-prerelease)"

[<Test>]
let ``can order simple at least requirements in package requirement``() =
require "A" ResolverStrategy.Max ">= 2.2" |> shouldBeGreaterThan (require "A" ResolverStrategy.Max ">= 2.3")
Expand Down
38 changes: 19 additions & 19 deletions tests/Paket.Tests/NugetVersionRangeSerializerSpecs.fs
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,40 @@ open FsUnit
let ``can format latest version``() =
VersionRequirement.Parse "" |> shouldEqual VersionRequirement.AllReleases

let format(versionRange:VersionRange) = versionRange.FormatInNuGetSyntax()
let format(versionRange:VersionRange) = VersionRequirement(versionRange,PreReleaseStatus.No).FormatInNuGetSyntax()

[<Test>]
let ``can format specific version``() =
VersionRange.Specific(SemVer.Parse "2.2").FormatInNuGetSyntax() |> shouldEqual "[2.2.0]"
VersionRange.Specific(SemVer.Parse "1.2").FormatInNuGetSyntax() |> shouldEqual "[1.2.0]"
VersionRange.Specific(SemVer.Parse "2.2") |> format |> shouldEqual "[2.2.0]"
VersionRange.Specific(SemVer.Parse "1.2") |> format |> shouldEqual "[1.2.0]"

[<Test>]
let ``can format minimum version``() =
VersionRange.Minimum(SemVer.Parse "2.2").FormatInNuGetSyntax() |> shouldEqual "2.2.0"
VersionRange.Minimum(SemVer.Parse "1.2").FormatInNuGetSyntax() |> shouldEqual "1.2.0"
VersionRange.Minimum(SemVer.Parse "7").FormatInNuGetSyntax() |> shouldEqual "7.0.0"
VersionRange.Minimum(SemVer.Parse "0").FormatInNuGetSyntax() |> shouldEqual ""
VersionRange.Minimum(SemVer.Parse "1.0-beta").FormatInNuGetSyntax() |> shouldEqual "1.0.0-beta"
VersionRange.Minimum(SemVer.Parse "2.2") |> format |> shouldEqual "2.2.0"
VersionRange.Minimum(SemVer.Parse "1.2") |> format |> shouldEqual "1.2.0"
VersionRange.Minimum(SemVer.Parse "7") |> format |> shouldEqual "7.0.0"
VersionRange.Minimum(SemVer.Parse "0") |> format |> shouldEqual ""
VersionRange.Minimum(SemVer.Parse "1.0-beta") |> format |> shouldEqual "1.0.0-beta"

[<Test>]
let ``can format greater than version``() =
VersionRange.GreaterThan(SemVer.Parse "2.2").FormatInNuGetSyntax() |> shouldEqual "(2.2.0,)"
VersionRange.GreaterThan(SemVer.Parse "1.2").FormatInNuGetSyntax() |> shouldEqual "(1.2.0,)"
VersionRange.GreaterThan(SemVer.Parse "2.2") |> format |> shouldEqual "(2.2.0,)"
VersionRange.GreaterThan(SemVer.Parse "1.2") |> format |> shouldEqual "(1.2.0,)"

[<Test>]
let ``can format maximum version``() =
VersionRange.Maximum(SemVer.Parse "2.2").FormatInNuGetSyntax() |> shouldEqual "(,2.2.0]"
VersionRange.Maximum(SemVer.Parse "0").FormatInNuGetSyntax() |> shouldEqual "(,0.0.0]"
VersionRange.Maximum(SemVer.Parse "1.2").FormatInNuGetSyntax() |> shouldEqual "(,1.2.0]"
VersionRange.Maximum(SemVer.Parse "2.2") |> format |> shouldEqual "(,2.2.0]"
VersionRange.Maximum(SemVer.Parse "0") |> format |> shouldEqual "(,0.0.0]"
VersionRange.Maximum(SemVer.Parse "1.2") |> format |> shouldEqual "(,1.2.0]"

[<Test>]
let ``can format less than version``() =
VersionRange.LessThan(SemVer.Parse "2.2").FormatInNuGetSyntax() |> shouldEqual "(,2.2.0)"
VersionRange.LessThan(SemVer.Parse "1.2").FormatInNuGetSyntax() |> shouldEqual "(,1.2.0)"
VersionRange.LessThan(SemVer.Parse "2.2") |> format |> shouldEqual "(,2.2.0)"
VersionRange.LessThan(SemVer.Parse "1.2") |> format |> shouldEqual "(,1.2.0)"

[<Test>]
let ``can format range version``() =
VersionRange.Range(VersionRangeBound.Excluding, SemVer.Parse "2.2", SemVer.Parse "3", VersionRangeBound.Excluding).FormatInNuGetSyntax() |> shouldEqual "(2.2.0,3.0.0)"
VersionRange.Range(VersionRangeBound.Excluding, SemVer.Parse "2.2", SemVer.Parse "3", VersionRangeBound.Including).FormatInNuGetSyntax() |> shouldEqual "(2.2.0,3.0.0]"
VersionRange.Range(VersionRangeBound.Including, SemVer.Parse "2.2", SemVer.Parse "3", VersionRangeBound.Excluding).FormatInNuGetSyntax() |> shouldEqual "[2.2.0,3.0.0)"
VersionRange.Range(VersionRangeBound.Including, SemVer.Parse "2.2", SemVer.Parse "3", VersionRangeBound.Including).FormatInNuGetSyntax() |> shouldEqual "[2.2.0,3.0.0]"
VersionRange.Range(VersionRangeBound.Excluding, SemVer.Parse "2.2", SemVer.Parse "3", VersionRangeBound.Excluding) |> format |> shouldEqual "(2.2.0,3.0.0)"
VersionRange.Range(VersionRangeBound.Excluding, SemVer.Parse "2.2", SemVer.Parse "3", VersionRangeBound.Including) |> format |> shouldEqual "(2.2.0,3.0.0]"
VersionRange.Range(VersionRangeBound.Including, SemVer.Parse "2.2", SemVer.Parse "3", VersionRangeBound.Excluding) |> format |> shouldEqual "[2.2.0,3.0.0)"
VersionRange.Range(VersionRangeBound.Including, SemVer.Parse "2.2", SemVer.Parse "3", VersionRangeBound.Including) |> format |> shouldEqual "[2.2.0,3.0.0]"

0 comments on commit 99d2a42

Please sign in to comment.