Skip to content

Commit

Permalink
Fixes to nuget install/uninstall script to use msbuild OM
Browse files Browse the repository at this point in the history
  • Loading branch information
nikhilk committed Jul 21, 2012
1 parent 0648b15 commit 5a3b16b
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 38 deletions.
2 changes: 1 addition & 1 deletion src/Tools/Nuget/Core.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<file src="..\..\..\bin\Release\ssc.exe" target="tools" />
<file src="Core\Install.ps1" target="tools" />
<file src="Core\Uninstall.ps1" target="tools" />
<file src="Core\Cleanup.psm1" target="tools" />
<file src="Core\Reset.psm1" target="tools" />
<file src="Core\ScriptSharp.PlaceHolder.txt" target="content" />
</files>
</package>
8 changes: 0 additions & 8 deletions src/Tools/Nuget/Core/Cleanup.psm1

This file was deleted.

25 changes: 10 additions & 15 deletions src/Tools/Nuget/Core/Install.ps1
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
param($installPath, $toolsPath, $package, $project)

Import-Module (Join-Path $toolsPath "Cleanup.psm1")
Import-Module (Join-Path $toolsPath "Reset.psm1")

function Compute-RelativePath ($basePath, $referencePath) {
$baseUri = New-Object -typename System.Uri -argumentlist $basePath
Expand All @@ -16,23 +16,18 @@ $placeholder = "ScriptSharp.PlaceHolder.txt"
$project.ProjectItems.Item($placeholder).Remove()
Split-Path $project.FullName -parent | Join-Path -ChildPath $placeholder | Remove-Item

# save any out-standing changes to the project first before modifying the project file
$project.Save()

# load project file as xml document
$doc = New-Object System.Xml.XmlDocument
$doc.Load($project.FullName)
$namespace = "http://schemas.microsoft.com/developer/msbuild/2003"
# Get the msbuild object associated with the project
Add-Type -AssemblyName "Microsoft.Build, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
$msbuild = [Microsoft.Build.Evaluation.ProjectCollection]::GlobalProjectCollection.GetLoadedProjects($project.FullName)[0]

# remove any old import reference in case one exists
Cleanup-Project $doc $namespace
# first remove any existing import reference
Reset-Project $msbuild

# add targets import by building a relative reference to targets file in package tools directory
# get a relative reference to the targets file in the package's tools directory
# and add it to the project
$targetsAbsolutePath = Join-Path $toolsPath "ScriptSharp.targets"
$targetsRelativePath = Compute-RelativePath $project.FullName $targetsAbsolutePath
$importNode = $doc.CreateElement('Import', $namespace)
$importNode.SetAttribute('Project', $targetsRelativePath)
$doc.Project.AppendChild($importNode)
$msbuild.Xml.AddImport($targetsRelativePath)

# finally save the project
$doc.Save($project.FullName)
$project.Save()
11 changes: 11 additions & 0 deletions src/Tools/Nuget/Core/Reset.psm1
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function Reset-Project($msbuild) {
$imports = $msbuild.Xml.Imports

if ($imports.Count -gt 0) {
foreach ($import in $imports) {
if ($import.Project.IndexOf("ScriptSharp.targets") -gt 0) {
$msbuild.Xml.RemoveChild($import)
}
}
}
}
21 changes: 7 additions & 14 deletions src/Tools/Nuget/Core/Uninstall.ps1
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
param($installPath, $toolsPath, $package, $project)

Import-Module (Join-Path $toolsPath "Cleanup.psm1")
Import-Module (Join-Path $toolsPath "Reset.psm1")

# save any unsaved changes first
$project.Save()

# load project file as xml document
$doc = New-Object System.Xml.XmlDocument
$doc.Load($project.FullName)
$namespace = "http://schemas.microsoft.com/developer/msbuild/2003"
# Get the msbuild object associated with the project and add an import
Add-Type -AssemblyName "Microsoft.Build, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
$msbuild = [Microsoft.Build.Evaluation.ProjectCollection]::GlobalProjectCollection.GetLoadedProjects($project.FullName)[0]

# remove import reference
Cleanup-Project $doc $namespace
Reset-Project $msbuild

# save changes
$doc.Save($project.FullName)

# reload the project since we made changes to the csproj
$project.DTE.ExecuteCommand("Project.ReloadProject")
# and then save the project
$project.Save()

0 comments on commit 5a3b16b

Please sign in to comment.