|
18 | 18 |
|
19 | 19 | import com.google.common.base.Preconditions; |
20 | 20 | import com.google.common.collect.ImmutableList; |
21 | | -import com.palantir.gradle.ideaconfiguration.IdeaConfigurationExtension; |
22 | | -import com.palantir.gradle.ideaconfiguration.IdeaConfigurationPlugin; |
23 | 21 | import java.nio.file.Paths; |
24 | | -import java.util.List; |
25 | 22 | import java.util.Optional; |
26 | 23 | import java.util.stream.Stream; |
27 | 24 | import javax.inject.Inject; |
@@ -82,23 +79,34 @@ public void apply(Project rootProject) { |
82 | 79 | task.getOutputFile().set(rootProject.file(".idea/workspace.xml")); |
83 | 80 | }); |
84 | 81 |
|
85 | | - // Add the task to the Gradle start parameters so it executes automatically. |
86 | | - StartParameter startParameter = rootProject.getGradle().getStartParameter(); |
87 | | - List<String> updateTasks = Stream.of(updateOpenJavaFormatXml, updateWorkspaceXml) |
88 | | - .map(taskProvider -> String.format(":%s", taskProvider.getName())) |
89 | | - .toList(); |
90 | | - List<String> taskNames = ImmutableList.<String>builder() |
91 | | - .addAll(startParameter.getTaskNames()) |
92 | | - .addAll(updateTasks) |
93 | | - .build(); |
94 | | - startParameter.setTaskNames(taskNames); |
| 82 | + runWithEveryBuild(rootProject, updateOpenJavaFormatXml, updateWorkspaceXml); |
95 | 83 | }); |
96 | 84 |
|
97 | | - rootProject.getPluginManager().apply(IdeaConfigurationPlugin.class); |
98 | | - IdeaConfigurationExtension extension = rootProject.getExtensions().getByType(IdeaConfigurationExtension.class); |
99 | | - extension |
100 | | - .getExternalDependencies() |
101 | | - .register("open-java-format", dep -> dep.atLeastVersion(MIN_IDEA_PLUGIN_VERSION)); |
| 85 | + // IntelliJ's Gradle sync sets idea.active. The project then lists the open-java-format IDEA plugin as |
| 86 | + // required, and IntelliJ offers to install it. |
| 87 | + if (rootProject |
| 88 | + .getProviders() |
| 89 | + .systemProperty("idea.active") |
| 90 | + .map(Boolean::parseBoolean) |
| 91 | + .getOrElse(false)) { |
| 92 | + TaskProvider<UpdateExternalDependenciesXmlFile> updateExternalDependenciesXml = rootProject |
| 93 | + .getTasks() |
| 94 | + .register("updateExternalDependenciesXml", UpdateExternalDependenciesXmlFile.class, task -> { |
| 95 | + task.getPluginId().set("open-java-format"); |
| 96 | + task.getMinVersion().set(MIN_IDEA_PLUGIN_VERSION); |
| 97 | + task.getOutputFile().set(rootProject.file(".idea/externalDependencies.xml")); |
| 98 | + }); |
| 99 | + runWithEveryBuild(rootProject, updateExternalDependenciesXml); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + // Adds the tasks to the Gradle start parameters, so they run with whatever the build was asked to do. |
| 104 | + private static void runWithEveryBuild(Project rootProject, TaskProvider<?>... tasks) { |
| 105 | + StartParameter startParameter = rootProject.getGradle().getStartParameter(); |
| 106 | + startParameter.setTaskNames(ImmutableList.<String>builder() |
| 107 | + .addAll(startParameter.getTaskNames()) |
| 108 | + .addAll(Stream.of(tasks).map(task -> ":" + task.getName()).toList()) |
| 109 | + .build()); |
102 | 110 | } |
103 | 111 |
|
104 | 112 | private Optional<Configuration> maybeGetNativeImplConfiguration() { |
|
0 commit comments