Skip to content

Commit

Permalink
Allow cleaner syntax in paket.dependencies - references #95
Browse files Browse the repository at this point in the history
  • Loading branch information
forki committed Sep 17, 2014
1 parent 66eff25 commit b6a3c10
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 47 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.2.0-alpha010 - 16.09.2014
* Support cleaner syntax in paket.dependencies - https://github.com/fsprojects/Paket/pull/95

#### 0.2.0-alpha009 - 16.09.2014
* Support content files from nuget - https://github.com/fsprojects/Paket/pull/84

Expand Down
31 changes: 17 additions & 14 deletions src/Paket/DependenciesFile.fs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ open Paket
/// [omit]
module DependenciesFileParser =

let private basicOperators = ["~>";">=";"="]
let private operators = basicOperators @ (basicOperators |> List.map (fun o -> "!" + o))

let parseVersionRange (text : string) : VersionRange =
let splitVersion (text:string) =
let tokens = ["~>";">=";"=" ]
match tokens |> List.tryFind(text.StartsWith) with
let splitVersion (text:string) =
match basicOperators |> List.tryFind(text.StartsWith) with
| Some token -> token, text.Replace(token + " ", "")
| None -> "=", text

Expand All @@ -37,11 +39,16 @@ module DependenciesFileParser =
let private (|Remote|Package|Blank|ReferencesMode|SourceFile|) (line:string) =
match line.Trim() with
| _ when String.IsNullOrWhiteSpace line -> Blank
| trimmed when trimmed.StartsWith "source" ->
let fst = trimmed.IndexOf("\"")
let snd = trimmed.IndexOf("\"",fst+1)
Remote (trimmed.Substring(fst,snd-fst).Replace("\"",""))
| trimmed when trimmed.StartsWith "nuget" -> Package(trimmed.Replace("nuget","").Trim())
| trimmed when trimmed.StartsWith "source" ->
let parts = trimmed.Split ' '
Remote (parts.[1].Replace("\"",""))
| trimmed when trimmed.StartsWith "nuget" ->
let parts = trimmed.Replace("nuget","").Trim().Replace("\"", "").Split ' ' |> Seq.toList
match parts with
| name :: operator :: version :: _
when List.exists ((=) operator) operators -> Package(name,operator + " " + version)
| name :: version :: _ -> Package(name,version)
| _ -> failwithf "could not retrieve nuget package from %s" trimmed
| trimmed when trimmed.StartsWith "references" -> ReferencesMode(trimmed.Replace("references","").Trim() = "strict")
| trimmed when trimmed.StartsWith "github" ->
let parts = trimmed.Replace("\"", "").Split ' '
Expand All @@ -64,14 +71,10 @@ module DependenciesFileParser =
| Remote newSource -> lineNo, referencesMode, (PackageSource.Parse(newSource.TrimEnd([|'/'|])) :: sources), packages, sourceFiles
| Blank -> lineNo, referencesMode, sources, packages, sourceFiles
| ReferencesMode mode -> lineNo, mode, sources, packages, sourceFiles
| Package details ->
let parts = details.Split('"')
if parts.Length < 4 || String.IsNullOrWhiteSpace parts.[1] || String.IsNullOrWhiteSpace parts.[3] then
failwith "missing \""
let version = parts.[3]
| Package(name,version) ->
lineNo, referencesMode, sources,
{ Sources = sources
Name = parts.[1]
Name = name
ResolverStrategy = if version.StartsWith "!" then ResolverStrategy.Min else ResolverStrategy.Max
VersionRange = parseVersionRange(version.Trim '!') } :: packages, sourceFiles
| SourceFile((owner,project, commit), path) ->
Expand Down
72 changes: 39 additions & 33 deletions tests/Paket.Tests/DependenciesFile/ParserSpecs.fs
Original file line number Diff line number Diff line change
Expand Up @@ -74,38 +74,6 @@ let ``should read config with multiple sources``() =
(cfg.Packages |> List.find (fun p -> p.Name = "MinPackage")).Sources |> shouldEqual [Nuget "http://nuget.org/api/v3"; Nuget "http://nuget.org/api/v2"]
(cfg.Packages |> List.find (fun p -> p.Name = "FAKE")).Sources |> shouldEqual [Nuget "http://nuget.org/api/v2"]

