mirror of
https://github.com/architectury/architectury-loom.git
synced 2026-04-02 21:47:42 -05:00
Auto generate vscode run configurations, inject our own pre launch tasks to tasks.json
This commit is contained in:
@@ -28,11 +28,16 @@ import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.apache.tools.ant.taskdefs.condition.Os;
|
||||
import org.gradle.api.Project;
|
||||
import org.gradle.api.tasks.TaskAction;
|
||||
|
||||
@@ -46,9 +51,12 @@ import net.fabricmc.loom.configuration.ide.RunConfig;
|
||||
public class GenVsCodeProjectTask extends AbstractLoomTask {
|
||||
@TaskAction
|
||||
public void genRuns() {
|
||||
Project project = getProject();
|
||||
LoomGradleExtension extension = getExtension();
|
||||
File projectDir = project.file(".vscode");
|
||||
clean(getProject());
|
||||
generate(getProject());
|
||||
}
|
||||
|
||||
public static void clean(Project project) {
|
||||
File projectDir = project.getRootProject().file(".vscode");
|
||||
|
||||
if (!projectDir.exists()) {
|
||||
projectDir.mkdir();
|
||||
@@ -59,12 +67,36 @@ public class GenVsCodeProjectTask extends AbstractLoomTask {
|
||||
if (launchJson.exists()) {
|
||||
launchJson.delete();
|
||||
}
|
||||
}
|
||||
|
||||
public static void generate(Project project) {
|
||||
LoomGradleExtension extension = project.getExtensions().getByType(LoomGradleExtension.class);
|
||||
File projectDir = project.getRootProject().file(".vscode");
|
||||
|
||||
if (!projectDir.exists()) {
|
||||
projectDir.mkdir();
|
||||
}
|
||||
|
||||
File launchJson = new File(projectDir, "launch.json");
|
||||
File tasksJson = new File(projectDir, "tasks.json");
|
||||
|
||||
Gson gson = new GsonBuilder().setPrettyPrinting().create();
|
||||
|
||||
VsCodeLaunch launch;
|
||||
|
||||
if (launchJson.exists()) {
|
||||
try {
|
||||
launch = gson.fromJson(FileUtils.readFileToString(launchJson, StandardCharsets.UTF_8), VsCodeLaunch.class);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException("Failed to read launch.json", e);
|
||||
}
|
||||
} else {
|
||||
launch = new VsCodeLaunch();
|
||||
}
|
||||
|
||||
VsCodeLaunch launch = new VsCodeLaunch();
|
||||
launch.add(RunConfig.clientRunConfig(project));
|
||||
launch.add(RunConfig.serverRunConfig(project));
|
||||
|
||||
Gson gson = new GsonBuilder().setPrettyPrinting().create();
|
||||
String json = gson.toJson(launch);
|
||||
|
||||
try {
|
||||
@@ -78,6 +110,39 @@ public class GenVsCodeProjectTask extends AbstractLoomTask {
|
||||
if (!runDir.exists()) {
|
||||
runDir.mkdirs();
|
||||
}
|
||||
|
||||
VsCodeTasks tasks;
|
||||
|
||||
if (tasksJson.exists()) {
|
||||
try {
|
||||
tasks = gson.fromJson(FileUtils.readFileToString(tasksJson, StandardCharsets.UTF_8), VsCodeTasks.class);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException("Failed to read launch.json", e);
|
||||
}
|
||||
} else {
|
||||
tasks = new VsCodeTasks();
|
||||
}
|
||||
|
||||
for (VsCodeConfiguration configuration : launch.configurations) {
|
||||
if (configuration.preLaunchTask != null && configuration.tasksBeforeRun != null) {
|
||||
String prefix = Os.isFamily(Os.FAMILY_WINDOWS) ? "gradlew.bat" : "./gradlew";
|
||||
tasks.add(new VsCodeTask(configuration.preLaunchTask, prefix + " " + configuration.tasksBeforeRun.stream()
|
||||
.map(s -> {
|
||||
int i = s.indexOf('/');
|
||||
return i == -1 ? s : s.substring(i + 1);
|
||||
}).collect(Collectors.joining(" ")), "shell", new String[0]));
|
||||
}
|
||||
}
|
||||
|
||||
if (!tasks.tasks.isEmpty()) {
|
||||
String jsonTasks = gson.toJson(tasks);
|
||||
|
||||
try {
|
||||
FileUtils.writeStringToFile(tasksJson, jsonTasks, StandardCharsets.UTF_8);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException("Failed to write tasks.json", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static class VsCodeLaunch {
|
||||
@@ -85,7 +150,25 @@ public class GenVsCodeProjectTask extends AbstractLoomTask {
|
||||
public List<VsCodeConfiguration> configurations = new ArrayList<>();
|
||||
|
||||
public void add(RunConfig runConfig) {
|
||||
configurations.add(new VsCodeConfiguration(runConfig));
|
||||
if (configurations.stream().noneMatch(config -> Objects.equals(config.name, runConfig.configName))) {
|
||||
VsCodeConfiguration configuration = new VsCodeConfiguration(runConfig);
|
||||
configurations.add(configuration);
|
||||
|
||||
if (!configuration.tasksBeforeRun.isEmpty()) {
|
||||
configuration.preLaunchTask = "generated_" + runConfig.configName;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static class VsCodeTasks {
|
||||
public String version = "2.0.0";
|
||||
public List<VsCodeTask> tasks = new ArrayList<>();
|
||||
|
||||
public void add(VsCodeTask vsCodeTask) {
|
||||
if (tasks.stream().noneMatch(task -> Objects.equals(task.label, vsCodeTask.label))) {
|
||||
tasks.add(vsCodeTask);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -100,12 +183,32 @@ public class GenVsCodeProjectTask extends AbstractLoomTask {
|
||||
public String mainClass;
|
||||
public String vmArgs;
|
||||
public String args;
|
||||
public Map<String, String> env = new LinkedHashMap<>();
|
||||
public transient List<String> tasksBeforeRun = new ArrayList<>();
|
||||
public String preLaunchTask = null;
|
||||
|
||||
VsCodeConfiguration(RunConfig runConfig) {
|
||||
this.name = runConfig.configName;
|
||||
this.mainClass = runConfig.mainClass;
|
||||
this.vmArgs = runConfig.vmArgs;
|
||||
this.args = runConfig.programArgs;
|
||||
this.env.putAll(runConfig.envVariables);
|
||||
this.tasksBeforeRun.addAll(runConfig.tasksBeforeRun);
|
||||
}
|
||||
}
|
||||
|
||||
private static class VsCodeTask {
|
||||
public String label;
|
||||
public String command;
|
||||
public String type;
|
||||
public String[] args;
|
||||
public String group = "build";
|
||||
|
||||
VsCodeTask(String label, String command, String type, String[] args) {
|
||||
this.label = label;
|
||||
this.command = command;
|
||||
this.type = type;
|
||||
this.args = args;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user