Skip to content

Commit

Permalink
Allow comments in the references file.
Browse files Browse the repository at this point in the history
Fixes #2371.
  • Loading branch information
fsoikin committed Jun 30, 2017
1 parent cdbeaa0 commit 5dc3772
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 11 deletions.
9 changes: 9 additions & 0 deletions src/Paket.Core/Common/Utils.fs
Original file line number Diff line number Diff line change
Expand Up @@ -900,6 +900,15 @@ let removeFile (fileName : string) =
let normalizeLineEndings (text : string) =
text.Replace("\r\n", "\n").Replace("\r", "\n").Replace("\n", Environment.NewLine)

let removeComment (text:string) =
let stripComment pos =
if pos = 0 || text.[pos-1] = ' ' then text.Substring(0,pos).Trim()
else text
match text.IndexOf "//", text.IndexOf "#" with
| -1 , -1 -> text
| -1, p | p , -1 -> stripComment p
| p1, p2 -> stripComment (min p1 p2)

// adapted from MiniRx
// http://minirx.codeplex.com/
[<AutoOpen>]
Expand Down
9 changes: 0 additions & 9 deletions src/Paket.Core/Dependencies/DependenciesFileParser.fs
Original file line number Diff line number Diff line change
Expand Up @@ -206,15 +206,6 @@ module DependenciesFileParser =
| PackageSource of PackageSource
| Cache of Cache

let private removeComment (text:string) =
let stripComment pos =
let ln = text.Substring(0,pos).Trim() in printfn "%s" ln; ln
match text.IndexOf "//", text.IndexOf "#" with
| -1 , -1 -> text
| -1, p | p , -1 -> stripComment p
| p1, p2 -> stripComment (min p1 p2)


let private (|Remote|_|) (line:string) =
match line.Trim() with
| String.RemovePrefix "source" _ as trimmed ->
Expand Down
3 changes: 2 additions & 1 deletion src/Paket.Core/PaketConfigFiles/ReferencesFile.fs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ type ReferencesFile =
static member FromLines(lines : string[]) =
let groupedLines =
lines
|> Array.fold (fun state line ->
|> Seq.map removeComment
|> Seq.fold (fun state line ->
match state with
| [] -> failwithf "error while parsing %A" lines
| ((name,lines) as currentGroup)::otherGroups ->
Expand Down
35 changes: 34 additions & 1 deletion tests/Paket.Tests/ReferencesFile/ReferencesFileSpecs.fs
Original file line number Diff line number Diff line change
Expand Up @@ -458,4 +458,37 @@ File:countdown.js Scripts link: false"""
[<Test>]
let ``should parse and serialize reffiles with aliases``() =
let refFile = ReferencesFile.FromLines(toLines refFileWithAliases).ToString()
normalizeLineEndings refFile |> shouldEqual (normalizeLineEndings refFileWithAliases)
normalizeLineEndings refFile |> shouldEqual (normalizeLineEndings refFileWithAliases)


let refFileWithComments = """
# separate-line comment with hash
Castle.Windsor # same-line comment with hash
// separate-line comment with slashes
Newtonsoft.Json // same-line comment with slashes
// multiline
// comment
// here
// throw in some leading spaces
# and a hash
FSharp.Core //
File: Some//File#With#Hashes.dot #and a comment after
File: AnotherFile.txt //pluscomment
// Some empty comments:
#
//
# and another comment at the very end
"""

[<Test>]
let ``should parse and serialize reffiles with comments``() =
let refFile = ReferencesFile.FromLines(toLines refFileWithComments).Groups.[Constants.MainDependencyGroup]

[for p in refFile.NugetPackages -> p.Name.Name]
|> shouldEqual ["Castle.Windsor"; "Newtonsoft.Json"; "FSharp.Core"]

[for f in refFile.RemoteFiles -> f.Name]
|> shouldEqual ["Some//File#With#Hashes.dot"; "AnotherFile.txt"]

0 comments on commit 5dc3772

Please sign in to comment.