Skip to content

Commit

Permalink
scoverage#171 Migrate ScoverageReport from Groovy to Kotlin
Browse files Browse the repository at this point in the history
  • Loading branch information
eyalroth committed Oct 2, 2021
1 parent 0dcd42f commit 90f14b1
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/main/groovy/org/scoverage/ScoverageAggregate.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class ScoverageAggregate extends DefaultTask {

@TaskAction
def aggregate() {
runner.run {
runner.runGroovy {
reportDir.get().deleteDir()
reportDir.get().mkdirs()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,49 +13,50 @@ import org.gradle.api.tasks.PathSensitive
import org.gradle.api.tasks.TaskAction
import scoverage.report.CoverageAggregator

import static org.gradle.api.tasks.PathSensitivity.RELATIVE
import org.gradle.api.tasks.PathSensitivity.RELATIVE
import java.io.File

@CacheableTask
class ScoverageReport extends DefaultTask {
abstract class ScoverageReport: DefaultTask() {

@Nested
ScoverageRunner runner
var runner: ScoverageRunner? = null

@InputDirectory
@PathSensitive(RELATIVE)
final Property<File> dataDir = project.objects.property(File)
val dataDir: Property<File> = project.objects.property(File::class.java)

@InputFiles
@PathSensitive(RELATIVE)
final Property<FileCollection> sources = project.objects.property(FileCollection)
val sources: Property<FileCollection> = project.objects.property(FileCollection::class.java)

@OutputDirectory
final Property<File> reportDir = project.objects.property(File)
val reportDir: Property<File> = project.objects.property(File::class.java)

@Input
final Property<String> sourceEncoding = project.objects.property(String)
val sourceEncoding: Property<String> = project.objects.property(String::class.java)

@Input
final Property<Boolean> coverageOutputCobertura = project.objects.property(Boolean)
val coverageOutputCobertura: Property<Boolean> = project.objects.property(Boolean::class.java)
@Input
final Property<Boolean> coverageOutputXML = project.objects.property(Boolean)
val coverageOutputXML: Property<Boolean> = project.objects.property(Boolean::class.java)
@Input
final Property<Boolean> coverageOutputHTML = project.objects.property(Boolean)
val coverageOutputHTML: Property<Boolean> = project.objects.property(Boolean::class.java)
@Input
final Property<Boolean> coverageDebug = project.objects.property(Boolean)
val coverageDebug: Property<Boolean> = project.objects.property(Boolean::class.java)

@TaskAction
def report() {
runner.run {
fun report() {
runner?.run {
reportDir.get().delete()
reportDir.get().mkdirs()

def coverage = CoverageAggregator.aggregate([dataDir.get()] as File[])
val coverage = CoverageAggregator.aggregate(arrayOf(dataDir.get()))

if (coverage.isEmpty()) {
if (coverage.isEmpty) {
project.logger.info("[scoverage] Could not find coverage file, skipping...")
} else {
new ScoverageWriter(project.logger).write(
ScoverageWriter(project.logger).write(
sources.get().getFiles(),
reportDir.get(),
coverage.get(),
Expand Down
10 changes: 7 additions & 3 deletions src/main/kotlin/org/scoverage/ScoverageRunner.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import java.net.URLClassLoader

class ScoverageRunner(@Classpath val runtimeClasspath: FileCollection) {

fun run(action: Closure<*>) {

fun run(action: () -> Unit) {
val cloader = Thread.currentThread().getContextClassLoader() as URLClassLoader

val method = URLClassLoader::class.java.getDeclaredMethod("addURL", URL::class.java)
Expand All @@ -22,6 +21,11 @@ class ScoverageRunner(@Classpath val runtimeClasspath: FileCollection) {
}
}

action.call()
action()
}

// TODO delete when no longer used by groovy code
fun runGroovy(action: Closure<*>) {
run { action.call() }
}
}

0 comments on commit 90f14b1

Please sign in to comment.