forked from neoforged/ModDevGradle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRunModel.java
More file actions
280 lines (237 loc) · 8.57 KB
/
RunModel.java
File metadata and controls
280 lines (237 loc) · 8.57 KB
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package net.neoforged.moddevgradle.dsl;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.regex.Pattern;
import javax.inject.Inject;
import net.neoforged.moddevgradle.internal.utils.ExtensionUtils;
import net.neoforged.moddevgradle.internal.utils.StringUtils;
import org.gradle.api.GradleException;
import org.gradle.api.Named;
import org.gradle.api.Project;
import org.gradle.api.Task;
import org.gradle.api.artifacts.Configuration;
import org.gradle.api.artifacts.dsl.Dependencies;
import org.gradle.api.file.DirectoryProperty;
import org.gradle.api.file.RegularFileProperty;
import org.gradle.api.provider.ListProperty;
import org.gradle.api.provider.MapProperty;
import org.gradle.api.provider.Property;
import org.gradle.api.provider.SetProperty;
import org.gradle.api.tasks.SourceSet;
import org.gradle.api.tasks.TaskProvider;
import org.slf4j.event.Level;
/**
* Model of a run. Each run will generate a {@code runXxx} gradle task and optionally
* (enabled by default) a corresponding IDE run.
*/
public abstract class RunModel implements Named, Dependencies {
private static final Pattern VALID_RUN_NAME = Pattern.compile("[a-zA-Z][\\w-]*");
private final String name;
private final Configuration configuration;
/**
* The Gradle tasks that should be run before running this run.
*/
private List<TaskProvider<?>> tasksBefore = new ArrayList<>();
@Inject
public RunModel(String name, Project project, Iterable<ModModel> defaultMods) {
this.name = name;
if (!VALID_RUN_NAME.matcher(name).matches()) {
throw new GradleException("Run name '" + name + "' is invalid! It must match " + VALID_RUN_NAME.pattern());
}
getLoadedMods().convention(defaultMods);
getGameDirectory().convention(project.getLayout().getProjectDirectory().dir("run"));
configuration = project.getConfigurations().create(InternalModelHelper.nameOfRun(this, "", "additionalRuntimeClasspath"), configuration -> {
configuration.setCanBeResolved(false);
configuration.setCanBeConsumed(false);
});
getLogLevel().convention(Level.INFO);
getDevLogin().convention(false);
// Build a nicer name for the IDE run configuration
boolean isSubProject = project.getRootProject() != project;
var ideName = StringUtils.capitalize(name);
if (isSubProject) {
ideName = project.getName() + " - " + ideName;
}
getIdeName().convention(ideName);
getIdeFolderName().convention("");
getSourceSet().convention(ExtensionUtils.getSourceSets(project).getByName(SourceSet.MAIN_SOURCE_SET_NAME));
}
@Override
public String getName() {
return name;
}
/**
* Name for the run configuration in the IDE.
* If this is set to {@code ""}, no config will be generated.
*/
public abstract Property<String> getIdeName();
/**
* Name for the folder the run configuration is contained by in the IDE.
* If this is set to {@code ""}, no folder will be used.
*/
public abstract Property<String> getIdeFolderName();
/**
* Directory that the game will run in. Defaults to {@code run/}.
*/
public abstract DirectoryProperty getGameDirectory();
/**
* Additional environment variables.
*/
public abstract MapProperty<String, String> getEnvironment();
/**
* Shorthand to set a single environment variable.
*/
public void environment(String key, String value) {
getEnvironment().put(key, value);
}
/**
* Additional system properties to add to the JVM arguments.
*/
public abstract MapProperty<String, String> getSystemProperties();
/**
* Shorthand to set a single system property.
*/
public void systemProperty(String key, String value) {
getSystemProperties().put(key, value);
}
/**
* Allows overriding the main class for this run.
*/
public abstract Property<String> getMainClass();
/**
* Additional program arguments to add to the run configuration.
*/
public abstract ListProperty<String> getProgramArguments();
/**
* Shorthand to add a single program argument.
*/
public void programArgument(String arg) {
getProgramArguments().add(arg);
}
/**
* Additional JVM arguments to be added to the run configuration.
*/
public abstract ListProperty<String> getJvmArguments();
/**
* Shorthand to add a single JVM argument.
*/
public void jvmArgument(String arg) {
getJvmArguments().add(arg);
}
/**
* The mods for this run. Defaults to all mods registered in the project.
*
* @see ModModel
*/
public abstract SetProperty<ModModel> getLoadedMods();
/**
* Sets the run configuration type from NeoForge that should be used.
*/
public abstract Property<String> getType();
/**
* Equivalent to setting {@code type = "client"}.
*/
public void client() {
getType().set("client");
}
/**
* Equivalent to setting {@code type = "clientData"}.
*
* <p>Should only be used for Minecraft versions starting from 1.21.4.
* (The first snapshot that supports this is 24w45a).
*/
public void clientData() {
getType().set("clientData");
}
/**
* Equivalent to setting {@code type = "data"}.
*
* <p>Should only be used for Minecraft versions up to 1.21.3 included.
* (The last snapshot that supports this is 24w44a).
*/
public void data() {
getType().set("data");
}
/**
* Equivalent to setting {@code type = "server"}.
*/
public void server() {
getType().set("server");
}
/**
* Equivalent to setting {@code type = "serverData"}.
*
* <p>Should only be used for Minecraft versions starting from 1.21.4.
* (The first snapshot that supports this is 24w45a).
*/
public void serverData() {
getType().set("serverData");
}
/**
* Equivalent to setting {@code ideName = ""}
*/
public void disableIdeRun() {
getIdeName().set("");
}
/**
* Gets the Gradle tasks that should be run before running this run.
*/
public List<TaskProvider<?>> getTasksBefore() {
return tasksBefore;
}
/**
* Sets the Gradle tasks that should be run before running this run.
* This also slows down running through your IDE since it will first execute Gradle to run the requested
* tasks, and then run the actual game.
*/
public void setTasksBefore(List<TaskProvider<?>> taskNames) {
this.tasksBefore = new ArrayList<>(Objects.requireNonNull(taskNames, "taskNames"));
}
/**
* Configures the given Task to be run before launching the game.
* This also slows down running through your IDE since it will first execute Gradle to run the requested
* tasks, and then run the actual game.
*/
public void taskBefore(TaskProvider<?> task) {
this.tasksBefore.add(task);
}
/**
* Configures the given Task to be run before launching the game.
* This also slows down running through your IDE since it will first execute Gradle to run the requested
* tasks, and then run the actual game.
*/
public void taskBefore(Task task) {
this.tasksBefore.add(task.getProject().getTasks().named(task.getName()));
}
public Configuration getAdditionalRuntimeClasspathConfiguration() {
return configuration;
}
/**
* Changes the games log-level.
*
* <p>Note that this property is ignored if {@link #getLoggingConfigFile()} is set.
*/
public abstract Property<Level> getLogLevel();
/**
* Overrides the {@code log4j2.xml} configuration file.
* If unset, MDG will use a default configuration file with the chosen {@link #getLogLevel() log level}.
*/
public abstract RegularFileProperty getLoggingConfigFile();
/**
* Sets the source set to be used as the main classpath of this run.
* Defaults to the {@code main} source set.
* Eclipse does not support having multiple different classpaths per project beyond a separate unit-testing
* classpath.
*/
public abstract Property<SourceSet> getSourceSet();
/**
* Enables <a href="https://github.com/covers1624/DevLogin">DevLogin</a> which is used to log into an
* official Minecraft account in development environments.
*/
public abstract Property<Boolean> getDevLogin();
@Override
public String toString() {
return "Run[" + getName() + "]";
}
}