Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update windows' script to support env variable #3821

Merged
merged 2 commits into from
Dec 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 31 additions & 41 deletions distribution/src/scripts/micro-integrator.bat
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 setEnvPath

if ""%1""==""-run"" goto commandLifecycle
if ""%1""==""--run"" goto commandLifecycle
if ""%1""==""run"" goto commandLifecycle
Expand All @@ -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

:setEnvPath
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
Expand Down Expand Up @@ -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
Expand Down
Loading