Skip to content

Commit fd4a1c5

Browse files
committed
check syntax command
1 parent 16525a8 commit fd4a1c5

File tree

6 files changed

+125
-46
lines changed

6 files changed

+125
-46
lines changed

src/main/java/com/cleanroommc/groovyscript/GroovyScript.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -243,14 +243,19 @@ public static File makeFile(File parent, String... pieces) {
243243
return new File(parent, fileJoiner.join(pieces));
244244
}
245245

246-
public static void postScriptRunResult(EntityPlayerMP player, boolean startup) {
246+
public static void postScriptRunResult(EntityPlayerMP player, boolean startup, boolean running) {
247247
List<String> errors = GroovyLogImpl.LOG.collectErrors();
248248
if (errors.isEmpty()) {
249249
if (!startup) {
250-
player.sendMessage(new TextComponentString(TextFormatting.GREEN + "Successfully ran scripts"));
250+
if (running) {
251+
player.sendMessage(new TextComponentString(TextFormatting.GREEN + "Successfully ran scripts"));
252+
} else {
253+
player.sendMessage(new TextComponentString(TextFormatting.GREEN + "No syntax errors found :)"));
254+
}
251255
}
252256
} else {
253-
player.sendMessage(new TextComponentString(TextFormatting.RED + "Found " + errors.size() + " errors while executing scripts"));
257+
String executing = running ? "running" : "checking";
258+
player.sendMessage(new TextComponentString(TextFormatting.RED + "Found " + errors.size() + " errors while " + executing + " scripts"));
254259
int n = errors.size();
255260
if (errors.size() >= 10) {
256261
player.sendMessage(new TextComponentString("Displaying the first 7 errors:"));

src/main/java/com/cleanroommc/groovyscript/command/GSCommand.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public static void runReload(EntityPlayerMP player, MinecraftServer server) {
5959
GroovyScript.getSandbox().run(LoadStage.POST_INIT);
6060
time = System.currentTimeMillis() - time;
6161
player.sendMessage(new TextComponentString("Reloading Groovy took " + time + "ms"));
62-
GroovyScript.postScriptRunResult(player, false);
62+
GroovyScript.postScriptRunResult(player, false, true);
6363

6464
NetworkHandler.sendToPlayer(new SReloadJei(), player);
6565
}
@@ -77,6 +77,17 @@ public GSCommand() {
7777
}
7878
}));
7979

80+
addSubcommand(new SimpleCommand("check", (server, sender, args) -> {
81+
if (sender instanceof EntityPlayerMP) {
82+
sender.sendMessage(new TextComponentString("Checking groovy syntax..."));
83+
long time = System.currentTimeMillis();
84+
GroovyScript.getSandbox().checkSyntax();
85+
time = System.currentTimeMillis() - time;
86+
sender.sendMessage(new TextComponentString("Checking syntax took " + time + "ms"));
87+
GroovyScript.postScriptRunResult((EntityPlayerMP) sender, false, false);
88+
}
89+
}));
90+
8091
addSubcommand(new SimpleCommand("hand", (server, sender, args) -> {
8192
if (sender instanceof EntityPlayer) {
8293
EntityPlayer player = (EntityPlayer) sender;

src/main/java/com/cleanroommc/groovyscript/event/EventHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public static void registerModels(ModelRegistryEvent event) {
4848
@SubscribeEvent
4949
public static void playerLogin(PlayerEvent.PlayerLoggedInEvent event) {
5050
if (event.player instanceof EntityPlayerMP) {
51-
GroovyScript.postScriptRunResult((EntityPlayerMP) event.player, true);
51+
GroovyScript.postScriptRunResult((EntityPlayerMP) event.player, true, true);
5252
}
5353
NBTTagCompound tag = event.player.getEntityData();
5454
NBTTagCompound data = new NBTTagCompound();

src/main/java/com/cleanroommc/groovyscript/sandbox/GroovySandbox.java

Lines changed: 50 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ protected void stopRunning() {
7373
currentSandbox.set(null);
7474
}
7575

76-
public void run() throws Exception {
76+
public void load(boolean run, boolean loadClasses) throws Exception {
7777
currentSandbox.set(this);
7878
preRun();
7979

@@ -85,49 +85,65 @@ public void run() throws Exception {
8585
postInitBindings(binding);
8686
Set<File> executedClasses = new ObjectOpenHashSet<>();
8787

88-
running.set(true);
88+
running.set(run);
8989
try {
90-
// load and run any configured class files
91-
for (File classFile : getClassFiles()) {
92-
Class<?> clazz = loadScriptClass(engine, classFile, false);
90+
if (loadClasses) {
91+
// load and run any configured class files
92+
loadClassScripts(engine, binding, executedClasses, run);
93+
}
94+
// now run all script files
95+
loadScripts(engine, binding, executedClasses, run);
96+
} finally {
97+
running.set(false);
98+
postRun();
99+
currentSandbox.set(null);
100+
setCurrentScript(null);
101+
}
102+
}
103+
104+
protected void loadScripts(GroovyScriptEngine engine, Binding binding, Set<File> executedClasses, boolean run) {
105+
for (File scriptFile : getScriptFiles()) {
106+
if (!executedClasses.contains(scriptFile)) {
107+
Class<?> clazz = loadScriptClass(engine, scriptFile, true);
93108
if (clazz == null) {
94-
// loading script fails if the file is a script that depends on a class file that isn't loaded yet
95-
// we cant determine if the file is a script or a class
109+
GroovyLog.get().errorMC("Error loading script for {}", scriptFile.getPath());
110+
GroovyLog.get().errorMC("Did you forget to register your class file in your run config?");
96111
continue;
97112
}
98-
// the superclass of class files is Object
99-
if (clazz.getSuperclass() != Script.class && shouldRunFile(classFile)) {
100-
executedClasses.add(classFile);
101-
setCurrentScript(classFile.toString());
102-
InvokerHelper.createScript(clazz, binding).run();
103-
setCurrentScript(null);
113+
if (clazz.getSuperclass() != Script.class) {
114+
GroovyLog.get().errorMC("Class file '{}' should be defined in the runConfig in the classes property!", scriptFile);
115+
continue;
104116
}
105-
}
106-
// now run all script files
107-
for (File scriptFile : getScriptFiles()) {
108-
if (!executedClasses.contains(scriptFile)) {
109-
Class<?> clazz = loadScriptClass(engine, scriptFile, true);
110-
if (clazz == null) {
111-
GroovyLog.get().errorMC("Error loading script for {}", scriptFile.getPath());
112-
GroovyLog.get().errorMC("Did you forget to register your class file in your run config?");
113-
continue;
114-
}
115-
if (clazz.getSuperclass() != Script.class) {
116-
GroovyLog.get().errorMC("Class file '{}' should be defined in the runConfig in the classes property!", scriptFile);
117-
continue;
118-
}
119-
if (shouldRunFile(scriptFile)) {
117+
if (shouldRunFile(scriptFile)) {
118+
Script script = InvokerHelper.createScript(clazz, binding);
119+
if (run) {
120120
setCurrentScript(scriptFile.toString());
121-
InvokerHelper.createScript(clazz, binding).run();
121+
script.run();
122122
setCurrentScript(null);
123123
}
124124
}
125125
}
126-
} finally {
127-
running.set(false);
128-
postRun();
129-
currentSandbox.set(null);
130-
setCurrentScript(null);
126+
}
127+
}
128+
129+
protected void loadClassScripts(GroovyScriptEngine engine, Binding binding, Set<File> executedClasses, boolean run) {
130+
for (File classFile : getClassFiles()) {
131+
Class<?> clazz = loadScriptClass(engine, classFile, false);
132+
if (clazz == null) {
133+
// loading script fails if the file is a script that depends on a class file that isn't loaded yet
134+
// we cant determine if the file is a script or a class
135+
continue;
136+
}
137+
// the superclass of class files is Object
138+
if (clazz.getSuperclass() != Script.class && shouldRunFile(classFile)) {
139+
executedClasses.add(classFile);
140+
Script script = InvokerHelper.createScript(clazz, binding);
141+
if (run) {
142+
setCurrentScript(script.toString());
143+
script.run();
144+
setCurrentScript(null);
145+
}
146+
}
131147
}
132148
}
133149

src/main/java/com/cleanroommc/groovyscript/sandbox/GroovyScriptSandbox.java

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@
2424
import java.io.File;
2525
import java.io.IOException;
2626
import java.net.URL;
27+
import java.util.ArrayList;
2728
import java.util.Collection;
29+
import java.util.List;
2830
import java.util.Objects;
2931

3032
public class GroovyScriptSandbox extends GroovySandbox {
@@ -61,6 +63,7 @@ public class GroovyScriptSandbox extends GroovySandbox {
6163
};
6264

6365
private LoadStage currentLoadStage;
66+
private boolean checkSyntaxMode = false;
6467

6568
public GroovyScriptSandbox(URL... scriptEnvironment) {
6669
super(scriptEnvironment);
@@ -71,23 +74,42 @@ public GroovyScriptSandbox(URL... scriptEnvironment) {
7174
registerBinding("event_manager", GroovyEventManager.INSTANCE);
7275
}
7376

77+
public void checkSyntax() {
78+
load(LoadStage.PRE_INIT, false, true);
79+
for (LoadStage loadStage : LoadStage.getLoadStages()) {
80+
if (loadStage != LoadStage.PRE_INIT) {
81+
load(loadStage, false, false);
82+
}
83+
}
84+
}
85+
86+
public void checkSyntax(LoadStage loadStage) {
87+
load(loadStage, false, true);
88+
}
89+
7490
public void run(LoadStage currentLoadStage) {
91+
load(currentLoadStage, true, true);
92+
}
93+
94+
public void load(LoadStage currentLoadStage, boolean run, boolean loadClasses) {
95+
this.checkSyntaxMode = !run;
7596
this.currentLoadStage = Objects.requireNonNull(currentLoadStage);
7697
try {
77-
super.run();
98+
super.load(run, loadClasses);
7899
} catch (IOException | ScriptException | ResourceException e) {
79100
GroovyLog.get().errorMC("An Exception occurred trying to run groovy!");
80101
GroovyScript.LOGGER.throwing(e);
81102
} catch (Exception e) {
82103
GroovyLog.get().exception(e);
83104
} finally {
84105
this.currentLoadStage = null;
106+
this.checkSyntaxMode = false;
85107
}
86108
}
87109

88110
@ApiStatus.Internal
89111
@Override
90-
public void run() throws Exception {
112+
public void load(boolean run, boolean loadClasses) throws Exception {
91113
throw new UnsupportedOperationException("Use run(Loader loader) instead!");
92114
}
93115

@@ -123,7 +145,11 @@ protected void initEngine(GroovyScriptEngine engine, CompilerConfiguration confi
123145

124146
@Override
125147
protected void preRun() {
126-
GroovyLog.get().info("Running scripts in loader '{}'", this.currentLoadStage);
148+
if (this.checkSyntaxMode) {
149+
GroovyLog.get().info("Checking syntax in loader '{}'", this.currentLoadStage);
150+
} else {
151+
GroovyLog.get().info("Running scripts in loader '{}'", this.currentLoadStage);
152+
}
127153
MinecraftForge.EVENT_BUS.post(new ScriptRunEvent.Pre());
128154
if (this.currentLoadStage.isReloadable() && !ReloadableRegistryManager.isFirstLoad()) {
129155
ReloadableRegistryManager.onReload();
@@ -134,7 +160,9 @@ protected void preRun() {
134160

135161
@Override
136162
protected boolean shouldRunFile(File file) {
137-
GroovyLog.get().info(" - executing {}", file.toString());
163+
if (!this.checkSyntaxMode) {
164+
GroovyLog.get().info(" - executing {}", file.toString());
165+
}
138166
return true;
139167
}
140168

src/main/java/com/cleanroommc/groovyscript/sandbox/LoadStage.java

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,31 @@
11
package com.cleanroommc.groovyscript.sandbox;
22

3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.Comparator;
6+
import java.util.List;
7+
38
public class LoadStage {
49

5-
public static final LoadStage PRE_INIT = new LoadStage("preInit", false);
6-
public static final LoadStage POST_INIT = new LoadStage("postInit", true);
10+
private static final List<LoadStage> STAGES = new ArrayList<>();
11+
12+
public static List<LoadStage> getLoadStages() {
13+
return Collections.unmodifiableList(STAGES);
14+
}
15+
16+
public static final LoadStage PRE_INIT = new LoadStage("preInit", false, -1000000);
17+
public static final LoadStage POST_INIT = new LoadStage("postInit", true, 0);
718

819
private final String name;
920
private final boolean reloadable;
21+
private final int priority;
1022

11-
public LoadStage(String name, boolean reloadable) {
23+
public LoadStage(String name, boolean reloadable, int priority) {
1224
this.name = name;
1325
this.reloadable = reloadable;
26+
this.priority = priority;
27+
STAGES.add(this);
28+
STAGES.sort(Comparator.comparingInt(LoadStage::getPriority));
1429
}
1530

1631
public String getName() {
@@ -21,6 +36,10 @@ public boolean isReloadable() {
2136
return reloadable;
2237
}
2338

39+
public int getPriority() {
40+
return priority;
41+
}
42+
2443
@Override
2544
public String toString() {
2645
return name;

0 commit comments

Comments
 (0)