Skip to content

Commit 887bfe6

Browse files
committed
Execute Idea synchronization task in the current Gradle execution
1 parent 9072350 commit 887bfe6

File tree

4 files changed

+131
-99
lines changed

4 files changed

+131
-99
lines changed

subprojects/gradle-plugin/src/main/java/org/spongepowered/gradle/vanilla/VanillaGradle.java

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -31,28 +31,22 @@
3131
import org.gradle.api.Project;
3232
import org.gradle.api.artifacts.Configuration;
3333
import org.gradle.api.initialization.Settings;
34-
import org.gradle.api.plugins.ExtensionAware;
3534
import org.gradle.api.plugins.JavaPlugin;
3635
import org.gradle.api.provider.Provider;
37-
import org.gradle.api.tasks.SourceSet;
38-
import org.gradle.api.tasks.SourceSetContainer;
3936
import org.gradle.api.tasks.TaskContainer;
4037
import org.gradle.api.tasks.TaskProvider;
4138
import org.gradle.plugins.ide.eclipse.EclipsePlugin;
42-
import org.gradle.plugins.ide.eclipse.model.EclipseModel;
43-
import org.gradle.plugins.ide.idea.model.IdeaModel;
4439
import org.jetbrains.gradle.ext.IdeaExtPlugin;
45-
import org.jetbrains.gradle.ext.ProjectSettings;
46-
import org.jetbrains.gradle.ext.TaskTriggersConfig;
4740
import org.spongepowered.gradle.vanilla.internal.Constants;
4841
import org.spongepowered.gradle.vanilla.internal.MinecraftExtensionImpl;
4942
import org.spongepowered.gradle.vanilla.internal.ProvideMinecraftPlugin;
5043
import org.spongepowered.gradle.vanilla.internal.ResolveMinecraftLibNames;
5144
import org.spongepowered.gradle.vanilla.internal.ShadowConfigurationApplier;
45+
import org.spongepowered.gradle.vanilla.internal.ide.EclipseIntegration;
46+
import org.spongepowered.gradle.vanilla.internal.ide.IdeaIntegration;
5247
import org.spongepowered.gradle.vanilla.internal.repository.MinecraftProviderService;
5348
import org.spongepowered.gradle.vanilla.internal.repository.MinecraftRepositoryPlugin;
5449
import org.spongepowered.gradle.vanilla.internal.util.ConfigurationUtils;
55-
import org.spongepowered.gradle.vanilla.internal.util.IdeConfigurer;
5650
import org.spongepowered.gradle.vanilla.internal.util.SelfPreferringClassLoader;
5751
import org.spongepowered.gradle.vanilla.task.DisplayMinecraftVersionsTask;
5852
import org.spongepowered.gradle.vanilla.task.DumpClassTask;
@@ -64,8 +58,10 @@
6458
import java.util.Arrays;
6559
import java.util.Collections;
6660
import java.util.List;
61+
import java.util.Optional;
6762
import java.util.Set;
6863
import java.util.concurrent.atomic.AtomicBoolean;
64+
import java.util.function.Supplier;
6965
import java.util.stream.Stream;
7066

