Updated build Helpers file

This commit is contained in:
Sarah Gerweck 2017-10-17 20:14:42 -07:00
parent 2936952a12
commit 136d2b59a3

View File

@ -1,30 +1,28 @@
import sbt._ import sbt._
import Keys._ import sbt.Keys._
import HelpersImpl._
object Helpers extends AnyRef with PropertyHelper with VersionHelper with PomHelper
/* Note: This file is shared among many projects. Avoid putting project-specific things here. */
object HelpersImpl {
sealed trait PropertyHelper {
private[this] lazy val boolNames = Set("yes", "y", "true", "t", "1")
object Helpers {
def getProp(name: String): Option[String] = sys.props.get(name) orElse sys.env.get(name) def getProp(name: String): Option[String] = sys.props.get(name) orElse sys.env.get(name)
def parseBool(str: String): Boolean = Set("yes", "y", "true", "t", "1") contains str.trim.toLowerCase def parseBool(str: String): Boolean = boolNames(str.trim.toLowerCase)
def boolFlag(name: String): Option[Boolean] = getProp(name) map { parseBool _ } def boolFlag(name: String): Option[Boolean] = getProp(name).map(parseBool)
def boolFlag(name: String, default: Boolean): Boolean = boolFlag(name) getOrElse default def boolFlag(name: String, default: Boolean): Boolean = boolFlag(name) getOrElse default
def opts(names: String*): Option[String] = names.view.map(getProp _).foldLeft(None: Option[String]) { _ orElse _ } 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 buildNumberOpt = sys.env.get("BUILD_NUMBER")
lazy val isJenkins = buildNumberOpt.isDefined lazy val isJenkins = buildNumberOpt.isDefined
import scala.xml._
def excludePomDeps(exclude: (String, String) => Boolean): Node => Node = { node: Node =>
val rewriteRule = new transform.RewriteRule {
override def transform(n: Node): NodeSeq = {
if ((n.label == "dependency") && exclude((n \ "groupId").text, (n \ "artifactId").text))
NodeSeq.Empty
else
n
}
}
val transformer = new transform.RuleTransformer(rewriteRule)
transformer.transform(node)(0)
} }
sealed trait VersionHelper {
final lazy val sver: Def.Initialize[SVer] = { final lazy val sver: Def.Initialize[SVer] = {
Def.map(scalaBinaryVersion)(SVer.apply) Def.map(scalaBinaryVersion)(SVer.apply)
} }
@ -80,4 +78,21 @@ object Helpers {
override final val backend = NewBackend override final val backend = NewBackend
override final val requireJava8 = true override final val requireJava8 = true
} }
}
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
}
}
} }