Skip to content

Commit

Permalink
Extract common code and suppress deprecations very specifically
Browse files Browse the repository at this point in the history
  • Loading branch information
etiennestuder committed Aug 22, 2021
1 parent 7f5e2cc commit be1c25c
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 16 deletions.
3 changes: 1 addition & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ dependencies {
tasks.withType(AbstractCompile).configureEach {
options.compilerArgs <<
"-Werror" <<
"-Xlint:all" <<
"-Xlint:-deprecation" // do not fail on deprecations introduced very lately in Gradle 7.1
"-Xlint:all"
}

tasks.withType(Test).configureEach {
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/nu/studer/gradle/rocker/GradleUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package nu.studer.gradle.rocker;

import org.gradle.util.GradleVersion;

final class GradleUtils {

static boolean isAtLeastGradleVersion(String version) {
return GradleVersion.current().getBaseVersion().compareTo(GradleVersion.version(version)) >= 0;
}

private GradleUtils() {
}

}
23 changes: 16 additions & 7 deletions src/main/java/nu/studer/gradle/rocker/RockerCompile.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import org.gradle.process.ExecOperations;
import org.gradle.process.ExecResult;
import org.gradle.process.JavaExecSpec;
import org.gradle.util.GradleVersion;
import org.gradle.work.ChangeType;
import org.gradle.work.InputChanges;

Expand All @@ -34,6 +33,8 @@
import java.util.HashSet;
import java.util.Set;

import static nu.studer.gradle.rocker.GradleUtils.isAtLeastGradleVersion;

public class RockerCompile extends DefaultTask {

private static final String ROCKER_FILE_EXTENSION_PREFIX = ".rocker";
Expand Down Expand Up @@ -226,12 +227,7 @@ private ExecResult executeRocker(final File templateDir) {

@Override
public void execute(JavaExecSpec spec) {
String mainClass = "com.fizzed.rocker.compiler.JavaGeneratorMain";
if (GradleVersion.current().getBaseVersion().compareTo(GradleVersion.version("6.4")) >= 0) {
spec.getMainClass().set(mainClass);
} else {
spec.setMain(mainClass);
}
setMainClass("com.fizzed.rocker.compiler.JavaGeneratorMain", spec);
spec.setClasspath(runtimeClasspath);
spec.setWorkingDir(projectLayout.getProjectDirectory());
spec.systemProperty("rocker.option.optimize", optimize.get().toString());
Expand All @@ -248,6 +244,19 @@ public void execute(JavaExecSpec spec) {
}
}

private void setMainClass(String mainClass, JavaExecSpec spec) {
if (isAtLeastGradleVersion("6.4")) {
spec.getMainClass().set(mainClass);
} else {
setMainClassDeprecated(mainClass, spec);
}
}

@SuppressWarnings("deprecation")
private void setMainClassDeprecated(String mainClass, JavaExecSpec spec) {
spec.setMain(mainClass);
}

private void systemPropertyIfNotNull(String option, String value, JavaExecSpec spec) {
if (value != null) {
spec.systemProperty(option, value);
Expand Down
22 changes: 15 additions & 7 deletions src/main/java/nu/studer/gradle/rocker/RockerPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.artifacts.ModuleVersionSelector;
import org.gradle.api.plugins.JavaBasePlugin;
import org.gradle.api.plugins.JavaPluginConvention;
import org.gradle.api.plugins.JavaPluginExtension;
import org.gradle.api.tasks.SourceSetContainer;
import org.gradle.api.tasks.TaskProvider;
Expand All @@ -14,6 +13,7 @@
import org.slf4j.LoggerFactory;

import static java.lang.String.format;
import static nu.studer.gradle.rocker.GradleUtils.isAtLeastGradleVersion;
import static nu.studer.gradle.rocker.StringUtils.capitalize;

@SuppressWarnings("unused")
Expand Down Expand Up @@ -49,12 +49,7 @@ public void apply(Project project) {

// add the output of the rocker task as a source directory of the source set with the matching name (which adds an implicit task dependency)
// add the rocker-runtime to the compile configuration in order to be able to compile the generated sources
SourceSetContainer sourceSets;
if (GradleVersion.current().getBaseVersion().compareTo(GradleVersion.version("7.1")) >= 0) {
sourceSets = project.getExtensions().getByType(JavaPluginExtension.class).getSourceSets();
} else {
sourceSets = project.getConvention().getPlugin(JavaPluginConvention.class).getSourceSets();
}
SourceSetContainer sourceSets = getSourceSets(project);
sourceSets.configureEach(sourceSet -> {
if (sourceSet.getName().equals(config.name)) {
sourceSet.getJava().srcDir(rocker.flatMap(RockerCompile::getOutputDir));
Expand All @@ -67,6 +62,19 @@ public void apply(Project project) {
enforceRockerVersion(project, rockerExtension);
}

private SourceSetContainer getSourceSets(Project project) {
if (isAtLeastGradleVersion("7.1")) {
return project.getExtensions().getByType(JavaPluginExtension.class).getSourceSets();
} else {
return getSourceSetsDeprecated(project);
}
}

@SuppressWarnings("deprecation")
private SourceSetContainer getSourceSetsDeprecated(Project project) {
return project.getConvention().getPlugin(org.gradle.api.plugins.JavaPluginConvention.class).getSourceSets();
}

private static Configuration createRockerCompilerRuntimeConfiguration(Project project) {
Configuration rockerCompilerRuntime = project.getConfigurations().create("rockerCompiler");
rockerCompilerRuntime.setDescription("The classpath used to invoke the Rocker template engine. Add your additional dependencies here.");
Expand Down

0 comments on commit be1c25c

Please sign in to comment.