7167
/**
@@ -168,26 +164,9 @@ private void configureIDEIntegrations(
168164
project.getPlugins().apply(IdeaExtPlugin.class);
169165
project.getPlugins().apply(EclipsePlugin.class);
170166

171-
IdeConfigurer.apply(project, new IdeConfigurer.IdeImportAction() {
172-
@Override
173-
public void idea(final Project project, final IdeaModel idea, final ProjectSettings ideaExtension) {
174-
// Navigate via the extension properties...
175-
// https://github.com/JetBrains/gradle-idea-ext-plugin/wiki
176-
final TaskTriggersConfig taskTriggers = ((ExtensionAware) ideaExtension).getExtensions().getByType(TaskTriggersConfig.class);
177-
178-
// Automatically prepare a workspace after importing
179-
if (shouldRunPrepare.get()) {
180-
taskTriggers.afterSync(prepareWorkspaceTask);
181-
}
182-
}
183-
184-
@Override
185-
public void eclipse(final Project project, final EclipseModel eclipse) {
186-
if (shouldRunPrepare.get()) {
187-
eclipse.synchronizationTasks(prepareWorkspaceTask);
188-
}
189-
}
190-
});
167+
final Supplier<Optional<TaskProvider<?>>> supplier = () -> shouldRunPrepare.get() ? Optional.of(prepareWorkspaceTask) : Optional.empty();
168+
IdeaIntegration.addSynchronizationTask(project, supplier);
169+
EclipseIntegration.addSynchronizationTask(project, supplier);
191170
}
192171

193172
private static void applyShadowConfiguration(final TaskContainer tasks, final Provider<Set<String>> minecraftNames, final Plugin<?> shadowPlugin) {

subprojects/gradle-plugin/src/main/java/org/spongepowered/gradle/vanilla/internal/ProvideMinecraftPlugin.java

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -56,18 +56,16 @@
5656
import org.gradle.api.tasks.TaskProvider;
5757
import org.gradle.jvm.toolchain.JavaLanguageVersion;
5858
import org.gradle.jvm.toolchain.JavaToolchainService;
59-
import org.gradle.plugins.ide.eclipse.model.EclipseModel;
60-
import org.gradle.plugins.ide.idea.model.IdeaModel;
6159
import org.jetbrains.gradle.ext.Application;
62-
import org.jetbrains.gradle.ext.ProjectSettings;
6360
import org.jetbrains.gradle.ext.RunConfigurationContainer;
6461
import org.spongepowered.gradle.vanilla.MinecraftExtension;
62+
import org.spongepowered.gradle.vanilla.internal.ide.EclipseIntegration;
63+
import org.spongepowered.gradle.vanilla.internal.ide.IdeaIntegration;
6564
import org.spongepowered.gradle.vanilla.internal.model.Library;
6665
import org.spongepowered.gradle.vanilla.internal.model.rule.OperatingSystemRule;
6766
import org.spongepowered.gradle.vanilla.internal.model.rule.RuleContext;
6867
import org.spongepowered.gradle.vanilla.internal.repository.MinecraftProviderService;
6968
import org.spongepowered.gradle.vanilla.internal.repository.MinecraftRepositoryPlugin;
70-
import org.spongepowered.gradle.vanilla.internal.util.IdeConfigurer;
7169
import org.spongepowered.gradle.vanilla.internal.util.StringUtils;
7270
import org.spongepowered.gradle.vanilla.repository.MinecraftPlatform;
7371
import org.spongepowered.gradle.vanilla.repository.MinecraftRepositoryExtension;
@@ -79,6 +77,7 @@
7977
import java.io.File;
8078
import java.util.Iterator;
8179
import java.util.Objects;
80+
import java.util.Optional;
8281

8382
/**
8483
* A plugin that creates the necessary tasks and configurations to provide the
@@ -331,33 +330,26 @@ private void configureIDEIntegrations(
331330
final Project project,
332331
final MinecraftExtensionImpl extension
333332
) {
334-
IdeConfigurer.apply(project, new IdeConfigurer.IdeImportAction() {
335-
@Override
336-
public void idea(final Project project, final IdeaModel idea, final ProjectSettings ideaExtension) {
337-
final RunConfigurationContainer runConfigurations =
333+
IdeaIntegration.apply(project, (idea, ideaExtension) -> {
334+
final RunConfigurationContainer runConfigurations =
338335
(RunConfigurationContainer) ((ExtensionAware) ideaExtension).getExtensions().getByName("runConfigurations");
339336

340-
extension.getRuns().all(run -> {
341-
final String displayName = run.getDisplayName().getOrNull();
342-
runConfigurations.create(displayName == null ? run.getName() + " (" + project.getName() + ")" : displayName, Application.class, ideaRun -> {
343-
ideaRun.setMainClass(run.getMainClass().get());
344-
final File runDirectory = run.getWorkingDirectory().get().getAsFile();
345-
ideaRun.setWorkingDirectory(runDirectory.getAbsolutePath());
346-
runDirectory.mkdirs();
347-
348-
ideaRun.moduleRef(project, run.getIdeaRunSourceSet().orElse(run.getSourceSet()).get());
349-
ideaRun.setJvmArgs(StringUtils.join(run.getAllJvmArgumentProviders(), true));
350-
ideaRun.setProgramParameters(StringUtils.join(run.getAllArgumentProviders(), true));
351-
ideaRun.setEnvs(run.getActualEnvironment());
352-
});
337+
extension.getRuns().all(run -> {
338+
final String displayName = run.getDisplayName().getOrNull();
339+
runConfigurations.create(displayName == null ? run.getName() + " (" + project.getName() + ")" : displayName, Application.class, ideaRun -> {
340+
ideaRun.setMainClass(run.getMainClass().get());
341+
final File runDirectory = run.getWorkingDirectory().get().getAsFile();
342+
ideaRun.setWorkingDirectory(runDirectory.getAbsolutePath());
343+
runDirectory.mkdirs();
344+
345+
ideaRun.moduleRef(project, run.getIdeaRunSourceSet().orElse(run.getSourceSet()).get());
346+
ideaRun.setJvmArgs(StringUtils.join(run.getAllJvmArgumentProviders(), true));
347+
ideaRun.setProgramParameters(StringUtils.join(run.getAllArgumentProviders(), true));
348+
ideaRun.setEnvs(run.getActualEnvironment());
353349
});
354-
}
355-
356-
@Override
357-
public void eclipse(final Project project, final EclipseModel eclipse) {
358-
eclipse.synchronizationTasks(project.getTasks().named(Constants.Tasks.GEN_ECLIPSE_RUNS));
359-
}
350+
});
360351
});
352+
EclipseIntegration.addSynchronizationTask(project, () -> Optional.of(project.getTasks().named(Constants.Tasks.GEN_ECLIPSE_RUNS)));
361353
}
362354

363355
private void createRunTasks(final MinecraftExtension extension, final TaskContainer tasks, final JavaToolchainService service) {
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* This file is part of VanillaGradle, licensed under the MIT License (MIT).
3+
*
4+
* Copyright (c) SpongePowered <https://www.spongepowered.org>
5+
* Copyright (c) contributors
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*/
25+
package org.spongepowered.gradle.vanilla.internal.ide;
26+
27+
import org.gradle.api.Project;
28+
import org.gradle.api.tasks.TaskProvider;
29+
import org.gradle.plugins.ide.eclipse.EclipsePlugin;
30+
import org.gradle.plugins.ide.eclipse.model.EclipseModel;
31+
32+
import java.util.Optional;
33+
import java.util.function.Consumer;
34+
import java.util.function.Supplier;
35+
36+
public final class EclipseIntegration {
37+
38+
/**
39+
* Applies the specified configuration action to configure Eclipse projects.
40+
*
41+
* <p>This does not apply the Eclipse plugin, but will perform the action when the plugin is applied.</p>
42+
*
43+
* @param project project to apply to
44+
* @param action the action to perform
45+
*/
46+
public static void apply(final Project project, final Consumer<EclipseModel> action) {
47+
project.getPlugins().withType(EclipsePlugin.class, plugin -> {
48+
final EclipseModel model = project.getExtensions().findByType(EclipseModel.class);
49+
if (model == null) {
50+
return;
51+
}
52+
action.accept(model);
53+
});
54+
}
55+
56+
/**
57+
* Executes a task when Eclipse performs a project synchronization.
58+
*
59+
* @param project project of the task
60+
* @param supplier supplier that may provide a task
61+
*/
62+
public static void addSynchronizationTask(final Project project, final Supplier<Optional<TaskProvider<?>>> supplier) {
63+
EclipseIntegration.apply(project, (eclipseModel -> supplier.get().ifPresent(eclipseModel::synchronizationTasks)));
64+
}
65+
}

