Skip to content

Commit

Permalink
Refactor Gradle plugin to allow use of config cache and parallel task…
Browse files Browse the repository at this point in the history
… execution
  • Loading branch information
jshiell committed Nov 17, 2024
1 parent aa6f9d3 commit a2ef698
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 109 deletions.
19 changes: 10 additions & 9 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@ import org.infernus.idea.checkstyle.build.CheckstyleVersions
import org.jetbrains.intellij.platform.gradle.TestFrameworkType
import org.jetbrains.intellij.platform.gradle.tasks.VerifyPluginTask

repositories {
mavenCentral()

intellijPlatform {
defaultRepositories()
}
}

plugins {
id("java")
id("jacoco")
id("idea")
id("org.jetbrains.intellij.platform") version "2.1.0"
id("com.dorongold.task-tree") version "2.1.1"

id("org.infernus.idea.checkstyle.build")
}

version = "5.98.0"

repositories {
mavenCentral()

intellijPlatform {
defaultRepositories()
}
}

intellijPlatform {
pluginConfiguration {
id = "CheckStyle-IDEA"
Expand Down Expand Up @@ -59,6 +59,7 @@ tasks {

withType<Test> {
setForkEvery(1)
jvmArgs("-Xshare:off")
}

withType<JavaCompile> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.infernus.idea.checkstyle.build;

import java.io.File;
import java.util.Set;

import groovy.lang.Closure;
import org.gradle.api.Project;
Expand Down Expand Up @@ -29,7 +28,8 @@ public class CsaccessTestTask extends Test {

public static final String CSVERSION_SYSPROP_NAME = "org.infernus.idea.checkstyle.version";

private String csVersion = null;
private FileCollection effectiveClassPath = null;

private Property<Boolean> dryRun;

public CsaccessTestTask() {
Expand All @@ -43,17 +43,16 @@ public CsaccessTestTask() {
GradlePluginMain.configureTestTask(this);
setTestClassesDirs(csaccessTestSourceSet.getOutput().getClassesDirs());
setClasspath(csaccessTestSourceSet.getRuntimeClasspath()
.plus(csaccessTestSourceSet.getCompileClasspath()));
.plus(csaccessTestSourceSet.getCompileClasspath())); // TODO delete?
}

public static String getTaskName(final String pCheckstyleVersion) {
return "xtest_" + CheckstyleVersions.toGradleVersion(pCheckstyleVersion);
}

public void setCheckstyleVersion(final String pCheckstyleVersion, final boolean isBaseVersion) {
csVersion = pCheckstyleVersion;
public void setCheckstyleVersion(final String checkstyleVersion, final boolean isBaseVersion) {
setDescription("Runs the '" + CustomSourceSetCreator.CSACCESSTEST_SOURCESET_NAME + "' unit tests against a "
+ "Checkstyle " + pCheckstyleVersion + " runtime.");
+ "Checkstyle " + checkstyleVersion + " runtime.");
getReports().getJunitXml().getRequired().set(false);
if (isBaseVersion) {
setGroup(LifecycleBasePlugin.VERIFICATION_GROUP);
Expand All @@ -67,12 +66,44 @@ public void setCheckstyleVersion(final String pCheckstyleVersion, final boolean
configure(new Closure<Void>(this) {
@Override
public Void call() {
systemProperty(CSVERSION_SYSPROP_NAME, pCheckstyleVersion);
systemProperty(CSVERSION_SYSPROP_NAME, checkstyleVersion);
return null;
}
});

effectiveClassPath = setClassPathForVersion(checkstyleVersion, getProject());
}

private @NotNull FileCollection setClassPathForVersion(final String checkstyleVersion, final Project project) {
final JavaPluginExtension jpc = project.getExtensions().getByType(JavaPluginExtension.class);
final Dependency csDep = CheckstyleVersions.createCheckstyleDependency(project, checkstyleVersion);
final ConfigurationContainer configurations = project.getConfigurations();
final Configuration detachedConfiguration = configurations.detachedConfiguration(csDep);
// workaround for Checkstyle#14123
detachedConfiguration
.getResolutionStrategy()
.getCapabilitiesResolution()
.withCapability("com.google.collections", "google-collections", resolutionDetails -> resolutionDetails.select("com.google.guava:guava:0"));

final SourceSetContainer sourceSets = jpc.getSourceSets();
final SourceSet mainSourceSet = sourceSets.getByName(SourceSet.MAIN_SOURCE_SET_NAME);
final SourceSet testSourceSet = sourceSets.getByName(SourceSet.TEST_SOURCE_SET_NAME);
final SourceSet csaccessSourceSet = sourceSets.getByName(CustomSourceSetCreator.CSACCESS_SOURCESET_NAME);
final SourceSet csaccessTestSourceSet = jpc.getSourceSets().getByName(CustomSourceSetCreator.CSACCESSTEST_SOURCESET_NAME);

return project.files(
csaccessTestSourceSet.getOutput().getResourcesDir(),
csaccessSourceSet.getOutput().getResourcesDir(),
mainSourceSet.getOutput().getResourcesDir())
.plus(csaccessTestSourceSet.getOutput().getClassesDirs())
.plus(csaccessSourceSet.getOutput().getClassesDirs())
.plus(mainSourceSet.getOutput().getClassesDirs())
.plus(project.files(detachedConfiguration.getFiles()))
.plus(csaccessTestSourceSet.getRuntimeClasspath())
.plus(csaccessTestSourceSet.getCompileClasspath())
.minus(testSourceSet.getOutput().getClassesDirs())
.minus(project.files(testSourceSet.getOutput().getResourcesDir()));
}

/**
* Overriding getClasspath() in order to set the final classpath is an unusual solution, but it was the only
Expand All @@ -83,47 +114,18 @@ public Void call() {
*/
@Override
public @NotNull FileCollection getClasspath() {
final FileCollection originalClasspath = super.getClasspath();

final Project project = getProject();
final JavaPluginExtension jpc = project.getExtensions().getByType(JavaPluginExtension.class);
final SourceSetContainer sourceSets = jpc.getSourceSets();
final SourceSet mainSourceSet = sourceSets.getByName(SourceSet.MAIN_SOURCE_SET_NAME);
final SourceSet testSourceSet = sourceSets.getByName(SourceSet.TEST_SOURCE_SET_NAME);
final SourceSet csaccessSourceSet = sourceSets.getByName(CustomSourceSetCreator.CSACCESS_SOURCESET_NAME);
final SourceSet csaccessTestSrcSet = sourceSets.getByName(CustomSourceSetCreator
.CSACCESSTEST_SOURCESET_NAME);

final Dependency csDep = CheckstyleVersions.createCheckstyleDependency(project, csVersion);
final ConfigurationContainer configurations = project.getConfigurations();
final Configuration detachedConfiguration = configurations.detachedConfiguration(csDep);
// workaround for Checkstyle#14123
detachedConfiguration
.getResolutionStrategy()
.getCapabilitiesResolution()
.withCapability("com.google.collections", "google-collections", resolutionDetails -> resolutionDetails.select("com.google.guava:guava:0"));
final Set<File> csJars = detachedConfiguration.getFiles();

FileCollection effectiveClasspath = project.files(
csaccessTestSrcSet.getOutput().getResourcesDir(),
csaccessSourceSet.getOutput().getResourcesDir(),
mainSourceSet.getOutput().getResourcesDir())
.plus(csaccessTestSrcSet.getOutput().getClassesDirs())
.plus(csaccessSourceSet.getOutput().getClassesDirs())
.plus(mainSourceSet.getOutput().getClassesDirs())
.plus(project.files(csJars))
.plus(originalClasspath)
.minus(testSourceSet.getOutput().getClassesDirs())
.minus(project.files(testSourceSet.getOutput().getResourcesDir()));
if (effectiveClassPath == null) {
throw new IllegalStateException("setCheckstyleVersion has not been called");
}

if (getLogger().isDebugEnabled()) {
getLogger().debug("--------------------------------------------------------------------------");
getLogger().debug("Effective classpath of " + getName() + ":");
for (File f : effectiveClasspath) {
for (File f : effectiveClassPath) {
getLogger().debug("\t- " + f.getAbsolutePath());
}
}
return effectiveClasspath;
return effectiveClassPath;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class GatherCheckstyleArtifactsTask
extends DefaultTask {
public static final String NAME = "gatherCheckstyleArtifacts";

private final Map<String, Set<File>> rawVersionsToDependencies = new HashMap<>();
private final CheckstyleVersions csVersions;

@OutputDirectory
Expand All @@ -36,7 +37,6 @@ public class GatherCheckstyleArtifactsTask
@OutputFile
private final File classPathsInfoFile;


public GatherCheckstyleArtifactsTask() {
super();
setGroup(LifecycleBasePlugin.BUILD_GROUP);
Expand All @@ -50,19 +50,21 @@ public GatherCheckstyleArtifactsTask() {
// Task Outputs: the directory full of JARs, and the classpath info file
bundledJarsDir = getTemporaryDir();
classPathsInfoFile = new File(project.getLayout().getBuildDirectory().getAsFile().get(), "resources-generated/checkstyle-classpaths.properties");
}

for (final String csVersion : csVersions.getVersions()) {
final Set<File> dependencies = resolveDependencies(project, csVersion);
rawVersionsToDependencies.put(csVersion, dependencies);
}
}

@TaskAction
public void runTask() {
final Set<File> bundledFiles = new TreeSet<>();
final Properties classPaths = new SortedProperties();
final Map<String, Set<File>> rawVersionsToDependencies = new HashMap<>();
final Set<String> availableFileNames = new HashSet<>();

for (final String csVersion : csVersions.getVersions()) {
final Set<File> dependencies = resolveDependencies(getProject(), csVersion);
rawVersionsToDependencies.put(csVersion, dependencies);
Set<File> dependencies = rawVersionsToDependencies.get(csVersion);
availableFileNames.addAll(dependencies.stream().map(File::getName).collect(toSet()));
}

Expand Down Expand Up @@ -109,7 +111,6 @@ private String convertToClassPath(final Collection<String> resolvedDependencies)
return sb.toString();
}


private void copyFiles(final Set<File> bundledJars) {
for (final File bundledJar : bundledJars) {
try {
Expand All @@ -120,7 +121,6 @@ private void copyFiles(final Set<File> bundledJars) {
}
}


private void createClassPathsFile(final Properties classPaths) {
//noinspection ResultOfMethodCallIgnored
classPathsInfoFile.getParentFile().mkdir();
Expand All @@ -133,7 +133,6 @@ private void createClassPathsFile(final Properties classPaths) {
}
}


public File getBundledJarsDir() {
return bundledJarsDir;
}
Expand Down
Loading

0 comments on commit a2ef698

Please sign in to comment.