Skip to content

Commit 3e95e74

Browse files
authored
Merge pull request #199 from romanowski/docs/demo-gif
Add proper demo gif
2 parents afe3262 + c2c2df5 commit 3e95e74

File tree

23 files changed

+368
-151
lines changed

23 files changed

+368
-151
lines changed

docs_checker/Readme.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
# Docs checker - simple tool to verify scala-cli cookbooks
22

33
Docs checker tests documentation is using simple regexes to run following actions:
4-
- extract/override code snippet to file in workspace for snippets starting with ````scala name:<file-name>` for example:
4+
- extract/override code snippet to file in workspace for snippets starting with ````scala title=<file-name>` for example:
55

66
````
7-
```scala-cli
7+
```bash
88
scala-cli ScalaVersion.scala
99
```
1010
````
1111

12-
- run scala-cli commands (or any commands) for snippets starting with ````scala-cli` for example:
12+
- run scala-cli commands (or any commands) for snippets starting with ````bash` for example:
1313

1414
````
15-
```scala name:ScalaVersion.scala
15+
```scala title=ScalaVersion.scala
1616
object ScalaVersion extends App
1717
```
1818
````

docs_checker/check.scala

Lines changed: 45 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,19 @@
11
// using scala 3.0.2
2-
// using "org.scalameta::munit:0.7.29"
32
// using com.lihaoyi::os-lib:0.7.8
43

54
import scala.util.matching.Regex
65

7-
import munit.Assertions.assert
8-
9-
val ScalaCodeBlock = """ *```scala name\:([\w\.]+)+""".r
10-
val CodeBlockEnds = """ *```""".r
11-
val ScalaCliBlock = """ *```scala-cli( +fail)?""".r
12-
val CheckBlock = """ *\<\!\-\- Expected(-regex):""".r
13-
val CheckBlockEnd = """ *\-\-\>""".r
6+
val ScalaCodeBlock = """ *```scala title=([\w\.]+) *""".r
7+
val CodeBlockEnds = """ *``` *""".r
8+
val ScalaCliBlock = """ *```bash *(fail)? *""".r
9+
val CheckBlock = """ *\<\!\-\- Expected(-regex)?: *""".r
10+
val CheckBlockEnd = """ *\-\-\> *""".r
1411

1512
enum Commands:
1613
def context: Context
1714

1815
case Snippet(name: String, lines: Seq[String], context: Context)
19-
case Run(cmd: Seq[String], shouldFail: Boolean, context: Context)
16+
case Run(cmd: Seq[Seq[String]], shouldFail: Boolean, context: Context)
2017
case Check(patterns: Seq[String], regex: Boolean, context: Context)
2118

