CodeGuard/windows/install-all.bat

188 lines
5.3 KiB
Batchfile

@echo off
chcp 65001 >nul
echo ========================================
echo CodeGuard All-in-One Installer
echo For Windows
echo ========================================
echo.
goto main
:error_exit
echo.
echo [ERROR] An error occurred. Please check the error messages above.
echo.
pause
exit /b 1
:main
setlocal
set SCRIPT_DIR=%~dp0..\
set SOURCE_DIR=%SCRIPT_DIR%installations
set USER_HOME=%USERPROFILE%
set SKILL_DIR=%USER_HOME%\.agents\skills\semgrep
set OPENCODE_DIR=%USERPROFILE%\.config\opencode
set SEMGREP_VENV=%SOURCE_DIR%\semgrep_offline\semgrep
set SEMGREP_SCRIPTS=%SEMGREP_VENV%\Scripts
echo ========================================
echo Part 1: Semgrep Installation
echo ========================================
echo.
set SEMGREP_OFFLINE_DIR=%SOURCE_DIR%\semgrep_offline
if not exist "%SEMGREP_OFFLINE_DIR%" (
echo ERROR: semgrep_offline directory not found at %SEMGREP_OFFLINE_DIR%
echo Installation cannot continue.
pause
exit /b 1
)
echo [1/4] Creating Python virtual environment...
if not exist "%SEMGREP_VENV%" (
cd /d "%SEMGREP_OFFLINE_DIR%"
python -m venv semgrep
if errorlevel 1 (
echo ERROR: Failed to create virtual environment
goto error_exit
)
echo Virtual environment created at %SEMGREP_VENV%
) else (
echo Virtual environment already exists at %SEMGREP_VENV%
)
echo.
echo [2/4] Installing semgrep...
cd /d "%SEMGREP_OFFLINE_DIR%"
"%SEMGREP_VENV%\Scripts\pip.exe" install --no-index --find-links=./wheels semgrep
if errorlevel 1 (
echo ERROR: Failed to install semgrep
goto error_exit
)
echo.
echo [3/4] Verifying semgrep installation...
"%SEMGREP_VENV%\Scripts\semgrep.exe" --version
if errorlevel 1 (
echo ERROR: Failed to run semgrep
goto error_exit
)
echo.
echo [4/4] Adding semgrep to PATH...
powershell -Command "[Environment]::SetEnvironmentVariable('Path', [Environment]::GetEnvironmentVariable('Path', 'User') + ';%SEMGREP_SCRIPTS%', 'User')"
echo.
echo ========================================
echo Part 2: OpenCode Configuration
echo ========================================
echo.
if not exist "%OPENCODE_DIR%" (
echo [1/2] Creating opencode config directory...
mkdir "%OPENCODE_DIR%"
)
set SOURCE_CONFIG=%SOURCE_DIR%\opencode-cli\opencode.json
if exist "%SOURCE_CONFIG%" (
echo [2/2] Copying opencode.json to %OPENCODE_DIR%...
copy /Y "%SOURCE_CONFIG%" "%OPENCODE_DIR%\opencode.json"
echo Configuration copied successfully!
) else (
echo WARNING: Source config not found at %SOURCE_CONFIG%
)
echo.
echo ========================================
echo Part 3: Semgrep Skill Installation
echo ========================================
echo.
echo [1/4] Creating skill directory...
if not exist "%SKILL_DIR%" mkdir "%SKILL_DIR%"
echo Created: %SKILL_DIR%
echo.
echo [2/4] Copying SKILL files...
set SEMGREP_SKILL_SOURCE=%SOURCE_DIR%\semgrep_skill
copy /Y "%SEMGREP_SKILL_SOURCE%\SKILL.md" "%SKILL_DIR%\SKILL.md"
if not exist "%SKILL_DIR%\references" mkdir "%SKILL_DIR%\references"
if exist "%SEMGREP_SKILL_SOURCE%\references" copy /Y "%SEMGREP_SKILL_SOURCE%\references\*.*" "%SKILL_DIR%\references\"
if not exist "%SKILL_DIR%\workflows" mkdir "%SKILL_DIR%\workflows"
if exist "%SEMGREP_SKILL_SOURCE%\workflows" copy /Y "%SEMGREP_SKILL_SOURCE%\workflows\*.*" "%SKILL_DIR%\workflows\"
if not exist "%SKILL_DIR%\scripts" mkdir "%SKILL_DIR%\scripts"
if exist "%SEMGREP_SKILL_SOURCE%\scripts" copy /Y "%SEMGREP_SKILL_SOURCE%\scripts\*.*" "%SKILL_DIR%\scripts\"
echo Copied all skill files
echo.
echo [3/4] Copying offline rules...
set SEMGREP_RULES_SOURCE=%SOURCE_DIR%\semgrep-offline-rules
if exist "%SEMGREP_RULES_SOURCE%" (
if not exist "%SKILL_DIR%\semgrep-offline-rules" (
xcopy /E /I /Y "%SEMGREP_RULES_SOURCE%" "%SKILL_DIR%\semgrep-offline-rules"
)
echo Rules copied to: %SKILL_DIR%\semgrep-offline-rules
) else (
echo WARNING: semgrep-offline-rules not found
)
echo.
echo [4/4] Semgrep verification...
if exist "%SKILL_DIR%\SKILL.md" (
echo [OK] SKILL.md
) else (
echo [FAIL] SKILL.md
)
if exist "%SKILL_DIR%\references\rulesets.md" (
echo [OK] references
) else (
echo [FAIL] references
)
if exist "%SKILL_DIR%\workflows\scan-workflow.md" (
echo [OK] workflows
) else (
echo [FAIL] workflows
)
echo.
echo ========================================
echo Part 4: Environment Setup
echo ========================================
echo.
echo Setting up OpenCode offline mode...
setx OPENCODE_OFFLINE 1 >nul
setx OPENCODE_DISABLE_MODELS_FETCH 1 >nul
echo Adding opencode to PATH (via PowerShell to avoid 1024 char limit)...
powershell -Command "[Environment]::SetEnvironmentVariable('Path', [Environment]::GetEnvironmentVariable('Path', 'User') + ';%SOURCE_DIR%\opencode-cli', 'User')"
echo Setting up Semgrep environment variables...
setx SEMGREP_OFFLINE_RULES_PATH "%SKILL_DIR%\semgrep-offline-rules\third-party" >nul
setx SEMGREP_OFFLINE "true" >nul
echo Setting up Python UTF-8 mode...
setx PYTHONUTF8 "1" >nul
echo.
echo ========================================
echo Installation Complete!
echo ========================================
echo.
echo OpenCode config: %OPENCODE_DIR%
echo Skill location: %SKILL_DIR%
echo.
echo Environment variables have been set for your user account.
echo Please restart your terminal or VSCode to apply them.
echo.
endlocal
pause