• 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

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

3
import com.google.common.base.Charsets;
4
import com.google.common.collect.ImmutableList;
5
import com.google.gson.JsonElement;
6
import com.google.gson.JsonObject;
7
import de.peeeq.wurstio.languageserver.requests.*;
8
import de.peeeq.wurstscript.WLogger;
9
import org.eclipse.lsp4j.ExecuteCommandParams;
10

11
import java.io.File;
12
import java.io.IOException;
13
import java.nio.file.Files;
14
import java.nio.file.Path;
15
import java.nio.file.Paths;
16
import java.util.Arrays;
17
import java.util.List;
18
import java.util.Optional;
19
import java.util.concurrent.CompletableFuture;
20
import java.util.stream.Collectors;
21
import java.util.stream.Stream;
22

23
/**
24
 *
25
 */
26
public class WurstCommands {
×
27

28
    public static final String WURST_RESTART = "wurst.restart";
29
    public static final String WURST_CLEAN = "wurst.clean";
30
    public static final String WURST_STARTMAP = "wurst.startmap";
31
    public static final String WURST_HOTSTARTMAP = "wurst.hotstartmap";
32
    public static final String WURST_HOTRELOAD = "wurst.hotreload";
33
    public static final String WURST_BUILDMAP = "wurst.buildmap";
34
    public static final String WURST_STARTLAST = "wurst.startlast";
35
    public static final String WURST_TESTS = "wurst.tests";
36
    public static final String WURST_TESTS_FILE = "wurst.tests_file";
37
    public static final String WURST_TESTS_FUNC = "wurst.tests_func";
38
    public static final String WURST_PERFORM_CODE_ACTION = "wurst.perform_code_action";
39

40
    static List<String> providedCommands() {
41
        return Arrays.asList(
×
42
            WURST_CLEAN
43
        );
44
    }
45

46
    public static CompletableFuture<Object> execute(WurstLanguageServer server, ExecuteCommandParams params) {
47
        switch (params.getCommand()) {
×
48
            case WURST_CLEAN:
49
                return server.worker().handle(new CleanProject()).thenApply(x -> x);
×
50
            case WURST_STARTMAP:
51
                return startmap(server, params);
×
52
            case WURST_HOTSTARTMAP:
53
                return startmap(server, params, "-hotstart");
×
54
            case WURST_HOTRELOAD:
55
                return startmap(server, params, "-hotreload");
×
56
            case WURST_TESTS:
57
                return testMap(server, params);
×
58
            case WURST_PERFORM_CODE_ACTION:
59
                return server.worker().handle(new PerformCodeActionRequest(server, params));
×
60
            case WURST_BUILDMAP:
61
                return buildmap(server, params);
×
62
        }
63
        WLogger.info("unhandled command: " + params.getCommand());
×
64
        throw new RuntimeException("unhandled command: " + params.getCommand());
×
65
    }
66

67

68
    private static CompletableFuture<Object> testMap(WurstLanguageServer server, ExecuteCommandParams params) {
69
        JsonObject options = (JsonObject) params.getArguments().get(0);
×
70
        Optional<String> filename = getString(options, "filename");
×
71
        int line = options.has("line") ? options.get("line").getAsInt() : -1;
×
72
        int column = options.has("column") ? options.get("column").getAsInt() : -1;
×
73
        int testTimeout = options.has("testTimeout") ? options.get("testTimeout").getAsInt() : 20;
×
74
        Optional<String> testName = getString(options, "testName");
×
75

76
        return server.worker().handle(new RunTests(filename, line, column, testName, testTimeout));
×
77
    }
78

79
    private static CompletableFuture<Object> buildmap(WurstLanguageServer server, ExecuteCommandParams params) {
80
        WFile workspaceRoot = server.getRootUri();
×
81
        if (params.getArguments().isEmpty()) {
×
82
            throw new RuntimeException("Missing arguments");
×
83
        }
84
        JsonObject options = (JsonObject) params.getArguments().get(0);
×
85
        Optional<String> mapPath = getString(options, "mappath");
×
86
        Optional<String> wc3Path = getString(options, "wc3path");
×
87
        if (!mapPath.isPresent()) {
×
88
            throw new RuntimeException("No mappath given");
×
89
        }
90

91
        Optional<File> map = mapPath.map(File::new);
×
92
        List<String> compileArgs = getCompileArgs(workspaceRoot);
×
93
        return server.worker().handle(new BuildMap(server.getConfigProvider(), workspaceRoot, wc3Path, map, compileArgs)).thenApply(x -> x);
×
94
    }
95

96
    private static CompletableFuture<Object> startmap(WurstLanguageServer server, ExecuteCommandParams params, String... additionalArgs) {
97
        WFile workspaceRoot = server.getRootUri();
×
98
        if (params.getArguments().isEmpty()) {
×
99
            throw new RuntimeException("Missing arguments");
×
100
        }
101
        JsonObject options = (JsonObject) params.getArguments().get(0);
×
102
        String key = "mappath";
×
103
        Optional<String> mapPath = getString(options, key);
×
104
        Optional<String> wc3Path = getString(options, "wc3path");
×
105

106
        Optional<File> map = mapPath.map(File::new);
×
107
        List<String> compileArgs = getCompileArgs(workspaceRoot, additionalArgs);
×
108
        return server.worker().handle(new RunMap(server.getConfigProvider(), workspaceRoot, wc3Path, map, compileArgs)).thenApply(x -> x);
×
109
    }
110

111
    private static Optional<String> getString(JsonObject options, String key) {
112
        try {
113
            return Optional.ofNullable(options.get(key)).map(JsonElement::getAsString);
×
114
        } catch (ClassCastException | IllegalStateException e) {
×
115
            WLogger.warning("Invalid configuration", e);
×
116
            return Optional.empty();
×
117
        }
118
    }
119

120
    private static final List<String> defaultArgs = ImmutableList.of("-runcompiletimefunctions", "-injectobjects", "-stacktraces");
×
121

122
    public static List<String> getCompileArgs(WFile rootPath, String... additionalArgs) {
123
        try {
124
            Path configFile = Paths.get(rootPath.toString(), "wurst_run.args");
×
125
            if (Files.exists(configFile)) {
×
126
                try (Stream<String> lines = Files.lines(configFile)) {
×
127
                    return Stream.concat(
×
128
                        lines.filter(s -> s.startsWith("-")),
×
129
                        Stream.of(additionalArgs)
×
130
                    ).collect(Collectors.toList());
×
131
                }
132
            } else {
133

134
                String cfg = String.join("\n", defaultArgs) + "\n";
×
135
                Files.write(configFile, cfg.getBytes(Charsets.UTF_8));
×
136
                return defaultArgs;
×
137
            }
138
        } catch (IOException e) {
×
139
            throw new RuntimeException("Could not access wurst run config file", e);
×
140
        }
141
    }
142

143
}
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