Skip to content

Instantly share code, notes, and snippets.

@m5knt
Last active March 28, 2023 01:51
Show Gist options
  • Save m5knt/0271f300a1d69be24506a06a435ca8f1 to your computer and use it in GitHub Desktop.
Save m5knt/0271f300a1d69be24506a06a435ca8f1 to your computer and use it in GitHub Desktop.
Automatically finds and executes sh.exe
@echo off
call :MAIN %* <nul
exit /b
:MAIN
if not exist "%BAT_SH_BAT%" call :AUTO_DETECTOR
if not exist "%BAT_SH_BAT%" (
echo sh not found
echo please install "Git for Windows" or "MSYS2" or "Visual Studio 2017 later"
echo see https://gitforwindows.org/
exit /b 1
)
"%BAT_SH_BAT%" -c "sh %*" <con
exit /b
::
::
::
:AUTO_DETECTOR
set BAT_SH_BAT=
call :SEARCH_PATH
if defined BAT_SH_BAT exit /b
rem if "%VisualStudioVersion%" == "17.0" call :SEARCH_VS2022
rem if "%VisualStudioVersion%" == "16.0" call :SEARCH_VS2019
rem if "%VisualStudioVersion%" == "15.0" call :SEARCH_VS2017
rem if defined BAT_SH_BAT exit /b
call :SEARCH_GITFORWIN
if defined BAT_SH_BAT exit /b
call :SEARCH_MSYS2
if defined BAT_SH_BAT exit /b
call :SEARCH_VS2022
if defined BAT_SH_BAT exit /b
call :SEARCH_VS2019
if defined BAT_SH_BAT exit /b
call :SEARCH_VS2017
if defined BAT_SH_BAT exit /b
exit /b
:SEARCH_PATH
where /q dash.exe
if "%ERRORLEVEL%" == "0" (
for /f "delims=" %%a in ('where dash.exe') do (
set "BAT_SH_BAT=%%a"
exit /b
)
)
exit /b
:SEARCH_GITFORWIN
rem tipical location "C:\Program Files\Git"
call :SEARCH "HKLM\SOFTWARE\GitForWindows" "InstallPath" ^
"" "usr\bin\dash.exe"
exit /b
:SEARCH_MSYS2
rem tipical location "C:\msys64"
call :SEARCH "HKCU\Software\Microsoft\Windows\CurrentVersion\Uninstall\{01fb27a8-dc97-429a-820c-95095bd6ab7d}" "InstallLocation" ^
"" "usr\bin\dash.exe"
exit /b
:SEARCH_VS2017
rem tipical location "C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional"
rem vs15.9.53/mingw32
call :SEARCH "HKLM\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\47121d9d" "InstallLocation" ^
"Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer\Git" "usr\bin\dash.exe"
exit /b
:SEARCH_VS2019
rem tipical location "C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional"
rem vs16.11.25/mingw32
call :SEARCH "HKLM\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\53313fde" "InstallLocation" ^
"Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer\Git" "usr\bin\dash.exe"
exit /b
:SEARCH_VS2022
rem tipical location "C:\Program Files\Microsoft Visual Studio\2022\Professional"
rem vs17.5.3/mingw64
call :SEARCH "HKLM\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\900f21fc" "InstallLocation" ^
"Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer\Git" "usr\bin\dash.exe"
exit /b
:SEARCH
:: %1 registory path
:: %2 registory key
:: %3 relative to base
:: %4 relative to exe
:: return %BAT_SH_BAT%
setlocal
call :REG_QUERY %*
if "%RETVAL%" == "" exit /b
for /f "delims=" %%a in ("%RETVAL%") do (
endlocal
if exist "%%a\mingw64\bin" (
path "%USERPROFILE%\bin\;%%a\mingw64\bin\;%%a\usr\local\bin\;%%a\usr\bin\;%%a\bin\;%PATH%"
) else (
path "%USERPROFILE%\bin\;%%a\mingw32\bin\;%%a\usr\local\bin\;%%a\usr\bin\;%%a\bin\;%PATH%"
)
set "BAT_SH_BAT=%%a\usr\bin\dash.exe"
exit /b
)
:REG_QUERY
set RETVAL=
reg query %1 /v %2 >nul 2>nul
if not "%ERRORLEVEL%" == "0" exit /b
for /f "tokens=1,2,*" %%a in ('reg query %1 /v %2') do set "RETVAL=%%c"
if not "%~3" == "" (
if "%RETVAL:~-1%" == "\" ( set "RETVAL=%RETVAL%%~3" ) else ( set "RETVAL=%RETVAL%\%~3" )
)
if exist "%RETVAL%\%~4" exit /b
set RETVAL=
exit /b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment