Skip to content

Commit

Permalink
Refactor to functional
Browse files Browse the repository at this point in the history
  • Loading branch information
forki authored and isaacabraham committed Nov 23, 2014
1 parent 46b5293 commit 966329f
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 33 deletions.
51 changes: 28 additions & 23 deletions src/Paket.Core/ProjectFile.fs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ type ProjectFile =

member this.Name = FileInfo(this.FileName).Name

member this.CustomReferenceNodes = lazy(
[for node in this.Document |> getDescendants "Reference" do
let isPaket = ref false
for child in node.ChildNodes do
if child.Name = "Paket" then
isPaket := true
if not !isPaket then
yield node]
)

/// Finds all project files
static member FindAllProjects(folder) =
FindAllFiles(folder, "*.*proj")
Expand Down Expand Up @@ -69,11 +79,20 @@ type ProjectFile =
for node in this.Document |> getDescendants name do
let isPaketNode = ref false
for child in node.ChildNodes do
if child.Name = "Paket" then isPaketNode := true
if child.Name = "Paket" then isPaketNode := true

if !isPaketNode then yield node
]

member this.GetFrameworkAssemblies() =
[for node in this.Document |> getDescendants "Reference" do
let hasHintPath = ref false
for child in node.ChildNodes do
if child.Name = "HintPath" then
hasHintPath := true
if not !hasHintPath then
yield node.Attributes.["Include"].InnerText.Split(',').[0] ]

member this.DeletePaketNodes(name) =
let nodesToDelete = this.FindPaketNodes(name)
if nodesToDelete |> Seq.isEmpty |> not then
Expand Down Expand Up @@ -134,33 +153,19 @@ type ProjectFile =
| None ->
let firstNode = fileItemsInSameDir |> Seq.head
firstNode.ParentNode.InsertBefore(paketNode, firstNode) |> ignore

this.DeleteIfEmpty("ItemGroup")

member this.HasCustomNodes(model:InstallModel) =
member this.GetCustomNodes(model:InstallModel) =
let libs = model.GetReferenceNames.Force()

let hasCustom = ref false
for node in this.Document |> getDescendants "Reference" do
if Set.contains (node.Attributes.["Include"].InnerText.Split(',').[0]) libs then
let isPaket = ref false
for child in node.ChildNodes do
if child.Name = "Paket" then
isPaket := true
if not !isPaket then
hasCustom := true

!hasCustom

this.CustomReferenceNodes.Force()
|> List.filter (fun node -> Set.contains (node.Attributes.["Include"].InnerText.Split(',').[0]) libs)

member this.DeleteCustomNodes(model:InstallModel) =
let nodesToDelete = List<_>()
let nodesToDelete = this.GetCustomNodes(model)

let libs = model.GetReferenceNames.Force()
for node in this.Document |> getDescendants "Reference" do
if Set.contains (node.Attributes.["Include"].InnerText.Split(',').[0]) libs then
nodesToDelete.Add node

if nodesToDelete |> Seq.isEmpty |> not then
if nodesToDelete <> [] then
let (PackageName name) = model.PackageName
verbosefn " - Deleting custom projects nodes for %s" name

Expand Down Expand Up @@ -270,7 +275,7 @@ type ProjectFile =
usedPackages
|> Seq.fold (fun (model:InstallModel) kv ->
let installModel = completeModel.[NormalizedPackageName kv.Key]
if this.HasCustomNodes(installModel) then
if this.GetCustomNodes(installModel) <> [] then
traceWarnfn " - custom nodes for %s ==> skipping" ((|PackageName|) kv.Key)
model
else model.MergeWith(installModel))
Expand Down
2 changes: 1 addition & 1 deletion src/Paket/Paket.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<WarningLevel>3</WarningLevel>
<DocumentationFile>
</DocumentationFile>
<StartArguments>install --hard</StartArguments>
<StartArguments>update</StartArguments>
<StartAction>Project</StartAction>
<StartProgram>paket.exe</StartProgram>
<StartWorkingDirectory>
Expand Down
16 changes: 8 additions & 8 deletions tests/Paket.Tests/InstallModel/Xml/ManualNodes.fs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ let ``should find custom nodes in doc``() =
@"..\Fantomas\lib\Fantomas.exe" ],
Nuspec.Explicit ["FantomasLib.dll"])

