Skip to content

Commit

Permalink
Merge pull request #151 from pcejrowski/feature/private-build-object
Browse files Browse the repository at this point in the history
Add package-private option
  • Loading branch information
eed3si9n authored Sep 10, 2019
2 parents 36a0c14 + e25c952 commit 81fc83a
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/main/scala/sbtbuildinfo/BuildInfoOption.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ object BuildInfoOption {
case object ToJson extends BuildInfoOption
case class Traits(names: String*) extends BuildInfoOption
case object BuildTime extends BuildInfoOption
case object PackagePrivate extends BuildInfoOption
}
2 changes: 2 additions & 0 deletions src/main/scala/sbtbuildinfo/BuildInfoRenderer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ object BuildInfoRenderer {

trait BuildInfoRenderer {

def options: Seq[BuildInfoOption]
def fileType: BuildInfoType
def extension: String
def renderKeys(infoKeysNameAndValues: Seq[BuildInfoResult]): Seq[String]

def isSource = fileType == BuildInfoType.Source
def isResource = fileType == BuildInfoType.Resource
def isPkgPriv: Boolean = options contains BuildInfoOption.PackagePrivate
}
4 changes: 2 additions & 2 deletions src/main/scala/sbtbuildinfo/ScalaCaseClassRenderer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ case class ScalaCaseClassRenderer(options: Seq[BuildInfoOption], pkg: String, ob
caseObjectLine(buildInfoResults)

private def caseClassDefinitionBegin = List(
s"case class $obj("
withPkgPriv(s"case class $obj(")
)

private def caseClassParameter(r: BuildInfoResult): Seq[String] = {
Expand All @@ -57,7 +57,7 @@ case class ScalaCaseClassRenderer(options: Seq[BuildInfoOption], pkg: String, ob
private def caseClassEnd = List("}")

private def caseObjectLine(buildInfoResults: Seq[BuildInfoResult]) = List(
s"case object $obj {",
withPkgPriv(s"case object $obj {"),
s" def apply(): $obj = new $obj(${buildInfoResults.map(r => s"\n ${r.identifier} = ${quote(r.value)}").mkString(",")})",
s" val get = apply()",
s" val value = apply()",
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/sbtbuildinfo/ScalaCaseObjectRenderer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ private[sbtbuildinfo] case class ScalaCaseObjectRenderer(options: Seq[BuildInfoO
"import scala.Predef._",
"",
s"/** This object was generated by sbt-buildinfo. */",
s"case object $obj$objTraits {"
withPkgPriv(s"case object $obj$objTraits {")
)

def footer = List("}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ case class ScalaFinalCaseObjectRenderer(options: Seq[BuildInfoOption], pkg: Stri
"import scala.Predef._",
"",
s"/** This object was generated by sbt-buildinfo. */",
s"case object $obj$objTraits {"
withPkgPriv(s"case object $obj$objTraits {")
)

def footer = List("}")
Expand Down
7 changes: 7 additions & 0 deletions src/main/scala/sbtbuildinfo/ScalaRenderer.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package sbtbuildinfo

abstract class ScalaRenderer extends BuildInfoRenderer {

protected def pkg: String

protected def getType(typeExpr: TypeExpression): Option[String] = {
def tpeToReturnType(tpe: TypeExpression): Option[String] =
tpe match {
Expand Down Expand Up @@ -62,5 +65,9 @@ abstract class ScalaRenderer extends BuildInfoRenderer {
str.replace("\\","\\\\").replace("\n","\\n").replace("\b","\\b").replace("\r","\\r").
replace("\t","\\t").replace("\'","\\'").replace("\f","\\f").replace("\"","\\\"")

protected def withPkgPriv(str: String): String =
if(pkg.nonEmpty && isPkgPriv)
s"private[${pkg.split('.').last}] $str"
else str

}
5 changes: 3 additions & 2 deletions src/sbt-test/sbt-buildinfo/options/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ lazy val root = (project in file(".")).
BuildInfoOption.ToJson,
BuildInfoOption.ToMap,
BuildInfoOption.Traits("TestTrait1", "TestTrait2"),
BuildInfoOption.Traits("TestTrait3")),
BuildInfoOption.Traits("TestTrait3"),
BuildInfoOption.PackagePrivate),
homepage := Some(url("http://example.com")),
licenses := Seq("MIT License" -> url("https://github.com/sbt/sbt-buildinfo/blob/master/LICENSE")),
check := {
Expand All @@ -27,7 +28,7 @@ lazy val root = (project in file(".")).
"""import scala.Predef._""" ::
"""""" ::
"""/** This object was generated by sbt-buildinfo. */""" ::
"""case object BuildInfo extends TestTrait1 with TestTrait2 with TestTrait3 {""" ::
"""private[hello] case object BuildInfo extends TestTrait1 with TestTrait2 with TestTrait3 {""" ::
""" /** The value is "helloworld". */"""::
""" val name: String = "helloworld"""" ::
""" /** The value is "2.12.7". */""" ::
Expand Down

0 comments on commit 81fc83a

Please sign in to comment.