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

wurstscript / WurstScript / 227

29 Nov 2023 11:46AM CUT coverage: 62.48% (-0.09%) from 62.574%
227

Pull #1083

circleci

Frotty
remove confusing mpq error, make some mpq loads readonly
Pull Request #1083: Show dialog for choosing game path

17295 of 27681 relevant lines covered (62.48%)

0.62 hits per line

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

0.0
de.peeeq.wurstscript/src/main/java/de/peeeq/wurstio/CompilationProcess.java
1
package de.peeeq.wurstio;
2

3
import config.WurstProjectConfigData;
4
import de.peeeq.wurstio.languageserver.requests.RunTests;
5
import de.peeeq.wurstio.mpq.MpqEditor;
6
import de.peeeq.wurstio.utils.FileUtils;
7
import de.peeeq.wurstscript.RunArgs;
8
import de.peeeq.wurstscript.WLogger;
9
import de.peeeq.wurstscript.ast.WurstModel;
10
import de.peeeq.wurstscript.attributes.CompileError;
11
import de.peeeq.wurstscript.gui.WurstGui;
12
import de.peeeq.wurstscript.intermediatelang.interpreter.ILStackFrame;
13
import de.peeeq.wurstscript.jassAst.JassProg;
14
import de.peeeq.wurstscript.jassprinter.JassPrinter;
15
import de.peeeq.wurstscript.translation.imtranslation.ImTranslator;
16
import de.peeeq.wurstscript.utils.Utils;
17
import org.eclipse.jdt.annotation.Nullable;
18

19
import java.io.File;
20
import java.io.IOException;
21
import java.io.PrintStream;
22
import java.util.Optional;
23
import java.util.function.Supplier;
24

25
/**
26
 *
27
 */
