dweiss commented on code in PR #15305:
URL: https://github.com/apache/lucene/pull/15305#discussion_r2414454589


##########
build-tools/build-infra/src/main/java/org/apache/lucene/gradle/plugins/java/ErrorPronePlugin.java:
##########
@@ -0,0 +1,962 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.lucene.gradle.plugins.java;
+
+import com.google.errorprone.scanner.BuiltInCheckerSuppliers;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.Set;
+import java.util.TreeSet;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+import net.ltgt.gradle.errorprone.CheckSeverity;
+import net.ltgt.gradle.errorprone.ErrorProneOptions;
+import org.apache.lucene.gradle.plugins.LuceneGradlePlugin;
+import org.apache.lucene.gradle.plugins.misc.CheckEnvironmentPlugin;
+import org.gradle.api.GradleException;
+import org.gradle.api.Project;
+import org.gradle.api.plugins.ExtensionAware;
+import org.gradle.api.plugins.JavaPlugin;
+import org.gradle.api.provider.Property;
+import org.gradle.api.provider.Provider;
+import org.gradle.api.tasks.compile.JavaCompile;
+
+/**
+ * Applies Google's error-prone project for additional linting.
+ *
+ * @see "https://github.com/google/error-prone";
+ */
+public class ErrorPronePlugin extends LuceneGradlePlugin {
+  private static final String TASK_ERROR_PRONE_SKIPPED = "errorProneSkipped";
+  public static final String OPT_VALIDATION_ERRORPRONE = 
"validation.errorprone";
+
+  public static class ErrorPronePluginRootExtPlugin extends LuceneGradlePlugin 
{
+
+    public abstract static class ErrorProneStatus {
+      public abstract Property<Boolean> getEnabled();
+    }
+
+    @Override
+    public void apply(Project project) {
+      applicableToRootProjectOnly(project);
+
+      // Ensure internal consistency with error-prone.
+      var verifyErrorProneCheckListSanity =
+          project
+              .getTasks()
+              .register(
+                  "verifyErrorProneCheckListSanity",
+                  t -> {
+                    t.doFirst(
+                        _ -> {
+                          ensureCheckListIsComplete();
+                        });
+                  });
+      project
+          .getTasks()
+          .named("check")
+          .configure(t -> t.dependsOn(verifyErrorProneCheckListSanity));
+
+      // check if error-prone should be hooked up to javac or not.
+      var buildGlobals = getLuceneBuildGlobals(project);
+      Provider<Boolean> errorproneOption =
+          getBuildOptions(project)
+              .addBooleanOption(
+                  OPT_VALIDATION_ERRORPRONE,
+                  "Applies error-prone for additional source code linting.",
+                  buildGlobals.isCIBuild);
+
+      var status = project.getExtensions().create("errorProneStatus", 
ErrorProneStatus.class);
+
+      String skipReason;
+      if (project
+          .getExtensions()
+          .getByType(AlternativeJdkSupportPlugin.AltJvmExtension.class)
+          .getAltJvmUsed()
+          .get()) {
+        // LUCENE-9650: Errorprone does not work when running as a plugin 
inside a forked Javac
+        // process.
+        skipReason = "won't work with alternative java toolchain";
+      } else {
+        if (errorproneOption.get()) {
+          // Enabled and fine to run.
+          skipReason = null;
+        } else {
+          if (buildGlobals.isCIBuild) {
+            throw new GradleException(
+                "Odd, errorprone linting should always be enabled on CI 
builds?");
+          } else {
+            skipReason =
+                "skipped on builds not running inside CI environments, pass -P"
+                    + OPT_VALIDATION_ERRORPRONE
+                    + "=true to enable";
+          }
+        }
+      }
+
+      if (skipReason != null) {
+        project
+            .getTasks()
+            .register(
+                TASK_ERROR_PRONE_SKIPPED,
+                task -> {
+                  task.doFirst(
+                      t -> {
+                        t.getLogger().warn("Errorprone linting turned off 
({})", skipReason);
+                      });
+                });
+      }
+
+      status.getEnabled().set(skipReason == null && errorproneOption.get());
+    }
+
+    // double check we have all the checks from the current version of 
errorprone covered.
+    private void ensureCheckListIsComplete() {
+      Set<String> allCanonicalNames = new TreeSet<>();
+      Map<String, String> altToCanonical = new HashMap<>();
+
+      Stream.of(
+              BuiltInCheckerSuppliers.ENABLED_ERRORS,
+              BuiltInCheckerSuppliers.ENABLED_WARNINGS,
+              BuiltInCheckerSuppliers.DISABLED_CHECKS)

Review Comment:
   I added all checks - even those disabled by default in error-prone. Those 
that were missing are turned off in the list below too.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to