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

Add package-private option #151

Merged
merged 2 commits into from
Sep 10, 2019
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
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