Skip to content

Commit

Permalink
Fix up-to-date checks for precommit related tasks (#57203) (#57294)
Browse files Browse the repository at this point in the history
* Fix up-to-date checks for precommit related tasks

- Do not use lambdas for doFirst / doLast action declarations as this is not supported by gradle up-to-date check
- Use marker output folder for dependencies license task to make task incremental build compliant

* Tweak formatting
  • Loading branch information
breskeby authored May 28, 2020
1 parent ad56dce commit f6b9a99
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,9 @@ public static void configureConfigurations(Project project) {

private static final Pattern LUCENE_SNAPSHOT_REGEX = Pattern.compile("\\w+-snapshot-([a-z0-9]+)");

/** Adds repositories used by ES dependencies */
/**
* Adds repositories used by ES dependencies
*/
public static void configureRepositories(Project project) {
// ensure all repositories use secure urls
// TODO: remove this with gradle 7.0, which no longer allows insecure urls
Expand Down Expand Up @@ -216,7 +218,9 @@ private static void assertRepositoryURIIsSecure(final String repositoryName, fin
}
}

/** Adds compiler settings to the project */
/**
* Adds compiler settings to the project
*/
public static void configureCompile(Project project) {
project.getExtensions().getExtraProperties().set("compactProfile", "full");

Expand Down Expand Up @@ -472,8 +476,11 @@ static void configureJars(Project project) {
// we put all our distributable files under distributions
jarTask.getDestinationDirectory().set(new File(project.getBuildDir(), "distributions"));
// fixup the jar manifest
jarTask.doFirst(
t -> {
// Explicitly using an Action interface as java lambdas
// are not supported by Gradle up-to-date checks
jarTask.doFirst(new Action<Task>() {
@Override
public void execute(Task task) {
// this doFirst is added before the info plugin, therefore it will run
// after the doFirst added by the info plugin, and we can override attributes
jarTask.getManifest()
Expand All @@ -486,7 +493,7 @@ static void configureJars(Project project) {
)
);
}
);
});
}
);
project.getPluginManager().withPlugin("com.github.johnrengelman.shadow", p -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import org.elasticsearch.gradle.VersionProperties;
import org.elasticsearch.gradle.util.Util;
import org.gradle.api.Action;
import org.gradle.api.Project;
import org.gradle.api.Task;
import org.gradle.api.artifacts.dsl.DependencyHandler;
Expand Down Expand Up @@ -62,17 +63,22 @@ public TaskProvider<? extends Task> createTask(Project project) {
copyCheckstyleConf.configure(t -> t.getInputs().files(checkstyleConfUrl.getFile(), checkstyleSuppressionsUrl.getFile()));
}

copyCheckstyleConf.configure(t -> t.doLast(task -> {
checkstyleDir.mkdirs();
try (InputStream stream = checkstyleConfUrl.openStream()) {
Files.copy(stream, checkstyleConf.toPath(), StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
try (InputStream stream = checkstyleSuppressionsUrl.openStream()) {
Files.copy(stream, checkstyleSuppressions.toPath(), StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
throw new UncheckedIOException(e);
// Explicitly using an Action interface as java lambdas
// are not supported by Gradle up-to-date checks
copyCheckstyleConf.configure(t -> t.doLast(new Action<Task>() {
@Override
public void execute(Task task) {
checkstyleDir.mkdirs();
try (InputStream stream = checkstyleConfUrl.openStream()) {
Files.copy(stream, checkstyleConf.toPath(), StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
try (InputStream stream = checkstyleSuppressionsUrl.openStream()) {
Files.copy(stream, checkstyleSuppressions.toPath(), StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
}));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.gradle.api.tasks.InputFiles;
import org.gradle.api.tasks.Internal;
import org.gradle.api.tasks.Optional;
import org.gradle.api.tasks.OutputDirectory;
import org.gradle.api.tasks.TaskAction;

import java.io.File;
Expand Down Expand Up @@ -223,6 +224,15 @@ public void checkDependencies() throws IOException, NoSuchAlgorithmException {
if (shaFiles.isEmpty() == false) {
throw new GradleException("Unused sha files found: \n" + joinFilenames(shaFiles));
}

}

// This is just a marker output folder to allow this task being up-to-date.
// The check logic is exception driven so a failed tasks will not be defined
// by this output but when successful we can safely mark the task as up-to-date.
@OutputDirectory
public File getOutputMarker() {
return new File(getProject().getBuildDir(), "dependencyLicense");
}

private void failIfAnyMissing(String item, Boolean exists, String type) {
Expand Down

0 comments on commit f6b9a99

Please sign in to comment.