|
| 1 | +/* |
| 2 | + * Copyright 2016 DiffPlug |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.diffplug.gradle.spotless; |
| 17 | + |
| 18 | +import java.io.File; |
| 19 | +import java.util.Collection; |
| 20 | +import java.util.HashMap; |
| 21 | +import java.util.Map; |
| 22 | +import java.util.Objects; |
| 23 | +import java.util.Set; |
| 24 | + |
| 25 | +import javax.annotation.Nullable; |
| 26 | + |
| 27 | +import org.gradle.api.Project; |
| 28 | +import org.gradle.util.GradleVersion; |
| 29 | + |
| 30 | +import com.diffplug.common.base.Preconditions; |
| 31 | +import com.diffplug.common.collect.ImmutableList; |
| 32 | +import com.diffplug.spotless.FormatterStep; |
| 33 | +import com.diffplug.spotless.Provisioner; |
| 34 | + |
| 35 | +class RegisterDependenciesInRoot { |
| 36 | + static final GradleVersion STRICT_CONFIG_ACCESS_WARNING = GradleVersion.version("6.0"); |
| 37 | + |
| 38 | + static final String ENABLE_KEY = "spotless_register_dependencies_in_root"; |
| 39 | + static final String TASK_NAME = "spotlessRegisterDependencies"; |
| 40 | + |
| 41 | + /** Determines if the "spotless_register_dependencies_in_root" mode is enabled. */ |
| 42 | + public static boolean isEnabled(Project project) { |
| 43 | + Object enable = project.getRootProject().findProperty(ENABLE_KEY); |
| 44 | + if (Boolean.TRUE.equals(enable) || "true".equals(enable)) { |
| 45 | + return true; |
| 46 | + } |
| 47 | + boolean onlyOneProjectInEntireBuild = project == project.getRootProject() |
| 48 | + && project.getChildProjects().isEmpty(); |
| 49 | + if (onlyOneProjectInEntireBuild) { |
| 50 | + return false; |
| 51 | + } |
| 52 | + if (GradleVersion.current().compareTo(STRICT_CONFIG_ACCESS_WARNING) >= 0) { |
| 53 | + return true; |
| 54 | + } |
| 55 | + return false; |
| 56 | + } |
| 57 | + |
| 58 | + /** Models a request to the provisioner. */ |
| 59 | + private static class Request { |
| 60 | + final boolean withTransitives; |
| 61 | + final ImmutableList<String> mavenCoords; |
| 62 | + |
| 63 | + public Request(boolean withTransitives, Collection<String> mavenCoords) { |
| 64 | + this.withTransitives = withTransitives; |
| 65 | + this.mavenCoords = ImmutableList.copyOf(mavenCoords); |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public int hashCode() { |
| 70 | + return withTransitives ? mavenCoords.hashCode() : ~mavenCoords.hashCode(); |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public boolean equals(Object obj) { |
| 75 | + if (this == obj) { |
| 76 | + return true; |
| 77 | + } else if (obj instanceof Request) { |
| 78 | + Request o = (Request) obj; |
| 79 | + return o.withTransitives == withTransitives && o.mavenCoords.equals(mavenCoords); |
| 80 | + } else { |
| 81 | + return false; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public String toString() { |
| 87 | + String coords = mavenCoords.toString(); |
| 88 | + StringBuilder builder = new StringBuilder(); |
| 89 | + builder.append(coords.substring(1, coords.length() - 1)); // strip off [] |
| 90 | + if (withTransitives) { |
| 91 | + builder.append(" with transitives"); |
| 92 | + } else { |
| 93 | + builder.append(" no transitives"); |
| 94 | + } |
| 95 | + return builder.toString(); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + /** The provisioner used for all sub-projects. */ |
| 100 | + static class SubProvisioner implements Provisioner { |
| 101 | + private final RootProvisioner root; |
| 102 | + private final Project project; |
| 103 | + |
| 104 | + public SubProvisioner(RootProvisioner root, Project project) { |
| 105 | + this.root = Objects.requireNonNull(root); |
| 106 | + this.project = Objects.requireNonNull(project); |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public Set<File> provisionWithTransitives(boolean withTransitives, Collection<String> mavenCoordinates) { |
| 111 | + return root.provisionForSub(project, withTransitives, mavenCoordinates); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + /** The provisioner used for the root project. */ |
| 116 | + static class RootProvisioner implements Provisioner { |
| 117 | + private final Project rootProject; |
| 118 | + |
| 119 | + RootProvisioner(Project rootProject) { |
| 120 | + Preconditions.checkArgument(rootProject == rootProject.getRootProject()); |
| 121 | + this.rootProject = rootProject; |
| 122 | + } |
| 123 | + |
| 124 | + @Override |
| 125 | + public Set<File> provisionWithTransitives(boolean withTransitives, Collection<String> mavenCoordinates) { |
| 126 | + return doProvision(new Request(withTransitives, mavenCoordinates), true); |
| 127 | + } |
| 128 | + |
| 129 | + private Map<Request, Set<File>> cache = new HashMap<>(); |
| 130 | + |
| 131 | + /** Guaranteed to return non-null for internal requests, but might return null for an external request which isn't cached already. */ |
| 132 | + private synchronized @Nullable Set<File> doProvision(Request req, boolean isRoot) { |
| 133 | + Set<File> result = cache.get(req); |
| 134 | + if (result != null) { |
| 135 | + return result; |
| 136 | + } |
| 137 | + if (isRoot) { |
| 138 | + result = GradleProvisioner.fromRootBuildscript(rootProject).provisionWithTransitives(req.withTransitives, req.mavenCoords); |
| 139 | + cache.put(req, result); |
| 140 | + return result; |
| 141 | + } else { |
| 142 | + return null; |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + private Set<File> provisionForSub(Project project, boolean withTransitives, Collection<String> mavenCoordinates) { |
| 147 | + Request req = new Request(withTransitives, mavenCoordinates); |
| 148 | + Set<File> result = doProvision(req, false); |
| 149 | + if (result != null) { |
| 150 | + return result; |
| 151 | + } else { |
| 152 | + // if it wasn't cached, complain loudly and use the crappy workaround |
| 153 | + GradleProvisioner.logger.severe(warningMsg(req)); |
| 154 | + return GradleProvisioner.fromRootBuildscript(project).provisionWithTransitives(withTransitives, mavenCoordinates); |
| 155 | + } |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + private static String warningMsg(Request requestedDeps) { |
| 160 | + FormatterStep beingResolved = FormatterStep.lazyStepBeingResolvedInThisThread(); |
| 161 | + return String.format( |
| 162 | + "This subproject is using a formatter that was not used in the root project. To enable%n" + |
| 163 | + "performance optimzations (and avoid Gradle 7 deprecation warnings), you must declare%n" + |
| 164 | + "all of your formatters within the root project. For example, if your subproject has%n" + |
| 165 | + "a `java {}` block but your root project does not, just add a matching `java {}` block to%n" + |
| 166 | + "your root project. If you want to make it clear that it is intentional that the target%n" + |
| 167 | + "is empty, you can do this in your root build.gradle:%n" + |
| 168 | + "%n" + |
| 169 | + " spotless {%n" + |
| 170 | + " java {%n" + |
| 171 | + " targetEmptyForDeclaration()%n" + |
| 172 | + " [...same steps as subproject...]%n" + |
| 173 | + " }%n" + |
| 174 | + " }%n" + |
| 175 | + "%n" + |
| 176 | + "To help you figure out which block is missing, the step you are missing is%n" + |
| 177 | + " step name: %s%n" + |
| 178 | + " requested: %s%n", |
| 179 | + beingResolved == null ? "(unknown)" : beingResolved.getName(), |
| 180 | + requestedDeps); |
| 181 | + } |
| 182 | +} |
0 commit comments