build_opencode.bat 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. @echo off
  2. setlocal EnableExtensions EnableDelayedExpansion
  3. rem Build opencode for multiple platforms and place binaries in both JetBrains and VSCode plugin resources.
  4. pushd "%~dp0\..\.."
  5. set "ROOT_DIR=%CD%"
  6. popd
  7. set "OPENCODE_DIR=%ROOT_DIR%\packages\opencode"
  8. set "DIST_DIR=%OPENCODE_DIR%\dist"
  9. set "JETBRAINS_BIN_DIR=%ROOT_DIR%\hosts\jetbrains-plugin\src\main\resources\bin"
  10. set "VSCODE_BIN_DIR=%ROOT_DIR%\hosts\vscode-plugin\resources\bin"
  11. if not exist "%OPENCODE_DIR%" (
  12. echo Error: opencode package directory not found at %OPENCODE_DIR% 1>&2
  13. exit /b 1
  14. )
  15. where bun >nul 2>nul
  16. if errorlevel 1 (
  17. echo Error: bun command not found in PATH 1>&2
  18. exit /b 1
  19. )
  20. call :prepare_output_dir "%JETBRAINS_BIN_DIR%"
  21. call :prepare_output_dir "%VSCODE_BIN_DIR%"
  22. if not defined OPENCODE_VERSION (
  23. for /f "delims=" %%V in ('node -p "require('%OPENCODE_DIR:\=/%/package.json').version"') do set "OPENCODE_VERSION=%%V"
  24. )
  25. echo => Building opencode distribution (version %OPENCODE_VERSION%)
  26. pushd "%OPENCODE_DIR%"
  27. bun script/build.ts
  28. if errorlevel 1 (
  29. popd
  30. exit /b 1
  31. )
  32. popd
  33. if not exist "%DIST_DIR%" (
  34. echo Error: expected dist directory not found at %DIST_DIR% 1>&2
  35. exit /b 1
  36. )
  37. set "FOUND_DIST=false"
  38. for /d %%D in ("%DIST_DIR%\opencode-*") do (
  39. call :process_dist "%%~fD"
  40. )
  41. if /I "%FOUND_DIST%"=="false" (
  42. echo Error: no opencode distribution folders found in %DIST_DIR% 1>&2
  43. exit /b 1
  44. )
  45. echo.
  46. echo All done. Binaries placed under:
  47. echo JetBrains: %JETBRAINS_BIN_DIR%
  48. echo VSCode: %VSCODE_BIN_DIR%
  49. echo opencode dists remain in %DIST_DIR%
  50. exit /b 0
  51. :process_dist
  52. set "DIST_PATH=%~1"
  53. if not exist "%DIST_PATH%" exit /b
  54. set "DIRNAME=%~nx1"
  55. set "SUFFIX=%DIRNAME:opencode-=%"
  56. if /I "%SUFFIX%"=="%DIRNAME%" (
  57. echo Warning: skipping unrecognised dist folder %DIRNAME% 1>&2
  58. exit /b
  59. )
  60. set "OS="
  61. set "ARCH="
  62. for /f "tokens=1* delims=-" %%a in ("%SUFFIX%") do (
  63. set "OS=%%a"
  64. set "ARCH=%%b"
  65. )
  66. if not defined ARCH (
  67. echo Warning: skipping %DIRNAME% due to missing architecture component 1>&2
  68. exit /b
  69. )
  70. if /I "%ARCH%"=="x64-baseline" (
  71. echo Skipping baseline target %DIRNAME%
  72. exit /b
  73. )
  74. set "OS_DIR=%OS%"
  75. if /I "%OS_DIR%"=="darwin" set "OS_DIR=macos"
  76. set "ARCH_DIR=%ARCH%"
  77. if /I "%ARCH_DIR%"=="x64" set "ARCH_DIR=amd64"
  78. set "BINARY_NAME=opencode"
  79. set "BINARY_SRC=%DIST_PATH%\bin\opencode"
  80. if /I "%OS%"=="windows" (
  81. set "BINARY_NAME=opencode.exe"
  82. set "BINARY_SRC=%DIST_PATH%\bin\opencode.exe"
  83. )
  84. if not exist "%BINARY_SRC%" (
  85. echo Warning: binary not found at %BINARY_SRC% 1>&2
  86. exit /b
  87. )
  88. set "JETBRAINS_TARGET=%JETBRAINS_BIN_DIR%\%OS_DIR%\%ARCH_DIR%"
  89. set "VSCODE_TARGET=%VSCODE_BIN_DIR%\%OS_DIR%\%ARCH_DIR%"
  90. if not exist "%JETBRAINS_TARGET%" mkdir "%JETBRAINS_TARGET%"
  91. if not exist "%VSCODE_TARGET%" mkdir "%VSCODE_TARGET%"
  92. copy /Y "%BINARY_SRC%" "%JETBRAINS_TARGET%\%BINARY_NAME%" >nul
  93. copy /Y "%BINARY_SRC%" "%VSCODE_TARGET%\%BINARY_NAME%" >nul
  94. echo => Prepared binaries for %OS%/%ARCH%
  95. set "FOUND_DIST=true"
  96. exit /b
  97. :prepare_output_dir
  98. set "TARGET=%~1"
  99. if "%TARGET%"=="" (
  100. echo Error: output directory path is empty 1>&2
  101. exit /b 1
  102. )
  103. if exist "%TARGET%" rd /s /q "%TARGET%"
  104. mkdir "%TARGET%"
  105. exit /b 0