From 9658ac0a67ae49021bd380d4a891b9de5fe2f9cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Wro=C5=84ski?= Date: Mon, 13 Sep 2021 11:54:08 +0200 Subject: [PATCH 1/5] Fix - ignore shebang lines only at the top of source files --- .../scala/build/preprocessing/ScriptPreprocessor.scala | 9 +++++++-- .../test/scala/scala/build/tests/SourcesTests.scala | 10 +++++++++- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/modules/build/src/main/scala/scala/build/preprocessing/ScriptPreprocessor.scala b/modules/build/src/main/scala/scala/build/preprocessing/ScriptPreprocessor.scala index 59ef619ded..e8bbd257eb 100644 --- a/modules/build/src/main/scala/scala/build/preprocessing/ScriptPreprocessor.scala +++ b/modules/build/src/main/scala/scala/build/preprocessing/ScriptPreprocessor.scala @@ -48,11 +48,16 @@ final case class ScriptPreprocessor(codeWrapper: CodeWrapper) extends Preprocess object ScriptPreprocessor { - private val sheBangRegex: Regex = s"""((#!.*)|(!#.*))""".r + private val sheBangRegex: Regex = s"""(^(#!.*\\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(), "\n" * firstMatch.toString().count(_ == '\n')) + case None => content + } } else { content diff --git a/modules/build/src/test/scala/scala/build/tests/SourcesTests.scala b/modules/build/src/test/scala/scala/build/tests/SourcesTests.scala index 2f7f03269a..1d1f391386 100644 --- a/modules/build/src/test/scala/scala/build/tests/SourcesTests.scala +++ b/modules/build/src/test/scala/scala/build/tests/SourcesTests.scala @@ -93,6 +93,11 @@ class SourcesTests extends munit.FunSuite { |!# | |println("Hello World") + |""".stripMargin, + os.rel / "something5.sc" -> + """#!/usr/bin/scala-cli + | + |println("Hello World #!") |""".stripMargin ) val expectedParsedCodes = Seq( @@ -108,7 +113,10 @@ class SourcesTests extends munit.FunSuite { | | | - |println("Hello World")""".stripMargin + |println("Hello World")""".stripMargin, + """ + | + |println("Hello World #!")""".stripMargin ) testInputs.withInputs { (_, inputs) => From 1c36712287aa0179aae8f061c63cdd4638a27294 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Wro=C5=84ski?= Date: Mon, 13 Sep 2021 12:25:06 +0200 Subject: [PATCH 2/5] Fix parsing new lines for windows --- .../build/preprocessing/ScriptPreprocessor.scala | 7 +++++-- .../scala/scala/build/tests/SourcesTests.scala | 15 +++++---------- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/modules/build/src/main/scala/scala/build/preprocessing/ScriptPreprocessor.scala b/modules/build/src/main/scala/scala/build/preprocessing/ScriptPreprocessor.scala index e8bbd257eb..41fac1fd2c 100644 --- a/modules/build/src/main/scala/scala/build/preprocessing/ScriptPreprocessor.scala +++ b/modules/build/src/main/scala/scala/build/preprocessing/ScriptPreprocessor.scala @@ -48,14 +48,17 @@ final case class ScriptPreprocessor(codeWrapper: CodeWrapper) extends Preprocess object ScriptPreprocessor { - private val sheBangRegex: Regex = s"""(^(#!.*\\n?)+\\s*(!#.*)?)""".r + private val sheBangRegex: Regex = s"""(^(#!.*(\\r\\n?|\\n)?)+(\\s*!#.*)?)""".r private def ignoreSheBangLines(content: String): String = { if (content.startsWith("#!")) { val regexMatch = sheBangRegex.findFirstMatchIn(content) regexMatch match { case Some(firstMatch) => - content.replace(firstMatch.toString(), "\n" * firstMatch.toString().count(_ == '\n')) + content.replace( + firstMatch.toString(), + System.lineSeparator() * firstMatch.toString().split(System.lineSeparator()).length + ) case None => content } } diff --git a/modules/build/src/test/scala/scala/build/tests/SourcesTests.scala b/modules/build/src/test/scala/scala/build/tests/SourcesTests.scala index 1d1f391386..074439479d 100644 --- a/modules/build/src/test/scala/scala/build/tests/SourcesTests.scala +++ b/modules/build/src/test/scala/scala/build/tests/SourcesTests.scala @@ -73,32 +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 + |println("Hello World #!")""".stripMargin ) val expectedParsedCodes = Seq( """ From 8e6ab8773f450a31df2eab856b1fe85043241af1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Wro=C5=84ski?= Date: Mon, 13 Sep 2021 15:56:31 +0200 Subject: [PATCH 3/5] Add scala-scripts doc sections --- website/docs/about.md | 2 +- website/docs/internals.md | 2 +- website/docs/misc/_category_.json | 2 +- website/docs/reference/_category_.json | 2 +- website/docs/run.md | 13 ++++++++++ website/docs/scala-scripts.md | 33 ++++++++++++++++++++++++++ 6 files changed, 50 insertions(+), 4 deletions(-) create mode 100644 website/docs/scala-scripts.md diff --git a/website/docs/about.md b/website/docs/about.md index 8d2aac35d9..ccf153a89b 100644 --- a/website/docs/about.md +++ b/website/docs/about.md @@ -1,6 +1,6 @@ --- title: About -sidebar_position: 16 +sidebar_position: 17 --- - developed at VirtusLab diff --git a/website/docs/internals.md b/website/docs/internals.md index a2145d4804..117faa750e 100644 --- a/website/docs/internals.md +++ b/website/docs/internals.md @@ -1,6 +1,6 @@ --- title: Internals -sidebar_position: 15 +sidebar_position: 16 --- - Bloop diff --git a/website/docs/misc/_category_.json b/website/docs/misc/_category_.json index 31ad0591e3..f0f94ae8dc 100644 --- a/website/docs/misc/_category_.json +++ b/website/docs/misc/_category_.json @@ -1,4 +1,4 @@ { "label": "Miscellaneous", - "position": 13 + "position": 14 } diff --git a/website/docs/reference/_category_.json b/website/docs/reference/_category_.json index 5897c902d1..756444f1b6 100644 --- a/website/docs/reference/_category_.json +++ b/website/docs/reference/_category_.json @@ -1,4 +1,4 @@ { "label": "Reference", - "position": 17 + "position": 18 } diff --git a/website/docs/run.md b/website/docs/run.md index ee4c063cb4..45103b2e04 100644 --- a/website/docs/run.md +++ b/website/docs/run.md @@ -64,6 +64,19 @@ and that Scala Native only supports Linux and macOS for now. scala-cli my-scala-native-app/ --native ``` +## ScalaScripts + +ScalaScripts can also be compiled and run. +```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`. diff --git a/website/docs/scala-scripts.md b/website/docs/scala-scripts.md new file mode 100644 index 0000000000..1146290871 --- /dev/null +++ b/website/docs/scala-scripts.md @@ -0,0 +1,33 @@ +--- +title: ScalaScripts +sidebar_position: 13 +--- + +The Scala CLI can compile, run, package, etc. ScalaScripts sources. + +Script can be easily run from one command line, you don't have to setup build tool for Scala. + +## Quick Start + +```bash +cat HelloScript.sc +# println("Hello world") + +scala-cli run HelloScript.sc +# Hello world +``` + +## Self executable ScalaScript + +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 +./HelloScript.sc +# Hello world +``` \ No newline at end of file From c52886275907551f9e5782aa3631b428aed9458c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Wro=C5=84ski?= Date: Tue, 14 Sep 2021 09:25:13 +0200 Subject: [PATCH 4/5] Fixes after review --- website/docs/run.md | 4 ++-- website/docs/scala-scripts.md | 7 ++++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/website/docs/run.md b/website/docs/run.md index 45103b2e04..816a76a33a 100644 --- a/website/docs/run.md +++ b/website/docs/run.md @@ -64,9 +64,9 @@ and that Scala Native only supports Linux and macOS for now. scala-cli my-scala-native-app/ --native ``` -## ScalaScripts +## Scala Scripts -ScalaScripts can also be compiled and run. +Scala CLI can also compile and run Scala scripts. ```bash cat HelloScript.sc # #!/usr/bin/env scala-cli diff --git a/website/docs/scala-scripts.md b/website/docs/scala-scripts.md index 1146290871..a84910ff67 100644 --- a/website/docs/scala-scripts.md +++ b/website/docs/scala-scripts.md @@ -1,9 +1,9 @@ --- -title: ScalaScripts +title: Scala Scripts sidebar_position: 13 --- -The Scala CLI can compile, run, package, etc. ScalaScripts sources. +The Scala CLI can compile, run, package, etc. Scala Scripts sources. Script can be easily run from one command line, you don't have to setup build tool for Scala. @@ -17,7 +17,7 @@ scala-cli run HelloScript.sc # Hello world ``` -## Self executable ScalaScript +## Self executable Scala Script You can define file with shebang header to self executable. It could be also run as a normal script. @@ -28,6 +28,7 @@ cat HelloScript.sc scala-cli run HelloScript.sc # Hello world +chmod +x HelloScript.sc ./HelloScript.sc # Hello world ``` \ No newline at end of file From 42016b78d8bfc22fc707038ec0791da9d68f74ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Wro=C5=84ski?= Date: Tue, 14 Sep 2021 16:04:36 +0200 Subject: [PATCH 5/5] Move shebang scala scripts sections to input-format --- website/docs/about.md | 2 +- website/docs/input.md | 16 ++++++++++++ website/docs/internals.md | 2 +- website/docs/misc/_category_.json | 2 +- website/docs/reference/_category_.json | 2 +- website/docs/scala-scripts.md | 34 -------------------------- 6 files changed, 20 insertions(+), 38 deletions(-) delete mode 100644 website/docs/scala-scripts.md diff --git a/website/docs/about.md b/website/docs/about.md index ccf153a89b..8d2aac35d9 100644 --- a/website/docs/about.md +++ b/website/docs/about.md @@ -1,6 +1,6 @@ --- title: About -sidebar_position: 17 +sidebar_position: 16 --- - developed at VirtusLab diff --git a/website/docs/input.md b/website/docs/input.md index 1acdc16199..e150340c5a 100644 --- a/website/docs/input.md +++ b/website/docs/input.md @@ -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 diff --git a/website/docs/internals.md b/website/docs/internals.md index 117faa750e..a2145d4804 100644 --- a/website/docs/internals.md +++ b/website/docs/internals.md @@ -1,6 +1,6 @@ --- title: Internals -sidebar_position: 16 +sidebar_position: 15 --- - Bloop diff --git a/website/docs/misc/_category_.json b/website/docs/misc/_category_.json index f0f94ae8dc..31ad0591e3 100644 --- a/website/docs/misc/_category_.json +++ b/website/docs/misc/_category_.json @@ -1,4 +1,4 @@ { "label": "Miscellaneous", - "position": 14 + "position": 13 } diff --git a/website/docs/reference/_category_.json b/website/docs/reference/_category_.json index 756444f1b6..5897c902d1 100644 --- a/website/docs/reference/_category_.json +++ b/website/docs/reference/_category_.json @@ -1,4 +1,4 @@ { "label": "Reference", - "position": 18 + "position": 17 } diff --git a/website/docs/scala-scripts.md b/website/docs/scala-scripts.md deleted file mode 100644 index a84910ff67..0000000000 --- a/website/docs/scala-scripts.md +++ /dev/null @@ -1,34 +0,0 @@ ---- -title: Scala Scripts -sidebar_position: 13 ---- - -The Scala CLI can compile, run, package, etc. Scala Scripts sources. - -Script can be easily run from one command line, you don't have to setup build tool for Scala. - -## Quick Start - -```bash -cat HelloScript.sc -# println("Hello world") - -scala-cli run HelloScript.sc -# Hello world -``` - -## 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 -``` \ No newline at end of file