generated from remal-gradle-plugins/template
-
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.
- Loading branch information
Showing
9 changed files
with
117 additions
and
119 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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
102 changes: 102 additions & 0 deletions
102
src/main/java/name/remal/gradle_plugins/finalize_by_jacoco/FinalizeByJacocoPlugin.java
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 |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package name.remal.gradle_plugins.finalize_by_jacoco; | ||
|
||
import static java.util.Collections.emptyList; | ||
import static java.util.stream.Collectors.toList; | ||
import static name.remal.gradle_plugins.toolkit.ExtensionContainerUtils.getOptionalExtension; | ||
import static name.remal.gradle_plugins.toolkit.PredicateUtils.not; | ||
|
||
import java.io.File; | ||
import java.util.Collection; | ||
import java.util.List; | ||
import javax.annotation.Nullable; | ||
import lombok.val; | ||
import org.gradle.api.Plugin; | ||
import org.gradle.api.Project; | ||
import org.gradle.api.Task; | ||
import org.gradle.testing.jacoco.plugins.JacocoTaskExtension; | ||
import org.gradle.testing.jacoco.tasks.JacocoBase; | ||
import org.gradle.testing.jacoco.tasks.JacocoReportBase; | ||
|
||
public abstract class FinalizeByJacocoPlugin implements Plugin<Project> { | ||
|
||
@Override | ||
public void apply(Project project) { | ||
project.getPluginManager().withPlugin("jacoco", __ -> { | ||
configureFinalizedBy(project); | ||
configureDependsOn(project); | ||
}); | ||
} | ||
|
||
|
||
private void configureFinalizedBy(Project project) { | ||
project.getTasks() | ||
.matching(task -> !(task instanceof JacocoBase)) | ||
.configureEach(task -> | ||
task.finalizedBy(project.provider(() -> getFinalizedBy(task))) | ||
); | ||
} | ||
|
||
private List<Object> getFinalizedBy(Task task) { | ||
val taskExecutionDataFile = getOptionalExtension(task, JacocoTaskExtension.class) | ||
.map(JacocoTaskExtension::getDestinationFile) | ||
.map(File::getAbsoluteFile) | ||
.orElse(null); | ||
if (taskExecutionDataFile == null) { | ||
return emptyList(); | ||
} | ||
|
||
return task.getProject().getTasks().stream() | ||
.filter(JacocoReportBase.class::isInstance) | ||
.map(JacocoReportBase.class::cast) | ||
.filter(reportTask -> { | ||
val reportExecutionDataFile = singleOrNullFile(reportTask.getExecutionData().getFiles()); | ||
if (reportExecutionDataFile == null) { | ||
return false; | ||
} | ||
return reportExecutionDataFile.equals(taskExecutionDataFile); | ||
}) | ||
.collect(toList()); | ||
} | ||
|
||
|
||
private void configureDependsOn(Project project) { | ||
project.getTasks() | ||
.withType(JacocoReportBase.class) | ||
.configureEach(reportTask -> | ||
reportTask.dependsOn(project.provider(() -> getDependsOn(reportTask))) | ||
); | ||
} | ||
|
||
private List<Object> getDependsOn(JacocoReportBase reportTask) { | ||
val reportExecutionDataFile = singleOrNullFile(reportTask.getExecutionData().getFiles()); | ||
if (reportExecutionDataFile == null) { | ||
return emptyList(); | ||
} | ||
|
||
return reportTask.getProject().getTasks().stream() | ||
.filter(not(JacocoBase.class::isInstance)) | ||
.filter(task -> { | ||
val taskExecutionDataFile = getOptionalExtension(task, JacocoTaskExtension.class) | ||
.map(JacocoTaskExtension::getDestinationFile) | ||
.map(File::getAbsoluteFile) | ||
.orElse(null); | ||
if (taskExecutionDataFile == null) { | ||
return false; | ||
} | ||
|
||
return taskExecutionDataFile.equals(reportExecutionDataFile); | ||
}) | ||
.collect(toList()); | ||
} | ||
|
||
|
||
@Nullable | ||
private static File singleOrNullFile(Collection<? extends File> collection) { | ||
if (collection.size() == 1) { | ||
return collection.iterator().next().getAbsoluteFile(); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
} |
4 changes: 4 additions & 0 deletions
4
src/main/java/name/remal/gradle_plugins/finalize_by_jacoco/package-info.java
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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
@NonNullApi | ||
package name.remal.gradle_plugins.finalize_by_jacoco; | ||
|
||
import org.gradle.api.NonNullApi; |
13 changes: 0 additions & 13 deletions
13
src/main/java/name/remal/gradle_plugins/template/TemplatePlugin.java
This file was deleted.
Oops, something went wrong.
4 changes: 0 additions & 4 deletions
4
src/main/java/name/remal/gradle_plugins/template/package-info.java
This file was deleted.
Oops, something went wrong.
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