Skip to content

Commit

Permalink
Generate one targets file per project
Browse files Browse the repository at this point in the history
  • Loading branch information
forki committed Dec 10, 2014
1 parent f4a7361 commit 44ddd68
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 43 deletions.
2 changes: 1 addition & 1 deletion src/Paket.Core/Constants.fs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ let ReferencesFile = "paket.references"
let PaketFilesFolderName = "paket-files"

[<Literal>]
let PackageTargetsFileName = "generated.paket.targets"
let PackageTargetsFileName = ".paket.targets"

[<Literal>]
let SolutionFolderProjectGuid = "2150E333-8FDC-42A3-9474-1A3956D46DE8"
Expand Down
75 changes: 39 additions & 36 deletions src/Paket.Core/ProjectFile.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 ->
Expand All @@ -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<NormalizedPackageName,InstallModel>, usedPackages : Set<NormalizedPackageName>, 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)

Expand Down
10 changes: 4 additions & 6 deletions tests/Paket.Tests/InstallModel/Xml/Fantomas.fs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ let fullDoc = """<?xml version="1.0" encoding="utf-16"?>
let docWithTargets = """<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<Import Project="..\..\packages\Fantomas\generated.paket.targets" Condition="Exists('..\..\packages\Fantomas\generated.paket.targets')" />
<Import Project="Empty.paket.targets" Condition="Exists('Empty.paket.targets')" />
</Project>"""

[<Test>]
Expand All @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit 44ddd68

Please sign in to comment.