Skip to content

Commit

Permalink
add nuget.commandline on convert if auto restore used, dependenciesfi…
Browse files Browse the repository at this point in the history
…le.add should fail if package already exists - references fsprojects#167 and fsprojects#168
  • Loading branch information
theimowski committed Sep 25, 2014
1 parent f55fd24 commit 6d53a3b
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 14 deletions.
4 changes: 3 additions & 1 deletion src/Paket/DependenciesFile.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 13 additions & 5 deletions src/Paket/NugetConvert.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Paket/Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand Down
11 changes: 5 additions & 6 deletions src/Paket/SolutionFile.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
34 changes: 33 additions & 1 deletion tests/Paket.Tests/DependenciesFile/AddPackageSpecs.fs
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,36 @@ let ``should add new packages with nuget package resolution strategy``() =
nuget FAKE !~> 1.2"""

cfg.ToString()
|> shouldEqual (normalizeLineEndings expected)
|> shouldEqual (normalizeLineEndings expected)


[<Test>]
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"


[<Test>]
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"

0 comments on commit 6d53a3b

Please sign in to comment.