rasc_version.bat 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. @echo off
  2. REM RASC version handler 2024-08-05
  3. setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
  4. REM Initialisations
  5. set "RascVersionFileHeader=# RASC version and installation file"
  6. set "RascDescRootKey=SOFTWARE\Renesas\RASC\Installations"
  7. set "VersionUnknown=Unknown"
  8. set "RascVersionValueName=Version"
  9. set "RascExeValueName=ExePath"
  10. set "RascSearchPath=C:\Renesas"
  11. set /a NumRascs=0
  12. set "TargetRascVersion="
  13. set "TargetRascExe="
  14. set "TargetRascVersionDiffers="
  15. REM First parameter is (possibly non-existent) file containing RASC version to invoke
  16. set "RascVersionFile=%~1"
  17. REM Second parameter specifies non-interactive mode
  18. set "NonInteractiveMode=%~2"
  19. REM Extract specific RASC version from file
  20. REM echo "%RascVersionFile%"
  21. if exist "%RascVersionFile%" (
  22. REM echo DEBUG: Have version file: "%RascVersionFile%"
  23. set /a idx=0
  24. for /f "usebackq tokens=*" %%a in ("%RascVersionFile%") do (
  25. if !idx! EQU 0 (
  26. if not "%%a" == "%RascVersionFileHeader%" (
  27. REM echo DEBUG: Header doesn't match
  28. goto _EndVersionFileParse
  29. )
  30. )
  31. if !idx! EQU 1 (
  32. set "TargetRascVersion=%%a"
  33. )
  34. if !idx! EQU 2 (
  35. set "TargetRascExe=%%a"
  36. )
  37. set /a idx+=1
  38. )
  39. )
  40. :_EndVersionFileParse
  41. REM echo DEBUG: Target version: "%TargetRascVersion%"
  42. REM echo DEBUG: Target exe: "%TargetRascExe%"
  43. REM Search through registry RASC descriptions for match on exe path and version
  44. for %%h in (HKCU HKLM) do (
  45. for %%v in (32 64) do (
  46. for /f "usebackq skip=1 tokens=*" %%a in (`reg query "%%h\%RascDescRootKey%" /reg:%%v 2^>nul`) do (
  47. set "RascDescKey=%%a"
  48. set "RascVersion="
  49. set "RascExe="
  50. REM echo DEBUG: Desc Key: !RascDescKey!
  51. for /f "usebackq skip=2 tokens=3" %%b in (`reg query "!RascDescKey!" /v "%RascVersionValueName%" /reg:%%v 2^>nul`) do (
  52. set "RascVersion=%%b"
  53. )
  54. REM echo DEBUG: Version: !RascVersion!
  55. for /f "usebackq skip=2 tokens=2*" %%b in (`reg query "!RascDescKey!" /v "%RascExeValueName%" /reg:%%v 2^>nul`) do (
  56. REM %%b is value name, so %%c is the value - supports values with spaces
  57. set "RascExe=%%c"
  58. )
  59. REM echo DEBUG: Exe: !RascExe!
  60. if not defined RascExe (
  61. REM Error - unable to extract executable
  62. set ErrorMessage=Unable to extract RASC executable path from the registry
  63. goto _Error
  64. )
  65. REM Check if exe exists, otherwise assume it's been removed
  66. if exist "!RascExe!" (
  67. REM Check for specified target version and exe path match
  68. if defined RascVersion (
  69. if defined TargetRascVersion (
  70. if /i "!RascExe!" == "%TargetRascExe%" (
  71. REM echo "!RascVersion!"
  72. REM echo "%TargetRascVersion%"
  73. if "!RascVersion!" == "%TargetRascVersion%" (
  74. REM echo DEBUG: Found match
  75. goto _RascVersionRewrite
  76. ) else (
  77. REM Indicate target RASC has a different version than
  78. REM the registry entry. In this case, target RASC has
  79. REM changed, so possibly prompt the user to select a
  80. REM RASC again
  81. set "TargetRascVersionDiffers=true"
  82. )
  83. )
  84. )
  85. ) else (
  86. REM Error - unable to extract version
  87. set ErrorMessage=Unable to extract RASC version from the registry
  88. goto _Error
  89. )
  90. call :SubAddFoundRasc "!RascExe!" "!RascVersion!"
  91. )
  92. )
  93. )
  94. )
  95. REM If target RASC exists and doesn't differ from the registry version (i.e.
  96. REM was not found in the registry), just run it
  97. if defined TargetRascExe (
  98. if exist "%TargetRascExe%" (
  99. if not defined TargetRascVersionDiffers (
  100. set "RascExe=%TargetRascExe%"
  101. set "RascVersion=%VersionUnknown%"
  102. goto _RascVersionRewrite
  103. )
  104. )
  105. )
  106. if %NumRascs% EQU 0 (
  107. REM No entries found in the registry, search C:\Renesas\ as fallback
  108. echo/
  109. echo Searching in "%RascSearchPath%" for RA Smart Configurator installations ...
  110. for /f "usebackq tokens=*" %%a in (`dir "%RascSearchPath%\rasc.exe" /s /b 2^>nul`) do (
  111. if not "%%a" == "" (
  112. call :SubAddFoundRasc "%%a" "%VersionUnknown%"
  113. )
  114. )
  115. )
  116. if %NumRascs% EQU 0 (
  117. REM Still no RASCs found - give up
  118. set ErrorMessage=No "RA Smart Configurator" installations found, download one from renesas.com
  119. goto _Error
  120. )
  121. if %NumRascs% EQU 1 (
  122. set "RascExe=%RascExeList[0]%"
  123. set "RascVersion=%RascVersionList[0]%"
  124. goto _RascVersionRewrite
  125. )
  126. REM Exit with status 1 if choice required in non-interactive mode
  127. if not "%NonInteractiveMode%"=="" exit /b 1
  128. REM Prompt for user to choose from multiple RASCs
  129. echo/
  130. echo Multiple RA Smart Configurators installed:
  131. set /a RascIdxMax=%NumRascs% - 1
  132. set Choices=""
  133. for /l %%a in (0,1,%RascIdxMax%) do (
  134. echo %%a: Version !RascVersionList[%%a]! ^("!RascExeList[%%a]!"^)
  135. set "Choices=!Choices!%%a"
  136. )
  137. echo/
  138. set /a ChosenIdx=%NumRascs%
  139. if %RascIdxMax% GTR 9 (
  140. set /p InputIdx=Select which one to run [0-%RascIdxMax%]?
  141. REM Check if the input string is a number
  142. set "NonNumber=" & for /f "delims=0123456789" %%i in ("!InputIdx!") do set "NonNumber=%%i"
  143. if not defined NonNumber (
  144. set /a ChosenIdx=!InputIdx!
  145. )
  146. ) else (
  147. choice /c %Choices% /m "Select which one to run"
  148. set /a ChosenIdx=!ERRORLEVEL! - 1
  149. )
  150. if %ChosenIdx% GEQ %NumRascs% (
  151. REM Out of range
  152. set ErrorMessage=Invalid selection
  153. goto _Error
  154. )
  155. set "RascExe=!RascExeList[%ChosenIdx%]!"
  156. set "RascVersion=!RascVersionList[%ChosenIdx%]!"
  157. :_RascVersionRewrite
  158. REM Carefully re-write specific version file, if required
  159. if exist "%RascVersionFile%" (
  160. if not defined TargetRascVersion (
  161. if not defined TargetRascExe (
  162. REM Unexpected version file contents, skip rewriting
  163. goto _EndRascVersionRewrite
  164. )
  165. )
  166. )
  167. if "!RascVersion!" == "%TargetRascVersion%" (
  168. if /i "!RascExe!" == "%TargetRascExe%" (
  169. REM Version file already up-to-date, skip rewriting
  170. goto _EndRascVersionRewrite
  171. )
  172. )
  173. echo %RascVersionFileHeader%>"%RascVersionFile%"
  174. echo %RascVersion%>>"%RascVersionFile%"
  175. echo %RascExe%>>"%RascVersionFile%"
  176. :_EndRascVersionRewrite
  177. goto :EOF
  178. REM Add specified RASC to pseudo-list
  179. REM Parameters:
  180. REM 1: RascExe
  181. REM 2: RascVersion
  182. :SubAddFoundRasc
  183. set "RascExeList[%NumRascs%]=%~1"
  184. set "RascVersionList[%NumRascs%]=%~2"
  185. set /a NumRascs+=1
  186. goto :EOF
  187. :_Error
  188. echo/
  189. echo %ErrorMessage%
  190. if "%NonInteractiveMode%"=="" pause
  191. exit /b 1