From 964fe2fd5b6bcb2e025a14b3af3dd353c0908c18 Mon Sep 17 00:00:00 2001 From: Kalaiyarasiganeshalingam Date: Wed, 11 Dec 2024 00:30:07 +0530 Subject: [PATCH 1/2] Update windows' script to support env variable --- distribution/src/scripts/micro-integrator.bat | 72 ++++++++----------- 1 file changed, 31 insertions(+), 41 deletions(-) diff --git a/distribution/src/scripts/micro-integrator.bat b/distribution/src/scripts/micro-integrator.bat index 001fecea91..6da14242e6 100644 --- a/distribution/src/scripts/micro-integrator.bat +++ b/distribution/src/scripts/micro-integrator.bat @@ -17,7 +17,7 @@ REM limitations under the License. rem --------------------------------------------------------------------------- rem Main Script for WSO2 Carbon rem -rem Environment Variable Prequisites +rem Environment Variable Prerequisites rem rem CARBON_HOME Home of CARBON installation. If not set I will try rem to figure it out. @@ -75,10 +75,14 @@ rem ----- Process the input command ------------------------------------------- rem Slurp the command line arguments. This loop allows for an unlimited number rem of arguments (up to the command line limit, anyway). +set ENV_FILE_PATH= :setupArgs if ""%1""=="""" goto doneStart +:: Check if the argument is --env-file +if "%~1"=="--env-file" goto getPath + if ""%1""==""-run"" goto commandLifecycle if ""%1""==""--run"" goto commandLifecycle if ""%1""==""run"" goto commandLifecycle @@ -103,15 +107,35 @@ if ""%1""==""car"" goto setCar if ""%1""==""-car"" goto setCar if ""%1""==""--car"" goto setCar -@REM Check if the argument starts with --env-file= -set "envfile=%~1" -if "!envfile:~0,11!"=="--env-file=" ( - set "file_path=!envfile:~11!" - call :export_env_file "!file_path!" -) shift goto setupArgs +:getPath +shift +set ENV_FILE_PATH=%1 +goto loadEnvFile + +:loadEnvFile +if not exist "%ENV_FILE_PATH%" ( + echo Error: File "%ENV_FILE_PATH%" not found. +) else ( + :: Read the .env file line by line and set environment variables + for /f "usebackq tokens=1,* delims==" %%A in ("%ENV_FILE_PATH%") do ( + :: Ignore lines starting with # or empty lines + if not "%%A"=="" if not "%%A:~0,1%"=="#" ( + set "key=%%A" + set "value=%%B" + :: Trim spaces if needed + for /f "tokens=* delims= " %%a in ("!key!") do set "key=%%a" + for /f "tokens=* delims= " %%b in ("!value!") do set "value=%%b" + :: Export the variable to the environment + set "!key!=!value!" + ) + ) + echo Environment variables loaded from "%ENV_FILE_PATH%". +) +goto setupArgs + rem ----- commandVersion ------------------------------------------------------- :commandVersion shift @@ -139,40 +163,6 @@ echo Stopping the Micro Integrator Server taskkill /F /PID %processId% goto end -:export_env_file -setlocal EnableDelayedExpansion - -set "file_path=%~1" - -REM Check if the file exists -if not exist "!file_path!" ( - echo Error: File '!file_path!' not found. - exit /b 1 -) - -REM Read each line in the file -for /f "usebackq tokens=1,* delims==" %%A in ("!file_path!") do ( - set "line=%%A" - - REM Ignore lines that start with '#' (comments) or are empty - if not "!line!"=="" ( - if "!line:~0,1!" neq "#" ( - set "key=%%A" - set "value=%%B" - - REM Trim surrounding whitespace from key and value - for /f "tokens=* delims= " %%i in ("!key!") do set "key=%%i" - for /f "tokens=* delims= " %%i in ("!value!") do set "value=%%i" - - REM Set the environment variable - setx "!key!" "!value!" >nul - set "!key!=!value!" - ) - ) -) -echo Environment variables loaded from !file_path!. -exit /b 0 - rem ----- commandLifecycle ----------------------------------------------------- :commandLifecycle goto findJdk From 2326ba6a31869d11b874a6297316443081c56102 Mon Sep 17 00:00:00 2001 From: Kalaiyarasiganeshalingam Date: Wed, 11 Dec 2024 00:31:38 +0530 Subject: [PATCH 2/2] Update method name --- distribution/src/scripts/micro-integrator.bat | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/distribution/src/scripts/micro-integrator.bat b/distribution/src/scripts/micro-integrator.bat index 6da14242e6..3be45c8fa2 100644 --- a/distribution/src/scripts/micro-integrator.bat +++ b/distribution/src/scripts/micro-integrator.bat @@ -81,7 +81,7 @@ set ENV_FILE_PATH= if ""%1""=="""" goto doneStart :: Check if the argument is --env-file -if "%~1"=="--env-file" goto getPath +if "%~1"=="--env-file" goto setEnvPath if ""%1""==""-run"" goto commandLifecycle if ""%1""==""--run"" goto commandLifecycle @@ -110,7 +110,7 @@ if ""%1""==""--car"" goto setCar shift goto setupArgs -:getPath +:setEnvPath shift set ENV_FILE_PATH=%1 goto loadEnvFile