Install.ps1 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. param($installPath, $toolsPath, $package, $project)
  2. # This is the MSBuild targets file to add
  3. $targetsFile = [System.IO.Path]::Combine($toolsPath, $package.Id + '.targets')
  4. # Need to load MSBuild assembly if it's not loaded yet.
  5. Add-Type -AssemblyName 'Microsoft.Build, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
  6. # Grab the loaded MSBuild project for the project
  7. $msbuild = [Microsoft.Build.Evaluation.ProjectCollection]::GlobalProjectCollection.GetLoadedProjects($project.FullName) | Select-Object -First 1
  8. # Make the path to the targets file relative.
  9. $projectUri = new-object Uri($project.FullName, [System.UriKind]::Absolute)
  10. $targetUri = new-object Uri($targetsFile, [System.UriKind]::Absolute)
  11. $relativePath = [System.Uri]::UnescapeDataString($projectUri.MakeRelativeUri($targetUri).ToString()).Replace([System.IO.Path]::AltDirectorySeparatorChar, [System.IO.Path]::DirectorySeparatorChar)
  12. # Add the import with a condition, to allow the project to load without the targets present.
  13. $import = $msbuild.Xml.AddImport($relativePath)
  14. $import.Condition = "Exists('$relativePath')"
  15. # Add a target to fail the build when our targets are not imported
  16. $target = $msbuild.Xml.AddTarget("EnsureBclBuildImported")
  17. $target.BeforeTargets = "BeforeBuild"
  18. $target.Condition = "'`$(BclBuildImported)' == ''"
  19. # if the targets don't exist at the time the target runs, package restore didn't run
  20. $errorTask = $target.AddTask("Error")
  21. $errorTask.Condition = "!Exists('$relativePath')"
  22. $errorTask.SetParameter("Text", "This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=317567.");
  23. $errorTask.SetParameter("HelpKeyword", "BCLBUILD2001");
  24. # if the targets exist at the time the target runs, package restore ran but the build didn't import the targets.
  25. $errorTask = $target.AddTask("Error")
  26. $errorTask.Condition = "Exists('$relativePath')"
  27. $errorTask.SetParameter("Text", "The build restored NuGet packages. Build the project again to include these packages in the build. For more information, see http://go.microsoft.com/fwlink/?LinkID=317568.");
  28. $errorTask.SetParameter("HelpKeyword", "BCLBUILD2002");
  29. $project.Save()