Skip to content

Commit cc67ab6

Browse files
authored
Fix - ignore shebang lines only at the top of source files (#129)
* Fix - ignore shebang lines only at the top of source files * Fix parsing new lines for windows
1 parent 2d1792d commit cc67ab6

File tree

4 files changed

+51
-11
lines changed

4 files changed

+51
-11
lines changed

modules/build/src/main/scala/scala/build/preprocessing/ScriptPreprocessor.scala

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,19 @@ final case class ScriptPreprocessor(codeWrapper: CodeWrapper) extends Preprocess
4949

5050
object ScriptPreprocessor {
5151

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

5454
private def ignoreSheBangLines(content: String): String = {
5555
if (content.startsWith("#!")) {
56-
sheBangRegex.replaceAllIn(content, "")
56+
val regexMatch = sheBangRegex.findFirstMatchIn(content)
57+
regexMatch match {
58+
case Some(firstMatch) =>
59+
content.replace(
60+
firstMatch.toString(),
61+
System.lineSeparator() * firstMatch.toString().split(System.lineSeparator()).length
62+
)
63+
case None => content
64+
}
5765
}
5866
else {
5967
content

modules/build/src/test/scala/scala/build/tests/SourcesTests.scala

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -109,27 +109,27 @@ class SourcesTests extends munit.FunSuite {
109109
os.rel / "something1.sc" ->
110110
"""#!/usr/bin/env scala-cli
111111
|
112-
|println("Hello World")
113-
|""".stripMargin,
112+
|println("Hello World")""".stripMargin,
114113
os.rel / "something2.sc" ->
115114
"""#!/usr/bin/scala-cli
116115
|
117-
|println("Hello World")
118-
|""".stripMargin,
116+
|println("Hello World")""".stripMargin,
119117
os.rel / "something3.sc" ->
120118
"""#!/usr/bin/scala-cli
121119
|#! nix-shell -i scala-cli
122120
|
123-
|println("Hello World")
124-
|""".stripMargin,
121+
|println("Hello World")""".stripMargin,
125122
os.rel / "something4.sc" ->
126123
"""#!/usr/bin/scala-cli
127124
|#! nix-shell -i scala-cli
128125
|
129126
|!#
130127
|
131-
|println("Hello World")
132-
|""".stripMargin
128+
|println("Hello World")""".stripMargin,
129+
os.rel / "something5.sc" ->
130+
"""#!/usr/bin/scala-cli
131+
|
132+
|println("Hello World #!")""".stripMargin
133133
)
134134
val expectedParsedCodes = Seq(
135135
"""
@@ -144,7 +144,10 @@ class SourcesTests extends munit.FunSuite {
144144
|
145145
|
146146
|
147-
|println("Hello World")""".stripMargin
147+
|println("Hello World")""".stripMargin,
148+
"""
149+
|
150+
|println("Hello World #!")""".stripMargin
148151
)
149152

150153
testInputs.withInputs { (_, inputs) =>

website/docs/input.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,22 @@ scala-cli my-app --main-class main
169169
Note that we pass an explicit main class. Both scripts automatically get a main class, so this
170170
is required to disambiguate them.
171171

172+
### Self executable Scala Script
173+
174+
You can define file with shebang header to self executable. It could be also run as a normal script.
175+
176+
```bash
177+
cat HelloScript.sc
178+
# #!/usr/bin/env scala-cli
179+
# println("Hello world")
180+
181+
scala-cli run HelloScript.sc
182+
# Hello world
183+
chmod +x HelloScript.sc
184+
./HelloScript.sc
185+
# Hello world
186+
```
187+
172188
### Difference with Ammonite scripts
173189

174190
[Ammonite](http://ammonite.io) is a popular REPL for Scala, that is also able to compile and run

website/docs/run.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,19 @@ and that Scala Native only supports Linux and macOS for now.
6464
scala-cli my-scala-native-app/ --native
6565
```
6666

67+
## Scala Scripts
68+
69+
Scala CLI can also compile and run Scala scripts.
70+
```bash
71+
cat HelloScript.sc
72+
# #!/usr/bin/env scala-cli
73+
74+
# println("Hello world from scala script")
75+
76+
scala-cli run HelloScript.sc
77+
# Hello world from scala script
78+
```
79+
6780
## ScalaCli from docker
6881

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

0 commit comments

Comments
 (0)