Skip to content

Instantly share code, notes, and snippets.

@markehammons
Last active June 4, 2023 21:39
Show Gist options
  • Save markehammons/ab20ae70aa74c1391008c99f1a49a8eb to your computer and use it in GitHub Desktop.
Save markehammons/ab20ae70aa74c1391008c99f1a49a8eb to your computer and use it in GitHub Desktop.
ScoverageReporting for bleep
package fr.hammons.slinc.scripts
import bleep.rewrites.BuildRewrite
import bleep.model.BuildRewriteName
import bleep.model.Build
import bleep.model.CrossProjectName
import bleep.model.Project
import bleep.model.Options
import bleep.PathOps
import java.nio.file.Paths
import java.nio.file.Files
object AddCoverage extends BuildRewrite:
override val name: BuildRewriteName = BuildRewriteName("add-coverage")
override protected def newExplodedProjects(
oldBuild: Build
): Map[CrossProjectName, Project] =
val buildTarget = Paths.get(".bleep").nn / "builds" / name.value / ".bloop"
oldBuild.explodedProjects
.map: (crossName, p) =>
if !p.isTestProject.getOrElse(false) then
val projectTarget =
buildTarget / crossName.value / "scoverage-reports"
Files.createDirectories(projectTarget)
crossName -> p.copy(scala =
p.scala.map(s =>
s.copy(options =
s.options.union(
Options.parse(
List(
s"-coverage-out:${projectTarget.toFile().nn.getAbsoluteFile().nn.toString()}/"
),
None
)
)
)
)
)
else crossName -> p
$schema: https://raw.githubusercontent.com/oyvindberg/bleep/master/schema.json
$version: 0.0.2
my-scripts:
extends:
- template-common
dependencies:
- org.scoverage::scalac-scoverage-reporter:2.0.10
- org.scoverage::scalac-scoverage-domain:2.0.10
- org.scoverage::scalac-scoverage-serializer:2.0.10
scripts:
my-publish-local: my-scripts/fr.hammons.slinc.scripts.PublishLocal
scoverage-report: my-scripts/fr.hammons.slinc.scripts.ScoverageReport
templates:
template-common:
platform:
name: jvm
scala:
version: 3.3.0
options:
- -deprecation
- -Wunused:all
- -feature
- -unchecked
- -Xcheck-macros
- -Xprint-suspension
- -Yexplicit-nulls
- -Ysafe-init
- -source:future
- -Ykind-projector
- -Vprofile
package fr.hammons.slinc.scripts
import bleep.BleepScript
import bleep.Started
import bleep.Commands
import bleep.rewrites.BuildRewrite
import bleep.PathOps
import scoverage.reporter.CoverageAggregator
import scoverage.reporter.ScoverageXmlWriter
import java.io.File
import java.nio.file.Files
import scala.jdk.CollectionConverters.*
import scala.jdk.StreamConverters.*
import java.nio.file.Paths
object ScoverageReport extends BleepScript("ScoverageReport"):
override val rewrites: List[BuildRewrite] = List(AddCoverage)
def run(started: Started, commands: Commands, args: List[String]): Unit =
val testProjects = started.build.explodedProjects
.filter((_, p) => p.isTestProject.getOrElse(false))
.keySet
.toList
commands.compile(
started.build.explodedProjects.keys.toList
)
commands.test(
started.build.explodedProjects
.filter((_, p) => p.isTestProject.exists(identity))
.keys
.toList
)
val projectPaths = started.build.explodedProjects
.filter((_, p) => !p.isTestProject.getOrElse(false))
.map((cp, p) => started.buildPaths.project(cp, p))
projectPaths
.flatMap: p =>
Files
.list(p.targetDir / "scoverage-reports")
.nn
.toScala(List)
.filter(_.endsWith("scoverage.coverage"))
.foreach: df =>
val mod = Files
.readAllLines(df)
.nn
.asScala
.map: s =>
if s.startsWith("../") then
started.buildPaths.buildDir.toAbsolutePath().nn.relativize(Paths.get(s.stripPrefix(".."))).nn.toString()
else s
Files.write(df, mod.mkString("\n").getBytes())
val coverage = projectPaths
.map(p => p.sourcesDirs.all -> p.targetDir / "scoverage-reports")
.map((sources, data) =>
(
sources,
data,
CoverageAggregator
.aggregate(
Seq(data.toFile().nn),
started.buildPaths.buildDir.toFile().nn
)
)
)
coverage.foreach: (sources, dataDir, coverage) =>
ScoverageXmlWriter(
sources.map(_.toFile().nn).toSeq,
dataDir.toFile().nn,
false,
None
).write(coverage.get)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment