Skip to content

Instantly share code, notes, and snippets.

@olafurpg
Last active October 17, 2017 02:28
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save olafurpg/cabbcc58105dac05d8b9644dcb406e0f to your computer and use it in GitHub Desktop.
Save olafurpg/cabbcc58105dac05d8b9644dcb406e0f to your computer and use it in GitHub Desktop.
import sbt._, Keys._
import scala.xml.{Node => XmlNode, NodeSeq => XmlNodeSeq, _}
import scala.xml.transform.{RewriteRule, RuleTransformer}
import sbt.plugins.JvmPlugin
object PPrintPlugin extends AutoPlugin {
override def requires = JvmPlugin
override def trigger: PluginTrigger = AllRequirements
override def projectSettings: Seq[Def.Setting[_]] = List(
libraryDependencies ++= {
if (scalaVersion.value.startsWith("2."))
List("com.lihaoyi" %% "pprint" % "0.5.2" % Provided)
else Nil
},
// (optional), if you publish libraries from your local computer then make
// sure to exclude pprint from your published POM.
pomPostProcess := { node =>
new RuleTransformer(new RewriteRule {
private def isPPrint(node: XmlNode): Boolean = {
def matchesArtifactId(node: XmlNode) =
node.label == "artifactId" && node.text.startsWith("pprint_")
def matchesScope(node: XmlNode) =
node.label == "scope" && node.text == "provided"
node.label == "dependency" &&
node.child.exists(matchesArtifactId) &&
node.child.exists(matchesScope)
}
override def transform(node: XmlNode): XmlNodeSeq = node match {
case e: Elem if isPPrint(node) => Text("")
case _ => node
}
}).transform(pomPostProcess.value(node)).head
}
)
}
@olafurpg
Copy link
Author

olafurpg commented Jul 5, 2017

Put this in ~/.sbt/0.13/plugins/pprint.scala to use pprint.log everywhere! Very useful for debugging nested data structures

screen shot 2017-07-05 at 13 28 23

Don't forget to remove the debugging statements before opening a PR

@sjrd
Copy link

sjrd commented Jul 5, 2017

More importantly, don't forget to remove that plugin before publishing any of the projects on your machine. Otherwise a dependency on pprint will leak in the Maven POMs.

This is a terrible idea ...

@olafurpg
Copy link
Author

olafurpg commented Jul 5, 2017

Good point @sjrd, I agree it's a bad idea if you publish public libraries from a local machine (which I personally do a lot). If you let CI publish artifacts for you or if you don't publish libraries in general then this should be fine.

@olafurpg
Copy link
Author

olafurpg commented Jul 6, 2017

I updated the gist @sjrd to

  • add the dependency to the Provided scope
  • add a pomPostProcess to remove any dependency on pprint in the provided scope

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment