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

wurstscript / WurstScript / 228

29 Nov 2023 05:00PM UTC coverage: 62.48% (-0.09%) from 62.574%
228

push

circleci

web-flow
Show dialog for choosing game path, cleanup (#1083)

* show dialog for choosing game path

* cleanup code

* remove logs and refactor

* remove confusing mpq error, make some mpq loads readonly

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/Main.java
1
package de.peeeq.wurstio;
2

3
import com.google.common.base.Charsets;
4
import com.google.common.io.Files;
5
import config.WurstProjectConfig;
6
import config.WurstProjectConfigData;
7
import de.peeeq.wurstio.compilationserver.WurstServer;
8
import de.peeeq.wurstio.gui.AboutDialog;
9
import de.peeeq.wurstio.gui.WurstGuiImpl;
10
import de.peeeq.wurstio.hotdoc.HotdocGenerator;
11
import de.peeeq.wurstio.languageserver.LanguageServerStarter;
12
import de.peeeq.wurstio.languageserver.ProjectConfigBuilder;
13
import de.peeeq.wurstio.languageserver.WFile;
14
import de.peeeq.wurstio.map.importer.ImportFile;
15
import de.peeeq.wurstio.mpq.MpqEditor;
16
import de.peeeq.wurstio.mpq.MpqEditorFactory;
17
import de.peeeq.wurstio.utils.W3InstallationData;
18
import de.peeeq.wurstscript.CompileTimeInfo;
19
import de.peeeq.wurstscript.ErrorReporting;
20
import de.peeeq.wurstscript.RunArgs;
21
import de.peeeq.wurstscript.WLogger;
22
import de.peeeq.wurstscript.attributes.CompileError;
23
import de.peeeq.wurstscript.attributes.prettyPrint.PrettyUtils;
24
import de.peeeq.wurstscript.gui.WurstGui;
25
import de.peeeq.wurstscript.gui.WurstGuiCliImpl;
26
import de.peeeq.wurstscript.utils.Utils;
27
import org.eclipse.jdt.annotation.Nullable;
28

29
import javax.swing.*;
30
import java.io.File;
31
import java.lang.management.ManagementFactory;
32
import java.lang.management.RuntimeMXBean;
33
import java.nio.file.Path;
34
import java.nio.file.Paths;
35
import java.nio.file.StandardCopyOption;
36
import java.util.LinkedList;
37
import java.util.List;
38
import java.util.Optional;
39

40
import static de.peeeq.wurstio.languageserver.ProjectConfigBuilder.FILE_NAME;
41
import static de.peeeq.wurstio.languageserver.WurstCommands.getCompileArgs;
42
import static java.util.Arrays.asList;
43

44
public class Main {
×
45

46
    /**
47
     * @param args
48
     */
49
    public static void main(String[] args) {
50
        if (args.length == 0) {
×
51
            // If no args are passed, display help
52
            new RunArgs("-help");
×
53
            return;
×
54
        }
55

56
        WurstGui gui = null;
×
57
        RunArgs runArgs = new RunArgs(args);
×
58
        try {
59
            if (runArgs.isLanguageServer()) {
×
60
                WLogger.setLogger("languageServer");
×
61
            }
62
            logStartup(args);
×
63

64
            if (runArgs.showHelp()) {
×
65
                return;
×
66
            }
67

68
            if (runArgs.isShowVersion()) {
×
69
                System.out.println(CompileTimeInfo.version);
×
70
                return;
×
71
            }
72

73
            if (runArgs.showAbout()) {
×
74
                new AboutDialog(null, false).setVisible(true);
×
75
                return;
×
76
            }
77

78
            if (runArgs.isStartServer()) {
×
79
                WurstServer.startServer();
×
80
                return;
×
81
            }
82

83
            if (runArgs.isLanguageServer()) {
×
84
                LanguageServerStarter.start();
×
85
                return;
×
86
            }
87

88
            if (runArgs.isPrettyPrint()) {
×
89
                PrettyUtils.pretty(runArgs.getFiles());
×
90
                return;
×
91
            }
92

93
            WLogger.info("runArgs.isExtractImports() = " + runArgs.isExtractImports());
×
94
            if (runArgs.isExtractImports()) {
×
95
                File mapFile = new File(runArgs.getMapFile());
×
96
                ImportFile.extractImportsFromMap(mapFile, runArgs);
×
97
                return;
×
98
            }
99

100
            if (runArgs.createHotDoc()) {
×
101
                HotdocGenerator hg = new HotdocGenerator(runArgs.getFiles());
×
102
                hg.generateDoc();
×
103
                return;
×
104
            }
105

106
            if (runArgs.isGui()) {
×
107
                gui = new WurstGuiImpl();
×
108
                // use the error reporting with GUI
109
                ErrorReporting.instance = new ErrorReportingIO();
×
110
            } else {
111
                gui = new WurstGuiCliImpl();
×
112
            }
113

114
            if (runArgs.showLastErrors()) {
×
115
                JOptionPane.showMessageDialog(null, "not implemented");
×
116
                return;
×
117
            }
118

119
            try {
120
                WurstProjectConfigData projectConfig = null;
×
121
                Path buildDir = null;
×
122
                Optional<Path> target = Optional.empty();
×
123
                String workspaceroot = runArgs.getWorkspaceroot();
×
124
                if (runArgs.isBuild() && runArgs.getInputmap() != null && workspaceroot != null) {
×
125
                    Path root = Paths.get(workspaceroot);
×
126
                    Path inputMap = root.resolve(runArgs.getInputmap());
×
127
                    projectConfig = WurstProjectConfig.INSTANCE.loadProject(root.resolve(FILE_NAME));
×
128

129
                    if (java.nio.file.Files.exists(inputMap) && projectConfig != null) {
×
130
                        buildDir = root.resolve("_build");
×
131
                        java.nio.file.Files.createDirectories(buildDir);
×
132
                        target = Optional.of(buildDir.resolve(projectConfig.getBuildMapData().getFileName() + ".w3x"));
×
133
                        java.nio.file.Files.copy(inputMap, target.get(), StandardCopyOption.REPLACE_EXISTING);
×
134
                        runArgs.setMapFile(target.get().toAbsolutePath().toString());
×
135
                    }
136
                }
137

138
                String mapFilePath = runArgs.getMapFile();
×
139

140
                RunArgs compileArgs = runArgs;
×
141
                if (workspaceroot != null) {
×
142
                    WLogger.info("workspaceroot: " + workspaceroot);
×
143
                    List<String> argList = new LinkedList<>(asList(args));
×
144
                    List<String> argsList = getCompileArgs(WFile.create(workspaceroot));
×
145
                    WLogger.info("workspaceroot: " + (argsList == null));
×
146
                    argList.addAll(argsList);
×
147
                    compileArgs = new RunArgs(argList);
×
148
                }
149
                CompilationProcess compilationProcess = new CompilationProcess(gui, compileArgs);
×
150
                @Nullable CharSequence compiledScript;
151

152
                if (mapFilePath != null && workspaceroot != null) {
×
153
                    try (MpqEditor mpqEditor = MpqEditorFactory.getEditor(Optional.of(new File(mapFilePath)))) {
×
154
                        File projectFolder = Paths.get(workspaceroot).toFile();
×
155
                        compiledScript = compilationProcess.doCompilation(mpqEditor, projectFolder, true);
×
156
                        if (compiledScript != null) {
×
157
                            gui.sendProgress("Writing to map");
×
158
                            mpqEditor.deleteFile("war3map.j");
×
159
                            byte[] war3map = compiledScript.toString().getBytes(Charsets.UTF_8);
×
160
                            mpqEditor.insertFile("war3map.j", war3map);
×
161
                        }
162
                        ImportFile.importFilesFromImports(projectFolder, mpqEditor);
×
163
                    }
164
                } else {
165
                    compiledScript = compilationProcess.doCompilation(null, true);
×
166
                }
167

168
                if (compiledScript != null) {
×
169
                    File scriptFile = new File("compiled.j.txt");
×
170
                    Files.write(compiledScript.toString().getBytes(Charsets.UTF_8), scriptFile);
×
171

172
                    if (projectConfig != null && target.isPresent()) {
×
173
                        ProjectConfigBuilder.apply(projectConfig, target.get().toFile(), scriptFile, buildDir.toFile(),
×
174
                            runArgs, new W3InstallationData(null, Paths.get(workspaceroot).toFile()));
×
175

176
                        WLogger.info("map build success");
×
177
                        System.out.println("Build succeeded. Output file: <" + target.get().toAbsolutePath() + ">");
×
178
                    }
179
                }
180

181
                gui.sendProgress("Finished!");
×
182
            } catch (AbortCompilationException e) {
×
183
                gui.showInfoMessage(e.getMessage());
×
184
            }
×
185
        } catch (Throwable t) {
×
186
            String source = "";
×
187
            // TODO add additional information to source
188
            ErrorReporting.instance.handleSevere(t, source);
×
189
            if (!runArgs.isGui()) {
×
190
                System.exit(2);
×
191
            }
192
        } finally {
193
            if (gui != null) {
×
194
                gui.sendFinished();
×
195
                if (!runArgs.isGui()) {
×
196
                    if (gui.getErrorCount() > 0) {
×
197
                        //         print error messages
198
                        for (CompileError err : gui.getErrorList()) {
×
199
                            System.out.println(err);
×
200
                        }
×
201
                        // signal that there was an error when compiling
202
                        System.exit(1);
×
203
                    }
204
                    // print warnings:
205
                    for (CompileError err : gui.getWarningList()) {
×
206
                        System.out.println(err);
×
207
                    }
×
208
                }
209
            }
210
        }
211
    }
×
212

213
    private static void logStartup(String[] args) {
214
        // VM Arguments
215
        RuntimeMXBean runtimeMxBean = ManagementFactory.getRuntimeMXBean();
×
216
        List<String> arguments = runtimeMxBean.getInputArguments();
×
217

218
        WLogger.info("### Started wurst version: (" + AboutDialog.version + ")");
×
219
        WLogger.info("### With wurst-args " + Utils.printSep(", ", args));
×
220
        if (arguments != null && arguments.size() > 0) {
×
221
            WLogger.info("### With vm-args " + Utils.printSep(", ", arguments.toArray(new String[0])));
×
222
        }
223
        try {
224
            WLogger.info("### compiler path1: " + Main.class.getProtectionDomain().getCodeSource().getLocation());
×
225
            WLogger.info("### compiler path2: " + ClassLoader.getSystemClassLoader().getResource(".").getPath());
×
226
        } catch (Throwable ignored) {
×
227
        }
×
228
        WLogger.info("### ============================================");
×
229
    }
×
230

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