2219
case class Context(file: String, line: Int)
@@ -43,13 +40,18 @@ def parse(content: Seq[String], currentCommands: Seq[Commands], context: Context
4340
case ScalaCliBlock(failGroup) :: tail =>
4441
val (codeLines, rest, newContext) = untilEndOfSnippet(tail)
4542
assert(codeLines.size != 0)
46-
val runCmd = Commands.Run(codeLines.head.split(" ").toList, failGroup != null, newContext)
43+
val runCmd = Commands.Run(
44+
codeLines.filterNot(_.trim.startsWith("#")).map(_.split(" ").toList),
45+
failGroup != null,
46+
newContext
47+
)
4748
parse(rest, currentCommands :+ runCmd, newContext)
4849
case CheckBlock(regexOpt) :: tail =>
4950
val isRegex = regexOpt == "-regex"
5051
val (patterns, rest, newContext) = untilEndOfSnippet(tail, CheckBlockEnd)
5152
parse(rest, currentCommands :+ Commands.Check(patterns, isRegex, context), newContext)
52-
case _ :: tail => parse(tail, currentCommands, context.copy(line = context.line + 1))
53+
case _ :: tail =>
54+
parse(tail, currentCommands, context.copy(line = context.line + 1))
5355

5456
case class TestCase(path: os.Path, failure: Option[Throwable])
5557

@@ -77,38 +79,49 @@ def checkFile(file: os.Path, dest: Option[os.Path]) =
7779
val destName = file.last.stripSuffix(".md")
7880
val out = os.temp.dir(prefix = destName)
7981

80-
var lastOutput = ""
81-
val allSources = Set.newBuilder[os.Path]
82+
var lastOutput: String = null
83+
val allSources = Set.newBuilder[os.Path]
8284

8385
try
8486
println(s"Using $out as output to process $file")
8587

8688
commands.foreach { cmd =>
8789
given Context = cmd.context
8890
cmd match
89-
case Commands.Run(cmd, shouldFail, _) =>
90-
println(s"### Running: ${cmd.mkString(" ")}")
91-
val res = os.proc(cmd).call(cwd = out, check = false)
92-
if shouldFail then
93-
assert(res.exitCode != 0)
94-
else
95-
assert(res.exitCode == 0)
96-
val outputChunks = res.chunks.map {
97-
case Left(c) =>
98-
c
99-
case Right(c) =>
100-
c
91+
case Commands.Run(cmds, shouldFail, _) =>
92+
cmds.foreach { cmd =>
93+
println(s"### Running: ${cmd.mkString(" ")}:")
94+
val res = os.proc(cmd).call(cwd = out, mergeErrIntoOut = true, check = false)
95+
println(res.out.text())
96+
if shouldFail then
97+
assert(res.exitCode != 0)
98+
else
99+
assert(res.exitCode == 0)
100+
101+
val outputChunks = res.chunks.map {
102+
case Left(c) =>
103+
c
104+
case Right(c) =>
105+
c
106+
}
107+
lastOutput = res.out.text()
101108
}
102-
lastOutput = geny.ByteData.Chunks(outputChunks).text()
103-
104109
case Commands.Snippet(name, code, c) =>
105-
println(s"### Writting $name with:\n${code.mkString("\n")}\n---")
106-
val prefix = (fakeLineMarker + "\n") * c.line
107-
val file = out / name
110+
val (prefixLines, codeLines) =
111+
code match
112+
case shbang :: tail if shbang.startsWith("#!") =>
113+
List(shbang + "\n") -> tail
114+
case other =>
115+
Nil -> other
116+
117+
val file = out / name
108118
allSources += file
119+
println(s"### Writting $name with:\n${codeLines.mkString("\n")}\n---")
120+
121+
val prefix = prefixLines.mkString("", "", s"$fakeLineMarker\n" * c.line)
109122
os.write(file, code.mkString(prefix, "\n", ""))
110123
case Commands.Check(patterns, regex, line) =>
111-
assert(lastOutput != "", msg("No output stored from previous commands"))
124+
assert(lastOutput != null, msg("No output stored from previous commands"))
112125
val lines = lastOutput.linesIterator.toList
113126

114127
if regex then
@@ -123,7 +136,7 @@ def checkFile(file: os.Path, dest: Option[os.Path]) =
123136
patterns.foreach { pattern =>
124137
assert(
125138
lines.exists(_.contains(pattern)),
126-
msg(s"Pattern: $pattern does not exisits in any line in:\n$lastOutput")
139+
msg(s"Pattern: $pattern does not exists in any line in:\n$lastOutput")
127140
)
128141
}
129142
}

examples/scala-jvm/README.md

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ You can use Scala CLI to test your code compatibility with various versions of `
99

1010
The following snippet uses the new method `Files.writeString` from Java 11.
1111

12-
```scala name:Main.scala
12+
```scala title=Main.scala
1313
import java.nio.file.Files
1414
import java.nio.file.Paths
1515

@@ -19,22 +19,21 @@ object Main extends App {
1919
val fileContent: String = Files.readString(filePath)
2020
println(fileContent)
2121
}
22-
```
23-
22+
```
2423

2524
Pass `--jvm` to the `scala-cli` command to run your application with the specified java version.
2625

27-
```scala-cli
28-
scala-cli Main.scala --jvm 11
26+
```bash ignore
27+
scala-cli --jvm adopt:11 Main.scala
2928
```
3029

31-
<!-- Expected:
30+
<!-- ignored Expected:
3231
Hello from ScalaCli
3332
-->
3433

3534
To test your application with Java 8, change the value of `--jvm` parameter.
36-
```scala-cli fail
37-
scala-cli Main.scala --jvm 8
35+
```bash ignore fail
36+
scala-cli --jvm 8 Main.scala
3837
# In this case, it raises an error because the `Files.createTempFile` method is not available in java 8
3938
#
4039
# Exception in thread main: java.lang.Exception: java.lang.NoSuchMethodError: java.nio.file.Files.writeString(Ljava/nio/file/Path;Ljava/lang/CharSequence;[Ljava/nio/file/OpenOption;)Ljava/nio/file/Path;
@@ -43,7 +42,7 @@ scala-cli Main.scala --jvm 8
4342
# at method main in modules/runner/src/main/scala/scala/cli/runner/Runner.scala:22 inside runner_3.jar
4443
```
4544

46-
<!-- Expected:
45+
<!-- ignored Expected:
4746
java.lang.NoSuchMethodError
4847
java.nio.file.Files.writeString
4948
-->

examples/scala-package/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Scala CLI allows you to package your application into a lightweight JAR file, th
77
It only contains the byte code of your sources, and automatically downloads its dependencies on its first run.
88

99
The following snippet contains a short application to detect the OS:
10-
```scala name:DetectOsApp.scala
10+
```scala title=DetectOsApp.scala
1111
object DetectOSApp extends App {
1212
def getOperatingSystem(): String = {
1313
val os: String = System.getProperty("os.name")
@@ -21,7 +21,7 @@ object DetectOSApp extends App {
2121

2222
By default, the `package` sub-command generates a lightweight JAR.
2323

24-
```scala-cli
24+
```bash
2525
scala-cli package DetectOsApp.scala
2626
```
2727

@@ -53,7 +53,7 @@ Only Jars built on MacOs / Linux are easily portable between this system. The Li
5353
### Assemblies
5454
Passing `--assembly` to the `package` sub-command generates so-called "assemblies" or "fat JARs".
5555

56-
```scala-cli
56+
```bash
5757
scala-cli package --assembly DetectOsApp.scala
5858
```
5959

examples/scala-scripts/README.md

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,27 @@ Scala Scripts are files containing Scala code without main method required. Thes
1111

1212
You can use `scala-cli` to run Scala scripts (no further setup is required):
1313

14-
```scala name:HelloScript.sc
14+
```scala title=HelloScript.sc
1515
val sv = scala.util.Properties.versionNumberString
1616

1717
val message = s"Hello from Scala ${sv}, Java ${System.getProperty("java.version")}"
1818
println(message)
1919
```
2020

21-
```scala-cli
21+
```bash
2222
scala-cli run HelloScript.sc
2323
```
2424

25-
<!-- Expected:
26-
Hello from Scala *, Java *
25+
<!-- Expected-regex:
26+
Hello from Scala .*, Java .*
2727
-->
2828

2929

3030
Alternatively, add a shebang header to your script, make your script executable, and execute it directly. `scala-cli` needs to be installed for this to work.
3131

32-
```scala name:HelloScriptSheBang.sc
32+
```scala title=HelloScriptSheBang.sc
3333
#!/usr/bin/env scala-cli
34+
3435
val sv = scala.util.Properties.versionNumberString
3536

3637
def printMessage(): Unit =
@@ -47,22 +48,31 @@ chmod +x HelloScriptSheBang.sc
4748
# Hello from Scala 2.13.6, Java 16.0.1
4849
```
4950

51+
<!-- Expected-regex:
52+
Hello from Scala .*, Java .*
53+
-->
54+
5055
## Features
5156

5257
All the features from non-scripts are working with Scala Scripts, such as waiting for changes (watch mode), dependencies menagement, packaging, compiling and many others.
5358

5459
### Package
5560

56-
Pass `--package` to the Scala CLI to package your script to Leighweight JAR file.
61+
Run `package` to the Scala CLI to package your script to Leighweight JAR file.
5762

5863
```bash
59-
scala-cli --package HelloScript.sc
64+
scala-cli package HelloScript.sc
65+
./HelloScript
6066
```
6167

68+
<!-- Expected-regex:
69+
Hello from Scala .*, Java .*
70+
-->
71+
6272
### Watch mode
6373

6474
Pass `--watch` to the Scala CLI to watch all sources for changes, and re-run them upon changes.
6575

66-
```bash
76+
```bash ignore
6777
scala-cli --watch HelloScript.sc
6878
```

0 commit comments

Comments
 (0)