|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the "Elastic License |
| 4 | + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side |
| 5 | + * Public License v 1"; you may not use this file except in compliance with, at |
| 6 | + * your election, the "Elastic License 2.0", the "GNU Affero General Public |
| 7 | + * License v3.0 only", or the "Server Side Public License, v 1". |
| 8 | + */ |
| 9 | + |
| 10 | +package org.elasticsearch.gradle.internal.conventions.precommit; |
| 11 | + |
| 12 | +import org.gradle.api.Plugin; |
| 13 | +import org.gradle.api.Project; |
| 14 | +import org.openrewrite.gradle.RewriteExtension; |
| 15 | + |
| 16 | +import org.openrewrite.gradle.RewritePlugin; |
| 17 | + |
| 18 | +import static java.lang.Boolean.parseBoolean; |
| 19 | +import static java.lang.System.getenv; |
| 20 | + |
| 21 | +/** |
| 22 | + * This plugin configures formatting for Java source using Spotless |
| 23 | + * for Gradle. Since the act of formatting existing source can interfere |
| 24 | + * with developers' workflows, we don't automatically format all code |
| 25 | + * (yet). Instead, we maintain a list of projects that are excluded from |
| 26 | + * formatting, until we reach a point where we can comfortably format them |
| 27 | + * in one go without too much disruption. |
| 28 | + * |
| 29 | + * <p>Any new sub-projects must not be added to the exclusions list! |
| 30 | + * |
| 31 | + * <p>To perform a reformat, run: |
| 32 | + * |
| 33 | + * <pre> ./gradlew spotlessApply</pre> |
| 34 | + * |
| 35 | + * <p>To check the current format, run: |
| 36 | + * |
| 37 | + * <pre> ./gradlew spotlessJavaCheck</pre> |
| 38 | + * |
| 39 | + * <p>This is also carried out by the `precommit` task. |
| 40 | + * |
| 41 | + * <p>See also the <a href="https://github.com/diffplug/spotless/tree/master/plugin-gradle" |
| 42 | + * >Spotless project page</a>. |
| 43 | + */ |
| 44 | +public class RewritePrecommitPlugin implements Plugin<Project> { |
| 45 | + |
| 46 | + private static final boolean IS_NON_CI = parseBoolean(getenv("isCI")) == false; |
| 47 | + private static final boolean SKIP_FORMATTING = parseBoolean(getenv("skipFormatting")); |
| 48 | + private static final boolean CODE_CLEANUP = parseBoolean(getenv("codeCleanup")); |
| 49 | + |
| 50 | + @SuppressWarnings({ "checkstyle:DescendantToken", "checkstyle:LineLength" }) |
| 51 | + @Override |
| 52 | + public void apply(Project project) { |
| 53 | + project.getPluginManager().withPlugin("java-base", javaBasePlugin -> { |
| 54 | + project.getPlugins().apply(PrecommitTaskPlugin.class); |
| 55 | + project.getPlugins().apply(RewritePlugin.class); |
| 56 | + project.getRepositories().mavenCentral(); // spotless & rewrite need mavenCentral |
| 57 | + project.getTasks().named("precommit").configure(precommitTask -> precommitTask.dependsOn( "rewriteDryRun")); |
| 58 | + project.getTasks().named("check").configure(check -> check.dependsOn("rewriteDryRun")); |
| 59 | + if (!SKIP_FORMATTING && IS_NON_CI && CODE_CLEANUP) { |
| 60 | + project.getTasks().named("assemble").configure(check -> check.dependsOn("rewriteRun")); |
| 61 | + } |
| 62 | + rewrite(project); |
| 63 | + }); |
| 64 | + } |
| 65 | + |
| 66 | + private static void rewrite(Project project) { |
| 67 | + RewriteExtension rewriteExtension = project.getExtensions().getByType(RewriteExtension.class); |
| 68 | + rewriteExtension.activeRecipe( |
| 69 | + "org.openrewrite.java.RemoveUnusedImports" |
| 70 | + //"org.openrewrite.staticanalysis.RemoveUnusedLocalVariables", |
| 71 | + //"org.openrewrite.staticanalysis.RemoveUnusedPrivateFields", |
| 72 | + //"org.openrewrite.staticanalysis.RemoveUnusedPrivateMethods" |
| 73 | + ); |
| 74 | + rewriteExtension.exclusion("**OpenSearchTestCaseTests.java"); |
| 75 | + rewriteExtension.setExportDatatables(true); |
| 76 | + rewriteExtension.setFailOnDryRunResults(true); |
| 77 | + } |
| 78 | +} |
0 commit comments