|
| 1 | +package com.github.sbt.jacoco.coveralls |
| 2 | + |
| 3 | +import java.io.File |
| 4 | +import java.{util => ju} |
| 5 | + |
| 6 | +import com.fasterxml.jackson.core.{JsonEncoding, JsonFactory} |
| 7 | +import org.apache.commons.codec.digest.DigestUtils |
| 8 | +import org.jacoco.core.analysis.{IBundleCoverage, ILine, IPackageCoverage, ISourceFileCoverage} |
| 9 | +import org.jacoco.core.data.{ExecutionData, SessionInfo} |
| 10 | +import org.jacoco.report.{IReportGroupVisitor, IReportVisitor, ISourceFileLocator} |
| 11 | +import sbt._ |
| 12 | + |
| 13 | +import scala.collection.JavaConverters._ |
| 14 | + |
| 15 | +class CoverallsReportVisitor( |
| 16 | + output: File, |
| 17 | + sourceDirs: Seq[File], |
| 18 | + projectRootDir: File, |
| 19 | + jobId: String, |
| 20 | + repoToken: Option[String]) |
| 21 | + extends IReportVisitor |
| 22 | + with IReportGroupVisitor { |
| 23 | + |
| 24 | + private val digest = new DigestUtils("MD5") |
| 25 | + |
| 26 | + private val jsonFactory = new JsonFactory() |
| 27 | + private val json = jsonFactory.createGenerator(output, JsonEncoding.UTF8) |
| 28 | + |
| 29 | + json.writeStartObject() |
| 30 | + |
| 31 | + repoToken foreach { token => |
| 32 | + json.writeStringField("repo_token", token) |
| 33 | + } |
| 34 | + |
| 35 | + json.writeStringField("service_job_id", jobId) |
| 36 | + json.writeStringField("service_name", "travis-ci") |
| 37 | + |
| 38 | + json.writeArrayFieldStart("source_files") |
| 39 | + |
| 40 | + override def visitInfo(sessionInfos: ju.List[SessionInfo], executionData: ju.Collection[ExecutionData]): Unit = {} |
| 41 | + |
| 42 | + override def visitGroup(name: String): IReportGroupVisitor = this |
| 43 | + |
| 44 | + override def visitBundle(bundle: IBundleCoverage, locator: ISourceFileLocator): Unit = { |
| 45 | + bundle.getPackages.asScala foreach { pkg: IPackageCoverage => |
| 46 | + pkg.getSourceFiles.asScala foreach { source: ISourceFileCoverage => |
| 47 | + json.writeStartObject() |
| 48 | + |
| 49 | + //noinspection ScalaStyle |
| 50 | + val (filename, md5) = findFile(pkg.getName, source.getName) match { |
| 51 | + case Some(file) => |
| 52 | + (IO.relativize(projectRootDir, file).getOrElse(file.getName), digest.digestAsHex(file)) |
| 53 | + |
| 54 | + case None => |
| 55 | + (source.getName, "") |
| 56 | + } |
| 57 | + |
| 58 | + json.writeStringField("name", filename) |
| 59 | + json.writeStringField("source_digest", md5) |
| 60 | + |
| 61 | + json.writeArrayFieldStart("coverage") |
| 62 | + |
| 63 | + (0 to source.getLastLine) foreach { l => |
| 64 | + val line: ILine = source.getLine(l) |
| 65 | + |
| 66 | + if (line.getInstructionCounter.getTotalCount == 0) { |
| 67 | + // non-code line |
| 68 | + json.writeNull() |
| 69 | + } else { |
| 70 | + json.writeNumber(line.getInstructionCounter.getCoveredCount) |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + json.writeEndArray() |
| 75 | + |
| 76 | + json.writeEndObject() |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + override def visitEnd(): Unit = { |
| 82 | + json.writeEndArray() |
| 83 | + json.writeEndObject() |
| 84 | + json.close() |
| 85 | + } |
| 86 | + |
| 87 | + private def findFile(packageName: String, fileName: String): Option[File] = { |
| 88 | + // TODO make common with source file locator |
| 89 | + sourceDirs.map(d => d / packageName / fileName).find(_.exists()) |
| 90 | + } |
| 91 | +} |
0 commit comments