Skip to content

Commit cdc77be

Browse files
committed
Add Powershell script for 'scala'
1 parent 12c8b17 commit cdc77be

File tree

3 files changed

+64
-87
lines changed

3 files changed

+64
-87
lines changed

dist/bin/cli-common-platform.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# We need to escape % in the java command path, for some reason this doesn't work in common.bat
2-
$global:_JAVACMD = $global:_JAVACMD -replace '%', '%%'
2+
#$_JAVACMD = $_JAVACMD -replace '%', '%%'
33

4-
$global:SCALA_CLI_CMD_WIN = "`"$global:_JAVACMD`" -jar `"$global:_PROG_HOME\bin\scala-cli.jar`""
4+
$SCALA_CLI_CMD_WIN = "`"$_JAVACMD`" -jar `"$_BIN_DIR/scala-cli.jar`""

dist/bin/common.ps1

Lines changed: 47 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,60 @@
1-
# #########################################################################
2-
# ## Code common to scalac.bat, scaladoc.bat and scala.bat
3-
4-
if ($env:JAVACMD) {
5-
$global:_JAVACMD = $env:JAVACMD
6-
} elseif ($env:JAVA_HOME) {
7-
$global:_JAVACMD = Join-Path $env:JAVA_HOME "bin\java.exe"
8-
} elseif ($env:JDK_HOME) {
9-
$global:_JAVACMD = Join-Path $env:JDK_HOME "bin\java.exe"
10-
} else {
11-
$javaPath = Get-Command java.exe -ErrorAction SilentlyContinue
12-
if ($javaPath) {
13-
$javaPathDir = Split-Path $javaPath -Parent
14-
if ($javaPathDir -notmatch "javapath") {
15-
$global:_JAVACMD = Join-Path $javaPathDir "java.exe"
16-
}
1+
###################################################################################################
2+
### POWERSHELL COMMON SCRIPT ###
3+
### ###
4+
### Author: Hamza REMMAL <[email protected]> ###
5+
### Since : Scala 3.5.0 ###
6+
###################################################################################################
7+
8+
9+
###################################################################################################
10+
######################################## UTILITY FUNCTIONS ########################################
11+
###################################################################################################
12+
13+
function Scala-GetJavaClasspathSeparator {
14+
if ($IsWindows) {
15+
return ';'
16+
} else {
17+
return ':'
1718
}
19+
}
1820

19-
if (-not $global:_JAVACMD) {
20-
$programFilesJava = Join-Path $env:ProgramFiles "Java"
21-
$javaHome = Get-ChildItem -Path $programFilesJava -Directory -Filter "jre*" | Select-Object -First 1
22-
if ($javaHome) {
23-
$global:_JAVA_HOME = $javaHome.FullName
24-
} else {
25-
$optPath = "C:\opt"
26-
$javaHome = Get-ChildItem -Path $optPath -Directory -Filter "jdk*" | Select-Object -First 1
27-
if ($javaHome) {
28-
$global:_JAVA_HOME = Join-Path $javaHome.FullName "jre"
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)
2926
}
3027
}
31-
32-
if ($global:_JAVA_HOME) {
33-
$global:_JAVACMD = Join-Path $global:_JAVA_HOME "bin\java.exe"
34-
}
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)"
3531
}
3632
}
3733

