-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathBuildMap.java
More file actions
105 lines (89 loc) · 4.58 KB
/
BuildMap.java
File metadata and controls
105 lines (89 loc) · 4.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package de.peeeq.wurstio.languageserver.requests;
import config.WurstProjectConfig;
import config.WurstProjectConfigData;
import de.peeeq.wurstio.gui.WurstGuiImpl;
import de.peeeq.wurstio.languageserver.ModelManager;
import de.peeeq.wurstio.languageserver.WFile;
import de.peeeq.wurstio.languageserver.WurstLanguageServer;
import de.peeeq.wurstio.mpq.MpqEditor;
import de.peeeq.wurstio.mpq.MpqEditorFactory;
import de.peeeq.wurstscript.WLogger;
import de.peeeq.wurstscript.attributes.CompileError;
import de.peeeq.wurstscript.gui.WurstGui;
import org.eclipse.lsp4j.MessageType;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.List;
import java.util.Optional;
import static de.peeeq.wurstio.languageserver.ProjectConfigBuilder.FILE_NAME;
/**
* Created by peter on 16.05.16.
*/
public class BuildMap extends MapRequest {
public BuildMap(WurstLanguageServer languageServer, WFile workspaceRoot, Optional<String> wc3Path, Optional<File> map,
List<String> compileArgs) {
super(languageServer, map, compileArgs, workspaceRoot, wc3Path, Optional.empty());
}
@Override
public Object execute(ModelManager modelManager) throws IOException {
if (modelManager.hasErrors()) {
throw new RequestFailedException(MessageType.Error, "Fix errors in your code before building a release.\n" + modelManager.getFirstErrorDescription());
}
WurstProjectConfigData projectConfig = WurstProjectConfig.INSTANCE.loadProject(workspaceRoot.getFile().toPath().resolve(FILE_NAME));
if (projectConfig == null) {
throw new RequestFailedException(MessageType.Error, FILE_NAME + " file doesn't exist or is invalid. " +
"Please install your project using grill or the wurst setup tool.");
}
WLogger.info("buildMap " + map + " " + compileArgs);
WurstGui gui = new WurstGuiImpl(workspaceRoot.getFile().getAbsolutePath());
try {
if (!map.isPresent()) {
throw new RequestFailedException(MessageType.Error, "Map is null");
}
if (!map.get().exists()) {
throw new RequestFailedException(MessageType.Error, map.get().getAbsolutePath() + " does not exist.");
}
MapRequest.mapLastModified = map.get().lastModified();
MapRequest.mapPath = map.get().getAbsolutePath();
gui.sendProgress("Copying map");
// first we copy in same location to ensure validity
File buildDir = getBuildDir();
String fileName = projectConfig.getBuildMapData().getFileName();
File targetMapFile = new File(buildDir, fileName.isEmpty() ? projectConfig.getProjectName() + ".w3x" : fileName + ".w3x");
targetMapFile = ensureWritableTargetFile(
targetMapFile,
"Build Map",
"The output map file is in use and cannot be replaced.\nClose Warcraft III and click Retry, choose Rename to use a temporary file name, or Cancel.",
"Build canceled because output map target is in use."
);
CompilationResult result = compileScript(modelManager, gui, Optional.of(targetMapFile), projectConfig, buildDir, true);
injectMapData(gui, Optional.of(targetMapFile), result);
targetMapFile = ensureWritableTargetFile(
targetMapFile,
"Build Map",
"The output map file is still in use and cannot be replaced.\nClick Retry, choose Rename to use a temporary file name, or Cancel.",
"Build canceled because output map target is in use."
);
Files.copy(getCachedMapFile().toPath(), targetMapFile.toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING);
gui.sendProgress("Finalizing map");
try (MpqEditor mpq = MpqEditorFactory.getEditor(Optional.of(targetMapFile))) {
if (mpq != null) {
mpq.closeWithCompression();
}
}
gui.sendProgress("Done.");
} catch (CompileError e) {
WLogger.info(e);
throw new RequestFailedException(MessageType.Error, "A compilation error occurred when building the map:\n" + e);
} catch (Exception e) {
WLogger.warning("Exception occurred", e);
throw new RequestFailedException(MessageType.Error, "An exception was thrown when building the map:\n" + e);
} finally {
if (gui.getErrorCount() == 0) {
gui.sendFinished();
}
}
return "ok"; // TODO
}
}