Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dtbiedrzycki/815e8c79bee9489658ad to your computer and use it in GitHub Desktop.
Save dtbiedrzycki/815e8c79bee9489658ad to your computer and use it in GitHub Desktop.
Get-VSSolutionReferences.ps1
#requires -version 2.0
[CmdletBinding()]
param (
[parameter(Mandatory=$true)]
[ValidateScript({ Test-Path -Path $_ -PathType Leaf })]
[string]
$Path
)
$ErrorActionPreference = 'Stop'
Set-StrictMode -Version Latest
$Path = ($Path | Resolve-Path).ProviderPath
$SolutionRoot = $Path | Split-Path
# List of VS Project Type GUIDs
# https://www.codeproject.com/Reference/720512/List-of-Visual-Studio-Project-Type-GUIDs
# C#
# Visual C++
# F#
# Portable Class Library
# VB.NET
$SolutionProjectPattern = @"
(?x)
^ Project \( " \{ FAE04EC0-301F-11D3-BF4B-00C04F79EFBC \} " \)
^ Project \( " \{ 8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942 \} " \)
^ Project \( " \{ F2A71F9B-5D33-465A-A702-920D77279786 \} " \)
^ Project \( " \{ 786C830F-07A1-408B-BD7F-6EE04809D6DB \} " \)
^ Project \( " \{ F184B08F-C81C-45F6-A57F-5ABD9991F28F \} " \)
\s* = \s*
" (?<name> [^"]* ) " , \s+
" (?<path> [^"]* ) " , \s+
"@
Get-Content -Path $Path |
ForEach-Object {
if ($_ -match $SolutionProjectPattern) {
$ProjectPath = $SolutionRoot | Join-Path -ChildPath $Matches['path']
$ProjectPath = ($ProjectPath | Resolve-Path).ProviderPath
$ProjectRoot = $ProjectPath | Split-Path
[xml]$Project = Get-Content -Path $ProjectPath
$nm = New-Object -TypeName System.Xml.XmlNamespaceManager -ArgumentList $Project.NameTable
$nm.AddNamespace('x', 'http://schemas.microsoft.com/developer/msbuild/2003')
# Get references
$DirectReferences = $Project.SelectNodes('/x:Project/x:ItemGroup/x:Reference', $nm) |
ForEach-Object {
$AssemblyInfo = New-Object -TypeName Reflection.AssemblyName -ArgumentList $_.Include
[pscustomobject]@{
ProjectPath = $ProjectPath
ReferenceType = "DLL"
Name = $AssemblyInfo.Name
AssemblyPath = $(if (($_ | Get-Member) | ?{$_.Name -eq "HintPath"}) { $_.HintPath } else { "" })
} |
Add-Member -Name Exists -MemberType ScriptMethod -Value {
try {
return Test-Path -Path $this.Path -PathType Leaf
} catch {
return $false
}
} -PassThru
}
# Get project references
$ProjectReferences = $Project.SelectNodes('/x:Project/x:ItemGroup/x:ProjectReference', $nm) |
ForEach-Object {
[pscustomobject]@{
ProjectPath = $ProjectPath
ReferenceType = "Project"
Name = $_.Name
AssemblyPath = $_.Include
} |
Add-Member -Name Exists -MemberType ScriptMethod -Value {
try {
return Test-Path -Path $this.Path -PathType Leaf
} catch {
return $false
}
} -PassThru
}
$AllReferences = $DirectReferences + $ProjectReferences
$AllReferences
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment