-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add class code wrapper for scala 3 scripts * Move wrapping scripts to right before building * Add warning when @main is used in scripts wrapped in class * NIT Move mainClassObject def to CodeWrapper object * Add new types to enforce wrapping scripts before building project
- Loading branch information
1 parent
9dee77c
commit 77237f0
Showing
27 changed files
with
651 additions
and
159 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
modules/build/src/main/scala/scala/build/internal/ClassCodeWrapper.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package scala.build.internal | ||
|
||
/** Script code wrapper that solves problem of deadlocks when using threads. The code is placed in a | ||
* class instance constructor, the created object is kept in 'mainObjectCode'.script to support | ||
* running interconnected scripts using Scala CLI <br> <br> Incompatible with Scala 2 - it uses | ||
* Scala 3 feature 'export'<br> Incompatible with native JS members - the wrapper is a class | ||
*/ | ||
case object ClassCodeWrapper extends CodeWrapper { | ||
private val userCodeNestingLevel = 1 | ||
def apply( | ||
code: String, | ||
pkgName: Seq[Name], | ||
indexedWrapperName: Name, | ||
extraCode: String, | ||
scriptPath: String | ||
) = { | ||
val name = CodeWrapper.mainClassObject(indexedWrapperName).backticked | ||
val wrapperClassName = Name(indexedWrapperName.raw ++ "$_").backticked | ||
val mainObjectCode = | ||
AmmUtil.normalizeNewlines(s"""|object $name { | ||
| private var args$$opt0 = Option.empty[Array[String]] | ||
| def args$$set(args: Array[String]): Unit = { | ||
| args$$opt0 = Some(args) | ||
| } | ||
| def args$$opt: Option[Array[String]] = args$$opt0 | ||
| def args$$: Array[String] = args$$opt.getOrElse { | ||
| sys.error("No arguments passed to this script") | ||
| } | ||
| | ||
| lazy val script = new $wrapperClassName | ||
| | ||
| def main(args: Array[String]): Unit = { | ||
| args$$set(args) | ||
| script.hashCode() // hashCode to clear scalac warning about pure expression in statement position | ||
| } | ||
|} | ||
| | ||
|export $name.script as ${indexedWrapperName.backticked} | ||
|""".stripMargin) | ||
|
||
val packageDirective = | ||
if (pkgName.isEmpty) "" else s"package ${AmmUtil.encodeScalaSourcePath(pkgName)}" + "\n" | ||
|
||
// indentation is important in the generated code, so we don't want scalafmt to touch that | ||
// format: off | ||
val top = AmmUtil.normalizeNewlines(s""" | ||
$packageDirective | ||
|
||
|
||
final class $wrapperClassName { | ||
def args = $name.args$$ | ||
def scriptPath = \"\"\"$scriptPath\"\"\" | ||
""") | ||
val bottom = AmmUtil.normalizeNewlines(s""" | ||
$extraCode | ||
} | ||
|
||
$mainObjectCode | ||
""") | ||
// format: on | ||
|
||
(top, bottom, userCodeNestingLevel) | ||
} | ||
} |
Oops, something went wrong.