Skip to content

Commit

Permalink
fix: resolve deprecations up to gradle 8.4
Browse files Browse the repository at this point in the history
  • Loading branch information
zml2008 committed Jan 24, 2024
1 parent d7d9f0f commit 694e129
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,39 +24,51 @@
*/
package org.spongepowered.eventimplgen;

import java.util.Collections;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.artifacts.ConfigurationContainer;
import org.gradle.api.artifacts.Dependency;
import org.gradle.api.artifacts.ExcludeRule;
import org.gradle.api.attributes.Attribute;
import org.gradle.api.attributes.AttributeContainer;
import org.gradle.api.file.CopySpec;
import org.gradle.api.file.DirectoryProperty;
import org.gradle.api.plugins.JavaPlugin;
import org.gradle.api.plugins.JavaPluginConvention;
import org.gradle.api.tasks.SourceSet;
import org.gradle.api.tasks.SourceSetContainer;
import org.gradle.util.GradleVersion;

public class EventImplGenPlugin implements Plugin<Project> {
static final boolean HAS_GRADLE_7 = GradleVersion.current().getBaseVersion().compareTo(GradleVersion.version("7.0.0")) >= 0;
static final boolean HAS_GRADLE_6 = GradleVersion.current().getBaseVersion().compareTo(GradleVersion.version("6.0.0")) >= 0;

private Configuration classpath;

@Override
public void apply(final Project project) {
project.getPlugins().apply(JavaPlugin.class);
final SourceSet mainSourceSet = project.getConvention().getPlugin(JavaPluginConvention.class)
.getSourceSets().getByName(SourceSet.MAIN_SOURCE_SET_NAME);
final SourceSet mainSourceSet;
if (HAS_GRADLE_7) {
mainSourceSet = project.getExtensions().getByType(SourceSetContainer.class)
.getByName(SourceSet.MAIN_SOURCE_SET_NAME);
} else {
mainSourceSet = project.getConvention().getPlugin(JavaPluginConvention.class)
.getSourceSets().getByName(SourceSet.MAIN_SOURCE_SET_NAME);
}

final EventImplGenTask task = project.getTasks().create("genEventImpl", EventImplGenTask.class);
task.source(mainSourceSet.getAllJava());
if (GradleVersion.current().compareTo(GradleVersion.version("7.0.0")) < 0) {
if (!HAS_GRADLE_7) {
task.conventionMapping("destinationDir", () -> project.getLayout().getBuildDirectory().dir("generated/event-factory").get().getAsFile());
}
if (GradleVersion.current().compareTo(GradleVersion.version("6.0.0")) >= 0) {
task.conventionMapping("destinationDirectory", () -> {
final DirectoryProperty dir = project.getObjects().directoryProperty();
dir.set(project.getLayout().getBuildDirectory().dir("generated/event-factory"));
return dir;
});
if (HAS_GRADLE_6) {
task.getDestinationDirectory()
.convention(project.getLayout().getBuildDirectory().dir("generated/event-factory"));
}
task.conventionMapping("classpath", () -> this.classpath);
task.setClasspath(project.files(project.provider(() -> this.classpath)));
// task.conventionMapping("classpath", () -> this.classpath);

// Include event factory classes in JAR
((CopySpec) project.getTasks().getByName(mainSourceSet.getJarTaskName())).from(task);
Expand All @@ -72,11 +84,32 @@ public void apply(final Project project) {
// before we add the event factory. This needs to be done in afterEvaluate
// however, so any dependencies added after event-impl-gen is applied will
// be included in the Spoon classpath.
this.classpath = project1.getConfigurations().getByName(mainSourceSet.getCompileClasspathConfigurationName()).copyRecursive();
this.classpath = this.makeConfigurationCopy(project1.getConfigurations().getByName(mainSourceSet.getCompileClasspathConfigurationName()), project1);

// Add the event factory to the compile dependencies
project1.getDependencies().add(mainSourceSet.getApiConfigurationName(), project.files(task));
});
}

// quick variant of copyRecursive that is less likely to trigger deprecation warnings
private Configuration makeConfigurationCopy(final Configuration incoming, final Project project) {
final ConfigurationContainer configs = project.getConfigurations();
final Configuration resolvable = configs.detachedConfiguration(incoming.getAllDependencies().toArray(new Dependency[0]));
resolvable.setCanBeResolved(true);

for (final ExcludeRule exclude : incoming.getExcludeRules()) {
resolvable.getExcludeRules().add(exclude);
}

final AttributeContainer resolvableAttr = resolvable.getAttributes(), incomingAttr = incoming.getAttributes();;
for (final Attribute<?> attr : incomingAttr.keySet()) {
this.transferAttr(incomingAttr, resolvableAttr, attr);
}
return resolvable;
}

private <T> void transferAttr(final AttributeContainer source, final AttributeContainer dest, final Attribute<T> attr) {
dest.attribute(attr, source.getAttribute(attr));
}

}
13 changes: 11 additions & 2 deletions src/main/java/org/spongepowered/eventimplgen/EventImplGenTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
*/
package org.spongepowered.eventimplgen;

import java.util.Arrays;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.gradle.api.JavaVersion;
import org.gradle.api.UncheckedIOException;
Expand Down Expand Up @@ -194,10 +195,18 @@ protected void compile() {
}
}

private File destinationDir() {
if (EventImplGenPlugin.HAS_GRADLE_7) {
return this.getDestinationDirectory().get().getAsFile();
} else {
return this.getDestinationDir();
}
}

@TaskAction
public void generateClasses() throws IOException {
// Clean the destination directory
getProject().delete(getDestinationDir());
getProject().delete(this.destinationDir());

// Initialize spoon
final SpoonAPI spoon = new Launcher();
Expand Down Expand Up @@ -239,7 +248,7 @@ private void dumpClasses(final Map<CtType<?>, List<Property>> foundProperties, f
final String packageName = this.outputFactory.substring(0, this.outputFactory.lastIndexOf('.'));
final ClassGeneratorProvider provider = new ClassGeneratorProvider(packageName);

final Path destinationDir = this.getDestinationDir().toPath();
final Path destinationDir = this.destinationDir().toPath();
// Create package directory
Files.createDirectories(destinationDir.resolve(packageName.replace('.', File.separatorChar)));

Expand Down

0 comments on commit 694e129

Please sign in to comment.