38-
if (-not (Test-Path $global:_JAVACMD)) {
39-
Write-Error "Error: Java executable not found ($global:_JAVACMD)"
40-
$global:_EXITCODE = 1
41-
exit $global:_EXITCODE
34+
35+
###################################################################################################
36+
############################################ LOAD JAVA ############################################
37+
###################################################################################################
38+
39+
$javaPath = Get-Command java -ErrorAction SilentlyContinue
40+
if ($javaPath) { $_JAVACMD = $javaPath.Source }
41+
42+
if (-not (Test-Path $_JAVACMD)) {
43+
Write-Error "Error: Java executable not found ($_JAVACMD)"
44+
exit 1
4245
}
4346

44-
if (-not $global:_PROG_HOME) {
47+
if (-not $_PROG_HOME) {
4548
Write-Error "Error: Variable _PROG_HOME undefined"
46-
$global:_EXITCODE = 1
47-
exit $global:_EXITCODE
49+
exit 1
4850
}
4951

50-
$global:_ETC_DIR = Join-Path $global:_PROG_HOME "etc"
52+
###################################################################################################
53+
######################################## VARIOUS VARIABLES ########################################
54+
###################################################################################################
5155

52-
$global:_PSEP = ";"
56+
$_ETC_DIR = Join-Path $_PROG_HOME "etc"
57+
$_BIN_DIR = Join-Path $_PROG_HOME "bin"
58+
$_MVN_REPOSITORY = "file:///$($_PROG_HOME -replace '\\', '/')/maven2"
59+
$_VERSION_FILE = Join-Path $_PROG_HOME "VERSION"
60+
$_PSEP = Scala-GetJavaClasspathSeparator

dist/bin/scala.ps1

Lines changed: 15 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,20 @@
1-
# Environment setup
2-
$global:_EXITCODE = 0
3-
4-
$scriptPath = $PSScriptRoot
5-
$global:_PROG_HOME = $scriptPath.TrimEnd('\')
6-
7-
. "$_PROG_HOME\bin\common.bat"
8-
if ($LASTEXITCODE -ne 0) { goto end }
1+
###################################################################################################
2+
### POWERSHELL SCALA SCRIPT ###
3+
### ###
4+
### Author: Hamza REMMAL <[email protected]> ###
5+
### Since : Scala 3.5.0 ###
6+
###################################################################################################
97

10-
# Main
11-
setScalaOpts
12-
13-
. "$_PROG_HOME\bin\cli-common-platform.ps1"
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
1414

1515
# SCALA_CLI_CMD_WIN is an array, set in cli-common-platform.bat.
1616
# WE NEED TO PASS '--skip-cli-updates' for JVM launchers but we actually don't need it for native launchers
17-
& $env:SCALA_CLI_CMD_WIN "--prog-name" "scala" "--skip-cli-updates" "--cli-default-scala-version" "$env:_SCALA_VERSION" "-r" "$env:MVN_REPOSITORY" $args
18-
19-
if ($LASTEXITCODE -ne 0) {
20-
$global:_EXITCODE = 1
21-
goto end
22-
}
23-
24-
goto end
25-
26-
# Subroutines
27-
28-
function setScalaOpts {
29-
# Find the index of the first colon in _PROG_HOME
30-
$index = $global:_PROG_HOME.IndexOf(':')
31-
if ($index -eq -1) { $index = 0 }
32-
33-
$global:_SCALA_VERSION = ""
34-
$global:MVN_REPOSITORY = "file:///$($global:_PROG_HOME -replace '\\', '/')/maven2"
35-
36-
# Read version from VERSION_FILE
37-
$versionFilePath = "$global:_PROG_HOME\VERSION"
38-
if (Test-Path $versionFilePath) {
39-
$lines = Get-Content $versionFilePath
40-
foreach ($line in $lines) {
41-
if ($line.StartsWith("version:=")) {
42-
$global:_SCALA_VERSION = $line.Substring(9)
43-
break
44-
}
45-
}
46-
}
47-
}
17+
& "$_JAVACMD" "-jar" "$_BIN_DIR/scala-cli.jar" "--prog-name" "scala" "--skip-cli-updates" "--cli-default-scala-version" "$_SCALA_VERSION" "-r" "$_MVN_REPOSITORY" $args
4818

49-
# Cleanups
50-
end
51-
exit $global:_EXITCODE
19+
if ($LASTEXITCODE -ne 0) { exit 1 }
20+
exit 0

0 commit comments

Comments
 (0)