Skip to content

Commit

Permalink
Add support for running jar as an input
Browse files Browse the repository at this point in the history
  • Loading branch information
lwronski committed Dec 29, 2022
1 parent 1234a52 commit 5a538d1
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 2 deletions.
Expand Up @@ -268,6 +268,7 @@ object CrossSources {
else if (sourcePath.ext == "scala") Right(Seq(SourceScalaFile(dir, subPath)))
else if (sourcePath.ext == "sc") Right(Seq(Script(dir, subPath)))
else if (sourcePath.ext == "java") Right(Seq(JavaFile(dir, subPath)))
else if (sourcePath.ext == "jar") Right(Seq(JarFile(dir, subPath)))
else if (sourcePath.ext == "md") Right(Seq(MarkdownFile(dir, subPath)))
else {
val msg =
Expand Down
5 changes: 5 additions & 0 deletions modules/build/src/main/scala/scala/build/input/Element.scala
Expand Up @@ -85,6 +85,11 @@ final case class JavaFile(base: os.Path, subPath: os.SubPath)
lazy val path: os.Path = base / subPath
}

final case class JarFile(base: os.Path, subPath: os.SubPath)
extends OnDisk with SourceFile {
lazy val path: os.Path = base / subPath
}

final case class CFile(base: os.Path, subPath: os.SubPath)
extends OnDisk with SourceFile with Compiled {
lazy val path: os.Path = base / subPath
Expand Down
Expand Up @@ -59,6 +59,7 @@ object ElementsUtils {
case _: CFile => "c:"
case _: Script => "sc:"
case _: MarkdownFile => "md:"
case _: JarFile => "jar:"
}
Iterator(prefix, elem.path.toString, "\n").map(bytes)
case v: Virtual =>
Expand Down
Expand Up @@ -286,6 +286,7 @@ object Inputs {
else if arg.endsWith(".sc") then Right(Seq(Script(dir, subPath)))
else if arg.endsWith(".scala") then Right(Seq(SourceScalaFile(dir, subPath)))
else if arg.endsWith(".java") then Right(Seq(JavaFile(dir, subPath)))
else if arg.endsWith(".jar") then Right(Seq(JarFile(dir, subPath)))
else if arg.endsWith(".c") || arg.endsWith(".h") then Right(Seq(CFile(dir, subPath)))
else if arg.endsWith(".md") then Right(Seq(MarkdownFile(dir, subPath)))
else if os.isDir(path) then Right(Seq(Directory(path)))
Expand Down
Expand Up @@ -9,9 +9,9 @@ import java.nio.charset.StandardCharsets
import scala.build.EitherCps.{either, value}
import scala.build.Logger
import scala.build.errors.BuildException
import scala.build.input.{Inputs, JavaFile, SingleElement, VirtualJavaFile}
import scala.build.input.{Inputs, JarFile, JavaFile, SingleElement, VirtualJavaFile}
import scala.build.internal.JavaParserProxyMaker
import scala.build.options.BuildRequirements
import scala.build.options.{BuildOptions, BuildRequirements, ClassPathOptions}
import scala.build.preprocessing.ExtractedDirectives.from
import scala.build.preprocessing.PreprocessingUtil.optionsAndPositionsFromDirectives
import scala.build.preprocessing.ScalaPreprocessor._
Expand Down Expand Up @@ -109,6 +109,21 @@ final case class JavaPreprocessor(
}
Some(res)

case jar: JarFile => Some(either {
val buildOptions = BuildOptions().copy(
classPathOptions = ClassPathOptions(
extraClassPath = Seq(jar.path)
)
)
Seq(PreprocessedSource.OnDisk(
jar.path,
Some(buildOptions),
Some(BuildRequirements()),
Nil,
None,
None
))
})
case _ => None
}
}
Expand Up @@ -1008,6 +1008,24 @@ abstract class RunTestDefinitions(val scalaVersionOpt: Option[String])
}
}

test("run jar file") {
val inputs = TestInputs(
os.rel / "Hello.scala" ->
s"""object Hello extends App {
| println("Hello World")
|}""".stripMargin
)
inputs.fromRoot { root =>
// build jar
val helloJarPath = root / "Hello.jar"
os.proc(TestUtil.cli, "package", ".", "--library", "-o", helloJarPath).call(cwd = root)

// run jar
val output = os.proc(TestUtil.cli, helloJarPath).call(cwd = root).out.trim()
expect(output == "Hello World")
}
}

if (actualScalaVersion.startsWith("3"))
test("should throw exception for code compiled by scala 3.1.3") {
val exceptionMsg = "Throw exception in Scala"
Expand Down
13 changes: 13 additions & 0 deletions website/docs/commands/run.md
Expand Up @@ -106,6 +106,19 @@ You can also add java options with the using directive `//> using javaOpt`:
//> using javaOpt "-Xmx1g", "-Dfoo=bar"
```

### JAR

`scala-cli` lets you run JAR files just like any other input.

```bash ignore
scala-cli Hello.jar
```

```text
Hello World
```

When you provide a JAR file as input to `scala-cli`, it will be added to the `classPath`.

## Define source files in using directives

Expand Down

0 comments on commit 5a538d1

Please sign in to comment.