/
RunConfig.java
637 lines (485 loc) · 17.9 KB
/
RunConfig.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
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
/*
* Copyright (c) Forge Development LLC and contributors
* SPDX-License-Identifier: LGPL-2.1-only
*/
package net.minecraftforge.gradle.common.util;
import org.gradle.api.NamedDomainObjectContainer;
import org.gradle.api.Project;
import org.gradle.api.plugins.JavaPluginExtension;
import org.gradle.api.tasks.SourceSet;
import groovy.lang.Closure;
import groovy.lang.GroovyObjectSupport;
import groovy.util.MapEntry;
import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.annotation.Nullable;
public class RunConfig extends GroovyObjectSupport implements Serializable {
public static final String RUNS_GROUP = "ForgeGradle runs";
private static final String MCP_CLIENT_MAIN = "mcp.client.Start";
private static final String MC_CLIENT_MAIN = "net.minecraft.client.main.Main";
private static final long serialVersionUID = 1L;
private transient final Project project;
private transient NamedDomainObjectContainer<ModConfig> mods;
private final String name;
private Boolean singleInstance = null;
private String taskName, main, ideaModule, workDir;
private List<SourceSet> sources;
private List<RunConfig> parents, children;
private List<String> args, jvmArgs;
private boolean forceExit = true;
private Boolean client; // so we can have it null
private Boolean inheritArgs;
private Boolean inheritJvmArgs;
private boolean buildAllProjects;
private Map<String, String> env, props, tokens;
private Map<String, Supplier<String>> lazyTokens;
public RunConfig(final Project project, final String name) {
this.project = project;
this.name = name;
this.mods = project.container(ModConfig.class, modName -> new ModConfig(project, modName));
}
public final String getName() {
return name;
}
public void setTaskName(String taskName) {
this.taskName = taskName;
}
public void taskName(String taskName) {
setTaskName(taskName);
}
public final String getTaskName() {
if (taskName == null) {
taskName = getName().replaceAll("[^a-zA-Z0-9\\-_]", "");
if (!taskName.startsWith("run")) {
taskName = "run" + Utils.capitalize(taskName);
}
}
return taskName;
}
public final String getUniqueFileName() {
return project.getPath().length() > 1 ? String.join("_", String.join("_", project.getPath().substring(1).split(":")), getTaskName()) : getTaskName();
}
public final String getUniqueName() {
return getUniqueFileName().replaceAll("_", " ");
}
public void environment(Map<String, Object> map) {
this.setEnvironment(map);
}
public void setEnvironment(Map<String, Object> map) {
map.forEach((k, v) -> getEnvironment().put(k, v instanceof File ? ((File) v).getAbsolutePath() : (String) v));
}
public void environment(String key, String value) {
getEnvironment().put(key, value);
}
public void environment(String key, File value) {
getEnvironment().put(key, value.getAbsolutePath());
}
public Map<String, String> getEnvironment() {
if (env == null) {
env = new HashMap<>();
}
return env;
}
public void main(String value) {
this.setMain(value);
}
public void setMain(String value) {
this.main = value;
}
public String getMain() {
return this.main;
}
public void inheritArgs(boolean value) {
setInheritArgs(value);
}
public void setInheritArgs(boolean value) {
this.inheritArgs = value;
}
public boolean getInheritArgs() {
// Both null and true mean inherit the args
return inheritArgs != Boolean.FALSE;
}
public void inheritJvmArgs(boolean value) {
setInheritJvmArgs(value);
}
public void setInheritJvmArgs(boolean value) {
this.inheritJvmArgs = value;
}
public boolean getInheritJvmArgs() {
// Both null and true mean inherit the args
return inheritJvmArgs != Boolean.FALSE;
}
public void buildAllProjects(boolean value) {
setBuildAllProjects(value);
}
public void setBuildAllProjects(boolean value) {
this.buildAllProjects = value;
}
public boolean getBuildAllProjects() {
return buildAllProjects;
}
public void args(List<Object> values) {
setArgs(values);
}
public void args(Object... values) {
args(Arrays.asList(values));
}
public void arg(Object value) {
args(value);
}
public void setArgs(List<Object> values) {
values.forEach(value -> getArgs().add(value instanceof File ? ((File) value).getAbsolutePath() : Objects.toString(value)));
}
public List<String> getArgs() {
if (args == null) {
args = new ArrayList<>();
}
return args;
}
public void jvmArgs(List<String> values) {
setJvmArgs(values);
}
public void jvmArgs(String... values) {
jvmArgs(Arrays.asList(values));
}
public void jvmArg(String value) {
jvmArgs(value);
}
public void setJvmArgs(List<String> values) {
getJvmArgs().addAll(values);
}
public List<String> getJvmArgs() {
if (jvmArgs == null) {
jvmArgs = new ArrayList<>();
}
return jvmArgs;
}
public void singleInstance(boolean value) {
this.setSingleInstance(value);
}
public void setSingleInstance(boolean singleInstance) {
this.singleInstance = singleInstance;
}
public boolean isSingleInstance() {
return singleInstance != null && singleInstance;
}
public void properties(Map<String, Object> map) {
this.setProperties(map);
}
public void setProperties(Map<String, Object> map) {
map.forEach((k, v) -> getProperties().put(k, v instanceof File ? ((File) v).getAbsolutePath() : (String) v));
}
public void property(String key, String value) {
getProperties().put(key, value);
}
public void property(String key, File value) {
getProperties().put(key, value.getAbsolutePath());
}
public Map<String, String> getProperties() {
if (props == null) {
props = new HashMap<>();
}
return props;
}
public void ideaModule(String value) {
this.setIdeaModule(value);
}
public void setIdeaModule(String value) {
this.ideaModule = value;
}
public final String getIdeaModule() {
if (ideaModule == null) {
ideaModule = project.getName().replace(' ', '_') + ".main";
}
return ideaModule;
}
public void workingDirectory(String value) {
setWorkingDirectory(value);
}
public void workingDirectory(File value) {
setWorkingDirectory(value.getAbsolutePath());
}
public void setWorkingDirectory(String value) {
this.workDir = value;
}
public String getWorkingDirectory() {
if (workDir == null) {
workDir = project.file("run").getAbsolutePath();
}
return workDir;
}
public void forceExit(boolean forceExit) {
this.setForceExit(forceExit);
}
public void setForceExit(boolean forceExit) {
this.forceExit = forceExit;
}
public boolean getForceExit() {
return this.forceExit;
}
public NamedDomainObjectContainer<ModConfig> mods(@SuppressWarnings("rawtypes") Closure closure) {
return mods.configure(closure);
}
public NamedDomainObjectContainer<ModConfig> getMods() {
return mods;
}
public void setSources(List<SourceSet> sources) {
this.sources = sources;
}
public void sources(final List<SourceSet> sources) {
getSources().addAll(sources);
}
public void sources(final SourceSet... sources) {
sources(Arrays.asList(sources));
}
public void source(final SourceSet source) {
sources(source);
}
public List<SourceSet> getSources() {
if (sources == null) {
sources = new ArrayList<>();
}
return sources;
}
private Stream<RunConfig> entriesToRuns(MapEntry... entries) {
return Stream.of(entries).map(entry -> {
final Project project = entry.getKey() == null
? this.project : this.project.project(entry.getKey().toString());
final MinecraftExtension minecraft = project.getExtensions().findByType(MinecraftExtension.class);
return minecraft == null ? null : minecraft.getRuns().maybeCreate(entry.getValue().toString());
}).filter(Objects::nonNull);
}
public void setParents(List<RunConfig> parents) {
this.parents = parents;
}
public void setParents(MapEntry... parents) {
setParents(entriesToRuns(parents).collect(Collectors.toList()));
}
public void setParents(Map<String, String> parents) {
setParents(parents.entrySet().stream()
.map((entry) -> new MapEntry(entry.getKey(), entry.getValue()))
.toArray(MapEntry[]::new));
}
public void parents(MapEntry... parents) {
getParents().addAll(entriesToRuns(parents).collect(Collectors.toList()));
}
public void parents(Map<String, String> parents) {
parents(parents.entrySet().stream()
.map((entry) -> new MapEntry(entry.getKey(), entry.getValue()))
.toArray(MapEntry[]::new));
}
public void parent(@Nullable String project, @Nullable String parent) {
parents(new MapEntry(project, parent));
}
public void parents(int index, MapEntry... parents) {
getParents().addAll(index, entriesToRuns(parents).collect(Collectors.toList()));
}
public void parents(int index, Map<String, String> parents) {
parents(index, parents.entrySet().stream()
.map((entry) -> new MapEntry(entry.getKey(), entry.getValue()))
.toArray(MapEntry[]::new));
}
public void parent(int index, @Nullable String project, @Nullable String parent) {
parents(index, new MapEntry(project, parent));
}
public void parents(RunConfig... parents) {
getParents().addAll(Arrays.asList(parents));
}
public void parent(RunConfig parent) {
parents(parent);
}
public void parents(int index, RunConfig... parents) {
getParents().addAll(Math.min(index, getParents().size()), Arrays.asList(parents));
}
public void parent(int index, RunConfig parent) {
parents(index, parent);
}
public final List<RunConfig> getParents() {
if (parents == null) {
parents = new ArrayList<>();
}
return parents;
}
public void setChildren(List<RunConfig> children) {
this.children = children;
}
public void setChildren(MapEntry... children) {
setChildren(entriesToRuns(children).collect(Collectors.toList()));
}
public void setChildren(Map<String, String> children) {
setChildren(children.entrySet().stream()
.map((entry) -> new MapEntry(entry.getKey(), entry.getValue()))
.toArray(MapEntry[]::new));
}
public void children(MapEntry... children) {
getChildren().addAll(entriesToRuns(children).collect(Collectors.toList()));
}
public void children(Map<String, String> children) {
children(children.entrySet().stream()
.map((entry) -> new MapEntry(entry.getKey(), entry.getValue()))
.toArray(MapEntry[]::new));
}
public void child(@Nullable String project, @Nullable String child) {
children(new MapEntry(project, child));
}
public void children(int index, MapEntry... children) {
getChildren().addAll(index, entriesToRuns(children).collect(Collectors.toList()));
}
public void children(int index, Map<String, String> children) {
children(index, children.entrySet().stream()
.map((entry) -> new MapEntry(entry.getKey(), entry.getValue()))
.toArray(MapEntry[]::new));
}
public void child(int index, @Nullable String project, @Nullable String child) {
children(index, new MapEntry(project, child));
}
public void children(RunConfig... children) {
getChildren().addAll(Arrays.asList(children));
}
public void child(RunConfig child) {
children(child);
}
public void children(int index, RunConfig... children) {
getChildren().addAll(Math.min(index, getParents().size()), Arrays.asList(children));
}
public void child(int index, RunConfig child) {
children(index, child);
}
public final List<RunConfig> getChildren() {
if (children == null) {
children = new ArrayList<>();
}
return children;
}
public void merge(final RunConfig other, boolean overwrite) {
RunConfig first = overwrite ? other : this;
RunConfig second = overwrite ? this : other;
List<String> args = new ArrayList<>();
if (this.getInheritArgs() && other.args != null)
args.addAll(other.args);
if (this.args != null)
args.addAll(this.args);
this.args = args;
List<String> jvmArgs = new ArrayList<>();
if (this.getInheritJvmArgs() && other.jvmArgs != null)
jvmArgs.addAll(other.jvmArgs);
if (this.jvmArgs != null)
jvmArgs.addAll(this.jvmArgs);
this.jvmArgs = jvmArgs;
main = first.main == null ? second.main : first.main;
mods = first.mods == null ? second.mods : first.mods;
sources = first.sources == null ? second.sources : first.sources;
workDir = first.workDir == null ? second.workDir : first.workDir;
ideaModule = first.ideaModule == null ? second.ideaModule : first.ideaModule;
singleInstance = first.singleInstance == null ? second.singleInstance : first.singleInstance;
this.client = first.client == null ? second.client : first.client;
if (overwrite) {
this.buildAllProjects = other.buildAllProjects;
}
if (other.env != null) {
other.env.forEach(overwrite
? (key, value) -> getEnvironment().put(key, value)
: (key, value) -> getEnvironment().putIfAbsent(key, value));
}
if (other.props != null) {
other.props.forEach(overwrite
? (key, value) -> getProperties().put(key, value)
: (key, value) -> getProperties().putIfAbsent(key, value));
}
if (other.mods != null) {
other.mods.forEach(otherMod -> {
final ModConfig thisMod = getMods().findByName(otherMod.getName());
if (thisMod == null) {
getMods().add(otherMod);
} else {
thisMod.merge(otherMod, false);
}
});
}
}
public void merge(List<RunConfig> runs) {
runs.stream().distinct().filter(run -> run != this).forEach(run -> merge(run, false));
}
public void mergeParents() {
merge(getParents());
}
public void mergeChildren() {
merge(getChildren());
}
public void setTokens(Map<String, String> tokens) {
this.tokens = new HashMap<>(tokens);
}
public void token(String key, String value) {
getTokens().put(key, value);
}
public void tokens(Map<String, String> tokens) {
getTokens().putAll(tokens);
}
public Map<String, String> getTokens() {
if (tokens == null) {
tokens = new HashMap<>();
}
return tokens;
}
public void setLazyTokens(Map<String, Supplier<String>> lazyTokens) {
this.lazyTokens = new HashMap<>(lazyTokens);
}
public void lazyToken(String key, Supplier<String> valueSupplier) {
getLazyTokens().put(key, valueSupplier);
}
public void lazyToken(String key, Closure<String> closure) {
lazyToken(key, closure::call);
}
public void lazyTokens(Map<String, Supplier<String>> lazyTokens) {
getLazyTokens().putAll(lazyTokens);
}
public Map<String, Supplier<String>> getLazyTokens() {
if (lazyTokens == null) {
lazyTokens = new HashMap<>();
}
return lazyTokens;
}
public String replace(Map<String, ?> vars, String value) {
if (value.length() <= 2 || value.indexOf('{') == -1) {
return value;
}
return Utils.replaceTokens(vars, value);
}
public void client(boolean value) {
setClient(value);
}
public void setClient(boolean value) {
this.client = value;
}
public boolean isClient() {
if (client == null) {
boolean isTargetClient = getEnvironment().getOrDefault("target", "").contains("client") || getName().contains("client");
client = isTargetClient || MCP_CLIENT_MAIN.equals(getMain()) || MC_CLIENT_MAIN.equals(getMain());
}
return client;
}
public List<SourceSet> getAllSources() {
List<SourceSet> sources = getSources();
getMods().stream().map(ModConfig::getSources).flatMap(Collection::stream).forEach(sources::add);
sources = sources.stream().distinct().collect(Collectors.toList());
if (sources.isEmpty()) {
final JavaPluginExtension main = project.getExtensions().getByType(JavaPluginExtension.class);
sources.add(main.getSourceSets().getByName(SourceSet.MAIN_SOURCE_SET_NAME));
}
return sources;
}
@Override
public String toString() {
return "RunConfig[project='" + project.getPath() + "', name='" + getName() + "']";
}
}