1+ // JacocoBaseKeysPlugin.scala | last modified in v1.0.0 + local changes
2+
13import JacocoBaseKeysPlugin .autoImport .*
4+ import sbt .*
25import sbt .Keys .*
3- import sbt .{ScopeFilter , inProjects as inP , * }
4-
5- import scala .sys .process .*
66
77/**
88 * JacocoAgentPlugin (no aggregation/merge)
@@ -70,6 +70,8 @@ object FilteredJacocoAgentPlugin extends AutoPlugin {
7070 }
7171 }
7272
73+ lazy val Jmf = config(" jmf" ).extend(Compile )
74+
7375 // ---- commands
7476 private lazy val jacocoCleanAllCmd = Command .command(" jacocoCleanAll" ) { state =>
7577 val targets = enabledUnder(state)
@@ -78,9 +80,25 @@ object FilteredJacocoAgentPlugin extends AutoPlugin {
7880 }
7981
8082 private lazy val jacocoReportAllCmd = Command .command(" jacocoReportAll" ) { state =>
81- val targets = enabledUnder(state)
82- if (targets.isEmpty) { println(" [jacoco] nothing to report (no enabled modules under this aggregate)." ); state }
83- else targets.foldLeft(state) { (st, ref) => Command .process(s " ${ref.project}/jacocoReport " , st) }
83+ val e = Project .extract(state)
84+ val current = e.currentRef
85+ // your existing helper (enabled projects under current aggregate)
86+ val under = enabledUnder(state)
87+
88+ // Also include current project if enabled
89+ val selfEnabled =
90+ e.getOpt(current / jacocoPluginEnabled).getOrElse(false )
91+
92+ val targets = (if (selfEnabled) current +: under else under).distinct
93+
94+ if (targets.isEmpty) {
95+ println(" [jacoco] nothing to report (no enabled modules here)." );
96+ state
97+ } else {
98+ targets.foldLeft(state) { (st, ref) =>
99+ Command .process(s " ${ref.project}/jacocoReport " , st)
100+ }
101+ }
84102 }
85103
86104 // ---- global defaults so keys exist everywhere (safe no-ops on projects without the plugin)
@@ -116,12 +134,12 @@ object FilteredJacocoAgentPlugin extends AutoPlugin {
116134
117135 // ---- coordinates
118136 jacocoVersion := " 0.8.12" ,
119- jmfCoreVersion := " 0.1.7 " ,
137+ jmfCoreVersion := " 1.0.0 " ,
120138 libraryDependencies ++= Seq (
121139 // pull the agent with the runtime classifier (this is the actual -javaagent jar)
122140 (" org.jacoco" % " org.jacoco.agent" % jacocoVersion.value % Test ).classifier(" runtime" ),
123141 (" org.jacoco" % " org.jacoco.cli" % jacocoVersion.value % Test ).classifier(" nodeps" ),
124- " io.github.moranaapps" % " jacoco-method-filter-core_2.12 " % jmfCoreVersion.value % Jmf .name,
142+ " io.github.moranaapps" %% " jacoco-method-filter-core " % jmfCoreVersion.value % Jmf .name,
125143 ),
126144 jacocoSetUserDirToBuildRoot := true ,
127145
@@ -138,7 +156,7 @@ object FilteredJacocoAgentPlugin extends AutoPlugin {
138156 s " Report: $moduleId - scala: ${scalaVersion.value}"
139157 },
140158
141- // --- JMF tool wiring
159+ // --- JMF tool wiring
142160 ivyConfigurations += Jmf ,
143161
144162 jmfOutDir := target.value / " jmf" ,
@@ -149,15 +167,36 @@ object FilteredJacocoAgentPlugin extends AutoPlugin {
149167
150168 // the rewrite task (your code, lightly cleaned)
151169 jmfRewrite := {
152- val log = streams.value.log
153- val enabled = jacocoPluginEnabled.value
154-
170+ // --- hoist all .value lookups BEFORE conditionals ---
155171 // ensure classes exist (safe to always do; test would compile anyway)
156172 val _ = (Compile / compile).value
157173
158- val classesIn = (Compile / classDirectory).value
159174 val rules = jmfRulesFile.value
160175 val upd = (Jmf / update).value // hoisted
176+ val log = streams.value.log
177+ val outRoot = jmfOutDir.value
178+ val mainCls = jmfCliMain.value
179+ val dryRun = jmfDryRun.value
180+ val workDir = baseDirectory.value
181+ val classesIn = (Compile / classDirectory).value
182+ val rulesFile = jmfRulesFile.value
183+ val enabled = jacocoPluginEnabled.value
184+
185+ // Compile classpath (scala-stdlib, scopt, your module classes, etc.)
186+ val compileCp : Seq [File ] = Attributed .data((Compile / fullClasspath).value)
187+
188+ // Jmf-resolved jars (your jacoco-method-filter-core, etc.)
189+ val jmfJars : Seq [File ] = (Jmf / update).value.matching(artifactFilter(`type` = " jar" )).distinct
190+
191+ // Final runtime CP
192+ val cp : Seq [File ] = (compileCp ++ jmfJars :+ (Compile / classDirectory).value).distinct
193+ val cpStr = cp.distinct.map(_.getAbsolutePath).mkString(java.io.File .pathSeparator)
194+
195+ val javaBin = {
196+ val h = sys.props.get(" java.home" ).getOrElse(" " )
197+ if (h.nonEmpty) new java.io.File (new java.io.File (h, " bin" ), " java" ).getAbsolutePath else " java"
198+ }
199+ // ----------------------------------------------------
161200
162201 if (! enabled) classesIn
163202 else if (! classesIn.exists) {
@@ -170,18 +209,17 @@ object FilteredJacocoAgentPlugin extends AutoPlugin {
170209 val outDir = jmfOutDir.value / " classes-filtered"
171210 IO .delete(outDir); IO .createDirectory(outDir)
172211
173- val toolJars = upd.matching(artifactFilter(`type` = " jar" )).distinct
174- log.info(" [jmf] tool CP:\n " + toolJars.map(f => s " - ${f.getAbsolutePath}" ).mkString(" \n " ))
212+ log.info(" [jmf] runtime CP:\n " + cp.map(f => s " - ${f.getAbsolutePath}" ).mkString(" \n " ))
175213
176- val cpStr = toolJars.mkString(java.io. File .pathSeparator)
177- val args = Seq ( " java " , " -cp" , cpStr, jmfCliMain.value,
178- " --in" , classesIn.getAbsolutePath,
179- " --out" , outDir.getAbsolutePath,
180- " --rules" , rules.getAbsolutePath) ++
181- (if (jmfDryRun.value) Seq (" --dry-run" ) else Seq ())
214+ val args = Seq (
215+ javaBin, " -cp" , cpStr, jmfCliMain.value,
216+ " --in" , classesIn.getAbsolutePath,
217+ " --out" , outDir.getAbsolutePath,
218+ " --rules" , rules.getAbsolutePath
219+ ) ++ (if (jmfDryRun.value) Seq (" --dry-run" ) else Seq ())
182220
183221 log.info(s " [jmf] rewrite: ${args.mkString(" " )}" )
184- val code = scala.sys.process.Process (args, baseDirectory.value ).!
222+ val code = scala.sys.process.Process (args, workDir ).!
185223 if (code != 0 ) sys.error(s " [jmf] rewriter failed ( $code) " )
186224 outDir
187225 }
@@ -333,3 +371,4 @@ object FilteredJacocoAgentPlugin extends AutoPlugin {
333371 }
334372 )
335373}
374+
0 commit comments