Skip to content
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

Improve main class detection #1055

Merged
merged 2 commits into from
Oct 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,19 @@ object StartScriptMainClassConfig {
* @param discoveredMainClasses all discovered main classes, e.g. from (discoveredMainClasses in Compile).value
* @return A start script configuration
*/
def from(mainClass: Option[String], discoveredMainClasses: Seq[String]): StartScriptMainClassConfig =
mainClass match {
def from(mainClass: Option[String], discoveredMainClasses: Seq[String]): StartScriptMainClassConfig = {
val additionalMainClasses = discoveredMainClasses.filterNot(mainClass == Some(_))
(mainClass, additionalMainClasses) match {
// only one main - create the default script
case Some(main) if discoveredMainClasses.size == 1 => SingleMain(main)
case (Some(main), Seq()) => SingleMain(main)
case (None, Seq(main)) => SingleMain(main)
// main explicitly set and multiple discoveredMainClasses
case Some(main) => ExplicitMainWithAdditional(main, discoveredMainClasses.filterNot(_ == main))
case (Some(main), additional) => ExplicitMainWithAdditional(main, additional)
// no main class at all
case None if discoveredMainClasses.isEmpty => NoMain
case (None, Seq()) => NoMain
// multiple main classes and none explicitly set. Create start script for each class
case None => MultipleMains(discoveredMainClasses)
case (None, additional) => MultipleMains(additional)
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.typesafe.sbt.packager

import com.typesafe.sbt.packager.archetypes.scripts._
import org.scalatest._

class StartScriptMainClassConfigSpec extends WordSpec with Matchers {

"StartScriptMainClassConfig" should {

"handle a single explicit main class" when {

"main class is not discovered" in {
StartScriptMainClassConfig.from(Some("mainClass"), Nil) should === (SingleMain("mainClass"))
}

"main class is discovered" in {
StartScriptMainClassConfig.from(Some("mainClass"), Seq("mainClass")) should === (SingleMain("mainClass"))
}
}

"handle an explicit main class with alternatives" when {

"main class is not discovered" in {
StartScriptMainClassConfig.from(Some("mainClass"), Seq("alternate")) should === (ExplicitMainWithAdditional("mainClass", Seq("alternate")))
}

"main class is discovered" in {
StartScriptMainClassConfig.from(Some("mainClass"), Seq("mainClass", "alternate")) should === (ExplicitMainWithAdditional("mainClass", Seq("alternate")))
}
}

"handle discovered main classes" when {

"a single main class is discovered" in {
StartScriptMainClassConfig.from(None, Seq("mainClass")) should === (SingleMain("mainClass"))
}

"multiple main classes are discovered" in {
StartScriptMainClassConfig.from(None, Seq("mainClass", "alternate")) should === (MultipleMains(Seq("mainClass", "alternate")))
}
}

"handle no main classes" in {
StartScriptMainClassConfig.from(None, Seq.empty) should === (NoMain)
}
}
}