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

wurstscript / WurstScript / 195

09 Oct 2023 10:08PM UTC coverage: 63.447% (+0.006%) from 63.441%
195

Pull #1079

circleci

Frotty
small performance fixes
Pull Request #1079: small performance fixes

17146 of 27024 relevant lines covered (63.45%)

0.63 hits per line

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

40.79
de.peeeq.wurstscript/src/main/java/de/peeeq/wurstio/Pjass.java
1
package de.peeeq.wurstio;
2

3
import com.google.common.base.Charsets;
4
import com.google.common.collect.Lists;
5
import com.google.common.io.Files;
6
import de.peeeq.wurstscript.WLogger;
7
import de.peeeq.wurstscript.attributes.CompileError;
8
import de.peeeq.wurstscript.parser.WPos;
9
import de.peeeq.wurstscript.utils.LineOffsets;
10
import de.peeeq.wurstscript.utils.Utils;
11
import net.moonlightflower.wc3libs.port.Orient;
12

13
import java.io.BufferedReader;
14
import java.io.File;
15
import java.io.IOException;
16
import java.io.InputStreamReader;
17
import java.util.ArrayList;
18
import java.util.Collections;
19
import java.util.List;
20
import java.util.regex.Matcher;
21
import java.util.regex.Pattern;
22

23
/**
24
 * a helper class to run pjass
25
 */
26
public class Pjass {
×
27

28
    public static class Result {
29

30
        private boolean ok;
31
        private String message;
32
        private File jassFile;
33

34
        public Result(File jassFile, boolean ok, String message) {
1✔
35
            this.jassFile = jassFile;
1✔
36
            this.ok = ok;
1✔
37
            this.message = message;
1✔
38
        }
1✔
39

40
        public boolean isOk() {
41
            return ok;
1✔
42
        }
43

44
        public String getMessage() {
45
            return message;
1✔
46
        }
47

48
        private static final Pattern pat = Pattern.compile(".*:([0-9]+):(.*)");
1✔
49

50
        public List<CompileError> getErrors() {
51
            if (isOk()) {
×
52
                return Collections.emptyList();
×
53
            }
54
            LineOffsets lineOffsets = new LineOffsets();
×
55
            try {
56
                String cont = Files.asCharSource(jassFile, Charsets.UTF_8).read();
×
57
                int line = 0;
×
58
                lineOffsets.set(1, 0);
×
59
                for (int i = 0; i < cont.length(); i++) {
×
60
                    if (cont.charAt(i) == '\n') {
×
61
                        line++;
×
62
                        lineOffsets.set(line + 1, i);
×
63
                    }
64
                }
65
                lineOffsets.set(line + 1, cont.length() - 1);
×
66
            } catch (IOException e) {
×
67
                throw new RuntimeException(e);
×
68
            }
×
69

70
            List<CompileError> result = Lists.newArrayList();
×
71
            for (String error : getMessage().split("([\n\r])+")) {
×
72
                Matcher match = pat.matcher(error);
×
73
                if (!match.matches()) {
×
74
                    WLogger.warning("no match: " + error);
×
75
                    continue;
×
76
                }
77
                int line = Integer.parseInt(match.group(1));
×
78
                String msg = match.group(2);
×
79
                result.add(new CompileError(
×
80
                        new WPos(jassFile.getAbsolutePath(), lineOffsets, lineOffsets.get(line), lineOffsets.get(line + 1)),
×
81
                        "This is a bug in the Wurst Compiler. Please Report it. Pjass has found the following problem: "
82
                                + msg));
83
            }
84

85
            return result;
×
86
        }
87

88

89
    }
90

91
    public static Result runPjass(File outputFile) {
92
        return runPjass(outputFile, Utils.getResourceFile("common.j"), Utils.getResourceFile("blizzard.j"));
1✔
93
    }
94

95
    public static Result runPjass(File outputFile, String commonJPath, String blizzardJPath) {
96
        try {
97
            Process p;
98
            WLogger.info("Starting pjass");
1✔
99
            List<String> args = new ArrayList<>();
1✔
100
            args.add(Utils.getResourceFile("pjass.exe"));
1✔
101
            args.add(commonJPath);
1✔
102
            args.add(blizzardJPath);
1✔
103
            args.add(outputFile.getPath());
1✔
104
            if (Orient.isLinuxSystem()) {
1✔
105
                File fileName = Utils.getResourceFileF("pjass");
1✔
106
                boolean success = fileName.setExecutable(true);
1✔
107
                if (!success) {
1✔
108
                    throw new RuntimeException("Could not make pjass executable.");
×
109
                }
110
                args.set(0, fileName.getAbsolutePath());
1✔
111
            } else if (Orient.isMacSystem()) {
1✔
112
                File fileName = Utils.getResourceFileF("pjass_osx");
×
113
                boolean success = fileName.setExecutable(true);
×
114
                if (!success) {
×
115
                    throw new RuntimeException("Could not make pjass_osx executable.");
×
116
                }
117
                args.set(0, fileName.getAbsolutePath());
×
118
            } else if (!Orient.isWindowsSystem()) {
×
119
                WLogger.info("Unknown operating system detected.");
×
120
                WLogger.info("Trying to run with wine ...");
×
121
                // try to run with wine
122
                args.add(0, "wine");
×
123
            }
124

125
            try {
126
                p = Runtime.getRuntime().exec(args.toArray(new String[0]));
1✔
127
            } catch (IOException e) {
×
128
               return new Result(outputFile, false, "Pjass execution error: \n" + e.toString());
×
129
            }
1✔
130

131
            StringBuilder output = new StringBuilder();
1✔
132

133
            try (BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()))) {
1✔
134
                String line;
135
                while ((line = input.readLine()) != null) {
1✔
136
                    WLogger.info(line);
1✔
137
                    output.append(line).append("\n");
1✔
138
                }
139
            }
140

141

142
            int exitValue = p.waitFor();
1✔
143
            if (exitValue != 0) {
1✔
144
                return new Result(outputFile, false, "pjass errors: \n" + output.toString());
×
145
            } else {
146
                return new Result(outputFile, true, output.toString());
1✔
147
            }
148
        } catch (IOException e) {
×
149
            WLogger.severe("Could not run pjass:");
×
150
            WLogger.severe(e);
×
151
            return new Result(outputFile, false, "IO Exception");
×
152
        } catch (InterruptedException e) {
×
153
            return new Result(outputFile, false, "Interrupted");
×
154
        }
155

156
    }
157
}
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