Skip to content

Commit 6d6f7e7

Browse files
Unify commands by removing toggleCoverage
1 parent b8cd81c commit 6d6f7e7

File tree

1 file changed

+63
-102
lines changed

1 file changed

+63
-102
lines changed

src/main/scala/scoverage/ScoverageSbtPlugin.scala

Lines changed: 63 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -24,55 +24,70 @@ object ScoverageSbtPlugin extends AutoPlugin {
2424
override def requires: JvmPlugin.type = plugins.JvmPlugin
2525
override def trigger: PluginTrigger = allRequirements
2626

27-
override lazy val projectSettings = {
28-
if (cmdlineScoverageEnabled == "true") {
29-
Seq(
30-
coverageEnabled := true,
31-
libraryDependencies ++= Seq(
32-
OrgScoverage %% ScalacPluginArtifact % DefaultScoverageVersion % "provided" intransitive(),
33-
OrgScoverage % (ScalacRuntimeArtifact + runtimeClassifier(libraryDependencies.value, scalaBinaryVersion.value)) % DefaultScoverageVersion % "provided" intransitive()
34-
),
35-
coverageReport <<= coverageReport0,
36-
coverageAggregate <<= coverageAggregate0,
37-
scalacOptions in(Compile, compile) ++= scoverageScalacOptions.value,
38-
aggregate in coverageAggregate := false,
39-
coverageExcludedPackages := "",
40-
coverageExcludedFiles := "",
41-
coverageMinimum := 0, // default is no minimum
42-
coverageFailOnMinimum := false,
43-
coverageHighlighting := true,
44-
coverageOutputXML := true,
45-
coverageOutputHTML := true,
46-
coverageOutputCobertura := true,
47-
coverageOutputDebug := false,
48-
coverageCleanSubprojectFiles := true,
49-
coverageOutputTeamCity := false
50-
)
51-
} else if (cmdlineScoverageEnabled == "false") {
52-
Seq()
53-
} else {
54-
// keep it as before, warts'n all
55-
Seq(
56-
coverageEnabled := false,
57-
commands += Command.command("coverage", "enable compiled code with instrumentation", "")(toggleCoverage(true)),
58-
commands += Command.command("coverageOff", "disable compiled code with instrumentation", "")(toggleCoverage(false)),
59-
coverageReport <<= coverageReport0,
60-
coverageAggregate <<= coverageAggregate0,
61-
scalacOptions in(Compile, compile) ++= scoverageScalacOptions.value,
62-
aggregate in coverageAggregate := false,
63-
coverageExcludedPackages := "",
64-
coverageExcludedFiles := "",
65-
coverageMinimum := 0, // default is no minimum
66-
coverageFailOnMinimum := false,
67-
coverageHighlighting := true,
68-
coverageOutputXML := true,
69-
coverageOutputHTML := true,
70-
coverageOutputCobertura := true,
71-
coverageOutputDebug := false,
72-
coverageCleanSubprojectFiles := true,
73-
coverageOutputTeamCity := false
74-
)
27+
override def globalSettings: Seq[Def.Setting[_]] = super.globalSettings ++ Seq(
28+
coverageEnabled := false,
29+
coverageExcludedPackages := "",
30+
coverageExcludedFiles := "",
31+
coverageMinimum := 0, // default is no minimum
32+
coverageFailOnMinimum := false,
33+
coverageHighlighting := true,
34+
coverageOutputXML := true,
35+
coverageOutputHTML := true,
36+
coverageOutputCobertura := true,
37+
coverageOutputDebug := false,
38+
coverageCleanSubprojectFiles := true,
39+
coverageOutputTeamCity := false
40+
)
41+
42+
override def buildSettings: Seq[Setting[_]] = super.buildSettings ++
43+
addCommandAlias("coverage", ";set coverageEnabled in ThisBuild := true") ++
44+
addCommandAlias("coverageOn", ";set coverageEnabled in ThisBuild := true") ++
45+
addCommandAlias("coverageOff", ";set coverageEnabled in ThisBuild := false")
46+
47+
override def projectSettings: Seq[Setting[_]] = Seq(
48+
coverageReport <<= coverageReport0,
49+
coverageAggregate <<= coverageAggregate0
50+
) ++ coverageSettings ++ scalacSettings
51+
52+
private lazy val coverageSettings = Seq(
53+
libraryDependencies ++= {
54+
if (coverageEnabled.value)
55+
Seq(
56+
// We only add for "compile"" because of macros. This setting could be optimed to just "test" if the handling
57+
// of macro coverage was improved.
58+
OrgScoverage % (scalacRuntime(libraryDependencies.value, scalaBinaryVersion.value)) % DefaultScoverageVersion,
59+
// We don't want to instrument the test code itself
60+
OrgScoverage %% ScalacPluginArtifact % DefaultScoverageVersion % "compile"
61+
)
62+
else
63+
Nil
64+
}
65+
)
66+
67+
private lazy val scalacSettings = Seq(
68+
scalacOptions in(Compile, compile) ++= {
69+
if (coverageEnabled.value) {
70+
val scoverageDeps: Seq[File] = update.value matching configurationFilter("compile")
71+
val pluginPath: File = scoverageDeps.find(_.getAbsolutePath.contains(ScalacPluginArtifact)) match {
72+
case None => throw new Exception(s"Fatal: $ScalacPluginArtifact not in libraryDependencies")
73+
case Some(pluginPath) => pluginPath
74+
}
75+
Seq(
76+
Some(s"-Xplugin:${pluginPath.getAbsolutePath}"),
77+
Some(s"-P:scoverage:dataDir:${crossTarget.value.getAbsolutePath}/scoverage-data"),
78+
Option(coverageExcludedPackages.value.trim).filter(_.nonEmpty).map(v => s"-P:scoverage:excludedPackages:$v"),
79+
Option(coverageExcludedFiles.value.trim).filter(_.nonEmpty).map(v => s"-P:scoverage:excludedFiles:$v"),
80+
// rangepos is broken in some releases of scala so option to turn it off
81+
if (coverageHighlighting.value) Some("-Yrangepos") else None
82+
).flatten
83+
} else {
84+
Nil
85+
}
7586
}
87+
)
88+
89+
private def scalacRuntime(deps: Seq[ModuleID], binaryVersion:String): String = {
90+
ScalacRuntimeArtifact + runtimeClassifier(deps, binaryVersion)
7691
}
7792

7893
private def runtimeClassifier(deps: Seq[ModuleID], binaryVersion:String): String = {
@@ -83,26 +98,6 @@ object ScoverageSbtPlugin extends AutoPlugin {
8398
sjsClassifier getOrElse "_" + binaryVersion
8499
}
85100

86-
/**
87-
* The "coverage" command enables or disables instrumentation for all projects
88-
* in the build.
89-
*/
90-
private def toggleCoverage(status: Boolean): State => State = { state =>
91-
val extracted = Project.extract(state)
92-
val newSettings = extracted.structure.allProjectRefs flatMap { proj =>
93-
Seq(
94-
coverageEnabled in proj := status,
95-
libraryDependencies in proj ++= {
96-
if (status) Seq(
97-
OrgScoverage % (ScalacRuntimeArtifact + "_" + scalaBinaryVersion.value) % DefaultScoverageVersion % "provided" intransitive(),
98-
OrgScoverage % (ScalacPluginArtifact + "_" + scalaBinaryVersion.value) % DefaultScoverageVersion % "provided" intransitive()
99-
) else Nil
100-
}
101-
)
102-
}
103-
extracted.append(newSettings, state)
104-
}
105-
106101
private lazy val coverageReport0 = Def.task {
107102
val target = crossTarget.value
108103
val log = streams.value.log
@@ -157,40 +152,6 @@ object ScoverageSbtPlugin extends AutoPlugin {
157152
}
158153
}
159154

160-
private lazy val scoverageScalacOptions = Def.task {
161-
val scoverageDeps: Seq[File] = update.value matching configurationFilter("provided")
162-
scoverageDeps.find(_.getAbsolutePath.contains(ScalacPluginArtifact)) match {
163-
case None => throw new Exception(s"Fatal: $ScalacPluginArtifact not in libraryDependencies")
164-
case Some(pluginPath) =>
165-
scalaArgs(coverageEnabled.value,
166-
pluginPath,
167-
crossTarget.value,
168-
coverageExcludedPackages.value,
169-
coverageExcludedFiles.value,
170-
coverageHighlighting.value)
171-
}
172-
}
173-
174-
private def scalaArgs(coverageEnabled: Boolean,
175-
pluginPath: File,
176-
target: File,
177-
excludedPackages: String,
178-
excludedFiles: String,
179-
coverageHighlighting: Boolean) = {
180-
if (coverageEnabled) {
181-
Seq(
182-
Some(s"-Xplugin:${pluginPath.getAbsolutePath}"),
183-
Some(s"-P:scoverage:dataDir:${target.getAbsolutePath}/scoverage-data"),
184-
Option(excludedPackages.trim).filter(_.nonEmpty).map(v => s"-P:scoverage:excludedPackages:$v"),
185-
Option(excludedFiles.trim).filter(_.nonEmpty).map(v => s"-P:scoverage:excludedFiles:$v"),
186-
// rangepos is broken in some releases of scala so option to turn it off
187-
if (coverageHighlighting) Some("-Yrangepos") else None
188-
).flatten
189-
} else {
190-
Nil
191-
}
192-
}
193-
194155
private def writeReports(crossTarget: File,
195156
compileSourceDirectories: Seq[File],
196157
coverage: Coverage,

0 commit comments

Comments
 (0)