Skip to content

Commit 38da491

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

12 files changed

+349
-269
lines changed

dist/bin/scala.bat

+7-44
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

+21
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

+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

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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.expandjavacp=true"
98+
$command += "-Dscala.usejavacp=true"
99+
$command += "-Dscala.home=$_PROG_HOME"
100+
$command += "dotty.tools.MainGenericCompiler"
101+
102+
if (-not [string]::IsNullOrEmpty($_SCALA_ARGS)) { $command += $_SCALA_ARGS }
103+
$commandString = $command -join ' '
104+
105+
$_JAVA_PROCESS = Start-Process -FilePath $_JAVACMD -ArgumentList $commandString -NoNewWindow -PassThru -Wait
106+
107+
if ($_JAVA_PROCESS.ExitCode -ne 0) { exit 1 }
108+
109+
exit 0

0 commit comments

Comments
 (0)