diff --git a/README.md b/README.md index 957833f..3343cc7 100644 --- a/README.md +++ b/README.md @@ -88,8 +88,8 @@ The **Custom Prompt** option allows maximum flexibility: ## 🔧 Building from Source ### Prerequisites -- **Visual Studio 2022** with C++ development tools -- **Windows 10 SDK** (latest version) +- **Visual Studio 2022 or later** (any edition, or Build Tools) with the "Desktop development with C++" workload +- **Windows SDK** (installed with that workload) - **Git** (for cloning the repository) ### Build Steps @@ -107,11 +107,13 @@ cd NotepadPlusPlus-LLM-Plugin ### Development Environment The build script automatically: -- Detects Visual Studio 2022 installation +- Finds the newest Visual Studio with the C++ tools (using `vswhere`) - Sets up the build environment -- Compiles with platform toolset v143 +- Compiles with the project's platform toolset (v145), or with the newest installed toolset if v145 is missing (for example v143 on Visual Studio 2022) - Links required Windows libraries +`deploy.bat` builds the same way, then copies the DLL into `C:\Program Files\Notepad++\plugins\LLMAssistant\`. Run it as Administrator. It closes Notepad++ the same way its close button does, so unsaved files are handled as usual: Notepad++ asks whether to save them, or, if session snapshot is on, keeps them in its backup for the next start. It won't copy anything while Notepad++ is still open. + ### Automated Tests The portable request, JSON, and UTF-8 regression tests use Catch2. Configure them with CMake: ```bash diff --git a/build.bat b/build.bat index 126fc5f..d635241 100644 --- a/build.bat +++ b/build.bat @@ -1,64 +1,110 @@ @echo off -echo Building Notepad++ LLM Assistant Plugin with Visual Studio 2022... +setlocal +REM Builds x64\Release\LLMAssistant.dll with the newest Visual Studio (2022 or +REM later, any edition or Build Tools) that has the C++ build tools. +REM build.bat build, then wait for a key press +REM build.bat /nopause build without waiting (used by deploy.bat) -REM Set Visual Studio 2022 environment -set VS2022_PATH="C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\Tools\VsDevCmd.bat" -set VS2022_PRO_PATH="C:\Program Files\Microsoft Visual Studio\2022\Professional\Common7\Tools\VsDevCmd.bat" -set VS2022_ENT_PATH="C:\Program Files\Microsoft Visual Studio\2022\Enterprise\Common7\Tools\VsDevCmd.bat" +set "ROOT=%~dp0" +set "NO_PAUSE=" +if /i "%~1"=="/nopause" set "NO_PAUSE=1" -REM Try to find and setup VS2022 environment -if exist %VS2022_PATH% ( - echo Found Visual Studio 2022 Community - call %VS2022_PATH% -) else if exist %VS2022_PRO_PATH% ( - echo Found Visual Studio 2022 Professional - call %VS2022_PRO_PATH% -) else if exist %VS2022_ENT_PATH% ( - echo Found Visual Studio 2022 Enterprise - call %VS2022_ENT_PATH% -) else ( - echo Visual Studio 2022 not found in standard locations. - echo Please run this from a Visual Studio 2022 Developer Command Prompt. - pause - exit /b 1 +REM Work from the repo folder, also when started elevated (cwd is System32 then) +cd /d "%ROOT%" + +echo Building Notepad++ LLM Assistant Plugin... + +REM Find the newest Visual Studio with the x64 C++ compiler +set "VSWHERE=%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe" +if not exist "%VSWHERE%" ( + echo ERROR: Visual Studio not found: "%VSWHERE%" does not exist. + echo Install Visual Studio 2022 or later with the "Desktop development with C++" workload. + goto :fail +) +set "VS_PATH=" +for /f "usebackq delims=" %%i in (`"%VSWHERE%" -latest -products * -requires Microsoft.VisualStudio.Component.VC.Tools.x86.x64 -property installationPath`) do set "VS_PATH=%%i" +if not defined VS_PATH ( + echo ERROR: No Visual Studio installation with the C++ build tools was found. + echo Add the "Desktop development with C++" workload in the Visual Studio Installer. + goto :fail ) +if not exist "%VS_PATH%\Common7\Tools\VsDevCmd.bat" ( + echo ERROR: "%VS_PATH%\Common7\Tools\VsDevCmd.bat" not found. + goto :fail +) + +echo Using Visual Studio at %VS_PATH% +call "%VS_PATH%\Common7\Tools\VsDevCmd.bat" -no_logo -startdir=none REM Check if MSBuild is available after setting up environment where msbuild >nul 2>nul -if %ERRORLEVEL% NEQ 0 ( - echo MSBuild not found. Please ensure Visual Studio 2022 is properly installed. - pause - exit /b 1 +if errorlevel 1 ( + echo ERROR: MSBuild not found after setting up "%VS_PATH%". + goto :fail ) -echo Using MSBuild from Visual Studio 2022... - -REM Build the plugin with VS2022 toolset -msbuild LLMAssistant.vcxproj /p:Configuration=Release /p:Platform=x64 /p:PlatformToolset=v143 /p:WindowsTargetPlatformVersion=10.0 - -if %ERRORLEVEL% EQU 0 ( - echo. - echo ======================================== - echo BUILD SUCCESSFUL! - echo ======================================== - echo The plugin DLL is located at: x64\Release\LLMAssistant.dll - echo. - echo Installation steps: - echo 1. Create folder: C:\Program Files\Notepad++\plugins\LLMAssistant\ - echo 2. Copy LLMAssistant.dll to that folder - echo 3. Restart Notepad++ - echo 4. Configure your OpenAI API key in Plugins ^> LLM Assistant ^> Settings - echo ======================================== +REM Keep the project's platform toolset when this Visual Studio has it; +REM otherwise build with the newest toolset it does have, e.g. v143 on +REM VS 2022 while the project asks for v145. +set "PROJECT_TOOLSET=" +for /f "tokens=2,3 delims=<>" %%a in ('findstr /c:"" LLMAssistant.vcxproj') do ( + if "%%a"=="PlatformToolset" if not defined PROJECT_TOOLSET set "PROJECT_TOOLSET=%%b" +) +set "TOOLSET_FOUND=" +set "NEWEST_TOOLSET=" +for /d %%v in ("%VS_PATH%\MSBuild\Microsoft\VC\v*") do ( + for /d %%t in ("%%v\Platforms\x64\PlatformToolsets\v*") do call :note_toolset %%~nxt +) +set "TOOLSET_ARG=" +if defined TOOLSET_FOUND ( + echo Platform toolset: %PROJECT_TOOLSET% +) else if defined NEWEST_TOOLSET ( + echo Toolset "%PROJECT_TOOLSET%" from LLMAssistant.vcxproj is not installed, so %NEWEST_TOOLSET% is used instead. + set "TOOLSET_ARG=/p:PlatformToolset=%NEWEST_TOOLSET%" ) else ( - echo. - echo ======================================== - echo BUILD FAILED! - echo ======================================== - echo Please check the error messages above. - echo Make sure you have: - echo - Visual Studio 2022 with C++ development tools - echo - Windows 10 SDK - echo ======================================== + echo ERROR: No C++ platform toolset found under "%VS_PATH%\MSBuild\Microsoft\VC". + goto :fail ) +echo. +msbuild LLMAssistant.vcxproj /p:Configuration=Release /p:Platform=x64 /p:WindowsTargetPlatformVersion=10.0 %TOOLSET_ARG% +if errorlevel 1 goto :fail +if not exist "x64\Release\LLMAssistant.dll" ( + echo ERROR: MSBuild finished but x64\Release\LLMAssistant.dll was not produced. + goto :fail +) + +echo. +echo ======================================== +echo BUILD SUCCESSFUL! +echo ======================================== +echo The plugin DLL is located at: x64\Release\LLMAssistant.dll +if defined NO_PAUSE exit /b 0 +echo. +echo Installation steps: +echo 1. Create folder: C:\Program Files\Notepad++\plugins\LLMAssistant\ +echo 2. Copy LLMAssistant.dll to that folder +echo 3. Restart Notepad++ +echo 4. Configure your OpenAI API key in Plugins ^> LLM Assistant ^> Settings +echo ======================================== pause +exit /b 0 + +:fail +echo. +echo ======================================== +echo BUILD FAILED! +echo ======================================== +echo Please check the error messages above. +echo Make sure you have: +echo - Visual Studio 2022 or later with the "Desktop development with C++" workload +echo - A Windows SDK, which that workload installs +echo ======================================== +if not defined NO_PAUSE pause +exit /b 1 + +REM Records one installed toolset folder name, such as v145 +:note_toolset +if /i "%~1"=="%PROJECT_TOOLSET%" set "TOOLSET_FOUND=1" +if "%~1" GTR "%NEWEST_TOOLSET%" set "NEWEST_TOOLSET=%~1" +exit /b 0 diff --git a/deploy.bat b/deploy.bat index 3eb16b6..698f7cf 100644 --- a/deploy.bat +++ b/deploy.bat @@ -1,46 +1,30 @@ @echo off +setlocal +REM Builds the plugin and copies it into C:\Program Files\Notepad++\plugins. +REM Run it as Administrator, since it writes to Program Files. + +REM Work from the repo folder, also when started elevated (cwd is System32 then) +cd /d "%~dp0" + echo ======================================== echo LLM Assistant Plugin - Auto Deploy echo ======================================== -echo. -echo [1/4] Building plugin... -echo Setting up Visual Studio 2022 environment... - -REM Set Visual Studio 2022 environment -set VS2022_PATH="C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\Tools\VsDevCmd.bat" -set VS2022_PRO_PATH="C:\Program Files\Microsoft Visual Studio\2022\Professional\Common7\Tools\VsDevCmd.bat" -set VS2022_ENT_PATH="C:\Program Files\Microsoft Visual Studio\2022\Enterprise\Common7\Tools\VsDevCmd.bat" - -REM Try to find and setup VS2022 environment -if exist %VS2022_PATH% ( - echo Found Visual Studio 2022 Community - call %VS2022_PATH% -) else if exist %VS2022_PRO_PATH% ( - echo Found Visual Studio 2022 Professional - call %VS2022_PRO_PATH% -) else if exist %VS2022_ENT_PATH% ( - echo Found Visual Studio 2022 Enterprise - call %VS2022_ENT_PATH% -) else ( - echo ERROR: Visual Studio 2022 not found in standard locations. - echo Please run this from a Visual Studio 2022 Developer Command Prompt. - pause - exit /b 1 -) - -REM Check if MSBuild is available after setting up environment -where msbuild >nul 2>nul -if %ERRORLEVEL% NEQ 0 ( - echo ERROR: MSBuild not found. Please ensure Visual Studio 2022 is properly installed. +REM Check for admin rights first, so a run without them stops before it +REM builds anything or closes Notepad++. +fltmc >nul 2>&1 +if errorlevel 1 ( + echo ERROR: Run this script as Administrator, because it copies the plugin + echo into C:\Program Files\Notepad++. Right-click deploy.bat and choose + echo "Run as administrator". pause exit /b 1 ) -echo Building with MSBuild... -msbuild LLMAssistant.vcxproj /p:Configuration=Release /p:Platform=x64 /p:PlatformToolset=v143 /p:WindowsTargetPlatformVersion=10.0 - -if %ERRORLEVEL% neq 0 ( +echo. +echo [1/4] Building plugin... +call "%~dp0build.bat" /nopause +if errorlevel 1 ( echo ERROR: Build failed! pause exit /b 1 @@ -49,12 +33,11 @@ echo Build completed successfully! echo. echo [2/4] Closing Notepad++... -taskkill /f /im notepad++.exe >nul 2>&1 -if %ERRORLEVEL% equ 0 ( - echo Notepad++ closed successfully - timeout /t 2 /nobreak >nul -) else ( - echo Notepad++ was not running +call :close_notepadpp +if errorlevel 1 ( + echo Deployment cancelled. The plugin was not copied. + pause + exit /b 1 ) echo. @@ -98,3 +81,43 @@ if /i "%choice%"=="Y" ( echo. echo Press any key to exit... pause >nul +exit /b 0 + +REM Closes Notepad++ the way its own close button does (taskkill without /f +REM sends WM_CLOSE), so it asks to save unsaved files, or keeps them in its +REM backup when session snapshot is on. Returns 1 if Notepad++ is still +REM running and the user cancels. +:close_notepadpp +call :notepadpp_running +if errorlevel 1 ( + echo Notepad++ was not running + exit /b 0 +) +echo Asking Notepad++ to close. If it asks to save changes, answer it to continue. +taskkill /im notepad++.exe >nul 2>&1 +for /l %%i in (1,1,20) do ( + timeout /t 1 /nobreak >nul + call :notepadpp_running + if errorlevel 1 ( + echo Notepad++ closed successfully + exit /b 0 + ) +) +:close_notepadpp_wait +echo. +echo Notepad++ is still running. It may be showing a save prompt, or a save +echo was cancelled. Close Notepad++ yourself, saving your work as needed. +choice /c RC /n /m "Press R once Notepad++ is closed, or C to cancel: " +if errorlevel 2 exit /b 1 +call :notepadpp_running +if errorlevel 1 ( + echo Notepad++ closed successfully + exit /b 0 +) +goto :close_notepadpp_wait + +REM Sets errorlevel 0 if notepad++.exe is running, 1 if not +:notepadpp_running +tasklist /fi "imagename eq notepad++.exe" /nh 2>nul | findstr /i /l /c:"notepad++.exe" >nul +if errorlevel 1 exit /b 1 +exit /b 0