Skip to content

Commit 41eaab6

Browse files
authored
Merge pull request #1606 from Gedochao/md-docs
Document recent features & changes affecting working with Markdown inputs
2 parents 030edaf + 6e25f7d commit 41eaab6

File tree

4 files changed

+396
-28
lines changed

4 files changed

+396
-28
lines changed

website/docs/commands/basics.md

Lines changed: 133 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ title: Basics
33
sidebar_position: 3
44
---
55

6+
import {ChainedSnippets} from "../../src/components/MarkdownComponents.js";
7+
68
Scala CLI is a command line tool that executes a given sub-command on the inputs it’s provided with, using a
79
given [configuration](../guides/configuration.md) to produce a result.
810

@@ -22,7 +24,7 @@ in which case it defaults to one of the sub-commands based on context:
2224
- if any inputs were passed, it defaults to the `run` sub-command
2325
- and so, `scala-cli a.scala` runs your `a.scala` file
2426
- additionally, when no inputs were passed, it defaults to the `run` sub-command in the following scenarios:
25-
- if a snippet was passed with `--execute-script`, `--execute-scala` or `--execute-java`
27+
- if a snippet was passed with `-e`, `--execute-script`, `--execute-scala`, `--execute-java` or `--execute-markdown`
2628
- if a main class was passed with the `--main-class` option alongside an extra `--classpath`
2729
- otherwise if no inputs were passed, it defaults to the `repl` sub-command
2830

