Skip to content

Commit

Permalink
Migrate cli scripts to Powershell
Browse files Browse the repository at this point in the history
  • Loading branch information
hamzaremmal committed Oct 6, 2024
1 parent 6fa81cf commit 287dc80
Show file tree
Hide file tree
Showing 11 changed files with 338 additions and 259 deletions.
51 changes: 7 additions & 44 deletions dist/bin/scala.bat
Original file line number Diff line number Diff line change
Expand Up @@ -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%
Expand Down
21 changes: 21 additions & 0 deletions dist/bin/scala.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
###################################################################################################
### POWERSHELL SCALA SCRIPT ###
### ###
### Author: Hamza REMMAL <[email protected]> ###
### Since : Scala 3.6.0 ###
###################################################################################################

# Environment setup
$_PROG_HOME = $PSScriptRoot.TrimEnd('\bin\')
# Load and execute the common script
. "$_PROG_HOME/libexec/common.ps1" # TODO: Will this work on Windows or not ?
. "$_LIBEXEC_DIR/cli-common-platform.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
& $SCALA_CLI_CMD_WIN "--prog-name" "scala" "--skip-cli-updates" "--cli-default-scala-version" "$_SCALA_VERSION" "-r" "$_MVN_REPOSITORY" $args

if ($LASTEXITCODE -ne 0) { exit 1 }
exit 0
84 changes: 4 additions & 80 deletions dist/bin/scalac.bat
Original file line number Diff line number Diff line change
Expand Up @@ -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%
Expand Down
108 changes: 108 additions & 0 deletions dist/bin/scalac.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
###################################################################################################
### POWERSHELL SCALAC SCRIPT ###
### ###
### Author: Hamza REMMAL <[email protected]> ###
### 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 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
88 changes: 4 additions & 84 deletions dist/bin/scaladoc.bat
Original file line number Diff line number Diff line change
Expand Up @@ -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%
Expand Down
Loading

0 comments on commit 287dc80

Please sign in to comment.