You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: website/docs/getting_started.md
+40-39Lines changed: 40 additions & 39 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,12 +6,12 @@ sidebar_position: 2
6
6
import {ChainedSnippets, GiflikeVideo} from "../src/components/MarkdownComponents.js";
7
7
8
8
:::info
9
-
This article requires knowledge of Scala language (how to define class or method) as well as Scala tooling (repl, basics of dependency management and unit tests).
9
+
This article requires knowledge of the Scala language (how to define a class or method) as well as Scala tooling (the REPL, and basics of dependency management and unit tests).
10
10
:::
11
11
12
-
In this article we will show how to use Scala CLI to create basic script followed by small project with things like dependencies, tests and IDE support. We aime to provide you with a knowledge how to create and develop your first projects using Scala CLI.
12
+
In this article we show how to use Scala CLI to create a basic script, followed by small project with features like dependencies, tests, and IDE support. We aim to provide you with a knowledge of how to create and develop your first projects using Scala CLI.
13
13
14
-
Firstly, lets verify if Scala CLI is properly [installed](/install) with a simple hello world command:
14
+
First, let's verify if Scala CLI is properly [installed](/install) with a simple "hello world" test:
15
15
16
16
<ChainedSnippets>
17
17
@@ -29,11 +29,11 @@ Hello
29
29
30
30
</ChainedSnippets>
31
31
32
-
Running the command for the first time may take a bit longer then usual and print a bit logs because Scala CLI needs to download all artifacts needed to compile and run the code.
32
+
Running this command the first time may take a bit longer then usual and print a fair number of logging output because Scala CLI needs to download all the artifacts it needs to compile and run the code.
33
33
34
34
## Scripting
35
35
36
-
In fact, we have just created a Scala Script, so let's create a script in a hello.sc file, that will actually greet properly.
36
+
In that example we actually just created a Scala Script. To demonstrate this more fully, let's create a script in a `hello.sc` file that greets more properly:
37
37
38
38
```scala title=hello.sc
39
39
defhelloMessage(names: Seq[String]) = names match
@@ -45,8 +45,7 @@ def helloMessage(names: Seq[String]) = names match
45
45
println(helloMessage(args.toSeq))
46
46
```
47
47
48
-
Now let run it with:
49
-
48
+
When that script is given no names, it prints `"Hello!"`, and when it’s given one or more names it prints the string that's created in the second `case` statement. With Scala CLI we run the script like this:
50
49
51
50
<ChainedSnippets>
52
51
@@ -59,7 +58,7 @@ Hello
59
58
```
60
59
</ChainedSnippets>
61
60
62
-
To provide arguments to the script we need to add them after `--`:
61
+
To provide arguments to the script we add them after `--`:
63
62
64
63
<ChainedSnippets>
65
64
@@ -73,19 +72,21 @@ Hello Jenny, Jake!
73
72
74
73
</ChainedSnippets>
75
74
76
-
You may wonder what kind of Scala version was used under the hood. The answer is the latest stable one. If we want to specify the Scala version we can use `-S` or `--scala` option. More about setting Scala version in a dedicated [cookbook](./cookbooks/scala-versions.md).
75
+
You may wonder, what kind of Scala version was used under the hood in that example? The answer is the latest stable one. If we want to specify the Scala version, we can use the `-S`(or `--scala`) option. You can read more about setting the Scala version in our dedicated [Scala Versions cookbook](./cookbooks/scala-versions.md).
77
76
78
-
Scala CLI offers much more features dedicated for scriptingdescribed in the [dedicated guide](./guides/scripts.md)
77
+
Scala CLI offers many more features dedicated for scripting, as described in the [dedicated guide](./guides/scripts.md).
79
78
80
79
## Dependencies
81
80
82
-
Let's build something more serious. Best start is prototyping inside REPL. We can start a repl by simply running `scala-cli repl` and here we can also set a Scala version with `-S` or `--scala`.
81
+
Now let's build something more serious. For this example, it's best to start with some prototyping inside the REPL. We can start a REPL session by running `scala-cli repl`. (If desired, you can also set the Scala version with `-S` or `--scala`.)
83
82
84
-
*Scala CLI reuses most of its options across all its comments.**
83
+
:::note
84
+
Scala CLI reuses most of its options across all its comments.
85
+
:::
85
86
86
-
One of the main strengths of Scala is its ecosystem. Scala CLI is designed in a way to expose Scala Ecosystem to all usages of Scala and running REPL is no exception.
87
+
One of the main strengths of Scala is its ecosystem. Scala CLI is designed in a way to expose the Scala ecosystem to all usages of Scala, and running the REPL is no exception.
87
88
88
-
Let's start prototyping with [os-lib](https://github.com/com-lihaoyi/os-lib)- a Scala interface to common OS filesystem and subprocess. To experiment with `os-lib` in repl we simply need to add a parameter `--dep com.lihaoyi::os-lib:0.7.8`
89
+
To demonstrate this, let's start prototyping with [os-lib](https://github.com/com-lihaoyi/os-lib)— a Scala interface to common OS filesystem and subprocess methods. To experiment with `os-lib` in the REPL, we simply need to add the parameter `--dep com.lihaoyi::os-lib:0.7.8`, as shown here:
89
90
90
91
<ChainedSnippets>
91
92
@@ -105,17 +106,17 @@ val res1: IndexedSeq[os.Path] = ArraySeq(...)
105
106
106
107
## A project
107
108
108
-
Now is time to write some logic, based on the prototyping we have just did: a filter function to display all files with given extension in current directory.
109
+
Now it's time to write some logic, based on the prototyping we just did. We'll create a filter function to display all files with the given filename extension in the current directory.
109
110
110
-
For the consistency of our results let's create a new directory and `cd` to it:
111
+
For the consistency of our results, let's create a new directory and `cd` to it:
111
112
112
113
```bash
113
114
mkdir scala-cli-getting-started
114
115
cd scala-cli-getting-started
115
116
```
116
117
<!-- clear -->
117
118
118
-
Now we can write our logic in `files.scala`:
119
+
Now we can write our logic in a file named `files.scala`:
119
120
120
121
```scala title=files.scala
121
122
// using lib com.lihaoyi::os-lib:0.7.8
@@ -128,32 +129,31 @@ def filesByExtension(
128
129
}
129
130
```
130
131
131
-
As you may have noticed we specified a dependency within the `.scala` using `// using lib com.lihaoyi::os-lib:0.7.8`. In Scala CLI configuration can be provided through so called using directives - a dedicated syntax that can be embedded in any `.scala` file. We have a dedicated [guide for using directives](./guides/using-directives.md).
132
+
As you may have noticed, we specified a dependency within `files.scala` using the `// using lib com.lihaoyi::os-lib:0.7.8` syntax. With Scala CLI, you can provide configuration information with `using` directives — a dedicated syntax that can be embedded in any `.scala` file. For more details, see our dedicated [guide for `using` directives](./guides/using-directives.md).
132
133
133
-
Let's check if our code compiles. We can do that by simply running:
134
+
Now let's check if our code compiles. We do that by running:
134
135
135
136
```bash
136
137
scala-cli compile .
137
138
```
138
139
139
-
This time we did not provide path to single files but rather used a (current) directory. For project-like use-cases we recommend providing directories rather then individual files. More most cases a current directory (`.`) is best choice.
140
+
Notice that this time we didn’t provide a path to single files, but rather used a directory; in this case, the current directory. For project-like usecases, we recommend providing directories rather than individual files. For most cases, specifying the current directory (`.`) is a best choice.
140
141
141
142
## IDE support
142
143
143
-
Some people are fine working using command line only, but most Scala Developers use an IDE. Let's open Metals with your favorite editor inside `scala-cli-getting-started` directory.
144
-
144
+
Some people are fine using the command line only, but most Scala developers use an IDE. To demonstrate this, let's open Metals with your favorite editor inside `scala-cli-getting-started` directory:
At this moment support for IntelliJ is often problematic. We are working on making it as rocksolid as Metals one.
148
+
At the present moment, support for IntelliJ is often problematic. But know that we are working on making it as rock-solid as Metals.
149
149
150
-
Actually, we've cheated a bit by running compilation first. In order for Metals or IntelliJ to pick up Scala CLI project we need to generate a BSP connection detail file. Scala CLI generate such details by default every time `compile`, `run` or `test` are run. We expose a `setup-ide` command to manually control creation of connection details file. You can find more details in [IDE guide](./guides/ide.md).
150
+
Actually, in this case, we cheated a bit by running the compilation first. In order for Metals or IntelliJ to pick up a Scala CLI project, we need to generate a BSP connection detail file. Scala CLI generates these details by default every time `compile`, `run`, or `test` are run. We also expose a `setup-ide` command to manually control creation of the connection details file. For more information on this, see our[IDE guide](./guides/ide.md).
151
151
152
152
## Tests
153
153
154
-
With IDE in place, how can we test if our code works correctly? Best way is to create unit test. The simplest way to add a test using scala-cli is by creating a file named that it ends with `.test.scala` like `files.test.scala`. There are also other ways to mark source a testdescribed in [tests guide](./commands/test.md#test-sources).
154
+
With our IDE in place, how can we test if our code works correctly? The best way is to create a unit test. The simplest way to add a test using scala-cli is by creating a file whose name ends with `.test.scala`, such as `files.test.scala`. (There are also other ways to mark source code files as containing a test, as described in [tests guide](./commands/test.md#test-sources).)
155
155
156
-
We also need to add a test framework. Scala CLI support most popular test frameworks and for this guide we will stick to[munit](https://scalameta.org/munit/). To add a test framework we just need an ordinary dependency that we will add with `using` directive again:
156
+
We also need to add a test framework. Scala CLI support most popular test frameworks, and for this guide we will stick with[munit](https://scalameta.org/munit/). To add a test framework, we just need an ordinary dependency, and once again we'll add that with the `using` directive:
157
157
158
158
```scala title=files.test.scala
159
159
// using lib org.scalameta::munit:1.0.0-M1
@@ -167,7 +167,7 @@ class TestSuite extends munit.FunSuite {
167
167
}
168
168
```
169
169
170
-
Now we can run our tests in command line:
170
+
Now we can run our tests at the command line:
171
171
172
172
<ChainedSnippets>
173
173
@@ -190,7 +190,7 @@ or directly within Metals:
190
190
191
191
## A project, vol 2
192
192
193
-
With our code ready and tested it is now time to turn it into a command-line tool to count files by extension. For that we can write a simple script. With Scala CLI, scripts and scala sources can be mixed.
193
+
With our code ready and tested, now it's time to turn it into a command-line tool that counts files by their extension. For this we can write a simple script. A great feature of Scala CLI is that scripts and Scala sources can be mixed:
194
194
195
195
```scala title=countByExtension.sc
196
196
val (ext, directory) = args.toSeq match
@@ -204,11 +204,11 @@ val files = filesByExtension(ext, directory)
As you probably noticed, we are using `os-lib` in our script without any using directive, how is that possible? Actually, configuration provided by using directives are global and applies to all files. Since `files.scala` and `countByExtension.sc` are compiled together. Defining a library dependency in more then one file is an anti-pattern.
207
+
As you probably noticed, we are using `os-lib` in our script without any `using` directive; how is that possible? The way this works is that configuration details provided by `using` directives are global, and apply to all files. Since `files.scala` and `countByExtension.sc` are compiled together, the `using` directives in `files.scala` are used when compiling both files. (Note that defining a library dependency in more than one file is an anti-pattern.)
208
208
209
-
<!-- TODO add pice about scala-cli warnings in such case -->
209
+
<!-- TODO add piece about scala-cli warnings in such case -->
210
210
211
-
Let's try it:
211
+
Now let's run our code, looking for all files that end with the `.scala` extension:
212
212
213
213
<ChainedSnippets>
214
214
@@ -224,30 +224,31 @@ files.test.scala
224
224
225
225
</ChainedSnippets>
226
226
227
-
Why do we have an additional `.scala` file inside `.scala` dir? Actually, under the hood, Scala CLI needs sometimes to preprocess provided source file (e.g. for scripts) and we compile such file from within `.scala` directory.
227
+
Seeing that output, you may wonder, why do we have an additional `.scala` file under the `.scala` dir? The way this works is that under the hood, Scala CLI sometimes needs to preprocess source code files — such as scripts. So these preprocessed files are created under the `.scala` directory, and then compiled from there.
228
228
229
229
## Packaging
230
230
231
-
We could stop here and call scala-cli on set of sources every time. Scala CLI uses caches aggressively so rollup runs are reasonable fast (less around 1500 milliseconds on my machine) but sometimes it is not fast enough or shipping sources and compiling them may be not convenient.
231
+
We could stop here and call `scala-cli` on our set of sources every time. Scala CLI uses caches aggressively, so rollup runs are reasonably fast — less than 1,500 milliseconds on my machine — but sometimes this isn't fast enough, or shipping sources and compiling them may be not convenient.
232
232
233
-
Scala CLI offers means to package your project. We can simply run:
233
+
For these use cases, Scala CLI offers means to package your project. For example, we can run this command to generate a thin, executable jar file, with the compiled code inside:
234
234
235
235
```bash
236
236
scala-cli package . -o countByExtension
237
237
```
238
238
239
-
It will generate a thin, executable jar with the compiled code inside. We provided `-o` flag to customize the binary name (by default application is written as `app`). Now we can run our project with:
239
+
The default binary name is `app`, so in this example we provide the `-o` flag to make the binary name `countByExtension`. Now we can run our project like this:
240
240
241
241
```bash
242
242
./countByExtension scala
243
243
```
244
244
245
-
This time it took 350 milliseconds so we have a big improvement. Created binary (a runnable jar) is self-contained and can be shipped to your colleagues or deployed.
245
+
This time it only took 350 milliseconds, so this is a big improvement. When you create a binary file (a runnable jar) like this, it's self-contained, and can be shipped to your colleagues or deployed.
246
+
247
+
We can reduce the startup time even further using [Scala Native](./guides/scala-native.md), or by packaging our application to other formats like [Docker container](./commands/package.md#docker-container), [assembly](./commands/package.md#assemblies), or even [OS-specific packages](./commands/package.md#os-specific-packages) (.dep, .pkg, etc.). See those resources for more information.
246
248
247
-
We can reduce the start up time even further using [Scala Native](./guides/scala-native.md) or package our application to other formats like [Docker container](./commands/package.md#docker-container), [assembly](./commands/package.md#assemblies) or even [os-specific packages](./commands/package.md#os-specific-packages) (.dep, .pgk etc.) All of this is outside of the scope of this guide.
248
249
249
250
## Summary
250
251
251
-
We've only scratch the surface what Scala CLI can do with this guide. We prepare a set of [cookbooks](./cookbooks/intro.md)showcasing solutions to some common problems as well as detailed set of [guides](./guides/intro.md) for our [commands](./commands/basics.md).
252
+
With this guide we've only scratched the surface of what Scala CLI can do. For many more details, we've prepared a set of [cookbooks](./cookbooks/intro.md)that showcase solutions to common problems, as well as a detailed set of [guides](./guides/intro.md) for our [commands](./commands/basics.md).
252
253
253
-
We have a dedicated [room on Scala discord](https://discord.gg/KzQdYkZZza)to ask for help or discuss anything that is Scala CLI related. For more in-depth discussion we are using [Github discussion in our repo](https://github.com/VirtusLab/scala-cli/discussions) and this is a best place to suggest a new feature or an improvements.
254
+
We also have a dedicated [room on Scala discord](https://discord.gg/KzQdYkZZza)where you can ask for help or discuss anything that's related to Scala CLI. For more in-depth discussions, we're using [Github discussions in our repo](https://github.com/VirtusLab/scala-cli/discussions); this is the best place to suggest a new feature or any improvements.
0 commit comments