Last active
June 4, 2018 21:36
-
-
Save jayotterbein/438ae58b5ab9f64399ea to your computer and use it in GitHub Desktop.
Open solution file in current directory with most recent visual studio
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Function sln([string]$hintPath = $null) | |
{ | |
[string[]]$installed = @(gci HKLM:\Software\Microsoft\VisualStudio\) |% { $_.Name } |% { Split-Path $_ -Leaf } | |
$versions = @() | |
foreach ($i in $installed) | |
{ | |
[Version]$v = "0.0" | |
if ([Version]::TryParse($i, [ref]$v)) | |
{ | |
$versions += $v | |
} | |
} | |
$versions = $versions | sort -Descending | |
[string]$maxVersion = $versions[0] | |
$maxVersionRegPath = "HKLM:\Software\Microsoft\VisualStudio\$maxVersion" | |
$maxVersionInstallPath = (Get-ItemProperty $maxVersionRegPath).InstallDir | |
if (-not $maxVersionInstallPath -or !(Test-Path $maxVersionInstallPath)) { | |
$maxVersionInstallPath = (Join-Path ${env:ProgramFiles(x86)} "Microsoft Visual Studio $maxVersion\Common7\IDE") | |
} | |
for ($vs2017Ver = 2020; $vs2017Ver -ge 2017; $vs2017Ver--) { | |
foreach ($vs2017Type in @('Enterprise', 'Professional', 'Community')) { | |
$vs2017VerPath = (Join-Path ${env:ProgramFiles(x86)} "Microsoft Visual Studio\$vs2017Ver\$vs2017Type\Common7\IDE") | |
if (Test-Path (Join-Path $vs2017VerPath "devenv.exe")) { | |
$maxVersionInstallPath = $vs2017VerPath | |
break | |
} | |
} | |
} | |
$devenv = (Join-Path $maxVersionInstallPath "devenv.exe") | |
if (!(Test-Path $devenv)) | |
{ | |
Write-Error "Unable to locate devenv: $devenv" | |
} | |
else | |
{ | |
if ([string]::IsNullOrEmpty($hintPath)) | |
{ | |
$searchPath = Resolve-Path '.' | |
if (Get-Command 'Get-GitDirectory' -ErrorAction SilentlyContinue) | |
{ | |
$gitDirectory = Get-GitDirectory | |
if ((-not [string]::IsNullOrEmpty($gitDirectory)) -and (Test-Path $gitDirectory)) | |
{ | |
$searchPath = Resolve-Path (Split-Path ($gitDirectory)) | |
} | |
} | |
$defaultHintPaths = @("src") | |
foreach ($hp in $defaultHintPaths) { | |
$p = Join-Path $searchPath $hp | |
if (Test-Path $p) { | |
if (gci $p *.sln -Recurse -ErrorAction SilentlyContinue) { | |
$searchPath = Resolve-Path $p | |
break | |
} | |
} | |
} | |
} | |
else | |
{ | |
Write-Host "Using hint path: " $hintPath | |
$searchPath = Resolve-Path $hintPath | |
} | |
$solutions = @(gci $searchPath *.sln -Recurse) | |
if ($solutions.Count -eq 0) | |
{ | |
Write-Error "Unable to find any solution files in $searchPath" | |
} | |
else | |
{ | |
$sln = Resolve-Path $solutions[0].FullName | |
Write-Host "Opening first solution file: $sln" | |
pushd (Split-Path $sln) | |
Start-Process cmd -ArgumentList @('/c', 'set GIT_DIR=.no_git_for_vs', '&', 'start', '""', ('"' + $devenv + '"'), '/nosplash', ('"' + $sln + '"')) -Verb runAs | |
popd | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment