package dev.caskeleton.buildlogic.apisurface; import dev.caskeleton.buildlogic.ApiSurfaceExtension; import java.io.File; import java.util.ArrayList; import java.util.List; import org.gradle.api.GradleException; import org.gradle.api.Plugin; import org.gradle.api.Project; public final class ApiSurfacePlugin implements Plugin { @Override public void apply(Project project) { ApiSurfaceExtension extension = project.getExtensions().create("apiSurface", ApiSurfaceExtension.class); project.afterEvaluate( ignored -> { String label = extension.getLabel(); File baseline = extension.getBaseline(); if ((label == null || label.isBlank()) && baseline == null) { return; } if (label == null || label.isBlank()) { throw new GradleException( project.getPath() + " declares an apiSurface baseline without a label"); } if (baseline == null) { throw new GradleException( project.getPath() + " declares an apiSurface label without a baseline"); } List sourcePaths = new ArrayList<>(); if (extension.getSourceRoot() != null && !extension.getSourceRoot().isBlank()) { sourcePaths.add(extension.getSourceRoot()); } List additional = extension.getAdditionalSourceRoots().stream() .filter(path -> path != null && !path.isBlank()) .map(project::file) .toList(); // A missing additional root used to be skipped without a word, so an approved update // recorded a surface with a whole module's types gone (2026-11-12 review). List missing = additional.stream().filter(root -> !root.isDirectory()).toList(); if (!missing.isEmpty()) { throw new GradleException( project.getPath() + " apiSurface.additionalSourceRoots names a folder that does not exist: " + missing); } List roots = new ArrayList<>(sourcePaths.stream().map(project::file).toList()); roots.addAll(additional); String verifyTaskName = "verify" + label + "ApiSurface"; String updateTaskName = "update" + label + "ApiSurface"; String approvalProperty = "approve" + label + "ApiSurfaceChange"; String ceilingProperty = "raise" + label + "ApiSurfaceCeiling"; boolean approved = project.hasProperty(approvalProperty); boolean ceilingApproved = project.hasProperty(ceilingProperty); var verify = project .getTasks() .register( verifyTaskName, VerifyApiSurfaceTask.class, task -> { task.setGroup("verification"); task.setDescription( "Fails without mutation when the committed " + label + " public API surface drifts."); task.getSourceRoots().from(roots); task.getBaseline().fileValue(baseline); task.getLabel().set(label); task.getSurfaceDescription() .set(extension.getDescription() == null ? "" : extension.getDescription()); task.getRationale().set(extension.getRationale()); task.getOwningProjectPath().set(project.getPath()); task.getUpdateTaskName().set(updateTaskName); task.getApprovalProperty().set(approvalProperty); task.getUpdateApproved().set(approved); if (extension.getAdopterBaseline() != null) { task.getAdopterBaseline().fileValue(extension.getAdopterBaseline()); if (extension.getAdopterSources() != null) { task.getAdopterSources().from(extension.getAdopterSources()); } } }); project .getTasks() .register( updateTaskName, UpdateApiSurfaceTask.class, task -> { task.setGroup("verification"); task.setDescription( "Rewrites the committed " + label + " public API surface baseline after review."); task.getSourceRoots().from(roots); task.getBaseline().fileValue(baseline); task.getLabel().set(label); task.getSurfaceDescription() .set(extension.getDescription() == null ? "" : extension.getDescription()); task.getRationale().set(extension.getRationale()); task.getOwningProjectPath().set(project.getPath()); task.getApprovalProperty().set(approvalProperty); task.getCeilingProperty().set(ceilingProperty); task.getApproved().set(approved); task.getCeilingRaiseApproved().set(ceilingApproved); }); // The leaf's `check`, so a pull request that changes a public type fails there (owner // decision 20, 2026-09-25). It used to run only in `mainCheck`, which reported the change // after it had merged. project .getTasks() .matching(task -> task.getName().equals("check")) .configureEach(task -> task.dependsOn(verify)); }); } }