let config5 = """source "http://nuget.org/api/v2"
nuget "RavenDB.Client" ">= 0"
nuget RavenDB.Server" ">= 0" // missing "
"""

[<Test>]
let ``should report errors if pacakge misses "``() =
try
DependenciesFile.FromCode config5 |> ignore
failwith "No message given"
with
| exn ->
exn.Message.Contains("paket.dependencies") |> shouldEqual true
exn.Message.Contains("line 4") |> shouldEqual true
exn.Message.Contains("missing \"") |> shouldEqual true

let config6 = """source "http://nuget.org/api/v2"
nuget "Fody" "1.25.0"
nuget "Obsolete.Fody" 3.1.0.0" // missing "
"""

[<Test>]
let ``should report errors if version misses "``() =
try
DependenciesFile.FromCode config6 |> ignore
failwith "No message given"
with
| exn ->
exn.Message.Contains("paket.dependencies") |> shouldEqual true
exn.Message.Contains("line 3") |> shouldEqual true
exn.Message.Contains("missing \"") |> shouldEqual true

let config7 = """nuget "Fody" "> 0"
"""
Expand All @@ -120,7 +88,7 @@ let ``should report errors if nuget is single``() =
exn.Message.Contains("paket.dependencies") |> shouldEqual true
exn.Message.Contains("line 1") |> shouldEqual true
exn.Message.Contains("could not parse version range") |> shouldEqual true
exn.Message.Contains("> 0") |> shouldEqual true
exn.Message.Contains(">") |> shouldEqual true

[<Test>]
let ``should read source file from config``() =
Expand Down Expand Up @@ -151,3 +119,41 @@ let ``should read strict config``() =
cfg.Strict |> shouldEqual true

(cfg.Packages |> List.find (fun p -> p.Name = "FAKE")).Sources |> shouldEqual [Nuget "http://nuget.org/api/v2"]


let configWithoutQuotes = """
source http://nuget.org/api/v2
nuget Castle.Windsor-log4net ~> 3.2
nuget Rx-Main ~> 2.0
nuget FAKE = 1.1
nuget SignalR = 3.3.2
"""

[<Test>]
let ``should read config without "``() =
let cfg = DependenciesFile.FromCode configWithoutQuotes
cfg.Strict |> shouldEqual false
cfg.DirectDependencies.Count |> shouldEqual 4

cfg.DirectDependencies.["Rx-Main"] |> shouldEqual (VersionRange.Between("2.0", "3.0"))
cfg.DirectDependencies.["Castle.Windsor-log4net"] |> shouldEqual (VersionRange.Between("3.2", "4.0"))
cfg.DirectDependencies.["FAKE"] |> shouldEqual (VersionRange.Exactly "1.1")
cfg.DirectDependencies.["SignalR"] |> shouldEqual (VersionRange.Exactly "3.3.2")


[<Test>]
let ``should read github source file from config without quotes``() =
let config = """github fsharp/FAKE src/app/FAKE/Cli.fs
github fsharp/FAKE:bla123zxc src/app/FAKE/FileWithCommit.fs """
let dependencies = DependenciesFile.FromCode config
dependencies.RemoteFiles
|> shouldEqual
[ { Owner = "fsharp"
Project = "FAKE"
Name = "src/app/FAKE/Cli.fs"
Commit = None }
{ Owner = "fsharp"
Project = "FAKE"
Name = "src/app/FAKE/FileWithCommit.fs"
Commit = Some "bla123zxc" } ]

0 comments on commit b6a3c10

Please sign in to comment.