|
| 1 | +/* |
| 2 | + * (c) Copyright 2019 Palantir Technologies Inc. All rights reserved. |
| 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 | + |
| 17 | +package com.palantir.javaformat.gradle; |
| 18 | + |
| 19 | +import com.google.common.base.Preconditions; |
| 20 | +import com.google.common.collect.ImmutableMap; |
| 21 | +import java.io.File; |
| 22 | +import java.net.URI; |
| 23 | +import java.util.List; |
| 24 | +import java.util.stream.Collectors; |
| 25 | +import org.gradle.api.Plugin; |
| 26 | +import org.gradle.api.Project; |
| 27 | +import org.gradle.api.artifacts.Configuration; |
| 28 | +import org.gradle.api.plugins.ExtensionAware; |
| 29 | +import org.gradle.plugins.ide.idea.model.IdeaModel; |
| 30 | +import org.jetbrains.gradle.ext.TaskTriggersConfig; |
| 31 | + |
| 32 | +public class JavaFormatPlugin implements Plugin<Project> { |
| 33 | + |
| 34 | + private static final String EXTENSION_NAME = "palantirJavaFormat"; |
| 35 | + |
| 36 | + @Override |
| 37 | + public void apply(Project project) { |
| 38 | + Preconditions.checkState( |
| 39 | + project == project.getRootProject(), "May only apply com.palantir.java-format to the root project"); |
| 40 | + |
| 41 | + JavaFormatExtension extension = |
| 42 | + project.getExtensions().create(EXTENSION_NAME, JavaFormatExtension.class, project); |
| 43 | + |
| 44 | + Configuration implConfiguration = project.getConfigurations().create("palantirJavaFormat", conf -> { |
| 45 | + conf.setDescription("Internal configuration for resolving the palantirJavaFormat implementation"); |
| 46 | + conf.setVisible(false); |
| 47 | + conf.setCanBeConsumed(false); |
| 48 | + // Using addLater instead of afterEvaluate, in order to delay reading the extension until after the user |
| 49 | + // has configured it. |
| 50 | + conf.defaultDependencies(deps -> deps.addLater(project.provider(() -> { |
| 51 | + String version = extension.getImplementationVersion().get(); |
| 52 | + |
| 53 | + return project.getDependencies().create(ImmutableMap.of( |
| 54 | + "group", "com.palantir.javaformat", |
| 55 | + "name", "palantir-java-format", |
| 56 | + "version", version)); |
| 57 | + }))); |
| 58 | + }); |
| 59 | + |
| 60 | + project.getPluginManager().withPlugin("idea", ideaPlugin -> { |
| 61 | + configureLegacyIdea(project, implConfiguration); |
| 62 | + configureIntelliJImport(project, implConfiguration); |
| 63 | + }); |
| 64 | + } |
| 65 | + |
| 66 | + private static void configureLegacyIdea(Project project, Configuration implConfiguration) { |
| 67 | + IdeaModel ideaModel = project.getExtensions().getByType(IdeaModel.class); |
| 68 | + ideaModel.getProject().getIpr().withXml(xmlProvider -> { |
| 69 | + // this block is lazy |
| 70 | + List<URI> uris = implConfiguration.getFiles().stream().map(File::toURI).collect(Collectors.toList()); |
| 71 | + ConfigureJavaFormatterXml.configure(xmlProvider.asNode(), uris); |
| 72 | + }); |
| 73 | + } |
| 74 | + |
| 75 | + private static void configureIntelliJImport(Project project, Configuration implConfiguration) { |
| 76 | + project.getPluginManager().apply("org.jetbrains.gradle.plugin.idea-ext"); |
| 77 | + |
| 78 | + ConfigurePalantirJavaFormatXml fixPalantirJavaFormatXmlTask = project.getTasks() |
| 79 | + .create("fixPalantirJavaFormatXml", ConfigurePalantirJavaFormatXml.class, task -> { |
| 80 | + task.getImplConfiguration().set(implConfiguration); |
| 81 | + }); |
| 82 | + |
| 83 | + ExtensionAware ideaProject = (ExtensionAware) project.getExtensions().getByType(IdeaModel.class).getProject(); |
| 84 | + ExtensionAware settings = (ExtensionAware) ideaProject.getExtensions().getByName("settings"); |
| 85 | + TaskTriggersConfig taskTriggers = settings.getExtensions().getByType(TaskTriggersConfig.class); |
| 86 | + taskTriggers.afterSync(fixPalantirJavaFormatXmlTask); |
| 87 | + } |
| 88 | +} |
0 commit comments