|
| 1 | +/* |
| 2 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | + * you may not use this file except in compliance with the License. |
| 4 | + * You may obtain a copy of the License at |
| 5 | + * |
| 6 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * |
| 8 | + * Unless required by applicable law or agreed to in writing, software |
| 9 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + * See the License for the specific language governing permissions and |
| 12 | + * limitations under the License. |
| 13 | + */ |
| 14 | + |
| 15 | +package com.palantir.javaformat.gradle; |
| 16 | + |
| 17 | +import java.util.Map; |
| 18 | +import org.gradle.api.NamedDomainObjectProvider; |
| 19 | +import org.gradle.api.Project; |
| 20 | +import org.gradle.api.artifacts.Configuration; |
| 21 | +import org.gradle.api.artifacts.ModuleDependency; |
| 22 | +import org.gradle.api.attributes.AttributeContainer; |
| 23 | +import org.gradle.api.attributes.Bundling; |
| 24 | +import org.gradle.api.attributes.Category; |
| 25 | +import org.gradle.api.attributes.LibraryElements; |
| 26 | +import org.gradle.api.attributes.Usage; |
| 27 | +import org.gradle.api.attributes.java.TargetJvmEnvironment; |
| 28 | +import org.gradle.api.model.ObjectFactory; |
| 29 | + |
| 30 | +/** |
| 31 | + * The formatter's jars for the tasks of one project, resolved in that project. |
| 32 | + * |
| 33 | + * <p>The formatter is declared once, in the root project's palantirJavaFormat. A task of a subproject cannot resolve |
| 34 | + * that configuration itself: in a parallel build it runs without the root project's lock, and Gradle refuses. So the |
| 35 | + * root project offers palantirJavaFormat as a variant of its own, and every project that formats resolves it through a |
| 36 | + * dependency on the root project, which Gradle resolves in the project that asks. |
| 37 | + */ |
| 38 | +final class FormatterClasspath { |
| 39 | + |
| 40 | + /** Picks the root project's variant out of its others, such as runtimeElements when it has java too. */ |
| 41 | + private static final String CAPABILITY = "dev.openjavaformat.internal:formatter-classpath"; |
| 42 | + |
| 43 | + private static final String ELEMENTS = "openJavaFormatFormatterElements"; |
| 44 | + private static final String DEPENDENCIES = "openJavaFormatFormatter"; |
| 45 | + private static final String CLASSPATH = "openJavaFormatFormatterClasspath"; |
| 46 | + |
| 47 | + private FormatterClasspath() {} |
| 48 | + |
| 49 | + /** Offers the root project's palantirJavaFormat to the projects that format. */ |
| 50 | + static void offer(Project rootProject, Configuration palantirJavaFormat) { |
| 51 | + rootProject.getConfigurations().consumable(ELEMENTS, conf -> { |
| 52 | + conf.setDescription("Internal: the open-java-format implementation, for the projects that format"); |
| 53 | + conf.extendsFrom(palantirJavaFormat); |
| 54 | + javaRuntime(conf.getAttributes(), rootProject.getObjects()); |
| 55 | + conf.getOutgoing().capability(CAPABILITY + ":1"); |
| 56 | + }); |
| 57 | + } |
| 58 | + |
| 59 | + /** The formatter's jars for the tasks of this project. */ |
| 60 | + static NamedDomainObjectProvider<Configuration> of(Project project) { |
| 61 | + var configurations = project.getConfigurations(); |
| 62 | + if (!configurations.getNames().contains(CLASSPATH)) { |
| 63 | + var rootProject = (ModuleDependency) project.getDependencies().project(Map.of("path", ":")); |
| 64 | + rootProject.capabilities(capabilities -> capabilities.requireCapability(CAPABILITY)); |
| 65 | + var dependencies = configurations.dependencyScope(DEPENDENCIES, conf -> { |
| 66 | + conf.setDescription("Internal: the root project's open-java-format implementation"); |
| 67 | + conf.getDependencies().add(rootProject); |
| 68 | + }); |
| 69 | + configurations.resolvable(CLASSPATH, conf -> { |
| 70 | + conf.setDescription("Internal: resolves the open-java-format implementation for this project"); |
| 71 | + conf.extendsFrom(dependencies.get()); |
| 72 | + javaRuntime(conf.getAttributes(), project.getObjects()); |
| 73 | + }); |
| 74 | + } |
| 75 | + return configurations.named(CLASSPATH); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * What a Java library's runtime jars are asked for with. Without them Gradle cannot pick between the variants of a |
| 80 | + * library that has one for the JRE and one for Android, as Guava does, which the formatter depends on. |
| 81 | + */ |
| 82 | + static void javaRuntime(AttributeContainer attributes, ObjectFactory objects) { |
| 83 | + attributes.attribute(Usage.USAGE_ATTRIBUTE, objects.named(Usage.class, Usage.JAVA_RUNTIME)); |
| 84 | + attributes.attribute(Category.CATEGORY_ATTRIBUTE, objects.named(Category.class, Category.LIBRARY)); |
| 85 | + attributes.attribute( |
| 86 | + LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE, objects.named(LibraryElements.class, LibraryElements.JAR)); |
| 87 | + attributes.attribute(Bundling.BUNDLING_ATTRIBUTE, objects.named(Bundling.class, Bundling.EXTERNAL)); |
| 88 | + attributes.attribute( |
| 89 | + TargetJvmEnvironment.TARGET_JVM_ENVIRONMENT_ATTRIBUTE, |
| 90 | + objects.named(TargetJvmEnvironment.class, TargetJvmEnvironment.STANDARD_JVM)); |
| 91 | + } |
| 92 | +} |
0 commit comments