28
public class CompilationProcess {
29

30
    private final WurstGui gui;
31
    private final RunArgs runArgs;
32
    private final TimeTaker timeTaker;
33

34
    public CompilationProcess(WurstGui gui, RunArgs runArgs) {
×
35
        this.gui = gui;
×
36
        this.runArgs = runArgs;
×
37
        if (runArgs.isMeasureTimes()) {
×
38
            this.timeTaker = new TimeTaker.Recording();
×
39
        } else {
40
            this.timeTaker = new TimeTaker.Default();
×
41
        }
42
    }
×
43

44
    @Nullable CharSequence doCompilation(@Nullable MpqEditor mpqEditor, boolean isProd) throws IOException {
45
        return doCompilation(mpqEditor, null, isProd);
×
46
    }
47

48
    @Nullable CharSequence doCompilation(@Nullable MpqEditor mpqEditor, @Nullable File projectFolder, boolean isProd) throws IOException {
49
        WurstCompilerJassImpl compiler = new WurstCompilerJassImpl(timeTaker, projectFolder, gui, mpqEditor, runArgs);
×
50
        gui.sendProgress("Check input map");
×
51
        if (mpqEditor != null && !mpqEditor.canWrite()) {
×
52
            WLogger.severe("The supplied map is invalid/corrupted/protected and Wurst cannot write to it.\n" +
×
53
                    "Please supply a valid .w3x input map that can be opened in the world editor.");
54
        }
55

56
        for (String file : runArgs.getFiles()) {
×
57
            compiler.loadFiles(file);
×
58
        }
×
59
        WurstModel model = timeTaker.measure("parse files",
×
60
            compiler::parseFiles);
×
61

62
        if (gui.getErrorCount() > 0) {
×
63
            return null;
×
64
        }
65
        if (model == null) {
×
66
            return null;
×
67
        }
68

69
        timeTaker.measure("Typecheck program",
×
70
                () -> compiler.checkProg(model));
×
71

72
        if (gui.getErrorCount() > 0) {
×
73
            return null;
×
74
        }
75

76
        timeTaker.measure("Translate program to Im",
×
77
                () -> compiler.translateProgToIm(model));
×
78

79
        if (gui.getErrorCount() > 0) {
×
80
            return null;
×
81
        }
82

83
        if (runArgs.isRunTests()) {
×
84
            timeTaker.measure("Run tests",
×
85
                    () -> runTests(compiler.getImTranslator(), compiler, runArgs.getTestTimeout()));
×
86
        }
87

88
        timeTaker.measure("Run compiletime functions", () ->compiler.runCompiletime(new WurstProjectConfigData(), isProd, false));
×
89

90
        JassProg jassProg = timeTaker.measure("Transform program to Jass",
×
91
            compiler::transformProgToJass);
×
92

93
        if (jassProg == null || gui.getErrorCount() > 0) {
×
94
            return null;
×
95
        }
96

97
        boolean withSpace;
98
        withSpace = !runArgs.isOptimize();
×
99

100
        gui.sendProgress("Printing Jass");
×
101

102
        JassPrinter printer = new JassPrinter(withSpace, jassProg);
×
103
        CharSequence mapScript = timeTaker.measure("Print Jass",
×
104
            (Supplier<String>) printer::printProg);
×
105

106
        // output to file
107
        File outputMapscript = timeTaker.measure("Print Jass",
×
108
                () -> writeMapscript(mapScript));
×
109

110
        if (!runArgs.isDisablePjass()) {
×
111
            boolean pjassError = timeTaker.measure("Run PJass",
×
112
                    () -> runPjass(outputMapscript));
×
113
            if (pjassError) return null;
×
114
        }
115
        timeTaker.printReport();
×
116
        return mapScript;
×
117
    }
118

119
    private boolean runPjass(File outputMapscript) {
120
        File commonJ = new File(outputMapscript.getParent(), "common.j");
×
121
        File blizzJ = new File(outputMapscript.getParent(), "blizzard.j");
×
122

123
        Pjass.Result pJassResult;
124
        if (commonJ.exists() && blizzJ.exists()) {
×
125
            pJassResult = Pjass.runPjass(outputMapscript, commonJ.getAbsolutePath(), blizzJ.getAbsolutePath());
×
126
        } else {
127
            pJassResult = Pjass.runPjass(outputMapscript);
×
128
        }
129
        WLogger.info(pJassResult.getMessage());
×
130
        if (!pJassResult.isOk()) {
×
131
            for (CompileError err : pJassResult.getErrors()) {
×
132
                gui.sendError(err);
×
133
            }
×
134
            return true;
×
135
        }
136
        return false;
×
137
    }
138

139
    private File writeMapscript(CharSequence mapScript) {
140
        gui.sendProgress("Writing output file");
×
141
        File outputMapscript;
142
        if (runArgs.getOutFile() != null) {
×
143
            outputMapscript = new File(runArgs.getOutFile());
×
144
        } else {
145
            outputMapscript = new File("./temp/output.j");
×
146
        }
147
        outputMapscript.getParentFile().mkdirs();
×
148
        try {
149
            FileUtils.write(mapScript, outputMapscript);
×
150
            return outputMapscript;
×
151
        } catch (IOException e) {
×
152
            throw new RuntimeException(e);
×
153
        }
154
    }
155

156
    private void runTests(ImTranslator translator, WurstCompilerJassImpl compiler, int testTimeout) {
157
        PrintStream out = System.out;
×
158
        // tests
159
        gui.sendProgress("Running tests");
×
160
        System.out.println("Running tests");
×
161
        RunTests runTests = new RunTests(Optional.empty(), 0, 0, Optional.empty(), testTimeout) {
×
162
            @Override
163
            protected void print(String message) {
164
                out.print(message);
×
165
            }
×
166
        };
167
        runTests.runTests(translator, compiler.getImProg(), Optional.empty(), Optional.empty());
×
168

169
        for (RunTests.TestFailure e : runTests.getFailTests()) {
×
170
            gui.sendError(new CompileError(e.getFunction(), e.getMessage()));
×
171
            if (runArgs.isGui()) {
×
172
                // when using graphical user interface, send stack trace to GUI
173
                for (ILStackFrame sf : Utils.iterateReverse(e.getStackTrace().getStackFrames())) {
×
174
                    gui.sendError(sf.makeCompileError());
×
175
                }
×
176
            }
177
        }
×
178

179
        System.out.println("Finished running tests");
×
180
    }
×
181
}
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