Skip to content

Commit a7a27cd

Browse files
committed
Migrate cli scripts to Powershell
1 parent 3dfd762 commit a7a27cd

10 files changed

+337
-256
lines changed

dist/bin/cli-common-platform.bat

-5
This file was deleted.

dist/bin/cli-common-platform.ps1

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# We need to escape % in the java command path, for some reason this doesn't work in common.bat
2+
#$_JAVACMD = $_JAVACMD -replace '%', '%%'
3+
4+
$SCALA_CLI_CMD_WIN = "`"$_JAVACMD`" -jar `"$_BIN_DIR/scala-cli.jar`""

dist/bin/common.bat

-43
This file was deleted.

dist/bin/common.ps1

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
###################################################################################################
2+
### POWERSHELL COMMON SCRIPT ###
3+
### ###
4+
### Author: Hamza REMMAL <[email protected]> ###
5+
### Since : Scala 3.6.0 ###
6+
###################################################################################################
7+
8+
9+
###################################################################################################
10+
######################################## UTILITY FUNCTIONS ########################################
11+
###################################################################################################
12+
13+
function Scala-GetJavaClasspathSeparator {
14+
if ($PSVersionTable.PSEdition -eq "Core") {
15+
return ':'
16+
} else {
17+
return ';'
18+
}
19+
}
20+
21+
function Scala-FetchScalaVersion {
22+
if (Test-Path $_VERSION_FILE) {
23+
foreach ($line in Get-Content $_VERSION_FILE) {
24+
if ($line.StartsWith("version:=")) {
25+
return $line.Substring(9)
26+
}
27+
}
28+
Write-Error "Error: 'VERSION' file does not contain 'version' entry in ($_VERSION_FILE)"
29+
} else {
30+
Write-Error "Error: 'VERSION' file is missing in ($_VERSION_FILE)"
31+
}
32+
}
33+
34+
###################################################################################################
35+
############################################ LOAD JAVA ############################################
36+
###################################################################################################
37+
38+
$javaPath = Get-Command java -ErrorAction SilentlyContinue
39+
if ($javaPath) { $_JAVACMD = $javaPath.Source }
40+
41+
if (-not (Test-Path $_JAVACMD)) {
42+
Write-Error "Error: Java executable not found ($_JAVACMD)"
43+
exit 1
44+
}
45+
46+
if (-not $_PROG_HOME) {
47+
Write-Error "Error: Variable _PROG_HOME undefined"
48+
exit 1
49+
}
50+
51+
###################################################################################################
52+
######################################## VARIOUS VARIABLES ########################################
53+
###################################################################################################
54+
55+
$_LIB_DIR = Join-Path $_PROG_HOME "lib"
56+
$_BIN_DIR = Join-Path $_PROG_HOME "bin"
57+
$_MVN_REPOSITORY = "file:///$($_PROG_HOME -replace '\\', '/')/maven2"
58+
$_VERSION_FILE = Join-Path $_PROG_HOME "VERSION"
59+
$_PSEP = Scala-GetJavaClasspathSeparator

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%\bin\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%\bin\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

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

dist/bin/scalac.bat

+4-80
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%\bin\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%

0 commit comments

Comments
 (0)