From 7b9ae34c1ea9568e0cb0d0cd70dac78aac9f846b Mon Sep 17 00:00:00 2001 From: modmuss50 Date: Tue, 20 Sep 2016 20:01:10 +0100 Subject: [PATCH] Added basic visual code support, run with vscode to generate workspace --- .../net/fabricmc/loom/LoomGradlePlugin.java | 2 + .../loom/task/GenVSCodeProjectTask.java | 83 +++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 src/main/java/net/fabricmc/loom/task/GenVSCodeProjectTask.java diff --git a/src/main/java/net/fabricmc/loom/LoomGradlePlugin.java b/src/main/java/net/fabricmc/loom/LoomGradlePlugin.java index 2d85b14..96c0a31 100644 --- a/src/main/java/net/fabricmc/loom/LoomGradlePlugin.java +++ b/src/main/java/net/fabricmc/loom/LoomGradlePlugin.java @@ -40,5 +40,7 @@ public class LoomGradlePlugin extends AbstractPlugin { makeTask("extractNatives", ExtractNativesTask.class).dependsOn("download"); makeTask("genIdeaRuns", GenIdeaProjectTask.class).dependsOn("cleanIdea").dependsOn("idea").dependsOn("extractNatives"); + + makeTask("vscode", GenVSCodeProjectTask.class).dependsOn("extractNatives"); } } diff --git a/src/main/java/net/fabricmc/loom/task/GenVSCodeProjectTask.java b/src/main/java/net/fabricmc/loom/task/GenVSCodeProjectTask.java new file mode 100644 index 0000000..7c6063e --- /dev/null +++ b/src/main/java/net/fabricmc/loom/task/GenVSCodeProjectTask.java @@ -0,0 +1,83 @@ +/* + * This file is part of fabric-loom, licensed under the MIT License (MIT). + * + * Copyright (c) 2016 FabricMC + * + * 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 com.google.gson.*; +import net.fabricmc.loom.LoomGradleExtension; +import net.fabricmc.loom.util.Constants; +import net.fabricmc.loom.util.Version; +import org.apache.commons.io.FileUtils; +import org.gradle.api.DefaultTask; +import org.gradle.api.tasks.TaskAction; + +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.nio.charset.Charset; +import java.util.ArrayList; +import java.util.List; + +/** + * Install https://marketplace.visualstudio.com/items?itemName=georgewfraser.vscode-javac into vscode + */ +public class GenVSCodeProjectTask extends DefaultTask { + + @TaskAction + public void genVsCodeProject() throws IOException { + LoomGradleExtension extension = this.getProject().getExtensions().getByType(LoomGradleExtension.class); + File classPathFile = new File("vscodeClasspath.txt"); + File configFile = new File("javaconfig.json"); + + Gson gson = new Gson(); + Version version = gson.fromJson(new FileReader(Constants.MINECRAFT_JSON.get(extension)), Version.class); + + List libs = new ArrayList<>(); + for (Version.Library library : version.libraries) { + if (library.allowed() && library.getFile(extension) != null && library.getFile(extension).exists()) { + libs.add(library.getFile(extension).getAbsolutePath()); + } + } + libs.add(Constants.MINECRAFT_MAPPED_JAR.get(extension).getAbsolutePath()); + for (File file : getProject().getConfigurations().getByName("compile").getFiles()) { + libs.add(file.getAbsolutePath()); + } + FileUtils.writeLines(classPathFile, libs); + + JsonObject jsonObject = new JsonObject(); + JsonArray jsonArray = new JsonArray(); + jsonArray.add("src/main/java"); + jsonArray.add("src/main/resorces"); + jsonArray.add("src/test/java"); + jsonArray.add("src/test/resorces"); + jsonObject.add("sourcePath", jsonArray); + JsonElement element = new JsonPrimitive(classPathFile.getName()); + jsonObject.add("classPathFile", element); + element = new JsonPrimitive("vscode"); + jsonObject.add("outputDirectory", element); + + FileUtils.write(configFile, gson.toJson(jsonObject), Charset.defaultCharset()); + } + +}