From 44ddd688680cce4403e53aa921e2adfa3ec748bb Mon Sep 17 00:00:00 2001 From: Steffen Forkmann Date: Wed, 10 Dec 2014 15:10:02 +0100 Subject: [PATCH] Generate one targets file per project --- src/Paket.Core/Constants.fs | 2 +- src/Paket.Core/ProjectFile.fs | 75 ++++++++++--------- .../Paket.Tests/InstallModel/Xml/Fantomas.fs | 10 +-- 3 files changed, 44 insertions(+), 43 deletions(-) diff --git a/src/Paket.Core/Constants.fs b/src/Paket.Core/Constants.fs index 283b23234a..a1ec5c7590 100644 --- a/src/Paket.Core/Constants.fs +++ b/src/Paket.Core/Constants.fs @@ -16,7 +16,7 @@ let ReferencesFile = "paket.references" let PaketFilesFolderName = "paket-files" [] -let PackageTargetsFileName = "generated.paket.targets" +let PackageTargetsFileName = ".paket.targets" [] let SolutionFolderProjectGuid = "2150E333-8FDC-42A3-9474-1A3956D46DE8" diff --git a/src/Paket.Core/ProjectFile.fs b/src/Paket.Core/ProjectFile.fs index e270c02aeb..ca50d62824 100644 --- a/src/Paket.Core/ProjectFile.fs +++ b/src/Paket.Core/ProjectFile.fs @@ -43,6 +43,11 @@ type ProjectFile = member this.GetCustomReferenceAndFrameworkNodes() = this.FindNodes false "Reference" + member this.TargetFileName = + let fi = FileInfo(this.FileName) + let name = fi.Name.Replace(fi.Extension,Constants.PackageTargetsFileName) + Path.Combine(fi.Directory.FullName,name) + /// Finds all project files static member FindAllProjects(folder) = FindAllFiles(folder, "*.*proj") @@ -176,13 +181,12 @@ type ProjectFile = for node in nodesToDelete do node.ParentNode.RemoveChild(node) |> ignore - member this.DeletePaketImportNodes() = + member this.DeletePaketImportNodes(fileName) = let nodesToDelete = [for node in this.Document |> getDescendants "Import" do let attr = node.Attributes.["Project"] - if attr <> null then - if attr.InnerText.EndsWith("\\" + Constants.PackageTargetsFileName) then - yield node] + if attr <> null && attr.InnerText = fileName then + yield node] for node in nodesToDelete do node.ParentNode.RemoveChild(node) |> ignore @@ -254,12 +258,16 @@ type ProjectFile = let generateTargetsFiles = true // TODO: Make parameter this.DeletePaketNodes("Reference") - this.DeletePaketImportNodes() + this.DeletePaketImportNodes(createRelativePath this.FileName this.TargetFileName) ["ItemGroup";"When";"Otherwise";"Choose";"When";"Choose"] |> List.iter this.DeleteIfEmpty + let targetsDocument = XmlDocument() + let project = targetsDocument.CreateElement("Project", Constants.ProjectDefaultNameSpace) + let referenced = ref false + completeModel |> Seq.filter (fun kv -> usedPackages.Contains kv.Key) |> Seq.map (fun kv -> @@ -268,44 +276,39 @@ type ProjectFile = kv.Value.PackageName,this.GenerateXml kv.Value) |> Seq.filter (fun (_,node) -> node.ChildNodes.Count > 0) - |> Seq.fold (fun targetFiles (packageName,node) -> + |> Seq.iter (fun (packageName,node) -> if generateTargetsFiles then - let (PackageName name) = packageName - let targetsFile = FileInfo(Path.Combine(rootPath,"packages",name,Constants.PackageTargetsFileName)) + if not !referenced then + let targetsNode = this.GenerateTargetImport this.TargetFileName + this.ProjectNode.AppendChild targetsNode |> ignore + referenced := true - let doc = XmlDocument() - let project = doc.CreateElement("Project", Constants.ProjectDefaultNameSpace) - let tempNode = doc.ImportNode(node, true) + let tempNode = targetsDocument.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 + targetsDocument.AppendChild project |> ignore else - this.ProjectNode.AppendChild node |> ignore - targetFiles) - [] + this.ProjectNode.AppendChild node |> ignore) + targetsDocument member this.UpdateReferences(rootPath:string,completeModel: Map, usedPackages : Set, hard) = - for targetFileName,doc in this.GenerateReferences(rootPath,completeModel, usedPackages, hard) do - let fi = FileInfo(targetFileName) - let originalText = - if fi.Exists then - try - let originalDoc = new XmlDocument() - originalDoc.Load fi.FullName - Utils.normalizeXml originalDoc - with - | _ -> "" - else - "" - - let newText = Utils.normalizeXml doc + let targetsDocument = this.GenerateReferences(rootPath,completeModel, usedPackages, hard) + let fi = FileInfo(this.TargetFileName) + let originalText = + if fi.Exists then + try + let originalDoc = new XmlDocument() + originalDoc.Load fi.FullName + Utils.normalizeXml originalDoc + with + | _ -> "" + else + "" + + let newText = Utils.normalizeXml targetsDocument - if newText <> originalText then - doc.Save(targetFileName) - this.Save() // Save in order to make Visual Studio reload the project + if newText <> originalText then + targetsDocument.Save(this.TargetFileName) + this.Save() // Save in order to make Visual Studio reload the project member this.Save() = this.Document.Save(this.FileName) diff --git a/tests/Paket.Tests/InstallModel/Xml/Fantomas.fs b/tests/Paket.Tests/InstallModel/Xml/Fantomas.fs index a8e2693ba5..c44d9b95dd 100644 --- a/tests/Paket.Tests/InstallModel/Xml/Fantomas.fs +++ b/tests/Paket.Tests/InstallModel/Xml/Fantomas.fs @@ -50,7 +50,7 @@ let fullDoc = """ let docWithTargets = """ - + """ [] @@ -65,15 +65,13 @@ 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 - let targetFiles = project.GenerateReferences(".",completeModel,used,false) + let targetDoc = project.GenerateReferences(".",completeModel,used,false) project.Document.OuterXml |> normalizeXml |> shouldEqual (normalizeXml docWithTargets) - let targetFileName,targetDoc = targetFiles |> List.head - - targetFileName.EndsWith (Path.Combine("packages","Fantomas","generated.paket.targets")) |> shouldEqual true + project.TargetFileName.EndsWith("Empty.paket.targets") |> shouldEqual true targetDoc.OuterXml |> normalizeXml |> shouldEqual (normalizeXml fullDoc) @@ -91,7 +89,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 - let targetFiles = project.GenerateReferences(".",completeModel,used,false) + let targetDoc = project.GenerateReferences(".",completeModel,used,false) project.Document.OuterXml |> normalizeXml