diff --git a/src/Paket.Core/Constants.fs b/src/Paket.Core/Constants.fs index 5681ffb310..0d130a44dd 100644 --- a/src/Paket.Core/Constants.fs +++ b/src/Paket.Core/Constants.fs @@ -15,6 +15,9 @@ let ReferencesFile = "paket.references" [] let PaketFilesFolderName = "paket-files" +[] +let PackageTargetsFileName = "paket.targets" + [] let SolutionFolderProjectGuid = "2150E333-8FDC-42A3-9474-1A3956D46DE8" diff --git a/src/Paket.Core/InstallProcess.fs b/src/Paket.Core/InstallProcess.fs index 3e7281b3f9..914ad4bdb0 100644 --- a/src/Paket.Core/InstallProcess.fs +++ b/src/Paket.Core/InstallProcess.fs @@ -150,7 +150,7 @@ let Install(sources,force, hard, withBindingRedirects, lockFile:LockFile) = |> Seq.map (fun x -> NormalizedPackageName x) |> Set.ofSeq - project.UpdateReferences(model,usedPackageNames,hard) + project.UpdateReferences(lockFile.Directory,model,usedPackageNames,hard) removeCopiedFiles project diff --git a/src/Paket.Core/LockFile.fs b/src/Paket.Core/LockFile.fs index db71dc17ee..268c2adf82 100644 --- a/src/Paket.Core/LockFile.fs +++ b/src/Paket.Core/LockFile.fs @@ -228,6 +228,7 @@ type LockFile(fileName:string,options,resolution:PackageResolution,remoteFiles:R member __.SourceFiles = remoteFiles member __.ResolvedPackages = resolution member __.FileName = fileName + member __.Directory = Path.GetDirectoryName fileName member __.Options = options /// Gets all dependencies of the given package diff --git a/src/Paket.Core/ProjectFile.fs b/src/Paket.Core/ProjectFile.fs index 20a9a3307a..9abe883f79 100644 --- a/src/Paket.Core/ProjectFile.fs +++ b/src/Paket.Core/ProjectFile.fs @@ -176,6 +176,15 @@ type ProjectFile = for node in nodesToDelete do node.ParentNode.RemoveChild(node) |> ignore + member this.GenerateTargetImport(filename:string) = + let relativePath = createRelativePath this.FileName filename + + let importNode = this.Document.CreateElement("Import", Constants.ProjectDefaultNameSpace) + let condition = sprintf "Exists('%s')" relativePath + importNode |> addAttribute "Project" relativePath |> ignore + importNode |> addAttribute "Condition" condition |> ignore + importNode + member this.GenerateXml(model:InstallModel) = let references = this.GetCustomReferenceAndFrameworkNodes() @@ -229,7 +238,9 @@ type ProjectFile = chooseNode - member this.UpdateReferences(completeModel: Map, usedPackages : Set, hard) = + member this.GenerateReferences(rootPath:string,completeModel: Map, usedPackages : Set, hard) = + let generateTargetsFiles = true // TODO: Make parameter + this.DeletePaketNodes("Reference") ["ItemGroup";"When";"Otherwise";"Choose";"When";"Choose"] @@ -242,9 +253,30 @@ type ProjectFile = if hard then this.DeleteCustomModelNodes(kv.Value) - this.GenerateXml kv.Value) - |> Seq.filter (fun node -> node.ChildNodes.Count > 0) - |> Seq.iter (this.ProjectNode.AppendChild >> ignore) + kv.Value.PackageName,this.GenerateXml kv.Value) + |> Seq.filter (fun (_,node) -> node.ChildNodes.Count > 0) + |> Seq.fold (fun targetFiles (packageName,node) -> + if generateTargetsFiles then + let (PackageName name) = packageName + let targetsFile = FileInfo(Path.Combine(rootPath,"packages",name,Constants.PackageTargetsFileName)) + + let doc = XmlDocument() + let project = doc.CreateElement("Project", Constants.ProjectDefaultNameSpace) + let tempNode = doc.ImportNode(node, true) + project.AppendChild tempNode |> ignore + doc.AppendChild project |> ignore + + let targetsNode = this.GenerateTargetImport targetsFile.FullName + this.ProjectNode.AppendChild targetsNode |> ignore + (targetsFile.FullName,doc)::targetFiles + else + this.ProjectNode.AppendChild node |> ignore + targetFiles) + [] + + member this.UpdateReferences(rootPath:string,completeModel: Map, usedPackages : Set, hard) = + for targetFileName,doc in this.GenerateReferences(rootPath,completeModel, usedPackages, hard) do + doc.Save(targetFileName) member this.Save() = if Utils.normalizeXml this.Document <> this.OriginalText then diff --git a/tests/Paket.Tests/InstallModel/Xml/Fantomas.fs b/tests/Paket.Tests/InstallModel/Xml/Fantomas.fs index 9fdc0bf26a..7ba26eb047 100644 --- a/tests/Paket.Tests/InstallModel/Xml/Fantomas.fs +++ b/tests/Paket.Tests/InstallModel/Xml/Fantomas.fs @@ -5,6 +5,7 @@ open NUnit.Framework open FsUnit open Paket.TestHelpers open Paket.Domain +open System.IO let expected = """ @@ -35,9 +36,8 @@ let emptyDoc = """ """ -let fullDoc = """ - - +let fullDoc = """ + ..\..\..\Fantomas\lib\FantomasLib.dll @@ -47,6 +47,12 @@ let fullDoc = """ """ +let docWithTargets = """ + + + +""" + [] let ``should generate full Xml for Fantomas 1.5``() = let model = @@ -59,10 +65,17 @@ let ``should generate full Xml for Fantomas 1.5``() = let project = ProjectFile.Load("./ProjectFile/TestData/Empty.fsprojtest").Value let completeModel = [NormalizedPackageName (PackageName "Fantomas"),model] |> Map.ofSeq let used = [NormalizedPackageName (PackageName "fantoMas")] |> Set.ofSeq - project.UpdateReferences(completeModel,used,false) + let targetFiles = project.GenerateReferences(".",completeModel,used,false) project.Document.OuterXml |> normalizeXml + |> shouldEqual (normalizeXml docWithTargets) + + let targetFileName,targetDoc = targetFiles |> List.head + + targetFileName.EndsWith (Path.Combine("packages","Fantomas","paket.targets")) |> shouldEqual true + targetDoc.OuterXml + |> normalizeXml |> shouldEqual (normalizeXml fullDoc) @@ -78,7 +91,7 @@ let ``should not generate full Xml for Fantomas 1.5 if not referenced``() = let project = ProjectFile.Load("./ProjectFile/TestData/Empty.fsprojtest").Value let completeModel = [NormalizedPackageName (PackageName "Fantomas"),model] |> Map.ofSeq let used = [NormalizedPackageName (PackageName "blub")] |> Set.ofSeq - project.UpdateReferences(completeModel,used,false) + let targetFiles = project.GenerateReferences(".",completeModel,used,false) project.Document.OuterXml |> normalizeXml