-
Notifications
You must be signed in to change notification settings - Fork 6
/
GTNHGradlePlugin.java
197 lines (176 loc) · 8.15 KB
/
GTNHGradlePlugin.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
* This Java source file was generated by the Gradle 'init' task.
*/
package com.gtnewhorizons.gtnhgradle;
import com.diffplug.blowdryer.Blowdryer;
import com.gtnewhorizons.retrofuturagradle.shadow.com.google.common.collect.ImmutableList;
import com.gtnewhorizons.retrofuturagradle.shadow.com.google.common.collect.ImmutableMap;
import com.gtnewhorizons.gtnhgradle.modules.AccessTransformerModule;
import com.gtnewhorizons.gtnhgradle.modules.CodeStyleModule;
import com.gtnewhorizons.gtnhgradle.modules.GitVersionModule;
import com.gtnewhorizons.gtnhgradle.modules.IdeIntegrationModule;
import com.gtnewhorizons.gtnhgradle.modules.MixinModule;
import com.gtnewhorizons.gtnhgradle.modules.ModernJavaModule;
import com.gtnewhorizons.gtnhgradle.modules.OldGradleEmulationModule;
import com.gtnewhorizons.gtnhgradle.modules.PublishingModule;
import com.gtnewhorizons.gtnhgradle.modules.ScalaModule;
import com.gtnewhorizons.gtnhgradle.modules.ShadowModule;
import com.gtnewhorizons.gtnhgradle.modules.StandardScriptsModules;
import com.gtnewhorizons.gtnhgradle.modules.StructureCheckModule;
import com.gtnewhorizons.gtnhgradle.modules.ToolchainModule;
import com.gtnewhorizons.gtnhgradle.modules.UpdaterModule;
import com.gtnewhorizons.gtnhgradle.modules.UtilityModule;
import com.gtnewhorizons.gtnhgradle.modules.WellKnownRepositoriesModule;
import com.gtnewhorizons.retrofuturagradle.UserDevPlugin;
import de.undercouch.gradle.tasks.download.DownloadTaskPlugin;
import org.ajoberstar.grgit.gradle.GrgitPlugin;
import org.gradle.api.Project;
import org.gradle.api.Plugin;
import org.gradle.api.file.ArchiveOperations;
import org.gradle.api.file.FileSystemOperations;
import org.gradle.api.file.ProjectLayout;
import org.gradle.api.logging.Logger;
import org.gradle.api.logging.Logging;
import org.gradle.api.model.ObjectFactory;
import org.gradle.api.plugins.ExtensionAware;
import org.gradle.api.plugins.JavaLibraryPlugin;
import org.gradle.api.plugins.PluginManager;
import org.gradle.api.provider.ProviderFactory;
import org.gradle.api.publish.maven.plugins.MavenPublishPlugin;
import org.gradle.process.ExecOperations;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import javax.inject.Inject;
import java.util.List;
import java.util.Map;
/**
* The main GTNH buildscript plugin object. You can access it in the buildscript as {@code gtnhGradle} and use it to
* activate specific modules manually. The GTNH project will use the GTNHConventionPlugin that automatically activates
* all modules.
*/
public class GTNHGradlePlugin implements Plugin<Project> {
/**
* Name of the project extension you can use to
*/
public static final String PROJECT_EXT_NAME = "gtnhGradle";
/**
* Ran by Gradle when the plugin is applied to the project.
* This applies the Java library and RetroFuturaGradle plugins, for the plugin to do anything else modules need to
* be opted-in using other functions in this class.
*/
public void apply(Project project) {
final PluginManager plugins = project.getPluginManager();
// Apply shared plugins used by all mods
plugins.apply(JavaLibraryPlugin.class);
plugins.apply(MavenPublishPlugin.class);
plugins.apply(GrgitPlugin.class);
plugins.apply(DownloadTaskPlugin.class);
plugins.apply(UserDevPlugin.class); // RFG
// Create the gtnhGradle extension as a Gradle DSL-extensible object
final GTNHExtension gtnh = project.getObjects()
.newInstance(GTNHExtension.class, project);
project.getExtensions()
.add(GTNHExtension.class, PROJECT_EXT_NAME, gtnh);
if (!gtnh.configuration.blowdryerTag.isEmpty()) {
// Make blowdryer available in "apply from:" scripts
project.getExtensions()
.add(Class.class, "Blowdryer", Blowdryer.class);
}
}
/**
* The object registered as {@code project.gtnhGradle}
*/
@SuppressWarnings("unused") // Forms the public API
public static abstract class GTNHExtension implements ExtensionAware {
/** Logging service used by the plugin */
public final @NotNull Logger logger;
/** Parsed properties associated with this project */
public @NotNull PropertiesConfiguration configuration;
/** A list of all available modules to activate */
@SuppressWarnings("unchecked") // too long, varargs can't handle generics
public static final List<Class<? extends GTNHModule>> ALL_MODULES = ImmutableList.of( //
GitVersionModule.class,
CodeStyleModule.class,
ToolchainModule.class,
ScalaModule.class,
StructureCheckModule.class,
AccessTransformerModule.class,
ShadowModule.class,
UtilityModule.class,
StandardScriptsModules.AddonScriptModule.class,
OldGradleEmulationModule.class,
WellKnownRepositoriesModule.class,
StandardScriptsModules.RepositoriesScriptModule.class,
MixinModule.class,
StandardScriptsModules.DependenciesScriptModule.class,
ModernJavaModule.class,
IdeIntegrationModule.class,
PublishingModule.class,
UpdaterModule.class,
StandardScriptsModules.LateAddonScriptModule.class
//
);
/** A map of all available modules, indexed by their class name */
public static final Map<String, Class<? extends GTNHModule>> MODULES_BY_NAME;
static {
final ImmutableMap.Builder<String, Class<? extends GTNHModule>> builder = ImmutableMap.builder();
for (final Class<? extends GTNHModule> m : ALL_MODULES) {
builder.put(m.getSimpleName(), m);
}
MODULES_BY_NAME = builder.build();
}
/**
* For internal use only.
*
* @param project passed in to avoid having to pass the project object to every apply function
*/
@Inject
@ApiStatus.Internal
public GTNHExtension(final Project project) {
logger = Logging.getLogger(GTNHGradlePlugin.class);
configuration = PropertiesConfiguration.GradleUtils.makePropertiesFrom(project);
}
/**
* Activates a module by its class name, if enabled in properties (e.g. {@code GitVersionModule}).
*
* @param project The project to activate the module on
* @param name Class name of the module, without the package name
* @throws IllegalArgumentException If the module was not found
*/
public void applyModuleByName(final Project project, final String name) {
final Class<? extends GTNHModule> klass = MODULES_BY_NAME.get(name);
if (klass == null) {
throw new IllegalArgumentException("Invalid module name " + name);
}
GTNHModule.applyIfEnabled(klass, this, project);
}
/**
* Activates all available and enabled modules on the given project.
*
* @param project The project to activate the modules on
*/
public void applyAllModules(final Project project) {
for (final Class<? extends GTNHModule> moduleClass : ALL_MODULES) {
GTNHModule.applyIfEnabled(moduleClass, this, project);
}
}
/** @return Gradle-provided injected service */
@Inject
public abstract @NotNull ObjectFactory getObjectFactory();
/** @return Gradle-provided injected service */
@Inject
public abstract @NotNull ProviderFactory getProviderFactory();
/** @return Gradle-provided injected service */
@Inject
public abstract @NotNull ProjectLayout getProjectLayout();
/** @return Gradle-provided injected service */
@Inject
public abstract @NotNull FileSystemOperations getFileSystemOperations();
/** @return Gradle-provided injected service */
@Inject
public abstract @NotNull ArchiveOperations getArchiveOperations();
/** @return Gradle-provided injected service */
@Inject
public abstract @NotNull ExecOperations getExecOperations();
}
}