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 563db30
Show file tree
Hide file tree
Showing 12 changed files with 323 additions and 272 deletions.
52 changes: 5 additions & 47 deletions dist/bin/scala.bat
Original file line number Diff line number Diff line change
Expand Up @@ -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
18 changes: 18 additions & 0 deletions dist/bin/scala.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
###################################################################################################
### 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"
. "$_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
89 changes: 4 additions & 85 deletions dist/bin/scalac.bat
Original file line number Diff line number Diff line change
Expand Up @@ -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
96 changes: 96 additions & 0 deletions dist/bin/scalac.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
###################################################################################################
### 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 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
93 changes: 4 additions & 89 deletions dist/bin/scaladoc.bat
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading

0 comments on commit 563db30

Please sign in to comment.