@@ -23,22 +23,8 @@ object ScoverageSbtPlugin extends AutoPlugin {
2323 override  def  requires :  JvmPlugin .type  =  plugins.JvmPlugin 
2424 override  def  trigger :  PluginTrigger  =  allRequirements
2525
26-  override  lazy   val   projectSettings   =  Seq (
26+  override  def   globalSettings :   Seq [ Def . Setting [_]]  =   super .globalSettings  ++  Seq (
2727 coverageEnabled :=  false ,
28-  commands +=  Command .command(" coverage" " enable compiled code with instrumentation" " " true )),
29-  commands +=  Command .command(" coverageOff" " disable compiled code with instrumentation" " " false )),
30-  coverageReport <<=  coverageReport0,
31-  coverageAggregate <<=  coverageAggregate0,
32-  ivyConfigurations :=  ivyConfigurations.value :+  ScoveragePluginConfig ,
33-  coverageScalacPluginVersion :=  DefaultScoverageVersion ,
34-  libraryDependencies ++=  {
35-  if  (coverageEnabled.value) Seq (
36-  OrgScoverage  %%  (ScalacRuntimeArtifact  +  optionalScalaJsSuffix(libraryDependencies.value)) %  coverageScalacPluginVersion.value,
37-  OrgScoverage  %%  ScalacPluginArtifact  %  coverageScalacPluginVersion.value %  ScoveragePluginConfig .name
38-  ) else  Nil 
39-  },
40-  scalacOptions in(Compile , compile) ++=  scoverageScalacOptions.value,
41-  aggregate in coverageAggregate :=  false ,
4228 coverageExcludedPackages :=  " " 
4329 coverageExcludedFiles :=  " " 
4430 coverageMinimum :=  0 , //  default is no minimum
@@ -49,22 +35,60 @@ object ScoverageSbtPlugin extends AutoPlugin {
4935 coverageOutputCobertura :=  true ,
5036 coverageOutputDebug :=  false ,
5137 coverageCleanSubprojectFiles :=  true ,
52-  coverageOutputTeamCity :=  false 
38+  coverageOutputTeamCity :=  false ,
39+  coverageScalacPluginVersion :=  DefaultScoverageVersion 
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+  ivyConfigurations +=  ScoveragePluginConfig ,
49+  coverageReport <<=  coverageReport0,
50+  coverageAggregate <<=  coverageAggregate0
51+  ) ++  coverageSettings ++  scalacSettings
52+ 
53+  private  lazy  val  coverageSettings  =  Seq (
54+  libraryDependencies ++=  {
55+  if  (coverageEnabled.value)
56+  Seq (
57+  //  We only add for "compile"" because of macros. This setting could be optimed to just "test" if the handling
58+  //  of macro coverage was improved.
59+  OrgScoverage  %%  (scalacRuntime(libraryDependencies.value)) %  coverageScalacPluginVersion.value,
60+  //  We don't want to instrument the test code itself, nor add to a pom when published with coverage enabled.
61+  OrgScoverage  %%  ScalacPluginArtifact  %  coverageScalacPluginVersion.value %  ScoveragePluginConfig .name
62+  )
63+  else 
64+  Nil 
65+  }
66+  )
67+ 
68+  private  lazy  val  scalacSettings  =  Seq (
69+  scalacOptions in(Compile , compile) ++=  {
70+  if  (coverageEnabled.value) {
71+  val  scoverageDeps :  Seq [File ] =  update.value matching configurationFilter(ScoveragePluginConfig .name)
72+  val  pluginPath :  File  =  scoverageDeps.find(_.getAbsolutePath.contains(ScalacPluginArtifact )) match  {
73+  case  None  =>  throw  new  Exception (s " Fatal:  $ScalacPluginArtifact not in libraryDependencies " )
74+  case  Some (pluginPath) =>  pluginPath
75+  }
76+  Seq (
77+  Some (s " -Xplugin: ${pluginPath.getAbsolutePath}" ),
78+  Some (s " -P:scoverage:dataDir: ${crossTarget.value.getAbsolutePath}/scoverage-data " ),
79+  Option (coverageExcludedPackages.value.trim).filter(_.nonEmpty).map(v =>  s " -P:scoverage:excludedPackages: $v" ),
80+  Option (coverageExcludedFiles.value.trim).filter(_.nonEmpty).map(v =>  s " -P:scoverage:excludedFiles: $v" ),
81+  //  rangepos is broken in some releases of scala so option to turn it off
82+  if  (coverageHighlighting.value) Some (" -Yrangepos" else  None 
83+  ).flatten
84+  } else  {
85+  Nil 
86+  }
87+  }
5388 )
5489
55-  /**  
56-  * The "coverage" command enables or disables instrumentation for all projects 
57-  * in the build. 
58-  */  
59-  private  def  toggleCoverage (status : Boolean ):  State  =>  State  =  { state => 
60-  val  extracted  =  Project .extract(state)
61-  val  currentProjRef  =  extracted.currentRef
62-  val  newSettings  =  extracted.structure.allProjectRefs.flatMap(proj => 
63-  Seq (coverageEnabled in proj :=  status)
64-  )
65-  val  appendSettings  =  Load .transformSettings(Load .projectScope(currentProjRef), currentProjRef.build, extracted.rootProject, newSettings)
66-  val  newSessionSettings  =  extracted.session.appendRaw(appendSettings)
67-  SessionSettings .reapply(newSessionSettings, state)
90+  private  def  scalacRuntime (deps : Seq [ModuleID ]):  String  =  {
91+  ScalacRuntimeArtifact  +  optionalScalaJsSuffix(deps)
6892 }
6993
7094 //  returns "_sjs$sjsVersion" for Scala.js projects or "" otherwise
@@ -130,39 +154,6 @@ object ScoverageSbtPlugin extends AutoPlugin {
130154 }
131155 }
132156
133-  private  lazy  val  scoverageScalacOptions  =  Def .task {
134-  update.value
135-  .matching(configurationFilter(ScoveragePluginConfig .name))
136-  .find(_.getAbsolutePath.contains(ScalacPluginArtifact ))
137-  .fold[Seq [String ]](Nil )(pluginPath => 
138-  scalaArgs(coverageEnabled.value,
139-  pluginPath,
140-  crossTarget.value,
141-  coverageExcludedPackages.value,
142-  coverageExcludedFiles.value,
143-  coverageHighlighting.value))
144-  }
145- 
146-  private  def  scalaArgs (coverageEnabled : Boolean ,
147-  pluginPath : File ,
148-  target : File ,
149-  excludedPackages : String ,
150-  excludedFiles : String ,
151-  coverageHighlighting : Boolean ) =  {
152-  if  (coverageEnabled) {
153-  Seq (
154-  Some (s " -Xplugin: ${pluginPath.getAbsolutePath}" ),
155-  Some (s " -P:scoverage:dataDir: ${target.getAbsolutePath}/scoverage-data " ),
156-  Option (excludedPackages.trim).filter(_.nonEmpty).map(v =>  s " -P:scoverage:excludedPackages: $v" ),
157-  Option (excludedFiles.trim).filter(_.nonEmpty).map(v =>  s " -P:scoverage:excludedFiles: $v" ),
158-  //  rangepos is broken in some releases of scala so option to turn it off
159-  if  (coverageHighlighting) Some (" -Yrangepos" else  None 
160-  ).flatten
161-  } else  {
162-  Nil 
163-  }
164-  }
165- 
166157 private  def  writeReports (crossTarget : File ,
167158 compileSourceDirectories : Seq [File ],
168159 coverage : Coverage ,
0 commit comments