-
Notifications
You must be signed in to change notification settings - Fork 145
improvement: Detect objects with main class in scripts #3479
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
88 changes: 88 additions & 0 deletions
88
modules/build/src/main/scala/scala/build/internal/WrapperUtils.scala
This file contains hidden or 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,88 @@ | ||
package scala.build.internal | ||
|
||
import scala.build.internal.util.WarningMessages | ||
|
||
object WrapperUtils { | ||
|
||
enum ScriptMainMethod: | ||
case Exists(name: String) | ||
case Multiple(names: Seq[String]) | ||
case ToplevelStatsPresent | ||
case ToplevelStatsWithMultiple(names: Seq[String]) | ||
case NoMain | ||
|
||
def warningMessage: List[String] = | ||
this match | ||
case ScriptMainMethod.Multiple(names) => | ||
List(WarningMessages.multipleMainObjectsInScript(names)) | ||
case ScriptMainMethod.ToplevelStatsPresent => List( | ||
WarningMessages.mixedToplvelAndObjectInScript | ||
) | ||
case ToplevelStatsWithMultiple(names) => | ||
List( | ||
WarningMessages.multipleMainObjectsInScript(names), | ||
WarningMessages.mixedToplvelAndObjectInScript | ||
) | ||
case _ => Nil | ||
|
||
def mainObjectInScript(scalaVersion: String, code: String): ScriptMainMethod = | ||
import scala.meta.* | ||
|
||
val scriptDialect = | ||
if scalaVersion.startsWith("3") then dialects.Scala3Future else dialects.Scala213Source3 | ||
|
||
given Dialect = scriptDialect.withAllowToplevelStatements(true).withAllowToplevelTerms(true) | ||
val parsedCode = code.parse[Source] match | ||
case Parsed.Success(Source(stats)) => stats | ||
case _ => Nil | ||
|
||
// Check if there is a main function defined inside an object | ||
def checkSignature(defn: Defn.Def) = | ||
defn.paramClauseGroups match | ||
case List(Member.ParamClauseGroup( | ||
Type.ParamClause(Nil), | ||
List(Term.ParamClause( | ||
List(Term.Param( | ||
Nil, | ||
_: Term.Name, | ||
Some(Type.Apply.After_4_6_0( | ||
Type.Name("Array"), | ||
Type.ArgClause(List(Type.Name("String"))) | ||
)), | ||
None | ||
)), | ||
None | ||
)) | ||
)) => true | ||
case _ => false | ||
|
||
def noToplevelStatements = parsedCode.forall { | ||
case _: Term => false | ||
case _ => true | ||
} | ||
|
||
def hasMainSignature(templ: Template) = templ.body.stats.exists { | ||
case defn: Defn.Def => | ||
defn.name.value == "main" && checkSignature(defn) | ||
case _ => false | ||
} | ||
def extendsApp(templ: Template) = templ.inits match | ||
case Init.After_4_6_0(Type.Name("App"), _, Nil) :: Nil => true | ||
case _ => false | ||
val potentialMains = parsedCode.collect { | ||
case Defn.Object(_, objName, templ) if extendsApp(templ) || hasMainSignature(templ) => | ||
Seq(objName.value) | ||
}.flatten | ||
|
||
potentialMains match | ||
case head :: Nil if noToplevelStatements => | ||
ScriptMainMethod.Exists(head) | ||
case head :: Nil => | ||
ScriptMainMethod.ToplevelStatsPresent | ||
case Nil => ScriptMainMethod.NoMain | ||
case seq if noToplevelStatements => | ||
ScriptMainMethod.Multiple(seq) | ||
case seq => | ||
ScriptMainMethod.ToplevelStatsWithMultiple(seq) | ||
|
||
} |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.