Skip to content

Commit 303534f

Browse files
committed
Merge pull request #164 from pbatko/phase-order
Configurable extra runsBefore and runsAfter phases
2 parents 200bc65 + e1ef472 commit 303534f

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

scalac-scoverage-plugin/src/main/scala/scoverage/plugin.scala

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,21 @@ class ScoveragePlugin(val global: Global) extends Plugin {
1414

1515
override val name: String = "scoverage"
1616
override val description: String = "scoverage code coverage compiler plugin"
17-
val instrumentationComponent = new ScoverageInstrumentationComponent(global)
17+
private val (extraAfterPhase, extraBeforePhase) = processPhaseOptions(pluginOptions)
18+
val instrumentationComponent = new ScoverageInstrumentationComponent(global, extraAfterPhase, extraBeforePhase)
1819
override val components: List[PluginComponent] = List(instrumentationComponent)
1920

2021
override def processOptions(opts: List[String], error: String => Unit) {
2122
val options = new ScoverageOptions
22-
for ( opt <- opts ) {
23+
for (opt <- opts) {
2324
if (opt.startsWith("excludedPackages:")) {
2425
options.excludedPackages = opt.substring("excludedPackages:".length).split(";").map(_.trim).filterNot(_.isEmpty)
2526
} else if (opt.startsWith("excludedFiles:")) {
2627
options.excludedFiles = opt.substring("excludedFiles:".length).split(";").map(_.trim).filterNot(_.isEmpty)
2728
} else if (opt.startsWith("dataDir:")) {
2829
options.dataDir = opt.substring("dataDir:".length)
30+
} else if (opt.startsWith("extraAfterPhase:") || opt.startsWith("extraBeforePhase:")){
31+
// skip here, these flags are processed elsewhere
2932
} else {
3033
error("Unknown option: " + opt)
3134
}
@@ -39,9 +42,32 @@ class ScoveragePlugin(val global: Global) extends Plugin {
3942
"-P:scoverage:dataDir:<pathtodatadir> where the coverage files should be written\n",
4043
"-P:scoverage:excludedPackages:<regex>;<regex> semicolon separated list of regexs for packages to exclude",
4144
"-P:scoverage:excludedFiles:<regex>;<regex> semicolon separated list of regexs for paths to exclude",
45+
"-P:scoverage:extraAfterPhase:<phaseName> phase after which scoverage phase runs (must be after typer phase)",
46+
"-P:scoverage:extraBeforePhase:<phaseName> phase before which scoverage phase runs (must be before patmat phase)",
4247
" Any classes whose fully qualified name matches the regex will",
4348
" be excluded from coverage."
4449
).mkString("\n"))
50+
51+
// copied from scala 2.11
52+
private def pluginOptions: List[String] = {
53+
// Process plugin options of form plugin:option
54+
def namec = name + ":"
55+
global.settings.pluginOptions.value filter (_ startsWith namec) map (_ stripPrefix namec)
56+
}
57+
58+
private def processPhaseOptions(opts: List[String]): (Option[String], Option[String]) = {
59+
var afterPhase: Option[String] = None
60+
var beforePhase: Option[String] = None
61+
for (opt <- opts) {
62+
if (opt.startsWith("extraAfterPhase:")) {
63+
afterPhase = Some(opt.substring("extraAfterPhase:".length))
64+
}
65+
if (opt.startsWith("extraBeforePhase:")) {
66+
beforePhase = Some(opt.substring("extraBeforePhase:".length))
67+
}
68+
}
69+
(afterPhase, beforePhase)
70+
}
4571
}
4672

4773
class ScoverageOptions {
@@ -50,7 +76,7 @@ class ScoverageOptions {
5076
var dataDir: String = IOUtils.getTempPath
5177
}
5278

53-
class ScoverageInstrumentationComponent(val global: Global)
79+
class ScoverageInstrumentationComponent(val global: Global, extraAfterPhase: Option[String], extraBeforePhase: Option[String])
5480
extends PluginComponent
5581
with TypingTransformers
5682
with Transform {
@@ -61,8 +87,8 @@ class ScoverageInstrumentationComponent(val global: Global)
6187
val coverage = new Coverage
6288

6389
override val phaseName: String = "scoverage-instrumentation"
64-
override val runsAfter: List[String] = List("typer")
65-
override val runsBefore = List[String]("patmat")
90+
override val runsAfter: List[String] = List("typer") ::: extraAfterPhase.toList
91+
override val runsBefore: List[String] = List("patmat") ::: extraBeforePhase.toList
6692

6793
/**
6894
* Our options are not provided at construction time, but shortly after,

scalac-scoverage-plugin/src/test/scala/scoverage/ScoverageCompiler.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class ScoverageCompiler(settings: scala.tools.nsc.Settings, reporter: scala.tool
7575
.getAbsolutePath
7676
}
7777

78-
val instrumentationComponent = new ScoverageInstrumentationComponent(this)
78+
val instrumentationComponent = new ScoverageInstrumentationComponent(this, None, None)
7979
instrumentationComponent.setOptions(new ScoverageOptions())
8080
val testStore = new ScoverageTestStoreComponent(this)
8181
val validator = new PositionValidator(this)

0 commit comments

Comments
 (0)