Skip to content

Commit

Permalink
when updating from nuget, remove NuGetPackageImportStamp - fixes #1864
Browse files Browse the repository at this point in the history
  • Loading branch information
0x53A committed Aug 21, 2016
1 parent 6341c5c commit 5768e9e
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Paket.Core/NugetConvert.fs
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ let convertProjects nugetEnv =
project.ReplaceNuGetPackagesFile()
project.RemoveNuGetTargetsEntries()
project.RemoveImportAndTargetEntries(packagesConfig.Packages |> List.map (fun p -> p.Id, p.Version))
project.RemoveNuGetPackageImportStamp()
yield project, convertPackagesConfigToReferencesFile project.FileName packagesConfig]

let createPaketEnv rootDirectory nugetEnv credsMirationMode = trial {
Expand Down
25 changes: 25 additions & 0 deletions src/Paket.Core/ProjectFile.fs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ open Paket.Logging
open System
open System.Collections.Generic
open System.IO
open System.Text
open System.Text.RegularExpressions
open System.Xml
open Paket.Xml
Expand Down Expand Up @@ -183,6 +184,12 @@ module ProjectFile =
use stream = fileInfo.OpenRead()
loadFromStream fileInfo.FullName stream

let loadFromString (fullName:string) (text:string) =
use stream =
let bytes = text |> Encoding.UTF8.GetBytes
new MemoryStream (bytes)
loadFromStream fullName stream

let tryLoad(fileName:string) =
try
Some(loadFromFile fileName)
Expand Down Expand Up @@ -1097,6 +1104,20 @@ module ProjectFile =
if not parent.HasChildNodes then
parent.ParentNode.RemoveChild parent |> ignore)

let removeNuGetPackageImportStamp project =
let toDelete =
project.Document
|> getDescendants "PropertyGroup"
|> List.collect (getDescendants "NuGetPackageImportStamp")

toDelete
|> List.iter
(fun node ->
let parent = node.ParentNode
node.ParentNode.RemoveChild node |> ignore
if not parent.HasChildNodes then
parent.ParentNode.RemoveChild parent |> ignore)

let removeImportAndTargetEntries (packages : list<string * SemVerInfo> ) (project:ProjectFile) =
let toDelete =
project.Document
Expand Down Expand Up @@ -1268,6 +1289,8 @@ type ProjectFile with

member this.RemoveNuGetTargetsEntries () = ProjectFile.removeNuGetTargetsEntries this

member this.RemoveNuGetPackageImportStamp () = ProjectFile.removeNuGetPackageImportStamp this

member this.RemoveImportAndTargetEntries (packages : list<string * SemVerInfo> ) = ProjectFile.removeImportAndTargetEntries packages this

member this.OutputType = ProjectFile.outputType this
Expand Down Expand Up @@ -1296,6 +1319,8 @@ type ProjectFile with

static member LoadFromFile(fileName:string) = ProjectFile.loadFromFile fileName

static member LoadFromString(fullName:string, text:string) = ProjectFile.loadFromString fullName text

static member TryLoad(fileName:string) = ProjectFile.tryLoad fileName

static member FindCorrespondingFile (projectFile:FileInfo,correspondingFile:string) =
Expand Down
1 change: 1 addition & 0 deletions tests/Paket.Tests/Paket.Tests.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@
<Compile Include="ProjectFile\OutputSpecs.fs" />
<Compile Include="ProjectFile\ProjectLanguageSpecs.fs" />
<Compile Include="ProjectFile\LocalizationSpecs.fs" />
<Compile Include="ProjectFile\UpdateFromNugetSpecs.fs" />
<Compile Include="InstallModel\FrameworkIdentifierSpecs.fs" />
<Compile Include="InstallModel\ProcessingSpecs.fs" />
<Compile Include="InstallModel\Xml\Fantomas.fs" />
Expand Down
74 changes: 74 additions & 0 deletions tests/Paket.Tests/ProjectFile/UpdateFromNugetSpecs.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
module Paket.ProjectFile.UpdateFromNugetSpecs

open Paket
open NUnit.Framework
open FsUnit
open System.Xml
open System.IO

let convertAndCompare source expectedResult =
let projectFile = ProjectFile.LoadFromString("Test.csproj", source)
ProjectFile.removeNuGetPackageImportStamp projectFile
let actualResult = Utils.normalizeXml projectFile.Document
let normalizedExpected =
let doc = XmlDocument()
doc.LoadXml(expectedResult)
doc |> Utils.normalizeXml
actualResult |> shouldEqual normalizedExpected

[<Test>]
let ``should remove NuGetPackageImportStamp and empty PropertyGroup``() =
let projectFile = """<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
</PropertyGroup>
<PropertyGroup>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
</Project>"""

let expectedResult = """<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
</Project>"""

convertAndCompare projectFile expectedResult

[<Test>]
let ``should remove NuGetPackageImportStamp but not PropertyGroup with items``() =
let projectFile = """<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<WarningLevel>4</WarningLevel>
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
</PropertyGroup>
</Project>"""

let expectedResult = """<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
</Project>"""

convertAndCompare projectFile expectedResult

let testDataRootPath = Path.Combine(__SOURCE_DIRECTORY__, "TestData")
let TestData: obj[][] = [|
for f in Directory.GetFiles testDataRootPath do
let allText = File.ReadAllText f
if not (allText.Contains "NuGetPackageImportStamp") then
yield [| Path.GetFileName f |]
|]


[<Test>]
[<TestCaseSource("TestData")>]
let ``should not modify projects without NuGetPackageImportStamp`` projectFile =
let text = File.ReadAllText (Path.Combine(testDataRootPath, projectFile))
convertAndCompare text text

0 comments on commit 5768e9e

Please sign in to comment.