forked from scoverage/gradle-scoverage
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
scoverage#75 Spawn tasks using Worker API instead of adding scoverage…
…'s classpath to the main gradle classloader
- Loading branch information
Showing
5 changed files
with
134 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,39 @@ | ||
package org.scoverage | ||
|
||
import org.gradle.api.Action | ||
import org.gradle.api.file.FileCollection | ||
import org.gradle.api.tasks.Classpath | ||
import org.gradle.workers.WorkAction | ||
import org.gradle.workers.WorkParameters | ||
import org.gradle.workers.WorkerExecutor | ||
|
||
import java.lang.reflect.Method | ||
import javax.inject.Inject | ||
|
||
class ScoverageRunner { | ||
|
||
private final WorkerExecutor workerExecutor | ||
|
||
@Classpath | ||
final FileCollection runtimeClasspath | ||
FileCollection runtimeClasspath | ||
|
||
ScoverageRunner(FileCollection runtimeClasspath) { | ||
@Inject | ||
ScoverageRunner(WorkerExecutor workerExecutor) { | ||
|
||
this.runtimeClasspath = runtimeClasspath | ||
this.workerExecutor = workerExecutor | ||
} | ||
|
||
def run(Closure<?> action) { | ||
|
||
URLClassLoader cloader = (URLClassLoader) Thread.currentThread().getContextClassLoader() | ||
|
||
Method method = URLClassLoader.class.getDeclaredMethod("addURL", URL.class) | ||
method.setAccessible(true) | ||
|
||
runtimeClasspath.files.each { f -> | ||
def url = f.toURI().toURL() | ||
if (!cloader.getURLs().contains(url)) { | ||
method.invoke(cloader, url) | ||
} | ||
/** | ||
* The runner makes use of Gradle's worker API to run tasks with scoverage's classpath without affecting the main | ||
* gradle classloader/classpath. | ||
* | ||
* @see <a href="https://docs.gradle.org/current/userguide/custom_tasks.html#worker_api">Worker API guide</a> | ||
* @see <a href="https://github.com/gradle/guides/issues/295">Worker API guide issue<a/> | ||
*/ | ||
def <T extends WorkParameters> void run(Class<? extends WorkAction<T>> workActionClass, Action<? super T> parameterAction) { | ||
|
||
def queue = workerExecutor.classLoaderIsolation() {spec -> | ||
spec.getClasspath().from(runtimeClasspath) | ||
} | ||
|
||
action.call() | ||
queue.submit(workActionClass, parameterAction) | ||
} | ||
} |