Installing Programming Environment (Visual Studio Code)
This page explains how to install, from scratch, a working environment for developing ScalaNeko programs using Visual Studio Code.
The instructions below are tested on a Mac environment and require some adaptation for Linux or Windows.
Preliminary Downloads
- (Mac only) make sure that you have installed the Xcode command-line tools. E.g., with
xcode-select --install
- Download and install Visual Studo Code. (Mac Only) you can also do that using Homebrew with
brew install vscode
. - Install
sbt
by downloading it or via a package manager. (Mac only) homebrew solution is recommended for Macs:
brew install sbt
At some point, vscode might query you to install a version of JDK. I suggest that you do it, but you can also set it up to find a version already installed.
Setting up Visual Studio Code
- Open Visual Studio Code.
- Install the following extensions:
-
Scala Syntax (official)
-
Scala (Metals)
-
Create and Configure New Project (from Scratch)
Create Project
In a terminal window,
- Navigate to your project directory
- Run the command:
sbt new scala/hello-world.g8
(NB: it can take several minutes before anything happens; just be patient) - Give a name to your project when queried (e.g.,
My Project
). - Open VScode if not already done:
code my-project
- Or navigate to your project directory if you are already in VScode (e.g.,
cd my-project
). - You should see the following:
build.sbt
is the construction file (like Makefile but smarter).src/main/scala/
is the directory with the source code.project/
is some meta information that you can generally ignore..metal/
ditto..vscode/
ditto..bloop/
ditto.
- If using
git
you can safely add the latter three directories to your.gitignore
file.
Configure New Project
- Open the file
build.sbt
. It is in the outline view on the left-hand side of the window. - Add the
libraryDependencies
andresolvers
lines to the file (make sure to keep an empty line between each line):
resolvers += "titech.c.coord" at "https://xdefago.github.io/ScalaNeko/sbt-repo/"
libraryDependencies += "titech.c.coord" %% "scalaneko" % "0.24.0"
You can also bump up the version number for Scala to 2.13.13
(latest version 2 at time of writing), but version 3 of Scala will probably not work with ScalaNeko.
- Save the file
- Open the terminal window (Menu: View > Terminal).
- Type
sbt compile
and see if there are any errors (if there are, you need to fix settings). The first time will probably download lots of things and hence take a very long time. - Type
sbt run
and see the hello world program running.
- Type
Clone the Distributed Systems Course Template
Setup the Project
- Navigate to your project directory
- Clone the project:
git clone https://github.com/xdefago/class-da-prog.git
- Open VSCode:
code class-da-prog
- It might take long the first time because Metals will download lots of dependencies
- If queried about “Import Build”, do it and, although it will take even longer (several minutes), this will enable code sense for your project which is very useful to see inferred types (among other things).
- If you missed the above import, you can trigger it manually as follows:
- Menu: _View > Command Palette…
- In the prompt, type:
import
- Select:
Metals: Import build
- wait…
- … et voilà!
Run the Hello World
- Open the terminal window (Menu: View > Terminal).
- Type
sbt run
- In the list of executable classes, pick
session0.a_hello_scala.HelloScala
(should probably be number4
). - You should see the output from the hello word:
[info] running (fork) session0.a_hello_scala.HelloScala [info] Hello Scala's world!
- Type
- To see the code itself, navigate on VScode to
src/main/scala/session0/a_hello_scala/HelloWorld.scala
package session0.a_hello_scala
object HelloScala extends App
{
println("Hello Scala's world!")
}
Run the Hello Neko
The next example creates a number of processes, each of which prints an hello message.
- Navigate on VScode to
src/main/scala/session0/b_hello_neko/HelloNeko.scala
package session0.b_hello_neko
import neko._
class Hello(p: ProcessConfig) extends ActiveProtocol(p, "hello")
{
def run(): Unit = {
println(s"Process ${me.name} says: 'Hello Neko World!'")
}
}
object HelloNeko
extends Main(topology.Clique(5))(
ProcessInitializer { p => new Hello(p) }
)
- Run the example (alternative method):
- In the terminal, launch the sbt interactive shell:
sbt
- In the sbt shell, type
run
(and select the class) - OR, type
runMain session0.b_hello_neko.HelloNeko
to directly run the class.
- In the terminal, launch the sbt interactive shell:
- You should observe the following (or similar):
sbt:DistribAlgo> runMain session0.b_hello_neko.HelloNeko [info] compiling 1 Scala source to ... [info] running (fork) session0.b_hello_neko.HelloNeko [info] Process p2 says: 'Hello Neko World!' [info] Process p0 says: 'Hello Neko World!' [info] Process p4 says: 'Hello Neko World!' [info] Process p3 says: 'Hello Neko World!' [info] Process p1 says: 'Hello Neko World!' [success] Total time: 4 s, completed ... sbt:DistribAlgo>
Alternatively, you can also use IntelliJ IDEA