From fb7d16482bfd677af15359ba19b95da5a1a63f51 Mon Sep 17 00:00:00 2001 From: Steffen Forkmann Date: Sat, 20 Dec 2014 16:04:00 +0100 Subject: [PATCH] Don't add Nuget package twice to ReferencesFile - fixes #454 --- src/Paket.Core/AddProcess.fs | 17 +-- src/Paket.Core/Paket.Core.fsproj | 134 +++++++++--------- src/Paket.Core/ProjectFile.fs | 13 ++ src/Paket.Core/ReferencesFile.fs | 21 ++- src/Paket/Paket.fsproj | 4 +- .../ReferencesFile/ReferencesFileSpecs.fs | 30 +++- 6 files changed, 129 insertions(+), 90 deletions(-) diff --git a/src/Paket.Core/AddProcess.fs b/src/Paket.Core/AddProcess.fs index 1456085baf..7c169a7c68 100644 --- a/src/Paket.Core/AddProcess.fs +++ b/src/Paket.Core/AddProcess.fs @@ -20,21 +20,12 @@ let Add(dependenciesFileName, package, version, force, hard, interactive, instal LockFile.LoadFrom(lockFileName.FullName) if interactive then - let (PackageName package) = package + let (PackageName packageName) = package for project in ProjectFile.FindAllProjects(Path.GetDirectoryName lockFile.FileName) do if Utils.askYesNo(sprintf " Install to %s?" project.Name) then - let proj = FileInfo(project.FileName) - match ProjectFile.FindReferencesFile proj with - | None -> - let newFileName = - let fi = FileInfo(Path.Combine(proj.Directory.FullName,Constants.ReferencesFile)) - if fi.Exists then - Path.Combine(proj.Directory.FullName,proj.Name + "." + Constants.ReferencesFile) - else - fi.FullName - - File.WriteAllLines(newFileName,[package]) - | Some fileName -> File.AppendAllLines(fileName,["";package]) + ProjectFile.FindOrCreateReferencesFile(FileInfo(project.FileName)) + .AddNuGetReference(package) + .Save() if installAfter then let sources = dependenciesFile.GetAllPackageSources() diff --git a/src/Paket.Core/Paket.Core.fsproj b/src/Paket.Core/Paket.Core.fsproj index 14d2b3b6ab..e38d45f2c0 100644 --- a/src/Paket.Core/Paket.Core.fsproj +++ b/src/Paket.Core/Paket.Core.fsproj @@ -66,74 +66,6 @@ --> - - - True - Streams.fs - - - - - FSharp.Core.optdata - Always - - - FSharp.Core.sigdata - Always - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -276,4 +208,70 @@ + + + True + Streams.fs + + + FSharp.Core.optdata + Always + + + FSharp.Core.sigdata + Always + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/Paket.Core/ProjectFile.fs b/src/Paket.Core/ProjectFile.fs index ed47023f2a..82ff5a6650 100644 --- a/src/Paket.Core/ProjectFile.fs +++ b/src/Paket.Core/ProjectFile.fs @@ -61,6 +61,19 @@ type ProjectFile = findInDir projectFile.Directory + static member FindOrCreateReferencesFile (projectFile : FileInfo) = + match ProjectFile.FindReferencesFile projectFile with + | None -> + let newFileName = + let fi = FileInfo(Path.Combine(projectFile.Directory.FullName,Constants.ReferencesFile)) + if fi.Exists then + Path.Combine(projectFile.Directory.FullName,projectFile.Name + "." + Constants.ReferencesFile) + else + fi.FullName + + ReferencesFile.New newFileName + | Some fileName -> ReferencesFile.FromFile fileName + member this.CreateNode(name) = this.Document.CreateElement(name, Constants.ProjectDefaultNameSpace) diff --git a/src/Paket.Core/ReferencesFile.fs b/src/Paket.Core/ReferencesFile.fs index b82b16cadd..86ef15c60a 100644 --- a/src/Paket.Core/ReferencesFile.fs +++ b/src/Paket.Core/ReferencesFile.fs @@ -11,11 +11,16 @@ type RemoteFileReference = type ReferencesFile = { FileName: string - NugetPackages: list - RemoteFiles: list } + NugetPackages: PackageName list + RemoteFiles: RemoteFileReference list } static member DefaultLink = Constants.PaketFilesFolderName + static member New(fileName) = + { FileName = fileName + NugetPackages = [] + RemoteFiles = [] } + static member FromLines(lines : string[]) = let isSingleFile (line: string) = line.StartsWith "File:" let notEmpty (line: string) = not <| String.IsNullOrWhiteSpace line @@ -41,10 +46,14 @@ type ReferencesFile = let lines = File.ReadAllLines(fileName) { ReferencesFile.FromLines lines with FileName = fileName } - member this.AddNuGetReference(reference : PackageName) = - let (PackageName referenceName) = reference - tracefn "Adding %s to %s" referenceName (this.FileName) - { this with NugetPackages = this.NugetPackages @ [reference] } + member this.AddNuGetReference(packageName : PackageName) = + let (PackageName referenceName) = packageName + let normalized = NormalizedPackageName packageName + if this.NugetPackages |> Seq.exists (fun p -> NormalizedPackageName p = normalized) then + this + else + tracefn "Adding %s to %s" referenceName (this.FileName) + { this with NugetPackages = this.NugetPackages @ [packageName] } member this.Save() = File.WriteAllText(this.FileName, this.ToString()) diff --git a/src/Paket/Paket.fsproj b/src/Paket/Paket.fsproj index 868a50f955..da4916a6b1 100644 --- a/src/Paket/Paket.fsproj +++ b/src/Paket/Paket.fsproj @@ -26,12 +26,12 @@ 3 - install --hard + add nuget NUNIT --interactive Project paket.exe - d:\code\PaketTest3 + c:\code\PaketKopie pdbonly diff --git a/tests/Paket.Tests/ReferencesFile/ReferencesFileSpecs.fs b/tests/Paket.Tests/ReferencesFile/ReferencesFileSpecs.fs index 110e56e4a8..d6dfafbad1 100644 --- a/tests/Paket.Tests/ReferencesFile/ReferencesFileSpecs.fs +++ b/tests/Paket.Tests/ReferencesFile/ReferencesFileSpecs.fs @@ -59,4 +59,32 @@ let ``should parse lines with trailing whitspace correctly``() = let refFile = ReferencesFile.FromLines(toLines refFileWithTrailingWhitespace) refFile.NugetPackages.Length |> shouldEqual 2 refFile.NugetPackages.Head |> shouldEqual (PackageName "Castle.Windsor") - refFile.NugetPackages.Tail.Head |> shouldEqual (PackageName "Newtonsoft.Json") \ No newline at end of file + refFile.NugetPackages.Tail.Head |> shouldEqual (PackageName "Newtonsoft.Json") + +[] +let ``should add nuget package``() = + let empty = ReferencesFile.New("file.txt") + empty.NugetPackages.Length |> shouldEqual 0 + empty.RemoteFiles.Length |> shouldEqual 0 + empty.FileName |> shouldEqual "file.txt" + + let refFile = empty.AddNuGetReference(PackageName "NUnit") + refFile.NugetPackages.Length |> shouldEqual 1 + refFile.NugetPackages.Head |> shouldEqual (PackageName "NUnit") + + let refFile' = refFile.AddNuGetReference(PackageName "xUnit") + refFile'.NugetPackages.Length |> shouldEqual 2 + refFile'.NugetPackages.Head |> shouldEqual (PackageName "NUnit") + refFile'.NugetPackages.Tail.Head |> shouldEqual (PackageName "xUnit") + + +[] +let ``should not add nuget package twice``() = + let refFile = + ReferencesFile.New("file.txt") + .AddNuGetReference(PackageName "NUnit") + .AddNuGetReference(PackageName "NUnit") + .AddNuGetReference(PackageName "NUnit") + + refFile.NugetPackages.Length |> shouldEqual 1 + refFile.NugetPackages.Head |> shouldEqual (PackageName "NUnit")