AddAllProjectRefsToSolution.ps1 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <#
  2. .SYNOPSIS
  3. This adds the complete closure of project references to a .sln file
  4. .EXAMPLE
  5. Let's say you have a folder of projects in src/Banana/, and a file src/Banana/Banana.sln.
  6. To traverse the ProjectReference graph to add all dependency projects, run this script:
  7. ./eng/scripts/AddAllProjectRefsToSolution.ps1 -WorkingDir ./src/Banana/
  8. .EXAMPLE
  9. If src/Banana/ has multiple .sln files, use the -sln parameter.
  10. ./eng/scripts/AddAllProjectRefsToSolution.ps1 -WorkingDir ./src/Banana/ -SolutionFile src/Banana/Solution1.sln
  11. #>
  12. [CmdletBinding(PositionalBinding = $false)]
  13. param(
  14. [string]$WorkingDir,
  15. [Alias('sln')]
  16. [string]$SolutionFile
  17. )
  18. $ErrorActionPreference = 'Stop'
  19. $repoRoot = Resolve-Path "$PSScriptRoot/../../"
  20. $listFile = New-TemporaryFile
  21. if (-not $WorkingDir) {
  22. $WorkingDir = Get-Location
  23. }
  24. Push-Location $WorkingDir
  25. try {
  26. if (-not $SolutionFile) {
  27. $slnCount = Get-ChildItem *.sln | Measure
  28. if ($slnCount.count -eq 0) {
  29. Write-Error "Could not find a solution in this directory. Specify one with -sln <PATH>"
  30. exit 1
  31. }
  32. if ($slnCount.count -gt 1) {
  33. Write-Error "Multiple solutions found in this directory. Specify which one to modify with -sln <PATH>"
  34. exit 1
  35. }
  36. $SolutionFile = Get-ChildItem *.sln | select -first 1
  37. }
  38. & "$repoRoot\build.ps1" -projects "$(Get-Location)\**\*.*proj" /t:ShowProjectClosure "/p:ProjectsReferencedOutFile=$listFile"
  39. foreach ($proj in (Get-Content $listFile)) {
  40. & dotnet sln $SolutionFile add $proj
  41. if ($lastexitcode -ne 0) {
  42. Write-Warning "Failed to add $proj to $SolutionFile"
  43. }
  44. }
  45. }
  46. finally {
  47. Pop-Location
  48. rm $listFile -ea ignore
  49. }