@@ -60,11 +62,18 @@ object Hello {
6062

6163
Then run it by passing it to `scala-cli`:
6264

65+
<ChainedSnippets>
66+
6367
```bash
6468
scala-cli Hello.scala
65-
# Hello from Scala
6669
```
6770

71+
```text
72+
Hello from Scala
73+
```
74+
75+
</ChainedSnippets>
76+
6877
You can also split your code into multiple files:
6978

7079
```scala title=Messages.scala
@@ -82,11 +91,18 @@ object Hello {
8291

8392
and the run them with `scala-cli`:
8493

94+
<ChainedSnippets>
95+
8596
```bash
8697
scala-cli Hello.scala Messages.scala
87-
# Hello from Scala
8898
```
8999

100+
```text
101+
Hello from Scala
102+
```
103+
104+
</ChainedSnippets>
105+
90106
:::note
91107
Scala CLI compiles only the provided inputs.
92108
For example, if we provide only one of the files above:
@@ -124,11 +140,18 @@ object Hello {
124140

125141
In this case, you can run all the source code files in `my-app` by supplying the directory name:
126142

143+
<ChainedSnippets>
144+
127145
```bash
128146
scala-cli my-app
129-
# Hello from Scala
130147
```
131148

149+
```text
150+
Hello from Scala
151+
```
152+
153+
</ChainedSnippets>
154+
132155
In our experience, `scala-cli .` is the most used command; it compiles and runs all sources in the current directory.
133156

134157
:::note
@@ -160,11 +183,20 @@ scala-cli https://gist.github.com/alexarchambault/f972d941bc4a502d70267cfbbc4d63
160183
`scala-cli` accepts input via Github Gist’s urls.
161184
It downloads the gist zip archive and runs it:
162185

186+
<ChainedSnippets>
187+
163188
```bash
164189
scala-cli https://gist.github.com/alexarchambault/7b4ec20c4033690dd750ffd601e540ec
165-
# Hello
166190
```
167191

192+
```text
193+
Hello
194+
```
195+
196+
</ChainedSnippets>
197+
198+
More details in the [GitHub gists cookbook](../cookbooks/gists.md).
199+
168200
### Zip archive
169201

170202
`scala-cli` accepts inputs via a `zip` archive path.
@@ -176,38 +208,94 @@ object Hello extends App {
176208
}
177209
```
178210

211+
<ChainedSnippets>
212+
179213
```bash ignore
180214
unzip -l hello.zip
181-
# Archive: hello.zip
182-
# Length Date Time Name
183-
# --------- ---------- ----- ----
184-
# 49 12-07-2021 00:06 Hello.scala
185-
# --------- -------
186-
# 49 1 file
215+
```
216+
217+
```text
218+
Archive: hello.zip
219+
Length Date Time Name
220+
--------- ---------- ----- ----
221+
49 12-07-2021 00:06 Hello.scala
222+
--------- -------
223+
49 1 file
224+
```
225+
226+
```bash ignore
187227
scala-cli hello.zip
188-
# Hello
189228
```
190229

230+
```text
231+
Hello
232+
```
233+
234+
</ChainedSnippets>
235+
191236
## Piping
192237

193238
You can also pipe code to `scala-cli` for execution:
194239

195240
- scripts
241+
242+
<ChainedSnippets>
243+
196244
```bash
197245
echo 'println("Hello")' | scala-cli _.sc
198-
# Hello
199246
```
247+
248+
```text
249+
Hello
250+
```
251+
252+
</ChainedSnippets>
253+
200254
- Scala code
255+
256+
<ChainedSnippets>
257+
201258
```bash
202259
echo '@main def hello() = println("Hello")' | scala-cli _.scala
203-
# Hello
204260
```
261+
262+
```text
263+
Hello
264+
```
265+
266+
</ChainedSnippets>
267+
205268
- Java code
269+
270+
<ChainedSnippets>
271+
206272
```bash
207273
echo 'class Hello { public static void main(String args[]) { System.out.println("Hello"); } }' | scala-cli _.java
208-
# Hello
274+
```
275+
276+
```text
277+
Hello
278+
```
279+
280+
</ChainedSnippets>
281+
282+
- Markdown code (experimental)
283+
284+
<ChainedSnippets>
285+
286+
```bash
287+
echo '# Example Snippet
288+
```scala
289+
println("Hello")
290+
```' | scala-cli _.md
291+
```
292+
293+
```text
294+
Hello
209295
```
210296

297+
</ChainedSnippets>
298+
211299
More details in the [Piping guide](../guides/piping.md).
212300

213301
## Scala CLI version
@@ -219,31 +307,56 @@ Running another Scala CLI version might be slower because it uses JVM-based Scal
219307

220308
To run another Scala CLI version, specify it with `--cli-version` before any other argument:
221309

310+
<ChainedSnippets>
311+
222312
```bash
223-
scala-cli --cli-version 0.1.3-51-g4d314eee-SNAPSHOT about
224-
# Scala CLI version 0.1.3-51-g4d314eee-SNAPSHOT
313+
scala-cli --cli-version 0.1.17-62-g21e1cf44-SNAPSHOT about
225314
```
226315

316+
```text
317+
Scala CLI version: 0.1.17-62-g21e1cf44-SNAPSHOT
318+
Scala version (default): 3.2.1
319+
```
320+
321+
</ChainedSnippets>
322+
227323
<!-- Expected:
228-
Scala CLI version 0.1.3-51-g4d314eee-SNAPSHOT
324+
Scala CLI version: 0.1.17-62-g21e1cf44-SNAPSHOT
325+
Scala version (default): 3.2.1
229326
-->
230327

231328
To use the latest Scala CLI nightly build, pass `nightly` to `--cli-version` parameter:
232329

330+
<ChainedSnippets>
331+
233332
```bash
234333
scala-cli --cli-version nightly about
235-
# Scala CLI version 0.1.3-8-g431cc15f-SNAPSHOT
236334
```
237335

336+
```text
337+
Fetching Scala CLI 0.1.17-62-g21e1cf44-SNAPSHOT
338+
Scala CLI version: 0.1.17-62-g21e1cf44-SNAPSHOT
339+
Scala version (default): 3.2.1
340+
```
341+
342+
</ChainedSnippets>
343+
238344
## Process substitution
239345

240346
Lastly, `scala-cli` also accepts input via shell process substitution:
241347

348+
<ChainedSnippets>
349+
242350
```bash
243351
scala-cli <(echo 'println("Hello")')
244-
# Hello
245352
```
246353

354+
```text
355+
Hello
356+
```
357+
358+
</ChainedSnippets>
359+
247360
<!-- Expected:
248361
Hello
249362
-->

website/docs/cookbooks/gists.md

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ title: Sharing and testing code with GitHub gists
33
sidebar_position: 6
44
---
55

6+
import {ChainedSnippets} from "../../src/components/MarkdownComponents.js";
7+
68
## Running code from gists
79

810
`scala-cli` lets you run Scala code straight from GitHub gists, without the need to manually download them first.
@@ -62,6 +64,7 @@ object Hello extends App {
6264
println(inputs.mkString(","))
6365
}
6466
```
67+
6568
```scala title=input
6669
1
6770
2
@@ -70,12 +73,47 @@ object Hello extends App {
7073
```
7174

7275
and run them:
76+
77+
<ChainedSnippets>
78+
7379
```bash
7480
scala-cli https://gist.github.com/lwronski/7ee12fa4b8b8bac3211841273df82080
75-
# 1,2,3,4
7681
```
82+
83+
```text
84+
1,2,3,4
85+
```
86+
87+
</ChainedSnippets>
88+
7789
<!-- Expected:
7890
1,2,3,4
7991
-->
8092

81-
it will print `1,2,3,4` to the standard output.
93+
it will print `1,2,3,4` to the standard output.
94+
95+
## Gists and Markdown code
96+
97+
:::note
98+
This feature is a work in progress and should currently be treated as experimental.
99+
Markdown sources are ignored by default unless passed explicitly as inputs.
100+
You can enable including non-explicit `.md` inputs by passing the `--enable-markdown` option.
101+
:::
102+
103+
It is possible to run markdown sources from a GitHub gist.
104+
The gist is technically treated as a zipped archive (which it is downloaded as), so it is necessary to pass
105+
the `--enable-markdown` option alongside the gist URL to run any contained Markdown sources.
106+
107+
<ChainedSnippets>
108+
109+
```bash
110+
scala-cli https://gist.github.com/Gedochao/6415211eeb8ca4d8d6db123f83f0f839 --enable-markdown
111+
```
112+
113+
```text
114+
Hello
115+
```
116+
117+
</ChainedSnippets>
118+
119+
You can find more information on working with Markdown in the [Markdown guide](../guides/markdown.md).

0 commit comments

Comments
 (0)