Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -405,29 +405,28 @@ protected File runJassHotCodeReload(File mapScript) throws IOException, Interrup
* Selects project Wurst sources, the map/core Jass scripts and their imports, without changing the editor model.
*/
private Set<CompilationUnit> compilationUnits(WurstModel model) {

Path mapScript = existingProjectMapScriptPath();
Set<CompilationUnit> imported = model.stream()
.filter(cu -> isInProjectWurstFolder(cu.getCuInfo().getFile())
|| isProjectWar3MapScript(cu.getCuInfo().getFile())
|| (mapScript != null && mapScript.equals(Paths.get(cu.getCuInfo().getFile()).toAbsolutePath().normalize()))
|| isRequiredBuildJass(cu.getCuInfo().getFile()))
.collect(Collectors.toSet());
addImports(imported, imported);

return imported;
}

private boolean isProjectWar3MapScript(String file) {
if (!file.endsWith("war3map.j")) {
return false;
}
Path p = Paths.get(file).toAbsolutePath().normalize();
Path projectWurstFolder;
private @Nullable Path existingProjectMapScriptPath() {
try {
projectWurstFolder = workspaceRoot.getPath().resolve("wurst").toAbsolutePath().normalize();
Path path = projectMapScript().getPath();
return java.nio.file.Files.isRegularFile(path) ? path : null;
} catch (FileNotFoundException e) {
return false;
return null;
}
return p.startsWith(projectWurstFolder) && java.nio.file.Files.exists(p);
}

private WFile projectMapScript() throws FileNotFoundException {
return WFile.create(workspaceRoot.getPath().resolve("wurst").resolve("war3map.j"));
}

private boolean isInProjectWurstFolder(String file) {
Expand Down Expand Up @@ -545,15 +544,10 @@ static boolean checksLikeTheModelManager(RunArgs runArgs) {
return !runArgs.isLegacyJassTypeChecks();
}

private static void replaceBaseScriptWithConfig(ModelManager modelManager, File scriptFile) throws IOException {
Optional<CompilationUnit> war3mapJ = modelManager.getModel()
.stream()
.filter((CompilationUnit cu) -> cu.getCuInfo().getFile().endsWith("war3map.j"))
.findFirst();

if (war3mapJ.isPresent()) {
modelManager.syncCompilationUnitContent(WFile.create(war3mapJ.get().getCuInfo().getFile()),
java.nio.file.Files.readString(scriptFile.toPath()));
private void replaceBaseScriptWithConfig(ModelManager modelManager, File scriptFile) throws IOException {
WFile mapScript = projectMapScript();
if (java.nio.file.Files.isRegularFile(mapScript.getPath())) {
modelManager.syncCompilationUnitContent(mapScript, java.nio.file.Files.readString(scriptFile.toPath()));
}
}

Expand Down Expand Up @@ -773,7 +767,7 @@ protected CompilationResult compileScript(ModelManager modelManager, WurstGui gu
result = new CompilationResult();
result.script = new File(buildDir, BUILD_CONFIGURED_SCRIPT_NAME);
if (!result.script.exists()) {
result.script = new File(new File(workspaceRoot.getFile(), "wurst"), "war3map.j");
result.script = projectMapScript().getFile();
}
if (!result.script.exists()) {
throw new RequestFailedException(MessageType.Error, "Could not find war3map.j file");
Expand All @@ -788,12 +782,7 @@ protected CompilationResult compileScript(ModelManager modelManager, WurstGui gu
// Compile the script
result.script = compileScript(gui, modelManager, compileArgs, testMap, projectConfigData, isProd, result.script);

Optional<WurstModel> model = Optional.ofNullable(modelManager.getModel());
if (!model.isPresent()
|| model
.get()
.stream()
.noneMatch((CompilationUnit cu) -> cu.getCuInfo().getFile().endsWith("war3map.j"))) {
if (modelManager.getCompilationUnit(projectMapScript()) == null) {
println("No 'war3map.j' file could be found inside the map nor inside the wurst folder");
println("Put the map's war3map.j into your wurst-folder, or use a map that contains it. ");
println("We will try to start the map now, but it will probably fail. ");
Expand Down Expand Up @@ -993,7 +982,7 @@ private static boolean startsWith(byte[] data, byte[] prefix) {
}

protected File loadMapScript(Optional<File> mapCopy, ModelManager modelManager, WurstGui gui) throws Exception {
File scriptFile = new File(new File(workspaceRoot.getFile(), "wurst"), "war3map.j");
File scriptFile = projectMapScript().getFile();
// If runargs are no extract, either use existing or throw error
// Otherwise try loading from map, if map was saved with wurst, try existing script, otherwise error
if (!mapCopy.isPresent() || runArgs.isNoExtractMapScript()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,38 @@ public void aDependencyMapScriptIsNotCompiled(boolean build, boolean lua) throws
assertEquals(project.manager.getModel().size(), units, "map compilation must leave the editor model alone");
}

@Test(dataProvider = "commands")
public void configuredScriptReplacesOnlyTheProjectMapScript(boolean build, boolean lua) throws Exception {
Project project = project();
File base = new File(project.root, "wurst/./war3map.j");
WFile baseUnit = WFile.create(base);
project.manager.reconcile(project.manager.removeCompilationUnit(baseUnit));
List<WFile> backupFiles = new java.util.ArrayList<>();
for (String relative : List.of("wurst/backup-war3map.j", "wurst/backup/war3map.j")) {
File backup = new File(project.root, relative);
Files.createDirectories(backup.toPath().getParent());
String backupScript = configuredScript(2);
Files.writeString(backup.toPath(), backupScript);
WFile backupUnit = WFile.create(backup);
project.manager.syncCompilationUnitContent(backupUnit, backupScript);
backupFiles.add(backupUnit);
}
project.manager.syncCompilationUnitContent(baseUnit, Files.readString(base.toPath()));
List<CompilationUnit> backups = backupFiles.stream().map(project.manager::getCompilationUnit).toList();
Files.writeString(project.script.toPath(), "function main takes nothing returns nothing\n"
+ "call BJDebugMsg(\"configured-project-map-script\")\nendfunction\n");
List<String> args = lua ? List.of("-lua") : List.of("-noPJass");
Probe request = build ? new BuildProbe(project.root, args) : new RunProbe(project.root, args);

String output = compile(request, project, new WurstGuiCliImpl());
assertTrue(output.contains("configured-project-map-script"), "the configured project script must be compiled");
assertEquals(backupFiles.stream().map(project.manager::getCompilationUnit).toList(), backups);

project.manager.reconcile(project.manager.removeCompilationUnit(baseUnit));
assertEquals(compile(request, project, new WurstGuiCliImpl()), output,
"the first reload must find the project map script even if it was absent from the editor model");
}

/** Runs used to drop unimported units from the model after the config overrides were computed, detaching them. */
@Test(dataProvider = "commands")
public void unimportedDependencyConfigPackagesAreNotCompiled(boolean build, boolean lua) throws Exception {
Expand Down
Loading