ProjectFile.Load("./ProjectFile/TestData/CustomFantomasNode.fsprojtest").Value.HasCustomNodes(model)
|> shouldEqual true
ProjectFile.Load("./ProjectFile/TestData/CustomFantomasNode.fsprojtest").Value.GetCustomNodes(model).IsEmpty
|> shouldEqual false


[<Test>]
Expand All @@ -27,8 +27,8 @@ let ``should not find custom nodes if there are none``() =
@"..\Fantomas\lib\Fantomas.exe" ],
Nuspec.Explicit ["FantomasLib.dll"])

ProjectFile.Load("./ProjectFile/TestData/NoCustomFantomasNode.fsprojtest").Value.HasCustomNodes(model)
|> shouldEqual false
ProjectFile.Load("./ProjectFile/TestData/NoCustomFantomasNode.fsprojtest").Value.GetCustomNodes(model).IsEmpty
|> shouldEqual true


[<Test>]
Expand All @@ -42,10 +42,10 @@ let ``should delete custom nodes if there are some``() =

let project = ProjectFile.Load("./ProjectFile/TestData/CustomFantomasNode.fsprojtest").Value

project.HasCustomNodes(model)
|> shouldEqual true
project.GetCustomNodes(model).IsEmpty
|> shouldEqual false

project.DeleteCustomNodes(model)

project.HasCustomNodes(model)
|> shouldEqual false
project.GetCustomNodes(model).IsEmpty
|> shouldEqual true
3 changes: 2 additions & 1 deletion tests/Paket.Tests/Paket.Tests.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,7 @@
<Compile Include="ProjectFile\ConditionSpecs.fs" />
<Compile Include="ProjectFile\FileBuildActionSpecs.fs" />
<Compile Include="ProjectFile\InterProjectDependencySpecs.fs" />
<Compile Include="ProjectFile\FrameworkReferencesSpecs.fs" />
<Compile Include="ProjectFile\OutputSpecs.fs" />
<Compile Include="InstallModel\FrameworkIdentifierSpecs.fs" />
<Compile Include="InstallModel\ProcessingSpecs.fs" />
Expand All @@ -458,7 +459,7 @@
<Compile Include="InstallModel\Xml\SystemNetHttpForNet4.fs" />
<Compile Include="InstallModel\Xml\SystemNetHttpWithFrameworkReferences.fs" />
<Compile Include="InstallModel\Xml\ManualNodes.fs" />
<Compile Include="InstallModel\Xml\FSharp.Data.SqlClient.fs" />
<Compile Include="InstallModel\Xml\FSharp.Data.SqlClient.fs" />
<Compile Include="DependencyModel\ProjectDependencySpecs.fs" />
<None Include="paket.references" />
</ItemGroup>
Expand Down
15 changes: 15 additions & 0 deletions tests/Paket.Tests/ProjectFile/FrameworkReferencesSpecs.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module Paket.FrameworkReferencesSpecs

open Paket
open NUnit.Framework
open FsUnit

[<Test>]
let ``should detect empty framework references in empty project``() =
ProjectFile.Load("./ProjectFile/TestData/Empty.fsprojtest").Value.GetFrameworkAssemblies()
|> shouldEqual []

[<Test>]
let ``should detect references in project1``() =
ProjectFile.Load("./ProjectFile/TestData/Project1.fsprojtest").Value.GetFrameworkAssemblies()
|> shouldEqual ["mscorlib"; "System"]

0 comments on commit 966329f

Please sign in to comment.