2018-12-01 17:04:15 +00:00
|
|
|
/*
|
|
|
|
* This file is part of fabric-loom, licensed under the MIT License (MIT).
|
|
|
|
*
|
2021-07-10 20:50:53 +00:00
|
|
|
* Copyright (c) 2018-2021 FabricMC
|
2018-12-01 17:04:15 +00:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
|
|
* copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
* SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package net.fabricmc.loom.task;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
2019-11-02 20:23:27 +00:00
|
|
|
import org.apache.commons.io.FileUtils;
|
2019-11-15 20:16:09 +00:00
|
|
|
import org.gradle.api.Project;
|
2019-11-02 20:23:27 +00:00
|
|
|
import org.gradle.api.tasks.TaskAction;
|
|
|
|
|
2021-05-13 21:06:34 +00:00
|
|
|
import net.fabricmc.loom.LoomGradlePlugin;
|
2020-12-24 20:58:30 +00:00
|
|
|
import net.fabricmc.loom.configuration.ide.RunConfig;
|
2021-01-29 18:04:39 +00:00
|
|
|
import net.fabricmc.loom.configuration.ide.RunConfigSettings;
|
2019-11-02 20:23:27 +00:00
|
|
|
|
2020-07-26 20:32:10 +00:00
|
|
|
// Recommended vscode plugins:
|
2018-12-01 17:04:15 +00:00
|
|
|
// https://marketplace.visualstudio.com/items?itemName=redhat.java
|
|
|
|
// https://marketplace.visualstudio.com/items?itemName=vscjava.vscode-java-debug
|
|
|
|
// https://marketplace.visualstudio.com/items?itemName=vscjava.vscode-java-pack
|
2019-05-10 11:32:11 +00:00
|
|
|
public class GenVsCodeProjectTask extends AbstractLoomTask {
|
2019-11-02 20:23:27 +00:00
|
|
|
@TaskAction
|
|
|
|
public void genRuns() {
|
2019-11-15 20:16:09 +00:00
|
|
|
Project project = getProject();
|
|
|
|
File projectDir = project.file(".vscode");
|
2019-11-02 20:23:27 +00:00
|
|
|
|
|
|
|
if (!projectDir.exists()) {
|
|
|
|
projectDir.mkdir();
|
|
|
|
}
|
|
|
|
|
|
|
|
File launchJson = new File(projectDir, "launch.json");
|
|
|
|
|
|
|
|
if (launchJson.exists()) {
|
|
|
|
launchJson.delete();
|
|
|
|
}
|
|
|
|
|
|
|
|
VsCodeLaunch launch = new VsCodeLaunch();
|
2021-01-29 18:04:39 +00:00
|
|
|
|
2021-02-11 18:18:07 +00:00
|
|
|
for (RunConfigSettings settings : getExtension().getRunConfigs()) {
|
2021-02-12 18:43:05 +00:00
|
|
|
if (!settings.isIdeConfigGenerated()) {
|
2021-02-11 18:50:25 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-01-29 18:04:39 +00:00
|
|
|
launch.add(RunConfig.runConfig(project, settings));
|
|
|
|
settings.makeRunDir();
|
|
|
|
}
|
2019-11-02 20:23:27 +00:00
|
|
|
|
2021-05-13 21:06:34 +00:00
|
|
|
String json = LoomGradlePlugin.GSON.toJson(launch);
|
2019-11-02 20:23:27 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
FileUtils.writeStringToFile(launchJson, json, StandardCharsets.UTF_8);
|
|
|
|
} catch (IOException e) {
|
|
|
|
throw new RuntimeException("Failed to write launch.json", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static class VsCodeLaunch {
|
|
|
|
public String version = "0.2.0";
|
|
|
|
public List<VsCodeConfiguration> configurations = new ArrayList<>();
|
|
|
|
|
|
|
|
public void add(RunConfig runConfig) {
|
|
|
|
configurations.add(new VsCodeConfiguration(runConfig));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-26 20:32:10 +00:00
|
|
|
@SuppressWarnings("unused")
|
2019-11-02 20:23:27 +00:00
|
|
|
private static class VsCodeConfiguration {
|
|
|
|
public String type = "java";
|
|
|
|
public String name;
|
|
|
|
public String request = "launch";
|
2021-01-29 18:04:39 +00:00
|
|
|
public String cwd;
|
2019-11-02 20:23:27 +00:00
|
|
|
public String console = "internalConsole";
|
|
|
|
public boolean stopOnEntry = false;
|
|
|
|
public String mainClass;
|
|
|
|
public String vmArgs;
|
|
|
|
public String args;
|
2018-12-01 17:04:15 +00:00
|
|
|
|
2019-11-02 20:23:27 +00:00
|
|
|
VsCodeConfiguration(RunConfig runConfig) {
|
|
|
|
this.name = runConfig.configName;
|
|
|
|
this.mainClass = runConfig.mainClass;
|
|
|
|
this.vmArgs = runConfig.vmArgs;
|
|
|
|
this.args = runConfig.programArgs;
|
2021-01-29 18:04:39 +00:00
|
|
|
this.cwd = "${workspaceFolder}/" + runConfig.runDir;
|
2019-11-02 20:23:27 +00:00
|
|
|
}
|
|
|
|
}
|
2018-12-01 17:04:15 +00:00
|
|
|
}
|