From 3eeb710981ed20a98402a7d85aba3ea1b3765baf 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 | 51 ++--------- dist/bin/scala.ps1 | 20 ++++ dist/bin/scalac.bat | 84 +---------------- dist/bin/scalac.ps1 | 108 ++++++++++++++++++++++ dist/bin/scaladoc.bat | 88 +----------------- dist/bin/scaladoc.ps1 | 131 +++++++++++++++++++++++++++ dist/libexec/cli-common-platform.bat | 5 - dist/libexec/common.bat | 43 --------- 8 files changed, 274 insertions(+), 256 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/cli-common-platform.bat delete mode 100644 dist/libexec/common.bat diff --git a/dist/bin/scala.bat b/dist/bin/scala.bat index bd5bf0b8dfbe..a8ae559fb424 100644 --- a/dist/bin/scala.bat +++ b/dist/bin/scala.bat @@ -11,56 +11,19 @@ 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 not %ERRORLEVEL%==0 ( + set _EXITCODE=1 + goto end ) -:foundVersion -goto :eof - @rem ######################################################################### -@rem ## Cleanups +@rem ## Main (if needed to continue batch processing after the PowerShell script) :end exit /b %_EXITCODE% diff --git a/dist/bin/scala.ps1 b/dist/bin/scala.ps1 new file mode 100644 index 000000000000..86c400c5084e --- /dev/null +++ b/dist/bin/scala.ps1 @@ -0,0 +1,20 @@ +################################################################################################### +### 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/bin/common.ps1" # TODO: Will this work on Windows or not ? +# Fetch the version of Scala +$_SCALA_VERSION = Scala-FetchScalaVersion + +# SCALA_CLI_CMD_WIN is an array, set in cli-common-platform.bat. +# WE NEED TO PASS '--skip-cli-updates' for JVM launchers but we actually don't need it for native launchers +& "$_JAVACMD" "-jar" "$_BIN_DIR/scala-cli.jar" "--prog-name" "scala" "--skip-cli-updates" "--cli-default-scala-version" "$_SCALA_VERSION" "-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..4f63becfbf5d 100644 --- a/dist/bin/scalac.bat +++ b/dist/bin/scalac.bat @@ -11,95 +11,19 @@ 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 we need to escape % in the java command path, for some reason this doesnt work in common.bat -set "_JAVACMD=!_JAVACMD:%%=%%%%!" +@rem ## Call the new PowerShell script with arguments -call "%_JAVACMD%" %_JAVA_ARGS% -classpath "%_JVM_CP_ARGS%" "-Dscala.usejavacp=true" "-Dscala.expandjavacp=true" "-Dscala.home=%_PROG_HOME%" dotty.tools.MainGenericCompiler %_SCALA_ARGS% +set "args=%*" +call powershell.exe -ExecutionPolicy Bypass -File "%_PROG_HOME%\bin\scalac.ps1" %args% if not %ERRORLEVEL%==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 +@rem ## Main (if needed to continue batch processing after the PowerShell script) :end exit /b %_EXITCODE% diff --git a/dist/bin/scalac.ps1 b/dist/bin/scalac.ps1 new file mode 100644 index 000000000000..061dc409029f --- /dev/null +++ b/dist/bin/scalac.ps1 @@ -0,0 +1,108 @@ +################################################################################################### +### 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/bin/common.ps1" + +################################################################################################### +############################################ FUNCTIONS ############################################ +################################################################################################### + +function Scala-ArgumentParsing { + param ( [string[]] $params ) + + $_JAVA_ARGS = "" # TODO: THIS SHOULD BE AN ARRAY + $_SCALA_ARGS = "" # TODO: THIS SHOULD BE AN ARRAY + $_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 -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 Scala-LoadCompilerClasspath { + param ( [string] $SCALA_CLASSPATH ) + + $__SCALA_JAR = Join-Path $_LIB_DIR "scala.jar" + $__WITH_COMPILER_JAR = Join-Path $_LIB_DIR with_compiler.jar + + $__TOOLCHAIN = "$__SCALA_JAR$_PSEP$__WITH_COMPILER_JAR" + + if ($SCALA_CLASSPATH) { + $__TOOLCHAIN += "$_PSEP$SCALA_CLASSPATH" + } + return $__TOOLCHAIN +} + + +################################################################################################### +############################################## SCRIPT ############################################# +################################################################################################### + +# Parse the arguments +$_ARG_RESULT = Scala-ArgumentParsing $args + +# Compute the classpath +$_CLASS_PATH = Scala-LoadCompilerClasspath $_ARG_RESULT.SCALA_CLASSPATH + +# Fetch the arguments +$_JAVA_ARGS = $_ARG_RESULT.JAVA_ARGS +$_SCALA_ARGS = $_ARG_RESULT.SCALA_ARGS + +# Build the java arguments +$command = @() +if (-not [string]::IsNullOrEmpty($_JAVA_ARGS)) { $command += $_JAVA_ARGS } +$command += "-classpath" +$command += $_CLASS_PATH +$command += "-Dscala.usejavacp=true" +$command += "-Dscala.home=$_PROG_HOME" +$command += "dotty.tools.MainGenericCompiler" + +if (-not [string]::IsNullOrEmpty($_SCALA_ARGS)) { $command += $_SCALA_ARGS } +$commandString = $command -join ' ' + +Start-Process -FilePath $_JAVACMD -ArgumentList $commandString -NoNewWindow -Wait + +if ($LASTEXITCODE -ne 0) { exit 1 } + +exit 0 diff --git a/dist/bin/scaladoc.bat b/dist/bin/scaladoc.bat index 2be13aa628b1..e8103e049e32 100644 --- a/dist/bin/scaladoc.bat +++ b/dist/bin/scaladoc.bat @@ -11,99 +11,19 @@ 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% +set "args=%*" +call powershell.exe -ExecutionPolicy Bypass -File "%_PROG_HOME%\bin\scaladoc.ps1" %args% if not %ERRORLEVEL%==0 ( - @rem echo Error: Scaladoc execution failed 1>&2 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 +@rem ## Main (if needed to continue batch processing after the PowerShell script) :end exit /b %_EXITCODE% diff --git a/dist/bin/scaladoc.ps1 b/dist/bin/scaladoc.ps1 new file mode 100644 index 000000000000..16d1413d6db2 --- /dev/null +++ b/dist/bin/scaladoc.ps1 @@ -0,0 +1,131 @@ +################################################################################################### +### POWERSHELL SCALADOC SCRIPT ### +### ### +### Author: Hamza REMMAL ### +### Since : Scala 3.6.0 ### +################################################################################################### + +# Environment setup +$_PROG_HOME = $PSScriptRoot.TrimEnd('\bin\') + +. "$_PROG_HOME/bin/common.ps1" + +$_DEFAULT_JAVA_OPTS = "-Xmx768m -Xms768m" + +################################################################################################### +############################################ FUNCTIONS ############################################ +################################################################################################### + +function Scaladoc-ArgumentParsing { + 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) { + # addScripting "$arg" TODO: What is this ? + } 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 = Scaladoc-ArgumentParsing $args + +# Main + +if ($JAVA_OPTS) { + $_JAVA_OPTS = $JAVA_OPTS +} else { + $_JAVA_OPTS = $_DEFAULT_JAVA_OPTS +} + + +$_JAVA_DEBUG = $scaladocParameters.JAVA_DEBUG +$_JAVA_ARGS = $scaladocParameters.JAVA_ARGS +$_SCALA_ARGS = $scaladocParameters.SCALA_ARGS +$_RESIDUAL_ARGS = $scaladocParameters.RESIDUAL_ARGS + +# Build the java arguments +$command = @() +if (-not [string]::IsNullOrEmpty($_JAVA_OPTS)) { $command += $_JAVA_OPTS } +if (-not [string]::IsNullOrEmpty($_JAVA_DEBUG)) { $command += $_JAVA_DEBUG } +if (-not [string]::IsNullOrEmpty($_JAVA_ARGS)) { $command += $_JAVA_ARGS } +$command += "-classpath" +$command += "$_LIB_DIR/scaladoc.jar" +$command += "-Dscala.usejavacp=true" +$command += "-Dscala.home=$_PROG_HOME" +$command += "dotty.tools.scaladoc.Main" +if (-not [string]::IsNullOrEmpty($_SCALA_ARGS)) { $command += $_SCALA_ARGS } +if (-not [string]::IsNullOrEmpty($_RESIDUAL_ARGS)) { $command += $_RESIDUAL_ARGS } + +$commandString = $command -join ' ' + +Start-Process -FilePath $_JAVACMD -ArgumentList $commandString -NoNewWindow -Wait + +if ($LASTEXITCODE -ne 0) { exit 1 } + +exit 0 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/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=;