From d9a93b04fc4910f38aa1ee39ac64aa93bbba9f15 Mon Sep 17 00:00:00 2001 From: Marek Moryl Date: Wed, 31 Aug 2022 22:41:59 +0200 Subject: [PATCH] Add platform option --- .../scala/cli/commands/SharedOptions.scala | 4 +++ .../cli/commands/util/SharedOptionsUtil.scala | 14 ++++++++--- .../cli/integration/RunTestDefinitions.scala | 18 +++++++++++++ website/docs/commands/run.md | 25 +++++++++++++++++++ website/docs/reference/cli-options.md | 4 +++ .../reference/scala-command/cli-options.md | 4 +++ 6 files changed, 65 insertions(+), 4 deletions(-) diff --git a/modules/cli-options/src/main/scala/scala/cli/commands/SharedOptions.scala b/modules/cli-options/src/main/scala/scala/cli/commands/SharedOptions.scala index bba1ef8beb..68c4851db6 100644 --- a/modules/cli-options/src/main/scala/scala/cli/commands/SharedOptions.scala +++ b/modules/cli-options/src/main/scala/scala/cli/commands/SharedOptions.scala @@ -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, diff --git a/modules/cli/src/main/scala/scala/cli/commands/util/SharedOptionsUtil.scala b/modules/cli/src/main/scala/scala/cli/commands/util/SharedOptionsUtil.scala index 38321dd188..4f99827ba0 100644 --- a/modules/cli/src/main/scala/scala/cli/commands/util/SharedOptionsUtil.scala +++ b/modules/cli/src/main/scala/scala/cli/commands/util/SharedOptionsUtil.scala @@ -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 diff --git a/modules/integration/src/test/scala/scala/cli/integration/RunTestDefinitions.scala b/modules/integration/src/test/scala/scala/cli/integration/RunTestDefinitions.scala index 44073a3e62..4ae3d2387c 100644 --- a/modules/integration/src/test/scala/scala/cli/integration/RunTestDefinitions.scala +++ b/modules/integration/src/test/scala/scala/cli/integration/RunTestDefinitions.scala @@ -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 = { diff --git a/website/docs/commands/run.md b/website/docs/commands/run.md index 21b33fc6c3..b1f0f97eac 100644 --- a/website/docs/commands/run.md +++ b/website/docs/commands/run.md @@ -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 @@ -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: diff --git a/website/docs/reference/cli-options.md b/website/docs/reference/cli-options.md index a987493b16..f9d4688475 100644 --- a/website/docs/reference/cli-options.md +++ b/website/docs/reference/cli-options.md @@ -1260,6 +1260,10 @@ Aliases: `--resource-dir` Add a resource directory +### `--platform` + +Specify platform + ### `--scala-library` [Internal] diff --git a/website/docs/reference/scala-command/cli-options.md b/website/docs/reference/scala-command/cli-options.md index 4799915c3b..f506bf2aa3 100644 --- a/website/docs/reference/scala-command/cli-options.md +++ b/website/docs/reference/scala-command/cli-options.md @@ -670,6 +670,10 @@ Aliases: `--resource-dir` Add a resource directory +### `--platform` + +Specify platform + ### `--scala-library` [Internal]