Skip to content

Commit 772f4d4

Browse files
committed
Use scala & scala_legacy in old runner migration guide; verify them with docs-tests
1 parent 4526da5 commit 772f4d4

File tree

5 files changed

+123
-61
lines changed

5 files changed

+123
-61
lines changed

build.sc

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,26 @@ trait DocsTests extends CrossSbtModule with ScalaCliScalafixModule with HasTests
172172
}
173173
def forkEnv = super.forkEnv() ++ extraEnv()
174174

175+
def constantsFile = T.persistent {
176+
val dir = T.dest / "constants"
177+
val dest = dir / "Constants.scala"
178+
val code =
179+
s"""package sclicheck
180+
|
181+
|/** Build-time constants. Generated by mill. */
182+
|object Constants {
183+
| def coursierOrg = "${Deps.coursier.dep.module.organization.value}"
184+
| def coursierCliModule = "${Deps.coursierCli.dep.module.name.value}"
185+
| def coursierCliVersion = "${Deps.Versions.coursierCli}"
186+
| def defaultScalaVersion = "${Scala.defaultUser}"
187+
|}
188+
|""".stripMargin
189+
if (!os.isFile(dest) || os.read(dest) != code)
190+
os.write.over(dest, code, createFolders = true)
191+
PathRef(dir)
192+
}
193+
def generatedSources = super.generatedSources() ++ Seq(constantsFile())
194+
175195
object test extends ScalaCliTests with ScalaCliScalafixModule {
176196
def forkEnv = super.forkEnv() ++ extraEnv() ++ Seq(
177197
"SCALA_CLI_EXAMPLES" -> (os.pwd / "examples").toString,

modules/docs-tests/src/main/scala/sclicheck/sclicheck.scala

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -187,15 +187,50 @@ def checkFile(file: os.Path, options: Options): Unit =
187187
val binDir = {
188188
val binDir0 = out / ".scala-cli"
189189
os.makeDir.all(binDir0)
190-
val escapedCommand = options.scalaCliCommand
191-
.map(arg => "\"" + arg.replace("\"", "\\\"") + "\"")
192-
.mkString(" ")
193-
val helperScript =
194-
s"""#!/usr/bin/env bash
195-
|exec $escapedCommand "$$@"
196-
|""".stripMargin
197-
os.write(binDir0 / "scala-cli", helperScript)
198-
os.perms.set(binDir0 / "scala-cli", "rwxr-xr-x")
190+
191+
def createHelperScript(command: Seq[String], scriptName: String): Unit = {
192+
val escapedCommand = command
193+
.map(arg => "\"" + arg.replace("\"", "\\\"") + "\"")
194+
.mkString(" ")
195+
val scriptCode =
196+
s"""#!/usr/bin/env bash
197+
|exec $escapedCommand "$$@"
198+
|""".stripMargin
199+
os.write(binDir0 / scriptName, scriptCode)
200+
os.perms.set(binDir0 / scriptName, "rwxr-xr-x")
201+
}
202+
createHelperScript(options.scalaCliCommand, "scala-cli")
203+
createHelperScript(options.scalaCliCommand, "scala")
204+
val coursierCliDep =
205+
s"${Constants.coursierOrg}:${Constants.coursierCliModule}:${Constants.coursierCliVersion}"
206+
createHelperScript(
207+
options.scalaCliCommand ++ Seq(
208+
"run",
209+
"--dep",
210+
coursierCliDep,
211+
"--",
212+
"launch",
213+
s"scala:${Constants.defaultScalaVersion}",
214+
"-M",
215+
"dotty.tools.MainGenericRunner",
216+
"--"
217+
),
218+
"scala_legacy"
219+
)
220+
createHelperScript(
221+
options.scalaCliCommand ++ Seq(
222+
"run",
223+
"--dep",
224+
coursierCliDep,
225+
"--",
226+
"launch",
227+
s"scala:${Constants.defaultScalaVersion}",
228+
"-M",
229+
"dotty.tools.dotc.Main",
230+
"--"
231+
),
232+
"scalac"
233+
)
199234
binDir0
200235
}
201236
val extraEnv = {

project/deps.sc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,8 @@ object Deps {
145145
def caseApp = ivy"com.github.alexarchambault::case-app:2.1.0-M28"
146146
def collectionCompat = ivy"org.scala-lang.modules::scala-collection-compat:2.12.0"
147147
// Force using of 2.13 - is there a better way?
148-
def coursier = ivy"io.get-coursier:coursier_2.13:${Versions.coursier}"
148+
def coursier = ivy"io.get-coursier:coursier_2.13:${Versions.coursier}"
149+
def coursierCli = ivy"io.get-coursier:coursier-cli_2.13:${Versions.coursierCli}"
149150
def coursierJvm = ivy"io.get-coursier:coursier-jvm_2.13:${Versions.coursier}"
150151
.exclude(("com.github.plokhotnyuk.jsoniter-scala", "jsoniter-scala-core_2.13"))
151152
def coursierLauncher = ivy"io.get-coursier:coursier-launcher_2.13:${Versions.coursier}"

website/docs/guides/introduction/old-runner-migration.md

Lines changed: 53 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,12 @@ Scala CLI is available as the `scala` command alongside the Scala distribution i
2929
Yes, even though its usage has been deprecated, it is still available under the `scala_legacy` command.
3030
However, it is likely to be dropped in a future version.
3131

32-
```bash ignore
33-
scala_legacy
32+
```bash
33+
scala_legacy -version
3434
# [warning] MainGenericRunner class is deprecated since Scala 3.5.0, and Scala CLI features will not work.
3535
# [warning] Please be sure to update to the Scala CLI launcher to use the new features.
3636
# [warning] Check the Scala 3.5.0 release notes to troubleshoot your installation.
37-
# Welcome to Scala 3.5.0 (17, Java OpenJDK 64-Bit Server VM).
38-
# Type in expressions for evaluation. Or try :help.
39-
#
40-
# scala>
37+
# Scala code runner version 3.5.0 -- Copyright 2002-2024, LAMP/EPFL
4138
```
4239

4340
## How has the passing of arguments been changed from the old `scala` runner to Scala CLI?
@@ -46,55 +43,63 @@ Let us take a closer look on how the old runner handled arguments when compared
4643

4744
### The old ways
4845

49-
In the old `scala` runner, the first argument was treated as the input source, while the second and following arguments
46+
In the old runner, the first argument was treated as the input source, while the second and following arguments
5047
were considered program arguments.
5148

52-
```bash ignore
53-
scala Source.scala programArg1 programArg2
49+
```scala title=Source.scala
50+
@main def main(args: String*): Unit = println(args.mkString(" "))
51+
```
52+
53+
```bash
54+
scala_legacy Source.scala programArg1 programArg2
5455
```
5556

5657
Since everything after the first argument had to be arbitrarily read as a program argument, regardless of format, all
5758
runner options had to be passed before the source input.
5859

59-
```bash ignore
60-
scala -save script.sc programArg1 programArg2
60+
```bash
61+
scala_legacy -save Source.scala programArg1 programArg2
6162
```
6263

6364
### The ways of Scala CLI
6465

6566
With Scala CLI's default way of handling arguments, inputs and program arguments have to be
6667
divided by `--`. There is no limit for the number of either.
6768

69+
```scala title=Source2.scala
70+
def placeholder = println("Example extra source")
71+
```
72+
6873
```bash ignore
69-
scala-cli Source1.scala Source2.scala -- programArg1 programArg2
74+
scala Source.scala Source2.scala -- programArg1 programArg2
7075
```
7176

7277
Additionally, a Scala CLI sub-command can be passed before the inputs section.
7378
For example, to call the above example specifying the `run` sub-command explicitly, pass it like this:
7479

75-
```bash ignore
76-
scala-cli run Source1.scala Source2.scala -- programArg1 programArg2
80+
```bash
81+
scala run Source.scala Source2.scala -- programArg1 programArg2
7782
```
7883

7984
More on sub-commands can be found [here](../../commands/basics.md).
8085

8186
Runner options can be passed on whatever position in the inputs section (before `--`).
8287
For example, all the following examples are correct ways to specify the Scala version explicitly as `3.2`
8388

84-
```bash ignore
85-
scala-cli -S 3.2 Source1.scala Source2.scala -- programArg1 programArg2
86-
scala-cli Source1.scala -S 3.2 Source2.scala -- programArg1 programArg2
87-
scala-cli Source1.scala Source2.scala -S 3.2 -- programArg1 programArg2
89+
```bash
90+
scala -S 3.2 Source.scala Source2.scala -- programArg1 programArg2
91+
scala Source.scala -S 3.2 Source2.scala -- programArg1 programArg2
92+
scala Source.scala Source2.scala -S 3.2 -- programArg1 programArg2
8893
```
8994

9095
:::note
9196
The exception to this rule are the launcher options, like `--cli-version` or `--cli-scala-version`.
9297
Those have to be passed before the inputs section (before any source inputs).
9398

94-
For example, to explicitly specify the launcher should run Scala CLI `v0.1.20`, pass it like this:
99+
For example, to explicitly specify the launcher should run Scala CLI `v1.5.0`, pass it like this:
95100

96-
```bash ignore
97-
scala-cli --cli-version 0.1.20 Source1.scala Source2.scala -- programArg1 programArg2
101+
```bash
102+
scala --cli-version 1.5.0 Source.scala Source2.scala -- programArg1 programArg2
98103
```
99104

100105
Also, if a Scala CLI sub-command is being passed explicitly, all launcher options have to be passed before the
@@ -103,7 +108,7 @@ sub-command.
103108
For example, to call [the `package` sub-command](../../commands/package.md) using the nightly CLI version, do it like this:
104109

105110
```bash ignore
106-
scala-cli --cli-version nightly package --help
111+
scala --cli-version nightly package --help
107112
```
108113

109114
:::
@@ -114,8 +119,8 @@ To provide better support for shebang scripts, Scala CLI
114119
has [a dedicated `shebang` sub-command](../../commands/shebang.md), which handles arguments similarly to the old `scala`
115120
script.
116121

117-
```bash ignore
118-
scala-cli shebang Source.scala programArg1 programArg2
122+
```bash
123+
scala shebang Source.scala programArg1 programArg2
119124
```
120125

121126
The purpose of the `shebang` sub-command is essentially to only be used in a shebang header (more
@@ -231,12 +236,12 @@ object Main {
231236

232237
<ChainedSnippets>
233238

234-
```bash ignore
235-
scala Main.scala Hello world
239+
```bash
240+
scala_legacy Main.scala Hello world
236241
```
237242

238243
```bash
239-
scala-cli Main.scala -- Hello world
244+
scala Main.scala -- Hello world
240245
```
241246

242247
```text
@@ -259,7 +264,7 @@ In other words, when explicitly declaring a main class when working with Scala C
259264
file.
260265

261266
```bash
262-
scala-cli main-in-script.sc -- Hello world
267+
scala main-in-script.sc -- Hello world
263268
# no output will be printed
264269
```
265270

@@ -280,7 +285,7 @@ However, it is supported by Scala CLI.
280285
<ChainedSnippets>
281286

282287
```bash
283-
scala-cli script.sc -- Hello world
288+
scala script.sc -- Hello world
284289
```
285290

286291
```text
@@ -303,7 +308,7 @@ However, both the old Scala `3.x` runner as well as Scala CLI do not support it.
303308
<ChainedSnippets>
304309

305310
```bash fail
306-
scala-cli script.scala -- Hello world
311+
scala script.scala -- Hello world
307312
```
308313

309314
```text
@@ -347,15 +352,15 @@ That is, all arguments starting with the second were treated as program args, ra
347352
This is in contrast with the Scala CLI default way of handling arguments, where inputs and program arguments have to be
348353
divided by `--`.
349354

350-
```bash ignore
351-
scala-cli Source1.scala Source2.scala -- programArg1 programArg2
355+
```bash
356+
scala Source.scala Source2.scala -- programArg1 programArg2
352357
```
353358

354359
To better support shebang scripts, Scala CLI has a dedicated `shebang` sub-command, which handles arguments similarly to
355360
the old `scala` script.
356361

357-
```bash ignore
358-
scala-cli shebang Source.scala programArg1 programArg2
362+
```bash
363+
scala shebang Source.scala programArg1 programArg2
359364
```
360365

361366
For more concrete examples on how to change the shebang header in your existing scripts, look below.
@@ -409,17 +414,18 @@ implicitly run any compiled class files it would find.
409414
```
410415

411416
This syntax has been dropped and is no longer supported with the new `scala` runner.
412-
```bash ignore
417+
```bash
413418
scalac hello.scala
414-
scala hello # NOTE: this syntax is not supported by Scala CLI
419+
scala_legacy hello # NOTE: this syntax is not supported by Scala CLI
415420
# Hello
416421
```
417422

418423
With Scala CLI, all inputs have to be passed explicitly, so any compiled classes in the current working directory
419424
would be ignored unless passed explicitly.
420425
```bash ignore
426+
scala clean .
421427
scalac hello.scala
422-
scala-cli run -cp .
428+
scala run -cp .
423429
# Hello
424430
```
425431

@@ -428,8 +434,9 @@ If only the classpath is passed with `-cp`, then the `run` sub-command can't be
428434
will default to the REPL (as there are no explicit source file inputs present).
429435

430436
```bash ignore
437+
scala clean .
431438
scalac hello.scala
432-
scala-cli -cp .
439+
scala -cp .
433440
# Welcome to Scala 3.5.0 (17, Java OpenJDK 64-Bit Server VM).
434441
# Type in expressions for evaluation. Or try :help.
435442
#
@@ -440,8 +447,9 @@ scala-cli -cp .
440447
It is possible to explicitly specify the main class to be run (for example, if there are multiple main classes
441448
in the build). The `run` sub-command becomes optional then, as passing `-M` indicates the intention to run something.
442449
```bash
450+
scala clean .
443451
scalac hello.scala
444-
scala-cli -cp . -M hello
452+
scala -cp . -M hello
445453
# Hello
446454
```
447455

@@ -451,16 +459,17 @@ with the `compile` sub-command, rather than the `scalac` script.
451459

452460
You don't have to specify the class files location, Scala CLI won't recompile them if they are up to date.
453461
```bash ignore
454-
scala-cli compile hello.scala
455-
scala-cli hello.scala
462+
scala clean hello.scala
463+
scala compile hello.scala
464+
scala hello.scala
456465
# Hello
457466
```
458467

459468
Alternatively, you can also specify the location for the compiled classes explicitly, and then add them
460469
to the classpath, as you would with `scalac`.
461-
```bash ignore
462-
scala-cli compile hello.scala -d compiled_classes
463-
scala-cli hello.scala
470+
```bash
471+
scala compile hello.scala -d compiled_classes
472+
scala run -cp compiled_classes
464473
# Hello
465474
```
466475
:::

website/docs/reference/scala-command/index.md

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ bit slower than a native launcher.
4949
Also, the specified version (and potentially any of its dependencies, if they are not already installed) would be
5050
downloaded if it's not available in the local cache, so it may require additional setup for isolated environments.
5151

52-
```bash ignore
52+
```bash
5353
scala --cli-version 1.5.0 version
5454
# Scala CLI version: 1.5.0
5555
# Scala version (default): 3.5.0
@@ -82,13 +82,10 @@ migrate any existing scripts and automations to Scala CLI under either `scala` o
8282
command may be dropped at some point in the future.
8383
:::
8484

85-
```bash ignore
86-
scala_legacy
85+
```bash
86+
scala_legacy -version
8787
# [warning] MainGenericRunner class is deprecated since Scala 3.5.0, and Scala CLI features will not work.
8888
# [warning] Please be sure to update to the Scala CLI launcher to use the new features.
8989
# [warning] Check the Scala 3.5.0 release notes to troubleshoot your installation.
90-
# Welcome to Scala 3.5.0 (17, Java OpenJDK 64-Bit Server VM).
91-
# Type in expressions for evaluation. Or try :help.
92-
#
93-
# scala>
90+
# Scala code runner version 3.5.0 -- Copyright 2002-2024, LAMP/EPFL
9491
```

0 commit comments

Comments
 (0)