• 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

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.*;
19
import de.peeeq.wurstscript.attributes.CompileError;
20
import de.peeeq.wurstscript.attributes.prettyPrint.PrettyUtils;
21
import de.peeeq.wurstscript.gui.WurstGui;
22
import de.peeeq.wurstscript.gui.WurstGuiCliImpl;
23
import de.peeeq.wurstscript.utils.Utils;
24
import org.eclipse.jdt.annotation.Nullable;
25

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

37
import static de.peeeq.wurstio.languageserver.ProjectConfigBuilder.FILE_NAME;
38
import static de.peeeq.wurstio.languageserver.WurstCommands.getCompileArgs;
39
import static java.util.Arrays.asList;
40

41
public class Main {
×
42

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

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

61
            if (runArgs.showHelp()) {
×
62
                return;
×
63
            }
64

65
            if (runArgs.isShowVersion()) {
×
66
                System.out.println(CompileTimeInfo.version);
×
67
                return;
×
68
            }
69

70
            if (runArgs.showAbout()) {
×
71
                new AboutDialog(null, false).setVisible(true);
×
72
                return;
×
73
            }
74

75
            if (runArgs.isStartServer()) {
×
76
                WurstServer.startServer();
×
77
                return;
×
78
            }
79

80
            if (runArgs.isLanguageServer()) {
×
81
                LanguageServerStarter.start();
×
82
                return;
×
83
            }
84

85
            if (runArgs.isPrettyPrint()) {
×
86
                PrettyUtils.pretty(runArgs.getFiles());
×
87
                return;
×
88
            }
89

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

97
            if (runArgs.createHotDoc()) {
×
98
                HotdocGenerator hg = new HotdocGenerator(runArgs.getFiles());
×
99
                hg.generateDoc();
×
100
                return;
×
101
            }
102

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

111
            if (runArgs.showLastErrors()) {
×
112
                JOptionPane.showMessageDialog(null, "not implemented");
×
113
                return;
×
114
            }
115

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

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

135
                String mapFilePath = runArgs.getMapFile();
×
136

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

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

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

169
                    if (projectConfig != null && target.isPresent()) {
×
170
                        ProjectConfigBuilder.apply(projectConfig, target.get().toFile(), scriptFile, buildDir.toFile(),
×
171
                            runArgs, new W3InstallationData());
172

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

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

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

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

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