Skip to content

Commit a55a34e

Browse files
committed
Migrate cli scripts to Powershell
1 parent 6fa81cf commit a55a34e

File tree

12 files changed

+348
-259
lines changed

12 files changed

+348
-259
lines changed

dist/bin/scala.bat

Lines changed: 7 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -11,56 +11,19 @@ for %%f in ("%~dp0.") do (
1111
@rem get rid of the trailing slash
1212
set "_PROG_HOME=!_PROG_HOME:~0,-1!"
1313
)
14-
call "%_PROG_HOME%\libexec\common.bat"
15-
if not %_EXITCODE%==0 goto end
1614

1715
@rem #########################################################################
18-
@rem ## Main
16+
@rem ## Call the new PowerShell script with arguments
1917

20-
call :setScalaOpts
21-
22-
call "%_PROG_HOME%\libexec\cli-common-platform.bat"
23-
24-
@rem SCALA_CLI_CMD_WIN is an array, set in cli-common-platform.bat.
25-
@rem WE NEED TO PASS '--skip-cli-updates' for JVM launchers but we actually don't need it for native launchers
26-
call %SCALA_CLI_CMD_WIN% "--prog-name" "scala" "--skip-cli-updates" "--cli-default-scala-version" "%_SCALA_VERSION%" "-r" "%MVN_REPOSITORY%" %*
27-
28-
if not %ERRORLEVEL%==0 ( set _EXITCODE=1& goto end )
29-
30-
goto end
31-
32-
@rem #########################################################################
33-
@rem ## Subroutines
34-
35-
:setScalaOpts
36-
37-
@REM sfind the index of the first colon in _PROG_HOME
38-
set "index=0"
39-
set "char=!_PROG_HOME:~%index%,1!"
40-
:findColon
41-
if not "%char%"==":" (
42-
set /a "index+=1"
43-
set "char=!_PROG_HOME:~%index%,1!"
44-
goto :findColon
45-
)
46-
47-
set "_SCALA_VERSION="
48-
set "MVN_REPOSITORY=file:///%_PROG_HOME:\=/%/maven2"
49-
50-
@rem read for version:=_SCALA_VERSION in VERSION_FILE
51-
FOR /F "usebackq delims=" %%G IN ("%_PROG_HOME%\VERSION") DO (
52-
SET "line=%%G"
53-
IF "!line:~0,9!"=="version:=" (
54-
SET "_SCALA_VERSION=!line:~9!"
55-
GOTO :foundVersion
56-
)
18+
set "args=%*"
19+
call powershell.exe -ExecutionPolicy Bypass -File "%_PROG_HOME%\bin\scala.ps1" %args%
20+
if not %ERRORLEVEL%==0 (
21+
set _EXITCODE=1
22+
goto end
5723
)
5824

59-
:foundVersion
60-
goto :eof
61-
6225
@rem #########################################################################
63-
@rem ## Cleanups
26+
@rem ## Main (if needed to continue batch processing after the PowerShell script)
6427

6528
:end
6629
exit /b %_EXITCODE%

dist/bin/scala.ps1

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
###################################################################################################
2+
### POWERSHELL SCALA SCRIPT ###
3+
### ###
4+
### Author: Hamza REMMAL <[email protected]> ###
5+
### Since : Scala 3.6.0 ###
6+
###################################################################################################
7+
8+
# Environment setup
9+
$_PROG_HOME = $PSScriptRoot.TrimEnd('\bin\')
10+
# Load and execute the common script
11+
. "$_PROG_HOME/libexec/common.ps1" # TODO: Will this work on Windows or not ?
12+
. "$_LIBEXEC_DIR/cli-common-platform.ps1" # TODO: Will this work on Windows or not ?
13+
# Fetch the version of Scala
14+
$_SCALA_VERSION = Scala-FetchScalaVersion
15+
16+
# SCALA_CLI_CMD_WIN is an array, set in cli-common-platform.bat.
17+
# WE NEED TO PASS '--skip-cli-updates' for JVM launchers but we actually don't need it for native launchers
18+
& $SCALA_CLI_CMD_WIN "--prog-name" "scala" "--skip-cli-updates" "--cli-default-scala-version" "$_SCALA_VERSION" "-r" "$_MVN_REPOSITORY" $args
19+
20+
if ($LASTEXITCODE -ne 0) { exit 1 }
21+
exit 0

dist/bin/scalac.bat

Lines changed: 4 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -11,95 +11,19 @@ for %%f in ("%~dp0.") do (
1111
@rem get rid of the trailing slash
1212
set "_PROG_HOME=!_PROG_HOME:~0,-1!"
1313
)
14-
call "%_PROG_HOME%\libexec\common.bat"
15-
if not %_EXITCODE%==0 goto end
16-
17-
call :args %*
1814

1915
@rem #########################################################################
20-
@rem ## Main
21-
22-
call :compilerJavaClasspathArgs
23-
24-
@rem we need to escape % in the java command path, for some reason this doesnt work in common.bat
25-
set "_JAVACMD=!_JAVACMD:%%=%%%%!"
16+
@rem ## Call the new PowerShell script with arguments
2617

27-
call "%_JAVACMD%" %_JAVA_ARGS% -classpath "%_JVM_CP_ARGS%" "-Dscala.usejavacp=true" "-Dscala.expandjavacp=true" "-Dscala.home=%_PROG_HOME%" dotty.tools.MainGenericCompiler %_SCALA_ARGS%
18+
set "args=%*"
19+
call powershell.exe -ExecutionPolicy Bypass -File "%_PROG_HOME%\bin\scalac.ps1" %args%
2820
if not %ERRORLEVEL%==0 (
2921
set _EXITCODE=1
3022
goto end
3123
)
32-
goto end
33-
34-
@rem #########################################################################
35-
@rem ## Subroutines
36-
37-
:args
38-
set _JAVA_ARGS=
39-
set _SCALA_ARGS=
40-
set _SCALA_CPATH=
41-
@rem replace inner while loop used in bash script
42-
set _CONSUME_REMAINING=
43-
44-
:args_loop
45-
if "%~1"=="" goto args_done
46-
set "__ARG=%~1"
47-
if defined _CONSUME_REMAINING (
48-
set _SCALA_ARGS=!_SCALA_ARGS! "%__ARG%"
49-
shift
50-
) else if "%__ARG%"=="--" (
51-
@rem pass all remaining arguments to scala, e.g. to avoid interpreting them here as -D or -J
52-
set _CONSUME_REMAINING=1
53-
set _SCALA_ARGS=!_SCALA_ARGS! "%__ARG%"
54-
shift
55-
) else if "%__ARG%"=="-script" (
56-
@rem pass all remaining arguments to scala, e.g. to avoid interpreting them here as -D or -J
57-
set _CONSUME_REMAINING=1
58-
set _SCALA_ARGS=!_SCALA_ARGS! "%__ARG%"
59-
shift
60-
) else if "%__ARG%"=="-Oshort" (
61-
@rem optimize for short-running applications, see https://github.com/scala/scala3/issues/222
62-
set _JAVA_ARGS=!_JAVA_ARGS! "-XX:+TieredCompilation" "-XX:TieredStopAtLevel=1"
63-
set _SCALA_ARGS=!_SCALA_ARGS! -Oshort
64-
shift
65-
) else if "%__ARG:~0,2%"=="-D" (
66-
@rem pass to scala as well: otherwise we lose it sometimes when we
67-
@rem need it, e.g. communicating with a server compiler.
68-
set _JAVA_ARGS=!_JAVA_ARGS! "%__ARG%"
69-
set _SCALA_ARGS=!_SCALA_ARGS! "%__ARG%"
70-
) else if "%__ARG:~0,2%"=="-J" (
71-
@rem as with -D, pass to scala even though it will almost
72-
@rem never be used.
73-
set _JAVA_ARGS=!_JAVA_ARGS! %__ARG:~2%
74-
set _SCALA_ARGS=!_SCALA_ARGS! "%__ARG%"
75-
) else if "%__ARG%"=="-classpath" (
76-
set "_SCALA_CPATH=%~2"
77-
shift
78-
) else if "%__ARG%"=="-cp" (
79-
set "_SCALA_CPATH=%~2"
80-
shift
81-
) else (
82-
set _SCALA_ARGS=!_SCALA_ARGS! "%__ARG%"
83-
)
84-
shift
85-
goto args_loop
86-
:args_done
87-
goto :eof
88-
89-
@rem output parameter: _JVM_CP_ARGS
90-
:compilerJavaClasspathArgs
91-
set "__TOOLCHAIN=%_LIB_DIR%\scala.jar"
92-
set "__TOOLCHAIN=%__TOOLCHAIN%%_PSEP%%_LIB_DIR%\with_compiler.jar%"
93-
94-
if defined _SCALA_CPATH (
95-
set "_JVM_CP_ARGS=%__TOOLCHAIN%%_SCALA_CPATH%"
96-
) else (
97-
set "_JVM_CP_ARGS=%__TOOLCHAIN%"
98-
)
99-
goto :eof
10024

10125
@rem #########################################################################
102-
@rem ## Cleanups
26+
@rem ## Main (if needed to continue batch processing after the PowerShell script)
10327

10428
:end
10529
exit /b %_EXITCODE%

dist/bin/scalac.ps1

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
###################################################################################################
2+
### POWERSHELL SCALAC SCRIPT ###
3+
### ###
4+
### Author: Hamza REMMAL <[email protected]> ###
5+
### Since : Scala 3.6.0 ###
6+
###################################################################################################
7+
8+
# Environment setup
9+
$_PROG_HOME = $PSScriptRoot.TrimEnd('\bin\')
10+
# Load and execute the common script
11+
. "$_PROG_HOME/libexec/common.ps1"
12+
13+
###################################################################################################
14+
############################################ FUNCTIONS ############################################
15+
###################################################################################################
16+
17+
function Scala-ArgumentParsing {
18+
param ( [string[]] $params )
19+
20+
$_JAVA_ARGS = "" # TODO: THIS SHOULD BE AN ARRAY
21+
$_SCALA_ARGS = "" # TODO: THIS SHOULD BE AN ARRAY
22+
$_SCALA_CLASSPATH = ""
23+
$_CONSUME_REMAINING = $false
24+
25+
while ($params.Count -gt 0) {
26+
$arg = $params[0]
27+
if ($_CONSUME_REMAINING) {
28+
$_SCALA_ARGS += " $arg"
29+
$params = $params[1..$params.Length]
30+
} elseif ($arg -eq "--" -or $arg -eq "-script") {
31+
$_CONSUME_REMAINING = $true
32+
$_SCALA_ARGS += " $arg"
33+
$params = $params[1..$params.Length]
34+
} elseif ($arg -eq "-Oshort") {
35+
$_JAVA_ARGS += " -XX:+TieredCompilation -XX:TieredStopAtLevel=1"
36+
$_SCALA_ARGS += " -Oshort"
37+
$params = $params[1..$params.Length]
38+
} elseif ($arg.StartsWith("-D")) {
39+
$_JAVA_ARGS += " $arg"
40+
$_SCALA_ARGS += " $arg"
41+
$params = $params[1..$params.Length]
42+
} elseif ($arg.StartsWith("-J")) {
43+
$_JAVA_ARGS += " ${arg:2}"
44+
$_SCALA_ARGS += " $arg"
45+
$params = $params[1..$params.Length]
46+
} elseif ($arg -eq "-classpath" -or $arg -eq "-cp") {
47+
$_SCALA_CLASSPATH = $params[1]
48+
$params = $params[2..$params.Length]
49+
} else {
50+
$_SCALA_ARGS += " $arg"
51+
$params = $params[1..$params.Length]
52+
}
53+
}
54+
55+
return @{
56+
JAVA_ARGS = $_JAVA_ARGS
57+
SCALA_ARGS = $_SCALA_ARGS
58+
SCALA_CLASSPATH = $_SCALA_CLASSPATH
59+
}
60+
61+
}
62+
63+
function Scala-LoadCompilerClasspath {
64+
param ( [string] $SCALA_CLASSPATH )
65+
66+
$__SCALA_JAR = Join-Path $_LIB_DIR "scala.jar"
67+
$__WITH_COMPILER_JAR = Join-Path $_LIB_DIR with_compiler.jar
68+
69+
$__TOOLCHAIN = "$__SCALA_JAR$_PSEP$__WITH_COMPILER_JAR"
70+
71+
if ($SCALA_CLASSPATH) {
72+
$__TOOLCHAIN += "$_PSEP$SCALA_CLASSPATH"
73+
}
74+
return $__TOOLCHAIN
75+
}
76+
77+
78+
###################################################################################################
79+
############################################## SCRIPT #############################################
80+
###################################################################################################
81+
82+
# Parse the arguments
83+
$_ARG_RESULT = Scala-ArgumentParsing $args
84+
85+
# Compute the classpath
86+
$_CLASS_PATH = Scala-LoadCompilerClasspath $_ARG_RESULT.SCALA_CLASSPATH
87+
88+
# Fetch the arguments
89+
$_JAVA_ARGS = $_ARG_RESULT.JAVA_ARGS
90+
$_SCALA_ARGS = $_ARG_RESULT.SCALA_ARGS
91+
92+
# Build the java arguments
93+
$command = @()
94+
if (-not [string]::IsNullOrEmpty($_JAVA_ARGS)) { $command += $_JAVA_ARGS }
95+
$command += "-classpath"
96+
$command += $_CLASS_PATH
97+
$command += "-Dscala.usejavacp=true"
98+
$command += "-Dscala.home=$_PROG_HOME"
99+
$command += "dotty.tools.MainGenericCompiler"
100+
101+
if (-not [string]::IsNullOrEmpty($_SCALA_ARGS)) { $command += $_SCALA_ARGS }
102+
$commandString = $command -join ' '
103+
104+
Start-Process -FilePath $_JAVACMD -ArgumentList $commandString -NoNewWindow -Wait
105+
106+
if ($LASTEXITCODE -ne 0) { exit 1 }
107+
108+
exit 0

dist/bin/scaladoc.bat

Lines changed: 4 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -11,99 +11,19 @@ for %%f in ("%~dp0.") do (
1111
@rem get rid of the trailing slash
1212
set "_PROG_HOME=!_PROG_HOME:~0,-1!"
1313
)
14-
call "%_PROG_HOME%\libexec\common.bat"
15-
if not %_EXITCODE%==0 goto end
16-
17-
set _DEFAULT_JAVA_OPTS=-Xmx768m -Xms768m
18-
19-
call :args %*
2014

2115
@rem #########################################################################
22-
@rem ## Main
23-
24-
if defined JAVA_OPTS ( set _JAVA_OPTS=%JAVA_OPTS%
25-
) else ( set _JAVA_OPTS=%_DEFAULT_JAVA_OPTS%
26-
)
16+
@rem ## Call the new PowerShell script with arguments
2717

28-
@rem we need to escape % in the java command path, for some reason this doesnt work in common.bat
29-
set "_JAVACMD=!_JAVACMD:%%=%%%%!"
30-
31-
call "%_JAVACMD%" %_JAVA_OPTS% %_JAVA_DEBUG% %_JAVA_ARGS% ^
32-
-classpath "%_LIB_DIR%\scaladoc.jar" ^
33-
-Dscala.expandjavacp=true ^
34-
-Dscala.usejavacp=true ^
35-
dotty.tools.scaladoc.Main %_SCALA_ARGS% %_RESIDUAL_ARGS%
18+
set "args=%*"
19+
call powershell.exe -ExecutionPolicy Bypass -File "%_PROG_HOME%\bin\scaladoc.ps1" %args%
3620
if not %ERRORLEVEL%==0 (
37-
@rem echo Error: Scaladoc execution failed 1>&2
3821
set _EXITCODE=1
3922
goto end
4023
)
41-
goto end
42-
43-
@rem #########################################################################
44-
@rem ## Subroutines
45-
46-
:args
47-
set _JAVA_DEBUG=
48-
set _HELP=
49-
set _VERBOSE=
50-
set _QUIET=
51-
set _COLORS=
52-
set _SCALA_ARGS=
53-
set _JAVA_ARGS=
54-
set _RESIDUAL_ARGS=
55-
56-
:args_loop
57-
if "%~1"=="" goto args_done
58-
set "__ARG=%~1"
59-
if "%__ARG%"=="--" (
60-
@rem for arg; do addResidual "$arg"; done; set -- ;;
61-
) else if "%__ARG%"=="-h" (
62-
set _HELP=true
63-
call :addScala "-help"
64-
) else if "%__ARG%"=="-help" (
65-
set _HELP=true
66-
call :addScala "-help"
67-
) else if "%__ARG%"=="-v" (
68-
set _VERBOSE=true
69-
call :addScala "-verbose"
70-
) else if "%__ARG%"=="-verbose" (
71-
set _VERBOSE=true
72-
call :addScala "-verbose"
73-
) else if "%__ARG%"=="-debug" ( set "_JAVA_DEBUG=%_DEBUG_STR%"
74-
) else if "%__ARG%"=="-q" ( set _QUIET=true
75-
) else if "%__ARG%"=="-quiet" ( set _QUIET=true
76-
) else if "%__ARG%"=="-colors" ( set _COLORS=true
77-
) else if "%__ARG%"=="-no-colors" ( set _COLORS=
78-
) else if "%__ARG:~0,2%"=="-D" ( call :addJava "%__ARG%"
79-
) else if "%__ARG:~0,2%"=="-J" ( call :addJava "%__ARG:~2%"
80-
) else (
81-
if defined _IN_SCRIPTING_ARGS ( call :addScripting "%__ARG%"
82-
) else ( call :addResidual "%__ARG%"
83-
)
84-
)
85-
shift
86-
goto args_loop
87-
:args_done
88-
goto :eof
89-
90-
@rem output parameter: _SCALA_ARGS
91-
:addScala
92-
set _SCALA_ARGS=%_SCALA_ARGS% %~1
93-
goto :eof
94-
95-
@rem output parameter: _JAVA_ARGS
96-
:addJava
97-
set _JAVA_ARGS=%_JAVA_ARGS% %~1
98-
goto :eof
99-
100-
@rem output parameter: _RESIDUAL_ARGS
101-
:addResidual
102-
set _RESIDUAL_ARGS=%_RESIDUAL_ARGS% %~1
103-
goto :eof
10424

10525
@rem #########################################################################
106-
@rem ## Cleanups
26+
@rem ## Main (if needed to continue batch processing after the PowerShell script)
10727

10828
:end
10929
exit /b %_EXITCODE%

0 commit comments

Comments
 (0)