Skip to content

Instantly share code, notes, and snippets.

@sovanesyan
Created July 26, 2012 08:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sovanesyan/3180872 to your computer and use it in GitHub Desktop.
Save sovanesyan/3180872 to your computer and use it in GitHub Desktop.
Nuget Package Add Import and Errors
# Need to load MSBuild assembly if it's not loaded yet.
Add-Type -AssemblyName 'Microsoft.Build, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
# Grab the loaded MSBuild project for the project
$msbuild = [Microsoft.Build.Evaluation.ProjectCollection]::GlobalProjectCollection.GetLoadedProjects($project.FullName) | Select-Object -First 1
# Make the path to the targets file relative.
$projectUri = new-object Uri('file://' + $project.FullName)
$targetUri = new-object Uri('file://' + $targetsFile)
$relativePath = $projectUri.MakeRelativeUri($targetUri).ToString().Replace([System.IO.Path]::AltDirectorySeparatorChar, [System.IO.Path]::DirectorySeparatorChar)
$existsCondition = "Exists('" + $relativePath + "')"
#Add the enhancer item definition
# Add the import
$importElement = $msbuild.Xml.AddImport($relativePath)
$importElement.Condition = $existsCondition
#hook the error task
$beforeBuildTask = $msbuild.Xml.Targets | Where {$_.Name -eq "BeforeBuild"}| Select-Object -First 1
if( $beforeBuildTask -eq $null)
{
$beforeBuildTask = $msbuild.Xml.AddTarget("BeforeBuild")
}
$errorTask = $hookTarget.AddTask("Error")
$errorTask.SetParameter("Text", "SomeText")
# Save the project
$project.Save()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment