Skip to content

Commit 128c015

Browse files
committed
Add support for sclicheck to run bash snippets with clean context
1 parent 772f4d4 commit 128c015

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

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

Lines changed: 19 additions & 7 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"
@@ -274,7 +278,15 @@ def checkFile(file: os.Path, options: Options): Unit =
274278
res.exitCode
275279

276280
cmd match
277-
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)
278290
val script = out / ".scala-build" / "run.sh"
279291
os.write.over(script, mkBashScript(cmds), createFolders = true)
280292
os.perms.set(script, "rwxr-xr-x")

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -422,8 +422,7 @@ scala_legacy hello # NOTE: this syntax is not supported by Scala CLI
422422

423423
With Scala CLI, all inputs have to be passed explicitly, so any compiled classes in the current working directory
424424
would be ignored unless passed explicitly.
425-
```bash ignore
426-
scala clean .
425+
```bash clean
427426
scalac hello.scala
428427
scala run -cp .
429428
# Hello
@@ -434,7 +433,6 @@ If only the classpath is passed with `-cp`, then the `run` sub-command can't be
434433
will default to the REPL (as there are no explicit source file inputs present).
435434

436435
```bash ignore
437-
scala clean .
438436
scalac hello.scala
439437
scala -cp .
440438
# Welcome to Scala 3.5.0 (17, Java OpenJDK 64-Bit Server VM).
@@ -446,8 +444,7 @@ scala -cp .
446444

447445
It is possible to explicitly specify the main class to be run (for example, if there are multiple main classes
448446
in the build). The `run` sub-command becomes optional then, as passing `-M` indicates the intention to run something.
449-
```bash
450-
scala clean .
447+
```bash clean
451448
scalac hello.scala
452449
scala -cp . -M hello
453450
# Hello
@@ -458,8 +455,7 @@ If you want to compile your sources with a separate command, and then run them l
458455
with the `compile` sub-command, rather than the `scalac` script.
459456

460457
You don't have to specify the class files location, Scala CLI won't recompile them if they are up to date.
461-
```bash ignore
462-
scala clean hello.scala
458+
```bash clean
463459
scala compile hello.scala
464460
scala hello.scala
465461
# Hello

0 commit comments

Comments
 (0)