|
| 1 | +/* |
| 2 | + * FlintMC |
| 3 | + * Copyright (C) 2020-2021 LabyMedia GmbH and contributors |
| 4 | + * |
| 5 | + * This program is free software; you can redistribute it and/or |
| 6 | + * modify it under the terms of the GNU Lesser General Public |
| 7 | + * License as published by the Free Software Foundation; either |
| 8 | + * version 3 of the License, or (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | + * Lesser General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU Lesser General Public License |
| 16 | + * along with this program; if not, write to the Free Software Foundation, |
| 17 | + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 18 | + */ |
| 19 | + |
| 20 | +package net.flintmc.gradle.java.instrumentation; |
| 21 | + |
| 22 | +import net.flintmc.gradle.java.instrumentation.tasks.InstrumentationTask; |
| 23 | +import net.flintmc.gradle.util.Util; |
| 24 | +import org.gradle.api.Project; |
| 25 | +import org.gradle.api.Task; |
| 26 | +import org.gradle.api.artifacts.Configuration; |
| 27 | +import org.gradle.api.file.ConfigurableFileCollection; |
| 28 | +import org.gradle.api.plugins.JavaBasePlugin; |
| 29 | +import org.gradle.api.tasks.SourceSet; |
| 30 | +import org.gradle.api.tasks.SourceSetContainer; |
| 31 | + |
| 32 | +public class Instrumentation { |
| 33 | + |
| 34 | + private static final String GROUP = "flint-instrumentation"; |
| 35 | + |
| 36 | + /** |
| 37 | + * Applies post compile instrumentation capabilities to a given project. |
| 38 | + * |
| 39 | + * @param project the project to apply the instrumentation to |
| 40 | + */ |
| 41 | + public void apply(final Project project) { |
| 42 | + // Whether the project has applied the java base plugin |
| 43 | + if (!project.getPlugins().hasPlugin(JavaBasePlugin.class)) { |
| 44 | + return; |
| 45 | + } |
| 46 | + |
| 47 | + Configuration instrumentation = project.getConfigurations().maybeCreate("instrumentation"); |
| 48 | + instrumentation.setCanBeResolved(true); |
| 49 | + |
| 50 | + SourceSetContainer sourceSets = project.getExtensions().getByType(SourceSetContainer.class); |
| 51 | + sourceSets.all(sourceSet -> this.configure(project, sourceSet, instrumentation)); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Configures the instrumentation tasks. |
| 56 | + * |
| 57 | + * @param project The project in which the instrumentation tasks are to be configured. |
| 58 | + * @param sourceSet The {@link SourceSet} in which the instrumentation tasks are to be |
| 59 | + * configured. |
| 60 | + */ |
| 61 | + private void configure(final Project project, final SourceSet sourceSet, Configuration instrumentationConfiguration) { |
| 62 | + InstrumentationTask instrumentationTask = project.getTasks() |
| 63 | + .create(sourceSet.getTaskName("instrument", "code") + Util.capitalize(project.getName()), |
| 64 | + InstrumentationTask.class); |
| 65 | + instrumentationTask.getOutputs().dir(sourceSet.getOutput().getClassesDirs().getSingleFile().getPath() + "-instrumented"); |
| 66 | + instrumentationTask.dependsOn(sourceSet.getClassesTaskName()); |
| 67 | + instrumentationTask.setGroup(GROUP); |
| 68 | + instrumentationTask.setSourceSet(sourceSet); |
| 69 | + instrumentationTask.setConfiguration(instrumentationConfiguration); |
| 70 | + |
| 71 | + Task postInstrumentationTask = project.getTasks() |
| 72 | + .create("post" + Util.capitalize(instrumentationTask.getName())); |
| 73 | + postInstrumentationTask.dependsOn(instrumentationTask); |
| 74 | + postInstrumentationTask.setGroup(GROUP); |
| 75 | + postInstrumentationTask.doLast(task -> { |
| 76 | + ((ConfigurableFileCollection) sourceSet.getOutput().getClassesDirs()).setFrom(instrumentationTask.getOutputs().getFiles().getSingleFile()); |
| 77 | + }); |
| 78 | + sourceSet.compiledBy(postInstrumentationTask); |
| 79 | + } |
| 80 | + |
| 81 | +} |
0 commit comments