Skip to content

Commit

Permalink
step 2: Find Out
Browse files Browse the repository at this point in the history
- moved all modules to a folder
- mostly redo gradle stuff
- fat jar no longer has sources and will not be published to maven
- still WIP, can build but can't launch dev
  • Loading branch information
TropheusJ committed Jan 14, 2024
1 parent c2d52be commit 88c79f2
Show file tree
Hide file tree
Showing 1,288 changed files with 395 additions and 412 deletions.
338 changes: 126 additions & 212 deletions build.gradle

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@

public class AddMissingIconsTask extends DefaultTask {
public static final String PATH = "assets/porting_lib/icon.png";
// at src/main/resources/assets/porting_lib/icon.png, this is where it goes after processResources
public static final String DEFAULT_ICON = "build/resources/main/" + PATH;
public static final String DEFAULT_ICON = "src/main/resources/assets/porting_lib/icon.png";

@TaskAction
public void addMissingIcons() throws IOException {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package io.github.fabricators_of_create.porting_lib_build;

public class Config {
private boolean expandFmj = true;

public boolean getExpandFmj() {
return this.expandFmj;
}

public void setExpandFmj(boolean value) {
this.expandFmj = value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.gradle.api.tasks.TaskAction;
import org.gradle.internal.impldep.bsh.commands.dir;

import java.io.BufferedReader;
import java.io.IOException;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
Expand Down Expand Up @@ -62,7 +63,7 @@ public void moveInclusions(Path jar, Path target) throws IOException {
if (!hasJijs || !hasFmj)
return;

JsonObject json = JsonParser.parseString(Files.readString(fmj)).getAsJsonObject();
JsonObject json = PortingLibBuildPlugin.jsonFromPath(fmj).getAsJsonObject();
json.remove("jars");
Files.writeString(fmj, PortingLibBuildPlugin.GSON.toJson(json));

Expand Down Expand Up @@ -98,7 +99,7 @@ public void reAddInclusions(Path jar, Path dir) throws IOException {
Path jars = root.resolve("META-INF").resolve("jars");
Files.createDirectories(jars);

JsonObject json = JsonParser.parseString(Files.readString(fmj)).getAsJsonObject();
JsonObject json = PortingLibBuildPlugin.jsonFromPath(fmj).getAsJsonObject();
JsonArray jarsJson = new JsonArray();

try (Stream<Path> files = Files.list(dir)) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
package io.github.fabricators_of_create.porting_lib_build;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import groovy.lang.Closure;

import org.gradle.api.Action;
import org.gradle.api.Project;
import org.gradle.api.file.FileCopyDetails;
import org.gradle.language.jvm.tasks.ProcessResources;

import java.io.FilterReader;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Map;

public class FmjExpander extends FilterReader {
public static final String RESOURCES = "src/main/resources";
public static final String FMJ = "fabric.mod.json";
public static final String TEMPLATE_FMJ = RESOURCES + "/template." + FMJ;


public static final String PROJECT_NAME_PARAM = "projectName";
public static final String PROJECT_DIR_PARAM = "projectDir";
public static final String ROOT_PROJECT_DIR_PARAM = "rootProjectDir";

private boolean hasExpanded = false;

private String projectName;
private String projectDir;
private String rootProjectDir;

private Path projectDirPath;
private Path rootProjectDirPath;

public FmjExpander(Reader in) {
super(in);
}

@Override
public int read(char[] cbuf, int off, int len) throws IOException {
if (!hasExpanded) {
expand();
}
return super.read(cbuf, off, len);
}

private void expand() throws IOException {
try {
validateParams();
JsonObject json = JsonParser.parseReader(this.in).getAsJsonObject();
JsonObject expanded = this.expandFmj(json);
String asString = PortingLibBuildPlugin.GSON.toJson(expanded);
this.in = new StringReader(asString);
this.hasExpanded = true;
} catch (Throwable t) {
throw new IOException("Error expanding FMJ", t);
}
}

private void validateParams() {
if (this.projectName == null) {
throw new IllegalStateException("projectName is null");
} else if (this.projectDir == null) {
throw new IllegalStateException("projectDir is null");
} else if (this.rootProjectDir == null) {
throw new IllegalStateException("rootProjectDir is null");
}

this.projectDirPath = Paths.get(this.projectDir);
this.rootProjectDirPath = Paths.get(this.rootProjectDir);
}

public JsonObject expandFmj(JsonObject moduleFmj) {
// load from template, add additional from project
JsonObject template = makeDefaults();
for (Map.Entry<String, JsonElement> entry : moduleFmj.entrySet()) {
String key = entry.getKey();
JsonElement value = entry.getValue();
tryMerge(template, key, value);
}

String name = "porting_lib_" + this.projectName;
Path resources = this.projectDirPath.resolve(RESOURCES);

// fill in mixins
String mixinsFileName = name + ".mixins.json";
Path mixins = resources.resolve(mixinsFileName);
if (Files.exists(mixins)) {
JsonArray array = new JsonArray();
array.add(mixinsFileName);
template.add("mixins", array);
}
// and AW
String awFileName = name + ".accesswidener";
Path aw = resources.resolve(awFileName);
if (Files.exists(aw)) {
template.addProperty("accessWidener", awFileName);
}

return template;
}

public void tryMerge(JsonObject root, String key, JsonElement value) {
JsonElement existing = root.get(key);
if (existing == null) {
root.add(key, value);
return;
}
if (value.getClass() != existing.getClass()) {
// no way to merge, just overwrite
root.add(key, value);
return;
}
if (value instanceof JsonObject obj) {
mergeObj(root, key, (JsonObject) existing, obj);
} else if (value instanceof JsonArray array) {
mergeArray(root, key, (JsonArray) existing, array);
} else {
// overwrite existing
root.add(key, value);
}
}

public void mergeObj(JsonObject json, String key, JsonObject existing, JsonObject toAdd) {
for (Map.Entry<String, JsonElement> entry : toAdd.entrySet()) {
String entryKey = entry.getKey();
JsonElement value = entry.getValue();
tryMerge(existing, entryKey, value);
}
json.add(key, existing);
}

public void mergeArray(JsonObject json, String key, JsonArray existing, JsonArray toAdd) {
for (JsonElement element : toAdd) {
existing.add(element);
}
json.add(key, existing);
}

public JsonObject makeDefaults() {
Path templateFmj = this.rootProjectDirPath.resolve(TEMPLATE_FMJ);
return PortingLibBuildPlugin.jsonFromPath(templateFmj);
}

public static class Configurator extends Closure<Object> {
private final Project project;

public Configurator(Project project) {
super(project);
this.project = project;
}

public Object doCall(Object task) {
if (task instanceof ProcessResources processResources) {
processResources.eachFile(new Applicator(this.project));
}
return task;
}
}

public record Applicator(Project project) implements Action<FileCopyDetails> {
@Override
public void execute(FileCopyDetails details) {
if (details.getName().equals(FMJ)) {
Map<String, String> params = Map.of(
PROJECT_NAME_PARAM, this.project.getName(),
PROJECT_DIR_PARAM, getDir(this.project),
ROOT_PROJECT_DIR_PARAM, getDir(this.project.getRootProject())
);

details.filter(params, FmjExpander.class);
}
}

private static String getDir(Project project) {
return project.getLayout().getProjectDirectory().getAsFile().toPath().toString();
}
}
}
Loading

0 comments on commit 88c79f2

Please sign in to comment.