From 563db30da472466b350681aa926f70c302e69fe2 Mon Sep 17 00:00:00 2001 From: Hamza Remmal Date: Sun, 6 Oct 2024 16:43:01 +0200 Subject: [PATCH] Migrate cli scripts to Powershell --- dist/bin/scala.bat | 52 +------- dist/bin/scala.ps1 | 18 +++ dist/bin/scalac.bat | 89 +------------ dist/bin/scalac.ps1 | 96 +++++++++++++ dist/bin/scaladoc.bat | 93 +------------ dist/bin/scaladoc.ps1 | 126 ++++++++++++++++++ .../cli-common-platform.bat | 3 - .../cli-common-platform.ps1 | 9 ++ dist/libexec/cli-common-platform.bat | 5 - dist/libexec/cli-common-platform.ps1 | 1 + dist/libexec/common.bat | 43 ------ dist/libexec/common.ps1 | 60 +++++++++ 12 files changed, 323 insertions(+), 272 deletions(-) create mode 100644 dist/bin/scala.ps1 create mode 100644 dist/bin/scalac.ps1 create mode 100644 dist/bin/scaladoc.ps1 delete mode 100644 dist/libexec-native-overrides/cli-common-platform.bat create mode 100644 dist/libexec-native-overrides/cli-common-platform.ps1 delete mode 100644 dist/libexec/cli-common-platform.bat create mode 100644 dist/libexec/cli-common-platform.ps1 delete mode 100644 dist/libexec/common.bat create mode 100644 dist/libexec/common.ps1 diff --git a/dist/bin/scala.bat b/dist/bin/scala.bat index bd5bf0b8dfbe..5fab34d96aa8 100644 --- a/dist/bin/scala.bat +++ b/dist/bin/scala.bat @@ -11,57 +11,15 @@ for %%f in ("%~dp0.") do ( @rem get rid of the trailing slash set "_PROG_HOME=!_PROG_HOME:~0,-1!" ) -call "%_PROG_HOME%\libexec\common.bat" -if not %_EXITCODE%==0 goto end @rem ######################################################################### -@rem ## Main +@rem ## Call the new PowerShell script with arguments -call :setScalaOpts - -call "%_PROG_HOME%\libexec\cli-common-platform.bat" - -@rem SCALA_CLI_CMD_WIN is an array, set in cli-common-platform.bat. -@rem WE NEED TO PASS '--skip-cli-updates' for JVM launchers but we actually don't need it for native launchers -call %SCALA_CLI_CMD_WIN% "--prog-name" "scala" "--skip-cli-updates" "--cli-default-scala-version" "%_SCALA_VERSION%" "-r" "%MVN_REPOSITORY%" %* - -if not %ERRORLEVEL%==0 ( set _EXITCODE=1& goto end ) - -goto end - -@rem ######################################################################### -@rem ## Subroutines - -:setScalaOpts - -@REM sfind the index of the first colon in _PROG_HOME -set "index=0" -set "char=!_PROG_HOME:~%index%,1!" -:findColon -if not "%char%"==":" ( - set /a "index+=1" - set "char=!_PROG_HOME:~%index%,1!" - goto :findColon -) - -set "_SCALA_VERSION=" -set "MVN_REPOSITORY=file:///%_PROG_HOME:\=/%/maven2" - -@rem read for version:=_SCALA_VERSION in VERSION_FILE -FOR /F "usebackq delims=" %%G IN ("%_PROG_HOME%\VERSION") DO ( - SET "line=%%G" - IF "!line:~0,9!"=="version:=" ( - SET "_SCALA_VERSION=!line:~9!" - GOTO :foundVersion - ) +set "args=%*" +call powershell.exe -ExecutionPolicy Bypass -File "%_PROG_HOME%\bin\scala.ps1" %args% +if %ERRORLEVEL% neq 0 ( + set _EXITCODE=1 ) -:foundVersion -goto :eof - -@rem ######################################################################### -@rem ## Cleanups - -:end exit /b %_EXITCODE% endlocal diff --git a/dist/bin/scala.ps1 b/dist/bin/scala.ps1 new file mode 100644 index 000000000000..1b28e376ccea --- /dev/null +++ b/dist/bin/scala.ps1 @@ -0,0 +1,18 @@ +################################################################################################### +### POWERSHELL SCALA SCRIPT ### +### ### +### Author: Hamza REMMAL ### +### Since : Scala 3.6.0 ### +################################################################################################### + +# Environment setup +$_PROG_HOME = $PSScriptRoot.TrimEnd('\bin\') +# Load and execute the common script +. "$_PROG_HOME/libexec/common.ps1" +. "$_LIBEXEC_DIR/cli-common-platform.ps1" + +# WE NEED TO PASS '--skip-cli-updates' for JVM launchers but we actually don't need it for native launchers +& $SCALA_CLI_CMD_WIN "--prog-name" "scala" "--skip-cli-updates" "--cli-default-scala-version" "$(Get-FetchScalaVersion)" "-r" "$_MVN_REPOSITORY" $args + +if ($LASTEXITCODE -ne 0) { exit 1 } +exit 0 diff --git a/dist/bin/scalac.bat b/dist/bin/scalac.bat index 038c733f24c8..c2d06fa34ef0 100644 --- a/dist/bin/scalac.bat +++ b/dist/bin/scalac.bat @@ -11,96 +11,15 @@ for %%f in ("%~dp0.") do ( @rem get rid of the trailing slash set "_PROG_HOME=!_PROG_HOME:~0,-1!" ) -call "%_PROG_HOME%\libexec\common.bat" -if not %_EXITCODE%==0 goto end - -call :args %* @rem ######################################################################### -@rem ## Main - -call :compilerJavaClasspathArgs +@rem ## Call the new PowerShell script with arguments -@rem we need to escape % in the java command path, for some reason this doesnt work in common.bat -set "_JAVACMD=!_JAVACMD:%%=%%%%!" - -call "%_JAVACMD%" %_JAVA_ARGS% -classpath "%_JVM_CP_ARGS%" "-Dscala.usejavacp=true" "-Dscala.expandjavacp=true" "-Dscala.home=%_PROG_HOME%" dotty.tools.MainGenericCompiler %_SCALA_ARGS% -if not %ERRORLEVEL%==0 ( +set "args=%*" +call powershell.exe -ExecutionPolicy Bypass -File "%_PROG_HOME%\bin\scalac.ps1" %args% +if %ERRORLEVEL% neq 0 ( set _EXITCODE=1 - goto end ) -goto end - -@rem ######################################################################### -@rem ## Subroutines - -:args -set _JAVA_ARGS= -set _SCALA_ARGS= -set _SCALA_CPATH= -@rem replace inner while loop used in bash script -set _CONSUME_REMAINING= - -:args_loop -if "%~1"=="" goto args_done -set "__ARG=%~1" -if defined _CONSUME_REMAINING ( - set _SCALA_ARGS=!_SCALA_ARGS! "%__ARG%" - shift -) else if "%__ARG%"=="--" ( - @rem pass all remaining arguments to scala, e.g. to avoid interpreting them here as -D or -J - set _CONSUME_REMAINING=1 - set _SCALA_ARGS=!_SCALA_ARGS! "%__ARG%" - shift -) else if "%__ARG%"=="-script" ( - @rem pass all remaining arguments to scala, e.g. to avoid interpreting them here as -D or -J - set _CONSUME_REMAINING=1 - set _SCALA_ARGS=!_SCALA_ARGS! "%__ARG%" - shift -) else if "%__ARG%"=="-Oshort" ( - @rem optimize for short-running applications, see https://github.com/scala/scala3/issues/222 - set _JAVA_ARGS=!_JAVA_ARGS! "-XX:+TieredCompilation" "-XX:TieredStopAtLevel=1" - set _SCALA_ARGS=!_SCALA_ARGS! -Oshort - shift -) else if "%__ARG:~0,2%"=="-D" ( - @rem pass to scala as well: otherwise we lose it sometimes when we - @rem need it, e.g. communicating with a server compiler. - set _JAVA_ARGS=!_JAVA_ARGS! "%__ARG%" - set _SCALA_ARGS=!_SCALA_ARGS! "%__ARG%" -) else if "%__ARG:~0,2%"=="-J" ( - @rem as with -D, pass to scala even though it will almost - @rem never be used. - set _JAVA_ARGS=!_JAVA_ARGS! %__ARG:~2% - set _SCALA_ARGS=!_SCALA_ARGS! "%__ARG%" -) else if "%__ARG%"=="-classpath" ( - set "_SCALA_CPATH=%~2" - shift -) else if "%__ARG%"=="-cp" ( - set "_SCALA_CPATH=%~2" - shift -) else ( - set _SCALA_ARGS=!_SCALA_ARGS! "%__ARG%" -) -shift -goto args_loop -:args_done -goto :eof - -@rem output parameter: _JVM_CP_ARGS -:compilerJavaClasspathArgs -set "__TOOLCHAIN=%_LIB_DIR%\scala.jar" -set "__TOOLCHAIN=%__TOOLCHAIN%%_PSEP%%_LIB_DIR%\with_compiler.jar%" - -if defined _SCALA_CPATH ( - set "_JVM_CP_ARGS=%__TOOLCHAIN%%_SCALA_CPATH%" -) else ( - set "_JVM_CP_ARGS=%__TOOLCHAIN%" -) -goto :eof - -@rem ######################################################################### -@rem ## Cleanups -:end exit /b %_EXITCODE% endlocal diff --git a/dist/bin/scalac.ps1 b/dist/bin/scalac.ps1 new file mode 100644 index 000000000000..abaacf4456b2 --- /dev/null +++ b/dist/bin/scalac.ps1 @@ -0,0 +1,96 @@ +################################################################################################### +### POWERSHELL SCALAC SCRIPT ### +### ### +### Author: Hamza REMMAL ### +### Since : Scala 3.6.0 ### +################################################################################################### + +# Environment setup +$_PROG_HOME = $PSScriptRoot.TrimEnd('\bin\') +# Load and execute the common script +. "$_PROG_HOME/libexec/common.ps1" + +################################################################################################### +############################################ FUNCTIONS ############################################ +################################################################################################### + +function Get-ScalaArgumentParsing { + param ( [string[]] $params ) + + $_JAVA_ARGS = @() + $_SCALA_ARGS = @() + $_SCALA_CLASSPATH = "" + $_CONSUME_REMAINING = $false + + while ($params.Count -gt 0) { + $arg = $params[0] + if ($_CONSUME_REMAINING) { + $_SCALA_ARGS += "$arg" + $params = $params[1..$params.Length] + } elseif ($arg -eq "--" -or $arg -eq "-script") { + $_CONSUME_REMAINING = $true + $_SCALA_ARGS += "$arg" + $params = $params[1..$params.Length] + } elseif ($arg -eq "-Oshort") { + $_JAVA_ARGS += "-XX:+TieredCompilation" + $_JAVA_ARGS += "-XX:TieredStopAtLevel=1" + $_SCALA_ARGS += "-Oshort" + $params = $params[1..$params.Length] + } elseif ($arg.StartsWith("-D")) { + $_JAVA_ARGS += "$arg" + $_SCALA_ARGS += "$arg" + $params = $params[1..$params.Length] + } elseif ($arg.StartsWith("-J")) { + $_JAVA_ARGS += "${arg:2}" + $_SCALA_ARGS += "$arg" + $params = $params[1..$params.Length] + } elseif ($arg -eq "-classpath" -or $arg -eq "-cp") { + $_SCALA_CLASSPATH = $params[1] + $params = $params[2..$params.Length] + } else { + $_SCALA_ARGS += "$arg" + $params = $params[1..$params.Length] + } + } + + return @{ + JAVA_ARGS = $_JAVA_ARGS + SCALA_ARGS = $_SCALA_ARGS + SCALA_CLASSPATH = $_SCALA_CLASSPATH + } + +} + +function Get-ScalaLoadCompilerClasspath { + param ( [string] $SCALA_CLASSPATH ) + $__TOOLCHAIN = @() + $__TOOLCHAIN += Join-Path $_LIB_DIR "scala.jar" + $__TOOLCHAIN += Join-Path $_LIB_DIR "with_compiler.jar" + if ($SCALA_CLASSPATH) { $__TOOLCHAIN += "$SCALA_CLASSPATH" } + return $__TOOLCHAIN -join $_PSEP +} + + +################################################################################################### +############################################## SCRIPT ############################################# +################################################################################################### + +# Parse the arguments +$_PARSE_RESULT = Get-ScalaArgumentParsing $args + +# Build the java arguments +$command = @() +$command += $_PARSE_RESULT.JAVA_ARGS +$command += "-classpath" +$command += "$(Get-ScalaLoadCompilerClasspath $_PARSE_RESULT.SCALA_CLASSPATH)" +$command += "-Dscala.expandjavacp=true" +$command += "-Dscala.usejavacp=true" +$command += "-Dscala.home=$_PROG_HOME" +$command += "dotty.tools.MainGenericCompiler" +$command += $_PARSE_RESULT.SCALA_ARGS + +$_JAVA_PROCESS = Start-Process -FilePath $_JAVACMD -ArgumentList $($command -join ' ') -NoNewWindow -PassThru -Wait + +if ($_JAVA_PROCESS.ExitCode -ne 0) { exit 1 } + +exit 0 diff --git a/dist/bin/scaladoc.bat b/dist/bin/scaladoc.bat index 2be13aa628b1..28423e62c52b 100644 --- a/dist/bin/scaladoc.bat +++ b/dist/bin/scaladoc.bat @@ -11,100 +11,15 @@ for %%f in ("%~dp0.") do ( @rem get rid of the trailing slash set "_PROG_HOME=!_PROG_HOME:~0,-1!" ) -call "%_PROG_HOME%\libexec\common.bat" -if not %_EXITCODE%==0 goto end - -set _DEFAULT_JAVA_OPTS=-Xmx768m -Xms768m - -call :args %* @rem ######################################################################### -@rem ## Main - -if defined JAVA_OPTS ( set _JAVA_OPTS=%JAVA_OPTS% -) else ( set _JAVA_OPTS=%_DEFAULT_JAVA_OPTS% -) +@rem ## Call the new PowerShell script with arguments -@rem we need to escape % in the java command path, for some reason this doesnt work in common.bat -set "_JAVACMD=!_JAVACMD:%%=%%%%!" - -call "%_JAVACMD%" %_JAVA_OPTS% %_JAVA_DEBUG% %_JAVA_ARGS% ^ --classpath "%_LIB_DIR%\scaladoc.jar" ^ --Dscala.expandjavacp=true ^ --Dscala.usejavacp=true ^ -dotty.tools.scaladoc.Main %_SCALA_ARGS% %_RESIDUAL_ARGS% -if not %ERRORLEVEL%==0 ( - @rem echo Error: Scaladoc execution failed 1>&2 +set "args=%*" +call powershell.exe -ExecutionPolicy Bypass -File "%_PROG_HOME%\bin\scaladoc.ps1" %args% +if %ERRORLEVEL% neq 0 ( set _EXITCODE=1 - goto end ) -goto end - -@rem ######################################################################### -@rem ## Subroutines - -:args -set _JAVA_DEBUG= -set _HELP= -set _VERBOSE= -set _QUIET= -set _COLORS= -set _SCALA_ARGS= -set _JAVA_ARGS= -set _RESIDUAL_ARGS= - -:args_loop -if "%~1"=="" goto args_done -set "__ARG=%~1" -if "%__ARG%"=="--" ( - @rem for arg; do addResidual "$arg"; done; set -- ;; -) else if "%__ARG%"=="-h" ( - set _HELP=true - call :addScala "-help" -) else if "%__ARG%"=="-help" ( - set _HELP=true - call :addScala "-help" -) else if "%__ARG%"=="-v" ( - set _VERBOSE=true - call :addScala "-verbose" -) else if "%__ARG%"=="-verbose" ( - set _VERBOSE=true - call :addScala "-verbose" -) else if "%__ARG%"=="-debug" ( set "_JAVA_DEBUG=%_DEBUG_STR%" -) else if "%__ARG%"=="-q" ( set _QUIET=true -) else if "%__ARG%"=="-quiet" ( set _QUIET=true -) else if "%__ARG%"=="-colors" ( set _COLORS=true -) else if "%__ARG%"=="-no-colors" ( set _COLORS= -) else if "%__ARG:~0,2%"=="-D" ( call :addJava "%__ARG%" -) else if "%__ARG:~0,2%"=="-J" ( call :addJava "%__ARG:~2%" -) else ( - if defined _IN_SCRIPTING_ARGS ( call :addScripting "%__ARG%" - ) else ( call :addResidual "%__ARG%" - ) -) -shift -goto args_loop -:args_done -goto :eof - -@rem output parameter: _SCALA_ARGS -:addScala -set _SCALA_ARGS=%_SCALA_ARGS% %~1 -goto :eof - -@rem output parameter: _JAVA_ARGS -:addJava -set _JAVA_ARGS=%_JAVA_ARGS% %~1 -goto :eof - -@rem output parameter: _RESIDUAL_ARGS -:addResidual -set _RESIDUAL_ARGS=%_RESIDUAL_ARGS% %~1 -goto :eof - -@rem ######################################################################### -@rem ## Cleanups -:end exit /b %_EXITCODE% endlocal diff --git a/dist/bin/scaladoc.ps1 b/dist/bin/scaladoc.ps1 new file mode 100644 index 000000000000..5fa0bd73fc21 --- /dev/null +++ b/dist/bin/scaladoc.ps1 @@ -0,0 +1,126 @@ +################################################################################################### +### POWERSHELL SCALADOC SCRIPT ### +### ### +### Author: Hamza REMMAL ### +### Since : Scala 3.6.0 ### +################################################################################################### + +# Environment setup +$_PROG_HOME = $PSScriptRoot.TrimEnd('\bin\') + +. "$_PROG_HOME/libexec/common.ps1" + +$_DEFAULT_JAVA_OPTS = "-Xmx768m -Xms768m" + +################################################################################################### +############################################ FUNCTIONS ############################################ +################################################################################################### + +function Get-ScaladocArgumentParsing { + param ( [string[]] $params ) + + $_JAVA_DEBUG = "" + $_VERBOSE = $false + $_QUIET = $false + $_COLORS = $false + $_SCALA_ARGS = @() + $_JAVA_ARGS = @() + $_RESIDUAL_ARGS = @() + $_IN_SCRIPTING_ARGS = $false + + while ($params.Count -gt 0) { + $arg = $params[0] + switch ($arg) { + "--" { + $_IN_SCRIPTING_ARGS = $true + } + "-h" { + $_SCALA_ARGS += "-help" + } + "-help" { + $_SCALA_ARGS += "-help" + } + "-v" { + $_VERBOSE = $true + $_SCALA_ARGS += "-verbose" + } + "-verbose" { + $_VERBOSE = $true + $_SCALA_ARGS += "-verbose" + } + "-debug" { + $_JAVA_DEBUG = $_DEBUG_STR + } + "-q" { + $_QUIET = $true + } + "-quiet" { + $_QUIET = $true + } + "-colors" { + $_COLORS = $true + } + "-no-colors" { + $_COLORS = $false + } + default { + if ($arg.StartsWith("-D")) { + $_JAVA_ARGS += "$arg" + } elseif ($arg.StartsWith("-J")) { + $_JAVA_ARGS += "${arg:2}" + } else { + if ($_IN_SCRIPTING_ARGS) { + } else { + $_RESIDUAL_ARGS += "$arg" + } + } + } + } + $params = $params[1..$params.Length] + } + + return @{ + JAVA_DEBUG = $_JAVA_DEBUG + SCALA_ARGS = $_SCALA_ARGS + JAVA_ARGS = $_JAVA_ARGS + RESIDUAL_ARGS = $_RESIDUAL_ARGS + } +} + +################################################################################################### +############################################## SCRIPT ############################################# +################################################################################################### + + +$scaladocParameters = Get-ScaladocArgumentParsing $args + +# Main + +if ($JAVA_OPTS) { + $_JAVA_OPTS = $JAVA_OPTS +} else { + $_JAVA_OPTS = $_DEFAULT_JAVA_OPTS +} + + +$_JAVA_DEBUG = $scaladocParameters.JAVA_DEBUG + +# Build the java arguments +$command = @() +if (-not [string]::IsNullOrEmpty($_JAVA_OPTS)) { $command += $_JAVA_OPTS } +if (-not [string]::IsNullOrEmpty($_JAVA_DEBUG)) { $command += $_JAVA_DEBUG } +$command += $scaladocParameters.JAVA_ARGS +$command += "-classpath" +$command += "$_LIB_DIR/scaladoc.jar" +$command += "-Dscala.expandjavacp=true" +$command += "-Dscala.usejavacp=true" +$command += "-Dscala.home=$_PROG_HOME" +$command += "dotty.tools.scaladoc.Main" +$command += $scaladocParameters.SCALA_ARGS +$command += $scaladocParameters.RESIDUAL_ARGS + +$_JAVA_PROCESS = Start-Process -FilePath $_JAVACMD -ArgumentList $($command -join ' ') -NoNewWindow -PassThru -Wait + +if ($_JAVA_PROCESS.ExitCode -ne 0) { exit 1 } + +exit 0 diff --git a/dist/libexec-native-overrides/cli-common-platform.bat b/dist/libexec-native-overrides/cli-common-platform.bat deleted file mode 100644 index 239ab40f1f28..000000000000 --- a/dist/libexec-native-overrides/cli-common-platform.bat +++ /dev/null @@ -1,3 +0,0 @@ -@echo off - -set SCALA_CLI_CMD_WIN="%_PROG_HOME%\libexec\scala-cli.exe" diff --git a/dist/libexec-native-overrides/cli-common-platform.ps1 b/dist/libexec-native-overrides/cli-common-platform.ps1 new file mode 100644 index 000000000000..736a33f4a48d --- /dev/null +++ b/dist/libexec-native-overrides/cli-common-platform.ps1 @@ -0,0 +1,9 @@ +function Scala-GetScalaCLIBinaryName { + if ($PSVersionTable.PSEdition -eq "Core") { + return 'scala-cli' + } else { + return 'scala-cli.exe' + } + } + +$SCALA_CLI_CMD_WIN = Join-Path $_LIBEXEC_DIR $(Scala-GetScalaCLIBinaryName) diff --git a/dist/libexec/cli-common-platform.bat b/dist/libexec/cli-common-platform.bat deleted file mode 100644 index 99103266c1d9..000000000000 --- a/dist/libexec/cli-common-platform.bat +++ /dev/null @@ -1,5 +0,0 @@ -@echo off - -@rem we need to escape % in the java command path, for some reason this doesnt work in common.bat -set "_JAVACMD=!_JAVACMD:%%=%%%%!" -set SCALA_CLI_CMD_WIN="%_JAVACMD%" "-jar" "%_PROG_HOME%\bin\scala-cli.jar" \ No newline at end of file diff --git a/dist/libexec/cli-common-platform.ps1 b/dist/libexec/cli-common-platform.ps1 new file mode 100644 index 000000000000..a07b6ff04778 --- /dev/null +++ b/dist/libexec/cli-common-platform.ps1 @@ -0,0 +1 @@ +$SCALA_CLI_CMD_WIN = "`"$_JAVACMD`" -jar `"$_LIBEXEC_DIR/scala-cli.jar`"" diff --git a/dist/libexec/common.bat b/dist/libexec/common.bat deleted file mode 100644 index 510771d43b6e..000000000000 --- a/dist/libexec/common.bat +++ /dev/null @@ -1,43 +0,0 @@ -@rem ######################################################################### -@rem ## Code common to scalac.bat, scaladoc.bat and scala.bat - -if defined JAVACMD ( - set "_JAVACMD=%JAVACMD%" -) else if defined JAVA_HOME ( - set "_JAVACMD=%JAVA_HOME%\bin\java.exe" -) else if defined JDK_HOME ( - set "_JAVACMD=%JDK_HOME%\bin\java.exe" -) else ( - where /q java.exe - if !ERRORLEVEL!==0 ( - set __JAVA_BIN_DIR= - for /f "delims=" %%i in ('where /f java.exe') do ( - set "__PATH=%%~dpi" - @rem we take first occurrence and ignore Oracle path for java executable - if not defined __JAVA_BIN_DIR if "!__PATH!"=="!__PATH:javapath=!" set "__JAVA_BIN_DIR=!__PATH!" - ) - if defined __JAVA_BIN_DIR set "_JAVACMD=!__JAVA_BIN_DIR!\java.exe" - ) - if not defined _JAVACMD ( - set "__PATH=%ProgramFiles%\Java" - for /f %%f in ('dir /ad /b "!__PATH!\jre*" 2^>NUL') do set "_JAVA_HOME=!__PATH!\%%f" - if not defined _JAVA_HOME ( - set __PATH=C:\opt - for /f %%f in ('dir /ad /b "!__PATH!\jdk*" 2^>NUL') do set "_JAVA_HOME=!__PATH!\%%f\jre" - ) - if defined _JAVA_HOME set "_JAVACMD=!_JAVA_HOME!\bin\java.exe" - ) -) -if not exist "%_JAVACMD%" ( - echo Error: Java executable not found ^(!_JAVACMD!^) 1>&2 - set _EXITCODE=1 - goto :eof -) -if not defined _PROG_HOME ( - echo Error: Variable _PROG_HOME undefined 1>&2 - set _EXITCODE=1 - goto :eof -) -set "_LIB_DIR=%_PROG_HOME%\lib" - -set _PSEP=; diff --git a/dist/libexec/common.ps1 b/dist/libexec/common.ps1 new file mode 100644 index 000000000000..89bcadd93ad0 --- /dev/null +++ b/dist/libexec/common.ps1 @@ -0,0 +1,60 @@ +################################################################################################### +### POWERSHELL COMMON SCRIPT ### +### ### +### Author: Hamza REMMAL ### +### Since : Scala 3.6.0 ### +################################################################################################### + + +################################################################################################### +######################################## UTILITY FUNCTIONS ######################################## +################################################################################################### + +function Get-JavaClasspathSeparator { + if ($PSVersionTable.PSEdition -eq "Core") { + return ':' + } else { + return ';' + } +} + +function Get-FetchScalaVersion { + if (Test-Path $_VERSION_FILE) { + foreach ($line in Get-Content $_VERSION_FILE) { + if ($line.StartsWith("version:=")) { + return $line.Substring(9) + } + } + Write-Error "Error: 'VERSION' file does not contain 'version' entry in ($_VERSION_FILE)" + } else { + Write-Error "Error: 'VERSION' file is missing in ($_VERSION_FILE)" + } +} + +################################################################################################### +############################################ LOAD JAVA ############################################ +################################################################################################### + +$javaPath = Get-Command java -ErrorAction SilentlyContinue +if ($javaPath) { $_JAVACMD = $javaPath.Source } + +if (-not (Test-Path $_JAVACMD)) { + Write-Error "Error: Java executable not found ($_JAVACMD)" + exit 1 +} + +if (-not $_PROG_HOME) { + Write-Error "Error: Variable _PROG_HOME undefined" + exit 1 +} + +################################################################################################### +######################################## VARIOUS VARIABLES ######################################## +################################################################################################### + +$_LIB_DIR = Join-Path $_PROG_HOME "lib" +$_LIBEXEC_DIR = Join-Path $_PROG_HOME "libexec" +$_BIN_DIR = Join-Path $_PROG_HOME "bin" +$_MVN_REPOSITORY = "file:///$($_PROG_HOME -replace '\\', '/')/maven2" +$_VERSION_FILE = Join-Path $_PROG_HOME "VERSION" +$_PSEP = Get-JavaClasspathSeparator