Skip to content

Commit

Permalink
NIT Refacto of main class detection stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
alexarchambault committed Jul 25, 2022
1 parent ddb22b3 commit 1da1a32
Showing 1 changed file with 41 additions and 33 deletions.
74 changes: 41 additions & 33 deletions modules/build/src/main/scala/scala/build/Build.scala
Original file line number Diff line number Diff line change
Expand Up @@ -66,39 +66,7 @@ object Build {
case Seq() => Left(new NoMainClassFoundError)
case Seq(mainClass) => Right(mainClass)
case _ =>
val scriptInferredMainClasses =
sources.inMemory.map(im => im.originalPath.map(_._1))
.flatMap {
case Right(originalRelPath) if originalRelPath.toString.endsWith(".sc") =>
Some {
originalRelPath
.toString
.replace(".", "_")
.replace("/", ".")
}
case Left(VirtualScriptNameRegex(name)) => Some(s"${name}_sc")
case _ => None
}
val filteredMainClasses =
mainClasses.filter(!scriptInferredMainClasses.contains(_))
if (filteredMainClasses.length == 1) {
val pickedMainClass = filteredMainClasses.head
if (scriptInferredMainClasses.nonEmpty) {
val firstScript = scriptInferredMainClasses.head
val scriptsString = scriptInferredMainClasses.mkString(", ")
logger.message(
s"Running $pickedMainClass. Also detected script main classes: $scriptsString"
)
logger.message(
s"You can run any one of them by passing option --main-class, i.e. --main-class $firstScript"
)
logger.message(
"All available main classes can always be listed by passing option --list-main-classes"
)
}
Right(pickedMainClass)
}
else
inferredMainClass(mainClasses, logger).left.flatMap { mainClasses =>
options.interactive
.chooseOne(
"Found several main classes. Which would you like to run?",
Expand All @@ -110,13 +78,53 @@ object Build {
Nil
)
}
}
}

defaultMainClassOpt match {
case Some(cls) => Right(cls)
case None => foundMainClass
}
}
private def inferredMainClass(
mainClasses: Seq[String],
logger: Logger
): Either[Seq[String], String] = {
val scriptInferredMainClasses =
sources.inMemory.map(im => im.originalPath.map(_._1))
.flatMap {
case Right(originalRelPath) if originalRelPath.toString.endsWith(".sc") =>
Some {
originalRelPath
.toString
.replace(".", "_")
.replace("/", ".")
}
case Left(VirtualScriptNameRegex(name)) => Some(s"${name}_sc")
case _ => None
}
val filteredMainClasses =
mainClasses.filter(!scriptInferredMainClasses.contains(_))
if (filteredMainClasses.length == 1) {
val pickedMainClass = filteredMainClasses.head
if (scriptInferredMainClasses.nonEmpty) {
val firstScript = scriptInferredMainClasses.head
val scriptsString = scriptInferredMainClasses.mkString(", ")
logger.message(
s"Running $pickedMainClass. Also detected script main classes: $scriptsString"
)
logger.message(
s"You can run any one of them by passing option --main-class, i.e. --main-class $firstScript"
)
logger.message(
"All available main classes can always be listed by passing option --list-main-classes"
)
}
Right(pickedMainClass)
}
else
Left(mainClasses)
}

def crossKey: CrossKey = {
val optKey = scalaParams.map { params =>
Expand Down

0 comments on commit 1da1a32

Please sign in to comment.