Skip to content

Commit

Permalink
Add platform option
Browse files Browse the repository at this point in the history
  • Loading branch information
wleczny committed Sep 14, 2022
1 parent 540e7d5 commit d9a93b0
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ final case class SharedOptions(
@Name("resourceDir")
resourceDirs: List[String] = Nil,

@HelpMessage("Specify platform")
@ValueDescription("scala-js|scala-native|jvm")
platform: Option[String] = None,

@Group("Scala")
@Hidden
scalaLibrary: Option[Boolean] = None,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,16 @@ object SharedOptionsUtil extends CommandHelpers {
jmhVersion: Option[String] = None,
ignoreErrors: Boolean = false
): bo.BuildOptions = {
val platformOpt =
if (js.js) Some(Platform.JS)
else if (native.native) Some(Platform.Native)
else None
val platformStr = platform.map(Platform.normalize)
val platformOpt = platformStr.map(Platform.parse) match {
case Some(p) => p
case None =>
if (native.native && js.js) Some(Platform.Native)
else if (js.js) Some(Platform.JS)
else if (native.native) Some(Platform.Native)
else None
}

bo.BuildOptions(
scalaOptions = bo.ScalaOptions(
scalaVersion = scalaVersion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,24 @@ abstract class RunTestDefinitions(val scalaVersionOpt: Option[String])
}
}

test("simple script JS via platform option") {
val message = "Hello"
val inputs = TestInputs(
os.rel / "simple.sc" ->
s"""//> using platform "scala-native"
|import scala.scalajs.js
|val console = js.Dynamic.global.console
|val msg = "$message"
|console.log(msg)
|""".stripMargin
)
inputs.fromRoot { root =>
val output =
os.proc(TestUtil.cli, extraOptions, ".", "--platform", "js").call(cwd = root).out.trim()
expect(output == message)
}
}

def platformNl: String = if (Properties.isWin) "\\r\\n" else "\\n"

def simpleNativeTests(): Unit = {
Expand Down
25 changes: 25 additions & 0 deletions website/docs/commands/run.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,12 @@ Note that this requires `node` to be [installed](/install#scala-js) on your syst
scala-cli Hello.scala --js
```

It is also possible to achieve it using `--platform` option:

```bash
scala-cli Hello.scala --platform js
```

See our dedicated [Scala.js guide](../guides/scala-js.md) for more information.

## Scala Native
Expand All @@ -146,8 +152,27 @@ Note that the [Scala Native requirements](https://scala-native.readthedocs.io/en
scala-cli Hello.scala --native -S 2.13.6
```

It is also possible to achieve it using `--platform` option:

```bash
scala-cli Hello.scala --platform native
```

We have a dedicated [Scala Native guide](../guides/scala-native.md) as well.

## Platform

The `--platform` option can be used to choose the platform, which should be used to compile and run application. Available platforms are:
* JVM (`jvm`)
* Scala.js (`scala.js` | `scala-js` | `scalajs` | `js`)
* Scala Native (`scala-native` | `scalanative` | `native`)

Please note that `--platform` option is prioritized over `--js` and `--native` options. For example, the following command will run using Scala Native:

```bash
scala-cli Hello.scala --js --platform scala-native
```

## Scala Scripts

Scala CLI can also compile and run Scala scripts:
Expand Down
4 changes: 4 additions & 0 deletions website/docs/reference/cli-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -1260,6 +1260,10 @@ Aliases: `--resource-dir`

Add a resource directory

### `--platform`

Specify platform

### `--scala-library`

[Internal]
Expand Down
4 changes: 4 additions & 0 deletions website/docs/reference/scala-command/cli-options.md
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,10 @@ Aliases: `--resource-dir`

Add a resource directory

### `--platform`

Specify platform

### `--scala-library`

[Internal]
Expand Down

0 comments on commit d9a93b0

Please sign in to comment.