From 6d53a3b2c5336226c9765b7595751041c80a2c34 Mon Sep 17 00:00:00 2001 From: Tomasz Heimowski Date: Thu, 25 Sep 2014 15:37:51 +0200 Subject: [PATCH] add nuget.commandline on convert if auto restore used, dependenciesfile.add should fail if package already exists - references #167 and #168 --- src/Paket/DependenciesFile.fs | 4 ++- src/Paket/NugetConvert.fs | 18 +++++++--- src/Paket/Program.fs | 2 +- src/Paket/SolutionFile.fs | 11 +++--- .../DependenciesFile/AddPackageSpecs.fs | 34 ++++++++++++++++++- 5 files changed, 55 insertions(+), 14 deletions(-) diff --git a/src/Paket/DependenciesFile.fs b/src/Paket/DependenciesFile.fs index de1d2c409e..468852bfa7 100644 --- a/src/Paket/DependenciesFile.fs +++ b/src/Paket/DependenciesFile.fs @@ -170,12 +170,14 @@ type DependenciesFile(fileName,strictMode,packages : UnresolvedPackage list, rem let dependencyMap = Map.ofSeq (packages |> Seq.map (fun p -> p.Name, p.VersionRequirement)) member __.DirectDependencies = dependencyMap member __.Packages = packages + member __.HasPackage (name : string) = packages |> List.exists (fun p -> p.Name.ToLower() = name.ToLower()) member __.RemoteFiles = remoteFiles member __.Strict = strictMode member __.FileName = fileName member this.Resolve(force) = this.Resolve(Nuget.GetVersions,Nuget.GetPackageDetails force) member __.Resolve(getVersionF, getPackageDetailsF) = PackageResolver.Resolve(getVersionF, getPackageDetailsF, packages) - member __.Add(packageName,version:string) = + member this.Add(packageName,version:string) = + if this.HasPackage packageName then failwithf "%s has already package %s" Constants.DependenciesFile packageName let versionRange = DependenciesFileParser.parseVersionRequirement (version.Trim '!') let sources = match packages |> List.rev with diff --git a/src/Paket/NugetConvert.fs b/src/Paket/NugetConvert.fs index b6dba89b14..9ee87103dd 100644 --- a/src/Paket/NugetConvert.fs +++ b/src/Paket/NugetConvert.fs @@ -12,6 +12,11 @@ let private readPackageSources(configFile : FileInfo) = doc.Load configFile.FullName [for node in doc.SelectNodes("//packageSources/add[@value]") -> node.Attributes.["value"].Value] +let removeFileIfExists file = + if File.Exists file then + File.Delete file + tracefn "Deleted %s" file + let private convertNugetsToDepFile(nugetPackagesConfigs) = let allVersions = nugetPackagesConfigs @@ -47,7 +52,7 @@ let private convertNugetsToDepFile(nugetPackagesConfigs) = match FindAllFiles(".", "nuget.config") |> Seq.firstOrDefault with | Some configFile -> let sources = readPackageSources(configFile) - File.Delete(configFile.FullName) + removeFileIfExists configFile.FullName sources @ [Constants.DefaultNugetStream] | None -> [Constants.DefaultNugetStream] |> Set.ofList @@ -88,7 +93,7 @@ let private convertNugetToRefFile(nugetPackagesConfig) = else tracefn "%s is up to date" refFile /// Converts all projects from NuGet to Paket -let ConvertFromNuget(force, installAfter) = +let ConvertFromNuget(force, installAfter, dependenciesFileName) = if File.Exists Constants.DependenciesFile && not force then failwithf "%s already exists, use --force to overwrite" Constants.DependenciesFile let nugetPackagesConfigs = FindAllFiles(".", "packages.config") |> Seq.map Nuget.ReadPackagesConfig @@ -113,14 +118,17 @@ let ConvertFromNuget(force, installAfter) = project.Save() for packagesConfigFile in nugetPackagesConfigs |> Seq.map (fun f -> f.File) do - File.Delete(packagesConfigFile.FullName) + removeFileIfExists packagesConfigFile.FullName match Directory.EnumerateDirectories(".", ".nuget", SearchOption.AllDirectories) |> Seq.firstOrDefault with | Some nugetDir -> let nugetTargets = Path.Combine(nugetDir, "nuget.targets") if File.Exists nugetTargets then - File.Delete(nugetTargets) - tracefn "Deleted %s" nugetTargets + let nugetExe = Path.Combine(nugetDir, "nuget.exe") + removeFileIfExists nugetExe + removeFileIfExists nugetTargets + let depFile = DependenciesFile.ReadFromFile(dependenciesFileName) + if not <| depFile.HasPackage("Nuget.CommandLine") then depFile.Add("Nuget.CommandLine", "").Save() VSIntegration.InitAutoRestore() if Directory.EnumerateFileSystemEntries(nugetDir) |> Seq.isEmpty diff --git a/src/Paket/Program.fs b/src/Paket/Program.fs index a319c1f63b..7ea8b2f4a3 100644 --- a/src/Paket/Program.fs +++ b/src/Paket/Program.fs @@ -108,7 +108,7 @@ try | Command.Update -> UpdateProcess.Update(dependenciesFileName,true,force,hard) | Command.Outdated -> FindOutdated.ListOutdated(dependenciesFileName) | Command.InitAutoRestore -> VSIntegration.InitAutoRestore() - | Command.ConvertFromNuget -> NuGetConvert.ConvertFromNuget(force,noInstall |> not) + | Command.ConvertFromNuget -> NuGetConvert.ConvertFromNuget(force,noInstall |> not,dependenciesFileName) | Command.Simplify -> Simplifier.Simplify(dependenciesFileName) | _ -> traceErrorfn "no command given.%s" (parser.Usage()) diff --git a/src/Paket/SolutionFile.fs b/src/Paket/SolutionFile.fs index aa60a3fc1f..843ead2b11 100644 --- a/src/Paket/SolutionFile.fs +++ b/src/Paket/SolutionFile.fs @@ -18,12 +18,11 @@ module SolutionFile = let RemoveNugetEntries(solutionName: string) = let slnContent = ResizeArray( File.ReadAllLines solutionName ) let mutable modified = false - match slnContent |> Seq.tryFindIndex (fun line -> line.Contains(".nuget\\nuget.targets")) with - | Some(index) -> slnContent.RemoveAt(index); modified <- true - | None -> () - match slnContent |> Seq.tryFindIndex (fun line -> line.Contains(".nuget\\packages.config")) with - | Some(index) -> slnContent.RemoveAt(index); modified <- true - | None -> () + for file in ["nuget.targets";"packages.config";"nuget.exe"] do + match slnContent |> Seq.tryFindIndex (fun line -> line.ToLower().Contains(sprintf ".nuget\\%s" file)) with + | Some(index) -> slnContent.RemoveAt(index); modified <- true + | None -> () + removeNugetSlnFolderIfEmpty(slnContent) if modified then File.WriteAllLines(solutionName, slnContent) \ No newline at end of file diff --git a/tests/Paket.Tests/DependenciesFile/AddPackageSpecs.fs b/tests/Paket.Tests/DependenciesFile/AddPackageSpecs.fs index 7c0699d0f0..067b20de66 100644 --- a/tests/Paket.Tests/DependenciesFile/AddPackageSpecs.fs +++ b/tests/Paket.Tests/DependenciesFile/AddPackageSpecs.fs @@ -116,4 +116,36 @@ let ``should add new packages with nuget package resolution strategy``() = nuget FAKE !~> 1.2""" cfg.ToString() - |> shouldEqual (normalizeLineEndings expected) \ No newline at end of file + |> shouldEqual (normalizeLineEndings expected) + + +[] +let ``should fail if package already exists``() = + let config = """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""" + + try + DependenciesFile.FromCode(noSha1,config).Add("FAKE","") |> ignore + failwith "adding the same package did not throw" + with e -> + e.Message |> shouldEqual <| sprintf "%s has already package %s" Constants.DependenciesFile "FAKE" + + +[] +let ``should fail if package already exists - case insensitive``() = + let config = """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""" + + try + DependenciesFile.FromCode(noSha1,config).Add("fAKe","") |> ignore + failwith "adding the same package did not throw" + with e -> + e.Message |> shouldEqual <| sprintf "%s has already package %s" Constants.DependenciesFile "fAKe" \ No newline at end of file