Skip to content

Instantly share code, notes, and snippets.

@lihaoyi
Created December 2, 2016 11:56
Show Gist options
  • Save lihaoyi/392c5a3294a834e0714afad5b9b51643 to your computer and use it in GitHub Desktop.
Save lihaoyi/392c5a3294a834e0714afad5b9b51643 to your computer and use it in GitHub Desktop.
package demo
import scala.tools.nsc.io.AbstractFile
import scala.tools.nsc.{Global, Phase}
import scala.tools.nsc.plugins.{Plugin, PluginComponent}
class DemoPlugin(val global: Global) extends Plugin {
import global._
override def init(options: List[String], error: String => Unit): Boolean = true
val name = "demo"
val description = "a plugin"
val components = List[PluginComponent](DemoComponent)
private object DemoComponent extends PluginComponent {
val global = DemoPlugin.this.global
import global._
override val runsAfter = List("parser")
override val runsBefore = List("namer")
val phaseName = "Demo"
override def newPhase(prev: Phase) = new GlobalPhase(prev) {
override def run() = {
val count = global.currentRun.units.length
val x: Iterator[Seq[String]] = global.currentRun.units.map(_.source.file.path.split("/"))
var start = x.next()
for(next <- x){
for(i <- next.indices){
if (i < start.length && start.lift(i) != next.lift(i)){
start = start.take(i)
}
}
}
val cwd = new java.io.File("").getAbsolutePath.split("/")
if (start.startsWith(cwd)) start = start.drop(cwd.length)
val msg = s"Compiling $count files in ${start.mkString("/")}"
val spaces = " " * ((90 - msg.length) / 2)
global.reporter.echo(
scala.Console.BLUE_B +
spaces + msg + spaces +
scala.Console.RESET
)
}
def name: String = phaseName
def apply(unit: global.CompilationUnit): Unit = {}
}
}
}
@kciesielski
Copy link

@lihaoyi How can this be added to sbt? I tried to put it in ~/.sbt/0.13/plugins/Plugin.scala but then sbt fails to start with:

[error] ...\.sbt\0.13\plugins\CompilationBar.scala:9: method init overrides nothing
[error]   override def init(options: List[String],  error: String => Unit): Boolean = true

Removing override doesn't seem to help ;)

I tried it on a project built with sbt 0.13.13

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