subprojects/gradle-plugin/src/main/java/org/spongepowered/gradle/vanilla/internal/util/IdeConfigurer.java renamed to subprojects/gradle-plugin/src/main/java/org/spongepowered/gradle/vanilla/internal/ide/IdeaIntegration.java

Lines changed: 40 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,26 @@
2222
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2323
* THE SOFTWARE.
2424
*/
25-
package org.spongepowered.gradle.vanilla.internal.util;
25+
package org.spongepowered.gradle.vanilla.internal.ide;
2626

27+
import org.gradle.StartParameter;
28+
import org.gradle.TaskExecutionRequest;
2729
import org.gradle.api.Project;
2830
import org.gradle.api.plugins.ExtensionAware;
29-
import org.gradle.plugins.ide.eclipse.EclipsePlugin;
30-
import org.gradle.plugins.ide.eclipse.model.EclipseModel;
31+
import org.gradle.api.tasks.TaskProvider;
32+
import org.gradle.internal.DefaultTaskExecutionRequest;
3133
import org.gradle.plugins.ide.idea.model.IdeaModel;
3234
import org.jetbrains.gradle.ext.IdeaExtPlugin;
3335
import org.jetbrains.gradle.ext.ProjectSettings;
3436

35-
/**
36-
* Configures different IDEs when applicable
37-
*/
38-
public final class IdeConfigurer {
37+
import java.util.ArrayList;
38+
import java.util.Collections;
39+
import java.util.List;
40+
import java.util.Optional;
41+
import java.util.function.BiConsumer;
42+
import java.util.function.Supplier;
43+
44+
public final class IdeaIntegration {
3945

4046
/**
4147
* Get whether Gradle is being invoked through IntelliJ IDEA.
@@ -44,31 +50,30 @@ public final class IdeConfigurer {
4450
*
4551
* @return whether this is an IntelliJ-based invocation
4652
*/
47-
public static boolean isIdeaImport() {
53+
public static boolean isIdea() {
4854
return Boolean.getBoolean("idea.active");
4955
}
5056

5157
/**
52-
* Get whether this Gradle invocation is from an Eclipse project import.
58+
* Get whether Gradle is being invoked through IntelliJ IDEA project synchronization.
5359
*
54-
* @return whether an eclipse import is ongoing
60+
* @return whether this is an IntelliJ-based synchronization
5561
*/
56-
public static boolean isEclipseImport() {
57-
return System.getProperty("eclipse.application") != null;
62+
public static boolean isIdeaSync() {
63+
return Boolean.getBoolean("idea.sync.active");
5864
}
5965

6066
/**
61-
* Applies the specified configuration action to configure IDE projects.
67+
* Applies the specified configuration action to configure Idea projects.
6268
*
63-
* <p>This does not apply the IDEs' respective plugins, but will perform
64-
* actions when those plugins are applied.</p>
69+
* <p>This does not apply the Idea plugin, but will perform the action when the plugin is applied.</p>
6570
*
6671
* @param project project to apply to
67-
* @param toPerform the actions to perform
72+
* @param action the action to perform
6873
*/
69-
public static void apply(final Project project, final IdeImportAction toPerform) {
74+
public static void apply(final Project project, final BiConsumer<IdeaModel, ProjectSettings> action) {
7075
project.getPlugins().withType(IdeaExtPlugin.class, plugin -> {
71-
if (!IdeConfigurer.isIdeaImport()) {
76+
if (!IdeaIntegration.isIdea()) {
7277
return;
7378
}
7479

@@ -84,36 +89,27 @@ public static void apply(final Project project, final IdeImportAction toPerform)
8489
final ProjectSettings ideaExt = ((ExtensionAware) model.getProject()).getExtensions().getByType(ProjectSettings.class);
8590

8691
// But actually perform the configuration with the subproject context
87-
toPerform.idea(project, model, ideaExt);
88-
});
89-
project.getPlugins().withType(EclipsePlugin.class, plugin -> {
90-
final EclipseModel model = project.getExtensions().findByType(EclipseModel.class);
91-
if (model == null) {
92-
return;
93-
}
94-
toPerform.eclipse(project, model);
92+
action.accept(model, ideaExt);
9593
});
9694
}
9795

98-
public interface IdeImportAction {
99-
100-
/**
101-
* Configure an IntelliJ project.
102-
*
103-
* @param project the project to configure on import
104-
* @param idea the basic idea gradle extension
105-
* @param ideaExtension JetBrain's extensions to the base idea model
106-
*/
107-
void idea(final Project project, final IdeaModel idea, final ProjectSettings ideaExtension);
96+
/**
97+
* Executes a task when Idea performs a project synchronization.
98+
*
99+
* @param project project of the task
100+
* @param supplier supplier that may provide a task
101+
*/
102+
public static void addSynchronizationTask(final Project project, final Supplier<Optional<TaskProvider<?>>> supplier) {
103+
if (!IdeaIntegration.isIdeaSync()) {
104+
return;
105+
}
108106

109-
/**
110-
* Configure an eclipse project.
111-
*
112-
* @param project the project being imported
113-
* @param eclipse the eclipse project model to modify
114-
*/
115-
void eclipse(final Project project, final EclipseModel eclipse);
107+
project.afterEvaluate(p -> supplier.get().ifPresent(task -> {
108+
final StartParameter startParameter = project.getGradle().getStartParameter();
109+
final List<TaskExecutionRequest> taskRequests = new ArrayList<>(startParameter.getTaskRequests());
116110

111+
taskRequests.add(new DefaultTaskExecutionRequest(Collections.singletonList(":" + project.getName() + ":" + task.getName())));
112+
startParameter.setTaskRequests(taskRequests);
113+
}));
117114
}
118-
119115
}

0 commit comments

Comments
 (0)