scalafx-utils/project/Helpers.scala

99 lines
3.4 KiB
Scala
Raw Normal View History

import sbt._
2017-10-18 03:14:42 +00:00
import sbt.Keys._
2017-10-18 03:14:42 +00:00
import HelpersImpl._
object Helpers extends AnyRef with PropertyHelper with VersionHelper with PomHelper
2016-09-25 04:06:36 +00:00
2017-10-18 03:14:42 +00:00
/* Note: This file is shared among many projects. Avoid putting project-specific things here. */
2017-10-18 03:14:42 +00:00
object HelpersImpl {
sealed trait PropertyHelper {
private[this] lazy val boolNames = Set("yes", "y", "true", "t", "1")
2016-09-25 04:06:36 +00:00
2017-10-18 03:14:42 +00:00
def getProp(name: String): Option[String] = sys.props.get(name) orElse sys.env.get(name)
def parseBool(str: String): Boolean = boolNames(str.trim.toLowerCase)
def boolFlag(name: String): Option[Boolean] = getProp(name).map(parseBool)
def boolFlag(name: String, default: Boolean): Boolean = boolFlag(name) getOrElse default
def intFlag(name: String) = getProp(name).map(_.toInt)
def intFlag(name: String, default: Int): Int = intFlag(name) getOrElse default
def opts(names: String*): Option[String] = names.collectFirst(Function.unlift(getProp))
lazy val buildNumberOpt = sys.env.get("BUILD_NUMBER")
lazy val isJenkins = buildNumberOpt.isDefined
}
2017-10-18 03:14:42 +00:00
sealed trait VersionHelper {
final lazy val sver: Def.Initialize[SVer] = {
Def.map(scalaBinaryVersion)(SVer.apply)
}
2017-10-18 03:14:42 +00:00
final lazy val forceOldInlineSyntax: Def.Initialize[Boolean] = {
val pat = """(?x)^ (?: 2\.12\.[0-2] (?:[^\d].*)? ) | (?: 2\.13\.0 .+ ) $""".r
2017-10-18 03:14:42 +00:00
Def.map(scalaVersion) {
case pat() => true
case _ => false
}
}
sealed trait Backend
case object NewBackend extends Backend
case object SupportsNewBackend extends Backend
case object OldBackend extends Backend
2017-10-18 03:14:42 +00:00
sealed trait SVer {
val backend: Backend
val requireJava8: Boolean
2017-10-18 03:14:42 +00:00
def supportsNewBackend = backend match {
case NewBackend => true
case SupportsNewBackend => true
case OldBackend => false
}
}
2017-10-18 03:14:42 +00:00
object SVer {
def apply(scalaVersion: String): SVer = {
CrossVersion.partialVersion(scalaVersion) match {
case Some((2, 10)) => SVer2_10
case Some((2, 11)) => SVer2_11
case Some((2, 12)) => SVer2_12
case Some((2, n)) if n >= 13 => SVer2_13
case _ =>
throw new IllegalArgumentException(s"Scala version $scalaVersion is not supported")
}
2016-09-25 04:06:36 +00:00
}
}
2017-10-18 03:14:42 +00:00
case object SVer2_10 extends SVer {
override final val backend = OldBackend
override final val requireJava8 = false
}
case object SVer2_11 extends SVer {
override final val backend = SupportsNewBackend
override final val requireJava8 = false
}
case object SVer2_12 extends SVer {
override final val backend = NewBackend
override final val requireJava8 = true
}
case object SVer2_13 extends SVer {
override final val backend = NewBackend
override final val requireJava8 = true
}
2016-09-25 04:06:36 +00:00
}
2017-10-18 03:14:42 +00:00
sealed trait PomHelper {
import scala.xml.{ Node, NodeSeq }
import scala.xml.transform.{ RewriteRule, RuleTransformer }
def excludePomDeps(exclude: (String, String) => Boolean): Node => Node = { node: Node =>
def shouldExclude(n: Node): Boolean =
n.label == "dependency" && exclude((n \ "groupId").text, (n \ "artifactId").text)
val rewriteRule = new RewriteRule {
override def transform(n: Node): NodeSeq = if (shouldExclude(n)) NodeSeq.Empty else n
}
val transformer = new RuleTransformer(rewriteRule)
transformer.transform(node).head
}
2016-09-25 04:06:36 +00:00
}
}