Skip to content

Commit 01de86a

Browse files
committed
Execute Idea synchronization task in the current Gradle execution
1 parent 6da0f95 commit 01de86a

File tree

6 files changed

+169
-145
lines changed

6 files changed

+169
-145
lines changed

build.gradle.kts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ dependencies {
6767
compileOnly(workerShared.map { it.output })
6868
privateRuntime.name(workerClasspath.map { it.output })
6969
privateRuntime.name(workerShared.map { it.output })
70-
compileOnly(libs.ideaExtPlugin)
7170

7271
testImplementation(libs.mammoth.test) {
7372
exclude(group = "org.jetbrains", module = "annotations")

gradle/libs.versions.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ spotless = "8.0.0"
1313
stylecheck = "0.2.1"
1414

1515
[libraries]
16-
ideaExtPlugin = { module = "gradle.plugin.org.jetbrains.gradle.plugin.idea-ext:gradle-idea-ext", version.ref = "ideaExt" }
1716
mammoth = { module = "net.kyori:mammoth", version.ref = "mammoth" }
1817
pebble = { module = "io.pebbletemplates:pebble", version.ref = "pebble" }
1918
snakeyamlEngine = { module = "org.snakeyaml:snakeyaml-engine", version.ref = "snakeyaml" }

src/main/java/net/kyori/blossom/Blossom.java

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,16 @@
2323
import java.io.File;
2424
import net.kyori.blossom.internal.BlossomExtensionImpl;
2525
import net.kyori.blossom.internal.BuildParameters;
26-
import net.kyori.blossom.internal.IdeConfigurer;
2726
import net.kyori.blossom.internal.TemplateSetInternal;
27+
import net.kyori.blossom.internal.ide.EclipseIntegration;
28+
import net.kyori.blossom.internal.ide.IdeaIntegration;
2829
import net.kyori.mammoth.ProjectPlugin;
2930
import org.gradle.api.NamedDomainObjectProvider;
3031
import org.gradle.api.Project;
3132
import org.gradle.api.artifacts.Configuration;
3233
import org.gradle.api.artifacts.ConfigurationContainer;
3334
import org.gradle.api.artifacts.dsl.DependencyHandler;
3435
import org.gradle.api.file.Directory;
35-
import org.gradle.api.plugins.ExtensionAware;
3636
import org.gradle.api.plugins.ExtensionContainer;
3737
import org.gradle.api.plugins.JavaBasePlugin;
3838
import org.gradle.api.plugins.PluginContainer;
@@ -41,11 +41,7 @@
4141
import org.gradle.api.tasks.SourceSetContainer;
4242
import org.gradle.api.tasks.TaskContainer;
4343
import org.gradle.api.tasks.TaskProvider;
44-
import org.gradle.plugins.ide.eclipse.model.EclipseModel;
45-
import org.gradle.plugins.ide.idea.model.IdeaModel;
4644
import org.gradle.util.GradleVersion;
47-
import org.jetbrains.gradle.ext.ProjectSettings;
48-
import org.jetbrains.gradle.ext.TaskTriggersConfig;
4945
import org.jspecify.annotations.NullMarked;
5046
import org.jspecify.annotations.Nullable;
5147

@@ -120,23 +116,14 @@ private void registerGenerateAllTask(final Project project, final TaskContainer
120116
task.dependsOn(tasks.withType(GenerateTemplates.class));
121117
});
122118

123-
IdeConfigurer.apply(project, new IdeConfigurer.IdeImportAction() {
124-
@Override
125-
public void idea(final Project project, final IdeaModel idea, final ProjectSettings ideaExtension) {
126-
((ExtensionAware) ideaExtension).getExtensions().getByType(TaskTriggersConfig.class).afterSync(generateTemplates);
127-
project.afterEvaluate(p -> {
128-
final IdeaModel projectIdea = p.getExtensions().getByType(IdeaModel.class);
129-
if (projectIdea.getModule() != null) {
130-
projectIdea.getModule().getGeneratedSourceDirs().addAll(outputDirs.get());
131-
}
132-
});
133-
}
119+
IdeaIntegration.addSynchronizationTask(project, generateTemplates);
120+
EclipseIntegration.addSynchronizationTask(project, generateTemplates);
134121

135-
@Override
136-
public void eclipse(final Project project, final EclipseModel eclipse) {
137-
eclipse.synchronizationTasks(generateTemplates);
122+
IdeaIntegration.apply(project, idea -> project.afterEvaluate(p -> {
123+
if (idea.getModule() != null) {
124+
idea.getModule().getGeneratedSourceDirs().addAll(outputDirs.get());
138125
}
139-
});
126+
}));
140127
}
141128

142129
@Override

src/main/java/net/kyori/blossom/internal/IdeConfigurer.java

Lines changed: 0 additions & 122 deletions
This file was deleted.
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* This file is part of blossom, licensed under the GNU Lesser General Public License.
3+
*
4+
* Copyright (c) 2023 KyoriPowered
5+
*
6+
* This library is free software; you can redistribute it and/or
7+
* modify it under the terms of the GNU Lesser General Public
8+
* License as published by the Free Software Foundation; either
9+
* version 2.1 of the License, or (at your option) any later version.
10+
*
11+
* This library is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
* Lesser General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Lesser General Public
17+
* License along with this library; if not, write to the Free Software
18+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
19+
* USA
20+
*/
21+
package net.kyori.blossom.internal.ide;
22+
23+
import java.util.function.Consumer;
24+
import org.gradle.api.Project;
25+
import org.gradle.api.tasks.TaskProvider;
26+
import org.gradle.plugins.ide.eclipse.EclipsePlugin;
27+
import org.gradle.plugins.ide.eclipse.model.EclipseModel;
28+
29+
public final class EclipseIntegration {
30+
31+
private EclipseIntegration() {
32+
}
33+
34+
/**
35+
* Applies the specified configuration action to configure Eclipse projects.
36+
*
37+
* <p>This does not apply the Eclipse plugin, but will perform the action when the plugin is applied.</p>
38+
*
39+
* @param project project to apply to
40+
* @param action the action to perform
41+
*/
42+
public static void apply(final Project project, final Consumer<EclipseModel> action) {
43+
project.getPlugins().withType(EclipsePlugin.class, plugin -> {
44+
final EclipseModel model = project.getExtensions().findByType(EclipseModel.class);
45+
if (model == null) {
46+
return;
47+
}
48+
action.accept(model);
49+
});
50+
}
51+
52+
/**
53+
* Executes a task when Eclipse performs a project synchronization.
54+
*
55+
* @param project project of the task
56+
* @param task the task to perform on synchronization
57+
*/
58+
public static void addSynchronizationTask(final Project project, final TaskProvider<?> task) {
59+
EclipseIntegration.apply(project, (eclipseModel -> eclipseModel.synchronizationTasks(task)));
60+
}
61+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
* This file is part of blossom, licensed under the GNU Lesser General Public License.
3+
*
4+
* Copyright (c) 2023 KyoriPowered
5+
*
6+
* This library is free software; you can redistribute it and/or
7+
* modify it under the terms of the GNU Lesser General Public
8+
* License as published by the Free Software Foundation; either
9+
* version 2.1 of the License, or (at your option) any later version.
10+
*
11+
* This library is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
* Lesser General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Lesser General Public
17+
* License along with this library; if not, write to the Free Software
18+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
19+
* USA
20+
*/
21+
package net.kyori.blossom.internal.ide;
22+
23+
import java.util.ArrayList;
24+
import java.util.Collections;
25+
import java.util.List;
26+
import java.util.function.Consumer;
27+
import org.gradle.StartParameter;
28+
import org.gradle.TaskExecutionRequest;
29+
import org.gradle.api.Project;
30+
import org.gradle.api.tasks.TaskProvider;
31+
import org.gradle.internal.DefaultTaskExecutionRequest;
32+
import org.gradle.plugins.ide.idea.IdeaPlugin;
33+
import org.gradle.plugins.ide.idea.model.IdeaModel;
34+
35+
public final class IdeaIntegration {
36+
37+
private IdeaIntegration() {
38+
}
39+
40+
/**
41+
* Get whether Gradle is being invoked through IntelliJ IDEA.
42+
*
43+
* <p>This can be through a project import, or a task execution.</p>
44+
*
45+
* @return whether this is an IntelliJ-based invocation
46+
*/
47+
public static boolean isIdea() {
48+
return Boolean.getBoolean("idea.active");
49+
}
50+
51+
/**
52+
* Get whether Gradle is being invoked through IntelliJ IDEA project synchronization.
53+
*
54+
* @return whether this is an IntelliJ-based synchronization
55+
*/
56+
public static boolean isIdeaSync() {
57+
return Boolean.getBoolean("idea.sync.active");
58+
}
59+
60+
/**
61+
* Applies the specified configuration action to configure Idea projects.
62+
*
63+
* <p>This does not apply the Idea plugin, but will perform the action when the plugin is applied.</p>
64+
*
65+
* @param project project to apply to
66+
* @param action the action to perform
67+
*/
68+
public static void apply(final Project project, final Consumer<IdeaModel> action) {
69+
project.getPlugins().withType(IdeaPlugin.class, plugin -> {
70+
if (!IdeaIntegration.isIdea()) {
71+
return;
72+
}
73+
final IdeaModel model = project.getExtensions().findByType(IdeaModel.class);
74+
if (model == null || model.getProject() == null) {
75+
return;
76+
}
77+
action.accept(model);
78+
});
79+
}
80+
81+
/**
82+
* Executes a task when Idea performs a project synchronization.
83+
*
84+
* @param project project of the task
85+
* @param task the task to perform on synchronization
86+
*/
87+
public static void addSynchronizationTask(final Project project, final TaskProvider<?> task) {
88+
if (!IdeaIntegration.isIdeaSync()) {
89+
return;
90+
}
91+
92+
project.afterEvaluate(p -> {
93+
final StartParameter startParameter = project.getGradle().getStartParameter();
94+
final List<TaskExecutionRequest> taskRequests = new ArrayList<>(startParameter.getTaskRequests());
95+
96+
taskRequests.add(new DefaultTaskExecutionRequest(Collections.singletonList(":" + project.getName() + ":" + task.getName())));
97+
startParameter.setTaskRequests(taskRequests);
98+
});
99+
}
100+
}

0 commit comments

Comments
 (0)