Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
with just-barely-functioning plugin
  • Loading branch information
SethTisue committed Nov 23, 2017
0 parents commit 337fcc1
Show file tree
Hide file tree
Showing 9 changed files with 134 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
target/
29 changes: 29 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
organization := "com.lightbend"
name := "cloc-plugin"
scalaVersion := "2.12.4"
libraryDependencies ++= Seq(
"org.scala-lang" % "scala-compiler" % scalaVersion.value % "provided",
"org.scalatest" %% "scalatest" % "3.0.4" % "test"
)
scalacOptions ++= Seq(
"-encoding",
"utf-8",
"-deprecation",
"-unchecked",
"-feature",
"-Xlint",
"-Xfatal-warnings",
"-Yno-adapted-args",
"-Ywarn-dead-code",
"-Ywarn-infer-any",
"-Ywarn-numeric-widen",
"-Ywarn-value-discard"
)
// so we can invoke the Scala compiler at runtime without weird problems
fork in Test := true

val pluginPath = taskKey[File]("path to compiler plugin jar")
enablePlugins(BuildInfoPlugin)
buildInfoKeys := Seq[BuildInfoKey](pluginPath)
pluginPath := (artifactPath in packageBin in Compile).value // TODO
buildInfoPackage := "com.lightbend.tools"
1 change: 1 addition & 0 deletions project/build.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sbt.version=0.13.16
2 changes: 2 additions & 0 deletions project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
addSbtPlugin("com.eed3si9n" % "sbt-buildinfo" % "0.6.1")
addSbtPlugin("com.dwijnand" % "sbt-dynver" % "2.0.0")
4 changes: 4 additions & 0 deletions src/main/resources/scalac-plugin.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<plugin>
<name>cloc</name>
<classname>com.lightbend.tools.ClocPlugin</classname>
</plugin>
31 changes: 31 additions & 0 deletions src/main/scala/com/lightbend/tools/ClocComponent.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.lightbend.tools

import scala.tools.nsc, nsc.plugins._
import java.io.File

abstract class ClocComponent extends PluginComponent {

override def description = "count lines of Scala code compiled"

// the following two members override abstract members in Transform
val phaseName: String = "cloc"
def newPhase(prev: nsc.Phase): StdPhase = new ClocPhase(prev)

class ClocPhase(prev: nsc.Phase) extends StdPhase(prev) {
override def apply(unit: global.CompilationUnit): Unit = {
val file = unit.source.file.file
println(s"$file: ${countLines(file)}")
}
override def run() {
super.run()
// TODO something at the end?
}
}

private def countLines(file: File): Int = {
val source = io.Source.fromFile(file)
try source.getLines.size
finally source.close()
}

}
30 changes: 30 additions & 0 deletions src/main/scala/com/lightbend/tools/ClocPlugin.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.lightbend.tools

import scala.tools.nsc,
nsc.Global,
nsc.plugins.{Plugin, PluginComponent}

class ClocPlugin(val global: Global) extends Plugin {

override val name = "cloc"
override val description = "count lines of Scala code compiled"

object component extends {
override val global = ClocPlugin.this.global
} with ClocComponent {
override val runsAfter = List("parser")
override val runsRightAfter = Some("parser")
override val terminal = true
}

override val components = List[PluginComponent](component)

override val optionsHelp: Option[String] = Some(
"""|TODO""".stripMargin)

override def init(options: List[String], error: String => Unit) = {
assume(options.isEmpty)
true
}

}
27 changes: 27 additions & 0 deletions src/test/scala/com/lightbend/tools/PluginScaffold.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.lightbend.tools

import scala.tools.nsc.{Settings, Global}
import scala.tools.nsc.io.VirtualDirectory
import scala.reflect.internal.util.BatchSourceFile
import scala.reflect.io.AbstractFile

object PluginScaffold {

def defaultSettings: Settings = {
val settings = new Settings
settings.processArgumentString(
s"""-usejavacp -Xplugin:${BuildInfo.pluginPath} -Xplugin-require:cloc""")
settings.outputDirs.setSingleOutput(new VirtualDirectory("(memory)", None))
settings
}

def compile(code: java.io.File,
showSourceInfo: Boolean = false): Unit = {
val settings = defaultSettings
val sources = List(new BatchSourceFile(AbstractFile.getFile(code)))
val compiler = new Global(settings)
val run = (new compiler.Run)
run.compileSources(sources)
}

}
9 changes: 9 additions & 0 deletions src/test/scala/com/lightbend/tools/PluginTests.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.lightbend.tools

import org.scalatest.FunSuite

class PluginTests extends FunSuite {
test("foo") {
PluginScaffold.compile(new java.io.File("/Users/tisue/euler/src/main/net/tisue/euler/BigRational.scala"))
}
}

0 comments on commit 337fcc1

Please sign in to comment.