Last active
February 27, 2023 17:05
-
-
Save exoosh/1c05431e8ddfa307cb0205ae5a68e58e to your computer and use it in GitHub Desktop.
How to detect Visual Studio installation path from Git Bash, Cygwin, MSYS2 etc.
This file contains 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
#!/usr/bin/env bash | |
[[ -t 1 ]] && { cG="\e[1;32m"; cR="\e[1;31m"; cB="\e[1;34m"; cW="\e[1;37m"; cY="\e[1;33m"; cG_="\e[0;32m"; cR_="\e[0;31m"; cB_="\e[0;34m"; cW_="\e[0;37m"; cY_="\e[0;33m"; cZ="\e[0m"; export cR cG cB cY cW cR_ cG_ cB_ cY_ cW_ cZ; } | |
for tool in env gawk tr; do type $tool > /dev/null 2>&1 || { echo -e "${cR}ERROR:${cZ} couldn't find '$tool' which is required by this script."; exit 1; }; done | |
function VsWhereLatest | |
{ | |
for tool in cygpath; do type $tool > /dev/null 2>&1 || { echo -e "${cR}ERROR:${cZ} couldn't find '$tool' which is required by this script."; exit 1; }; done | |
# Bash chokes on variable names with embedded parentheses, hence this mumbo jumbo | |
local PF86="$(cygpath -u "$(env|tr -d '\r'|gawk -F= '$1 == "ProgramFiles(x86)" {print $2}')")" | |
# This is a well-known path that _can_ legitimately be hardcoded as per Microsoft! | |
local VSWHERE="$PF86/Microsoft Visual Studio/Installer/vswhere.exe" | |
echo -e "${cW}VSWHERE${cZ}=$VSWHERE" > /dev/stderr | |
if [[ -f "$VSWHERE" && -x "$VSWHERE" ]]; then | |
# Result of the following line will be something like: C:\Program Files\Microsoft Visual Studio\2022\Professional | |
local WIN_VSINSTPATH="$(set -x; "$VSWHERE" -products \* -format value -property installationPath -latest|tr -d '\r')" | |
# Result of the following line will be something like: /c/Program Files/Microsoft Visual Studio/2022/Professional | |
# Note: depending on your environment a prefix like /cygdrive may have been prepended as well. | |
local VSINSTPATH="$(cygpath -u "$WIN_VSINSTPATH")" | |
fi | |
echo "$VSINSTPATH" | |
} | |
VSINSTPATH=$(VsWhereLatest) | |
if [[ -n "$VSINSTPATH" ]]; then | |
echo -e "${cW}VSINSTPATH${cZ}=$VSINSTPATH" | |
else | |
echo -e "${cR}ERROR:${cZ} Could not determine path of latest Visual Studio installation." | |
exit 1 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Small update to tackle the
\r
in the output ofvswhere.exe
and delete that for further use in a variable.