Skip to content

Commit 7f5b16f

Browse files
authored
Merge pull request #3155 from Gedochao/docs/scala-cli-as-scala-revamp
Update Scala CLI as `scala` related docs
2 parents e99f770 + d006e26 commit 7f5b16f

File tree

8 files changed

+290
-79
lines changed

8 files changed

+290
-79
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ Scala CLI is a command-line tool to interact with the Scala language. It lets yo
88
Scala code. (and more!) It shares some similarities with build tools, but it doesn't aim at supporting multi-module
99
projects, nor to be extended via a task system.
1010

11-
As of [SIP-46](https://github.com/scala/improvement-proposals/pull/46), Scala CLI has been accepted as the new `scala`
12-
command and is currently in the experimental phase. If you want to try it out, check for more
13-
details [here](https://scala-cli.virtuslab.org/docs/reference/scala-command/).
11+
As of Scala 3.5.0, Scala CLI has become the official `scala` runner of the language (for more information
12+
refer to [SIP-46](https://github.com/scala/improvement-proposals/pull/46)). For more details on using Scala CLI
13+
via the `scala` command, refer to [this doc](https://scala-cli.virtuslab.org/docs/reference/scala-command/).
1414

1515
## Docs
1616
- user-facing documentation: [scala-cli.virtuslab.org](https://scala-cli.virtuslab.org/)

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: 63 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import scala.util.matching.Regex
1313
val SnippetBlock = """ *(`{2}`+)[^ ]+ title=([\w\d.\-/_]+) *""".r
1414
val CompileBlock = """ *(`{2}`+) *(\w+) +(compile|fail) *(?:title=([\w\d.\-/_]+))? *(power)? *""".r
1515
def compileBlockEnds(backticks: String) = s""" *$backticks *""".r
16-
val BashCommand = """ *```bash *(fail|run-fail)? *""".r
16+
val BashCommand = """ *```bash *(fail|run-fail)? *(clean)? *""".r
1717
val CheckBlock = """ *\<\!-- Expected(-regex)?: *""".r
1818
val CheckBlockEnd = """ *\--> *""".r
1919
val Clear = """ *<!--+ *clear *-+-> *""".r
@@ -35,8 +35,12 @@ enum Commands:
3535
case Check(patterns, regex, _) =>
3636
val kind = if regex then "regexes" else "patterns"
3737
s"last output matches $kind: ${patterns.map(p => s"'$p'").mkString(", ")}"
38-
case Run(cmd, shouldFail, _) =>
39-
val prefix = if shouldFail then "[failure expected] " else ""
38+
case Run(cmd, shouldFail, shouldClean, _) =>
39+
val prefix = shouldFail -> shouldClean match
40+
case (true, true) => "[failure expected, clean]"
41+
case (true, false) => "[failure expected]"
42+
case (false, true) => "[clean]"
43+
case _ => ""
4044
cmd.mkString(prefix, " ", "")
4145
case Write(name, _, _) =>
4246
name
@@ -53,7 +57,7 @@ enum Commands:
5357
shouldFail: Boolean,
5458
power: Boolean
5559
)
56-
case Run(scriptLines: Seq[String], shouldFail: Boolean, context: Context)
60+
case Run(scriptLines: Seq[String], shouldFail: Boolean, shouldClean: Boolean, context: Context)
5761
case Check(patterns: Seq[String], regex: Boolean, context: Context)
5862
case Clear(context: Context)
5963

@@ -100,8 +104,8 @@ def parse(content: Seq[String], currentCommands: Seq[Commands], context: Context
100104
compileBlockEnds(backticks)
101105
)
102106

103-
case BashCommand(failGroup) :: tail =>
104-
parseMultiline(tail, Commands.Run(_, failGroup != null, context))
107+
case BashCommand(failGroup, clean) :: tail =>
108+
parseMultiline(tail, Commands.Run(_, failGroup != null, clean != null, context))
105109

106110
case CheckBlock(regexOpt) :: tail =>
107111
val isRegex = regexOpt == "-regex"
@@ -187,15 +191,50 @@ def checkFile(file: os.Path, options: Options): Unit =
187191
val binDir = {
188192
val binDir0 = out / ".scala-cli"
189193
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")
194+
195+
def createHelperScript(command: Seq[String], scriptName: String): Unit = {
196+
val escapedCommand = command
197+
.map(arg => "\"" + arg.replace("\"", "\\\"") + "\"")
198+
.mkString(" ")
199+
val scriptCode =
200+
s"""#!/usr/bin/env bash
201+
|exec $escapedCommand "$$@"
202+
|""".stripMargin
203+
os.write(binDir0 / scriptName, scriptCode)
204+
os.perms.set(binDir0 / scriptName, "rwxr-xr-x")
205+
}
206+
createHelperScript(options.scalaCliCommand, "scala-cli")
207+
createHelperScript(options.scalaCliCommand, "scala")
208+
val coursierCliDep =
209+
s"${Constants.coursierOrg}:${Constants.coursierCliModule}:${Constants.coursierCliVersion}"
210+
createHelperScript(
211+
options.scalaCliCommand ++ Seq(
212+
"run",
213+
"--dep",
214+
coursierCliDep,
215+
"--",
216+
"launch",
217+
s"scala:${Constants.defaultScalaVersion}",
218+
"-M",
219+
"dotty.tools.MainGenericRunner",
220+
"--"
221+
),
222+
"scala_legacy"
223+
)
224+
createHelperScript(
225+
options.scalaCliCommand ++ Seq(
226+
"run",
227+
"--dep",
228+
coursierCliDep,
229+
"--",
230+
"launch",
231+
s"scala:${Constants.defaultScalaVersion}",
232+
"-M",
233+
"dotty.tools.dotc.Main",
234+
"--"
235+
),
236+
"scalac"
237+
)
199238
binDir0
200239
}
201240
val extraEnv = {
@@ -239,7 +278,15 @@ def checkFile(file: os.Path, options: Options): Unit =
239278
res.exitCode
240279

241280
cmd match
242-
case Commands.Run(cmds, shouldFail, _) =>
281+
case Commands.Run(cmds, shouldFail, shouldClean, _) =>
282+
if shouldClean then
283+
os.list(out)
284+
.filterNot(_ == binDir)
285+
.filterNot(_.last.endsWith(".scala"))
286+
.filterNot(_.last.endsWith(".sc"))
287+
.filterNot(_.last.endsWith(".java"))
288+
.filterNot(_.last.endsWith(".md"))
289+
.foreach(os.remove.all)
243290
val script = out / ".scala-build" / "run.sh"
244291
os.write.over(script, mkBashScript(cmds), createFolders = true)
245292
os.perms.set(script, "rwxr-xr-x")

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/_advanced_install.mdx

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,17 @@ import SectionAbout from "../src/components/SectionAbout"
44
import DownloadButton from "../src/components/DownloadButton"
55
import {currentOs} from "../src/components/osUtils";
66

7+
8+
<SectionAbout title="Scala 3 installation"/>
9+
<div className="row">
10+
<div className="col col--9 col--offset-1 padding--lg">
11+
If you install Scala 3.5.0 or newer on your machine, you can use Scala CLI under the `scala` command. <br/>
12+
For Scala 3 installation instructions refer <a className="no_monospace" href="https://www.scala-lang.org/download/">here</a>.
13+
14+
Note that even when installing the latest Scala 3 version, its `scala` command may refer to an older Scala CLI version.
15+
For the latest stable Scala CLI release it usually is necessary to install it separately.
16+
</div>
17+
</div>
718
<SectionAbout title="Advanced Installation">
819
<div className="margin--lg"/>
920
<Tabs
@@ -550,7 +561,7 @@ Scala Native page contains detailed [installation guide](https://scala-native.re
550561
</div></div>
551562

552563

553-
<SectionAbout title="Uninstall scala CLI">
564+
<SectionAbout title="Uninstall Scala CLI">
554565
<div className="margin--lg"/>
555566
<Tabs
556567
groupId="uninstall-specific"

0 commit comments

Comments
 (0)