• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

wurstscript / WurstScript / 210

26 Oct 2023 10:10PM UTC coverage: 62.609% (-1.1%) from 63.756%
210

push

circleci

web-flow
Jass And Wurst Formatter (#620)

Support for formatting .wurst and .j files.

Thanks @karlek 

Co-authored-by: Frotty <Frotty@users.noreply.github.com>
Co-authored-by: Peter Zeller <Peter.peq@googlemail.com>

17297 of 27627 relevant lines covered (62.61%)

0.63 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

68.0
de.peeeq.wurstscript/src/main/java/de/peeeq/wurstscript/RunArgs.java
1
package de.peeeq.wurstscript;
2

3
import com.google.common.collect.Lists;
4
import org.eclipse.jdt.annotation.Nullable;
5

6
import java.io.File;
7
import java.util.Collections;
8
import java.util.List;
9
import java.util.Set;
10
import java.util.function.Consumer;
11
import java.util.stream.Stream;
12

13
public class RunArgs {
14

15

16
    private final String[] args;
17
    private final RunOption optionLua;
18
    private final RunOption optionCompiletimeCache;
19
    private List<String> files = Lists.newArrayList();
1✔
20
    private @Nullable String mapFile = null;
1✔
21
    private @Nullable String outFile = null;
1✔
22
    private @Nullable String workspaceroot = null;
1✔
23
    private @Nullable String inputmap = null;
1✔
24
    private @Nullable int testTimeout = 20;
1✔
25
    private List<RunOption> options = Lists.newArrayList();
1✔
26
    private List<File> libDirs = Lists.newArrayList();
1✔
27
    private RunOption optionHelp;
28
    private RunOption optionOpt;
29
    private RunOption optionInline;
30
    private RunOption optionLocalOptimizations;
31
    private RunOption optionRuntests;
32
    private RunOption optionGui;
33
    private RunOption optionAbout;
34
    private RunOption optionHotdoc;
35
    private RunOption optionShowErrors;
36
    private RunOption optionRunCompileTimeFunctions;
37
    private RunOption optionStacktraces;
38
    private RunOption uncheckedDispatch;
39
    private RunOption optionNodebug;
40
    private RunOption optionInjectCompiletimeObjects;
41
    private RunOption optionExtractImports;
42
    private RunOption optionStartServer;
43
    private RunOption optionLanguageServer;
44
    private RunOption optionNoExtractMapScript;
45
    private RunOption optionFixInstall;
46
    private RunOption optionCopyMap;
47
    private RunOption optionDisablePjass;
48
    private RunOption optionShowVersion;
49
    private RunOption optionPrettyPrint;
50
    private RunOption optionMeasureTimes;
51
    private RunOption optionHotStartmap;
52
    private RunOption optionHotReload;
53
    private RunOption optionTestTimeout;
54
    private int functionSplitLimit = 10000;
1✔
55

56
    private RunOption optionBuild;
57

58
    public RunArgs with(String... additionalArgs) {
59
        return new RunArgs(Stream.concat(Stream.of(args), Stream.of(additionalArgs))
1✔
60
                .toArray(String[]::new));
1✔
61
    }
62

63
    private static class RunOption {
64

65
        final String name;
66
        final String descr;
67
        final @Nullable Consumer<String> argHandler;
68
        boolean isSet;
69
        RunOption(String name, String descr) {
1✔
70
            this.name = name;
1✔
71
            this.descr = descr;
1✔
72
            this.argHandler = null;
1✔
73
        }
1✔
74

75
        RunOption(String name, String descr, Consumer<String> argHandler2) {
1✔
76
            this.name = name;
1✔
77
            this.descr = descr;
1✔
78
            this.argHandler = argHandler2;
1✔
79
        }
1✔
80

81
    }
82

83
    public static RunArgs defaults() {
84
        return new RunArgs();
1✔
85
    }
86

87
    public RunArgs(String... args) {
1✔
88
        this.args = args;
1✔
89
        // interpreter
90
        optionRuntests = addOption("runtests", "Run all test functions found in the scripts.");
1✔
91
        optionTestTimeout = addOptionWithArg("testTimeout", "Timeout in seconds after which tests will be cancelled and considered failed, if they did not yet succeed.", arg -> testTimeout = Integer.parseInt(arg));
1✔
92
        optionRunCompileTimeFunctions = addOption("runcompiletimefunctions", "Run all compiletime functions found in the scripts.");
1✔
93
        optionInjectCompiletimeObjects = addOption("injectobjects", "Injects the objects generated by compiletime functions into the map.");
1✔
94
        // optimization
95
        optionOpt = addOption("opt", "Enables identifier name compression and whitespace removal.");
1✔
96
        optionInline = addOption("inline", "Enables function inlining.");
1✔
97
        optionLocalOptimizations = addOption("localOptimizations", "Enables local optimizations (cpu and ram extensive, recommended for release)");
1✔
98
        // debug options
99
        optionStacktraces = addOption("stacktraces", "Generate stacktrace information in the script (useful for debugging).");
1✔
100
        optionNodebug = addOption("nodebug", "Remove all error messages from the script. (Not recommended)");
1✔
101
        uncheckedDispatch = addOption("uncheckedDispatch", "(dangerous) Removes checks from method-dispatch code. With unchecked dispatch "
1✔
102
                + "some programming errors like null-pointer-dereferences or accessing of destroyed objects can no longer be detected. "
103
                + "It is strongly recommended to not use this option, but it can give some performance benefits.");
104
        optionMeasureTimes = addOption("measure", "Measure how long each step of the translation process takes.");
1✔
105
        // tools
106
        optionAbout = addOption("-about", "Show the 'about' window.");
1✔
107
        optionFixInstall = addOption("-fixInstallation", "Checks your wc3 installation and applies compatibility fixes");
1✔
108
        optionCopyMap = addOption("-copyMap", "copies map");
1✔
109
        optionStartServer = addOption("-startServer", "Starts the compilation server.");
1✔
110
        optionHotdoc = addOption("-hotdoc", "Generate hotdoc html documentation.");
1✔
111
        optionShowErrors = addOption("-showerrors", "(currently not implemented.) Show errors generated by last compile.");
1✔
112
        optionExtractImports = addOptionWithArg("-extractImports", "Extract all files from a map into a folder next to the mapp.", arg -> mapFile = arg);
1✔
113
        optionShowVersion = addOption("-version", "Shows the version of the compiler");
1✔
114

115
        // other
116
        optionNoExtractMapScript = addOption("noExtractMapScript", "Do not extract the map script from the map and use the one from the Wurst folder instead.");
1✔
117
        optionGui = addOption("gui", "Show a graphical user interface (progress bar and error window).");
1✔
118
        addOptionWithArg("lib", "The next argument should be a library folder which is lazily added to the build.", arg -> libDirs.add(new File(arg)));
1✔
119
        addOptionWithArg("out", "Outputs the compiled script to this file.", arg -> outFile = arg);
1✔
120

121
        optionLanguageServer = addOption("languageServer", "Starts a language server which can be used by editors to get services "
1✔
122
                + "like code completion, validations, and find declaration. The communication to the language server is via standard input output.");
123

124
        optionHelp = addOption("help", "Prints this help message.");
1✔
125
        optionDisablePjass = addOption("noPJass", "Disables PJass checks for the generated code.");
1✔
126
        optionHotStartmap = addOption("hotstart", "Uses Jass Hot Code Reload (JHCR) to start the map.");
1✔
127
        optionHotReload = addOption("hotreload", "Reloads the mapscript after running the map with Jass Hot Code Reload (JHCR).");
1✔
128

129
        optionBuild = addOption("build", "Builds an output map from the input map and library directories.");
1✔
130
        addOptionWithArg("workspaceroot", "The next argument should be the root folder of the project to build.", arg -> workspaceroot = arg);
1✔
131
        addOptionWithArg("inputmap", "The next argument should be the input map.", arg -> inputmap = arg);
1✔
132
        optionLua = addOption("lua", "Choose Lua as the compilation target.");
1✔
133
        optionCompiletimeCache = addOption("compiletimeCache", "(Experimental) Cache results of compiletime invocations without side effects");
1✔
134

135
        addOptionWithArg("functionSplitLimit", "The maximum number of operations in a function before it is split by the function splitter (used for compiletime functions)",
1✔
136
            s -> functionSplitLimit = Integer.parseInt(s, 10));
×
137

138
        optionPrettyPrint = addOption("prettyPrint", "Pretty print the input file, or all sub-directory if the given path is: '...'");
1✔
139

140
        nextArg:
141
        for (int i = 0; i < args.length; i++) {
1✔
142
            String a = args[i];
1✔
143
            if (a.startsWith("-")) {
1✔
144
                for (RunOption o : options) {
1✔
145
                    if (("-" + o.name).equals(a)) {
1✔
146
                        Consumer<String> argHandler = o.argHandler;
1✔
147
                        if (argHandler != null) {
1✔
148
                            i++;
1✔
149
                            argHandler.accept(args[i]);
1✔
150
                        }
151
                        o.isSet = true;
1✔
152
                        continue nextArg;
1✔
153
                    } else if ((o.argHandler != null && isDoubleArg(a, o))) {
1✔
154
                        continue nextArg;
×
155
                    }
156
                }
1✔
157
                throw new RuntimeException("Unknown option: " + a);
×
158
            } else {
159
                files.add(a);
×
160
                if (a.endsWith(".w3x") || a.endsWith(".w3m")) {
×
161
                    mapFile = a;
×
162
                }
163
            }
164
        }
165

166
        if (optionHelp.isSet) {
1✔
167
            printHelpAndExit();
×
168
        }
169
    }
1✔
170

171
    private boolean isDoubleArg(String arg, RunOption option) {
172
        return (arg.contains(" ") && ("-" + option.name).equals(arg.substring(0, arg.indexOf(" "))));
1✔
173
    }
174

175
    private RunOption addOption(String name, String descr) {
176
        RunOption opt = new RunOption(name, descr);
1✔
177
        options.add(opt);
1✔
178
        return opt;
1✔
179
    }
180

181
    private RunOption addOptionWithArg(String name, String descr, Consumer<String> argHandler) {
182
        RunOption opt = new RunOption(name, descr, argHandler);
1✔
183
        options.add(opt);
1✔
184
        return opt;
1✔
185
    }
186

187
    public RunArgs(List<String> runArgs) {
188
        this(runArgs.toArray(new String[0]));
×
189
    }
×
190

191
    public void printHelpAndExit() {
192
        System.out.println("Usage: ");
×
193
        System.out.println("wurst <options> <files>");
×
194
        System.out.println();
×
195
        System.out.println("Example: wurst -opt common.j Blizzard.j myMap.w3x");
×
196
        System.out.println("Compiles the given map with the two script files and optimizations enabled.");
×
197
        System.out.println();
×
198
        System.out.println("Options:");
×
199
        System.out.println();
×
200
        for (RunOption opt : options) {
×
201
            System.out.println("-" + opt.name);
×
202
            System.out.println("        " + opt.descr);
×
203
            System.out.println();
×
204
        }
×
205
    }
×
206

207
    public List<String> getFiles() {
208
        return files;
×
209
    }
210

211
    public boolean isOptimize() {
212
        return optionOpt.isSet;
1✔
213
    }
214

215
    public boolean isGui() {
216
        return optionGui.isSet;
×
217
    }
218

219
    public @Nullable String getMapFile() {
220
        return mapFile;
×
221
    }
222

223
    public void setMapFile(String file) {
224
        mapFile = file;
×
225
    }
×
226

227
    public @Nullable String getOutFile() {
228
        return outFile;
×
229
    }
230

231
    public boolean showAbout() {
232
        return optionAbout.isSet;
×
233
    }
234

235
    public boolean isFixInstall() {
236
        return optionFixInstall.isSet;
×
237
    }
238

239
    public boolean isStartServer() {
240
        return optionStartServer.isSet;
×
241
    }
242

243
    public boolean showLastErrors() {
244
        return optionShowErrors.isSet;
×
245
    }
246

247
    public boolean isInline() {
248
        return optionInline.isSet;
1✔
249
    }
250

251
    public boolean runCompiletimeFunctions() {
252
        return optionRunCompileTimeFunctions.isSet;
1✔
253
    }
254

255

256
    public boolean createHotDoc() {
257
        return optionHotdoc.isSet;
×
258
    }
259

260
    public boolean isNullsetting() {
261
        return isOptimize();
1✔
262
    }
263

264
    public boolean isLocalOptimizations() {
265
        return optionLocalOptimizations.isSet;
1✔
266
    }
267

268
    public boolean isIncludeStacktraces() {
269
        return optionStacktraces.isSet;
1✔
270
    }
271

272
    public boolean isNoDebugMessages() {
273
        return optionNodebug.isSet;
1✔
274
    }
275

276
    public boolean isInjectObjects() {
277
        return !isHotReload() && optionInjectCompiletimeObjects.isSet;
1✔
278
    }
279

280
    public List<File> getAdditionalLibDirs() {
281
        return Collections.unmodifiableList(libDirs);
1✔
282
    }
283

284
    public void addLibs(Set<String> dependencies) {
285
        for (String dep : dependencies) {
1✔
286
            libDirs.add(new File(dep));
1✔
287
        }
1✔
288
    }
1✔
289

290
    public void addLibDirs(Set<File> dependencies) {
291
        libDirs.addAll(dependencies);
1✔
292
    }
1✔
293

294
    public boolean showHelp() {
295
        return optionHelp.isSet;
×
296
    }
297

298
    public boolean isExtractImports() {
299
        return optionExtractImports.isSet;
×
300
    }
301

302
    public boolean isShowVersion() {
303
        return optionShowVersion.isSet;
×
304
    }
305

306
    public boolean isUncheckedDispatch() {
307
        return uncheckedDispatch.isSet;
1✔
308
    }
309

310
    public boolean isLanguageServer() {
311
        return optionLanguageServer.isSet;
×
312
    }
313

314
    public boolean isNoExtractMapScript() {
315
        return optionNoExtractMapScript.isSet;
×
316
    }
317

318
    public boolean isCopyMap() {
319
        return optionCopyMap.isSet;
×
320
    }
321

322
    public boolean isDisablePjass() {
323
        return optionDisablePjass.isSet;
×
324
    }
325

326
    public boolean isRunTests() {
327
        return optionRuntests.isSet;
×
328
    }
329

330
    public boolean isPrettyPrint() {
331
        return optionPrettyPrint.isSet;
1✔
332
    }
333

334
    public int getTestTimeout() {
335
        return testTimeout;
×
336
    }
337

338
    public boolean isMeasureTimes() {
339
        return optionMeasureTimes.isSet;
×
340
    }
341

342
    public boolean isHotStartmap() {
343
        return optionHotStartmap.isSet;
1✔
344
    }
345

346
    public boolean isHotReload() {
347
        return optionHotReload.isSet;
1✔
348
    }
349

350
    public boolean isBuild() {
351
        return optionBuild.isSet;
×
352
    }
353

354
    public String getWorkspaceroot() {
355
        return workspaceroot;
×
356
    }
357

358
    public String getInputmap() {
359
        return inputmap;
×
360
    }
361

362
    public boolean isLua() {
363
        return optionLua.isSet;
1✔
364
    }
365

366
    public boolean isCompiletimeCache() {
367
        return optionCompiletimeCache.isSet;
×
368
    }
369

370

371
    public int getFunctionSplitLimit() {
372
        return functionSplitLimit;
1✔
373
    }
374

375
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc