Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,19 @@ final case class ScriptPreprocessor(codeWrapper: CodeWrapper) extends Preprocess

object ScriptPreprocessor {

private val sheBangRegex: Regex = s"""((#!.*)|(!#.*))""".r
private val sheBangRegex: Regex = s"""(^(#!.*(\\r\\n?|\\n)?)+(\\s*!#.*)?)""".r

private def ignoreSheBangLines(content: String): String = {
if (content.startsWith("#!")) {
sheBangRegex.replaceAllIn(content, "")
val regexMatch = sheBangRegex.findFirstMatchIn(content)
regexMatch match {
case Some(firstMatch) =>
content.replace(
firstMatch.toString(),
System.lineSeparator() * firstMatch.toString().split(System.lineSeparator()).length
)
case None => content
}
}
else {
content
Expand Down
21 changes: 12 additions & 9 deletions modules/build/src/test/scala/scala/build/tests/SourcesTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -73,27 +73,27 @@ class SourcesTests extends munit.FunSuite {
os.rel / "something1.sc" ->
"""#!/usr/bin/env scala-cli
|
|println("Hello World")
|""".stripMargin,
|println("Hello World")""".stripMargin,
os.rel / "something2.sc" ->
"""#!/usr/bin/scala-cli
|
|println("Hello World")
|""".stripMargin,
|println("Hello World")""".stripMargin,
os.rel / "something3.sc" ->
"""#!/usr/bin/scala-cli
|#! nix-shell -i scala-cli
|
|println("Hello World")
|""".stripMargin,
|println("Hello World")""".stripMargin,
os.rel / "something4.sc" ->
"""#!/usr/bin/scala-cli
|#! nix-shell -i scala-cli
|
|!#
|
|println("Hello World")
|""".stripMargin
|println("Hello World")""".stripMargin,
os.rel / "something5.sc" ->
"""#!/usr/bin/scala-cli
|
|println("Hello World #!")""".stripMargin
)
val expectedParsedCodes = Seq(
"""
Expand All @@ -108,7 +108,10 @@ class SourcesTests extends munit.FunSuite {
|
|
|
|println("Hello World")""".stripMargin
|println("Hello World")""".stripMargin,
"""
|
|println("Hello World #!")""".stripMargin
)

testInputs.withInputs { (_, inputs) =>
Expand Down
16 changes: 16 additions & 0 deletions website/docs/input.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,22 @@ scala-cli my-app --main-class main
Note that we pass an explicit main class. Both scripts automatically get a main class, so this
is required to disambiguate them.

### Self executable Scala Script

You can define file with shebang header to self executable. It could be also run as a normal script.

```bash
cat HelloScript.sc
# #!/usr/bin/env scala-cli
# println("Hello world")

scala-cli run HelloScript.sc
# Hello world
chmod +x HelloScript.sc
./HelloScript.sc
# Hello world
```

### Difference with Ammonite scripts

[Ammonite](http://ammonite.io) is a popular REPL for Scala, that is also able to compile and run
Expand Down
13 changes: 13 additions & 0 deletions website/docs/run.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,19 @@ and that Scala Native only supports Linux and macOS for now.
scala-cli my-scala-native-app/ --native
```

## Scala Scripts

Scala CLI can also compile and run Scala scripts.
```bash
cat HelloScript.sc
# #!/usr/bin/env scala-cli

# println("Hello world from scala script")

scala-cli run HelloScript.sc
# Hello world from scala script
```

## ScalaCli from docker

Scala applications can also be compiled and run using docker image with `scala-cli`.
Expand Down