Skip to content

Commit 66b5168

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

12 files changed

+323
-272
lines changed

dist/bin/scala.bat

+5-47
Original file line numberDiff line numberDiff line change
@@ -11,57 +11,15 @@ 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 %ERRORLEVEL% neq 0 (
21+
set _EXITCODE=1
5722
)
5823

59-
:foundVersion
60-
goto :eof
61-
62-
@rem #########################################################################
63-
@rem ## Cleanups
64-
65-
:end
6624
exit /b %_EXITCODE%
6725
endlocal

dist/bin/scala.ps1

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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"
12+
. "$_LIBEXEC_DIR/cli-common-platform.ps1"
13+
14+
# WE NEED TO PASS '--skip-cli-updates' for JVM launchers but we actually don't need it for native launchers
15+
& $SCALA_CLI_CMD_WIN "--prog-name" "scala" "--skip-cli-updates" "--cli-default-scala-version" "$(Get-FetchScalaVersion)" "-r" "$_MVN_REPOSITORY" $args
16+
17+
if ($LASTEXITCODE -ne 0) { exit 1 }
18+
exit 0

dist/bin/scalac.bat

+4-85
Original file line numberDiff line numberDiff line change
@@ -11,96 +11,15 @@ 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
16+
@rem ## Call the new PowerShell script with arguments
2317

24-
@rem we need to escape % in the java command path, for some reason this doesnt work in common.bat
25-
set "_JAVACMD=!_JAVACMD:%%=%%%%!"
26-
27-
call "%_JAVACMD%" %_JAVA_ARGS% -classpath "%_JVM_CP_ARGS%" "-Dscala.usejavacp=true" "-Dscala.expandjavacp=true" "-Dscala.home=%_PROG_HOME%" dotty.tools.MainGenericCompiler %_SCALA_ARGS%
28-
if not %ERRORLEVEL%==0 (
18+
set "args=%*"
19+
call powershell.exe -ExecutionPolicy Bypass -File "%_PROG_HOME%\bin\scalac.ps1" %args%
20+
if %ERRORLEVEL% neq 0 (
2921
set _EXITCODE=1
30-
goto end
3122
)
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
100-
101-
@rem #########################################################################
102-
@rem ## Cleanups
10323

104-
:end
10524
exit /b %_EXITCODE%
10625
endlocal

dist/bin/scalac.ps1

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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 Get-ScalaArgumentParsing {
18+
param ( [string[]] $params )
19+
20+
$_JAVA_ARGS = @()
21+
$_SCALA_ARGS = @()
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"
36+
$_JAVA_ARGS += "-XX:TieredStopAtLevel=1"
37+
$_SCALA_ARGS += "-Oshort"
38+
$params = $params[1..$params.Length]
39+
} elseif ($arg.StartsWith("-D")) {
40+
$_JAVA_ARGS += "$arg"
41+
$_SCALA_ARGS += "$arg"
42+
$params = $params[1..$params.Length]
43+
} elseif ($arg.StartsWith("-J")) {
44+
$_JAVA_ARGS += "${arg:2}"
45+
$_SCALA_ARGS += "$arg"
46+
$params = $params[1..$params.Length]
47+
} elseif ($arg -eq "-classpath" -or $arg -eq "-cp") {
48+
$_SCALA_CLASSPATH = $params[1]
49+
$params = $params[2..$params.Length]
50+
} else {
51+
$_SCALA_ARGS += "$arg"
52+
$params = $params[1..$params.Length]
53+
}
54+
}
55+
56+
return @{
57+
JAVA_ARGS = $_JAVA_ARGS
58+
SCALA_ARGS = $_SCALA_ARGS
59+
SCALA_CLASSPATH = $_SCALA_CLASSPATH
60+
}
61+
62+
}
63+
64+
function Get-ScalaLoadCompilerClasspath {
65+
param ( [string] $SCALA_CLASSPATH )
66+
$__TOOLCHAIN = @()
67+
$__TOOLCHAIN += Join-Path $_LIB_DIR "scala.jar"
68+
$__TOOLCHAIN += Join-Path $_LIB_DIR "with_compiler.jar"
69+
if ($SCALA_CLASSPATH) { $__TOOLCHAIN += "$SCALA_CLASSPATH" }
70+
return $__TOOLCHAIN -join $_PSEP
71+
}
72+
73+
74+
###################################################################################################
75+
############################################## SCRIPT #############################################
76+
###################################################################################################
77+
78+
# Parse the arguments
79+
$_PARSE_RESULT = Get-ScalaArgumentParsing $args
80+
81+
# Build the java arguments
82+
$command = @()
83+
$command += $_PARSE_RESULT.JAVA_ARGS
84+
$command += "-classpath"
85+
$command += "$(Get-ScalaLoadCompilerClasspath $_PARSE_RESULT.SCALA_CLASSPATH)"
86+
$command += "-Dscala.expandjavacp=true"
87+
$command += "-Dscala.usejavacp=true"
88+
$command += "-Dscala.home=$_PROG_HOME"
89+
$command += "dotty.tools.MainGenericCompiler"
90+
$command += $_PARSE_RESULT.SCALA_ARGS
91+
92+
$_JAVA_PROCESS = Start-Process -FilePath $_JAVACMD -ArgumentList $($command -join ' ') -NoNewWindow -PassThru -Wait
93+
94+
if ($_JAVA_PROCESS.ExitCode -ne 0) { exit 1 }
95+
96+
exit 0

dist/bin/scaladoc.bat

+4-89
Original file line numberDiff line numberDiff line change
@@ -11,100 +11,15 @@ 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%
36-
if not %ERRORLEVEL%==0 (
37-
@rem echo Error: Scaladoc execution failed 1>&2
18+
set "args=%*"
19+
call powershell.exe -ExecutionPolicy Bypass -File "%_PROG_HOME%\bin\scaladoc.ps1" %args%
20+
if %ERRORLEVEL% neq 0 (
3821
set _EXITCODE=1
39-
goto end
4022
)
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
104-
105-
@rem #########################################################################
106-
@rem ## Cleanups
10723

108-
:end
10924
exit /b %_EXITCODE%
11025
endlocal

0 commit comments

Comments
 (0)