-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1057046
commit 4698e5b
Showing
10 changed files
with
339 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
loader/src/test/java/net/neoforged/fml/test/RuntimeCompiler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package net.neoforged.fml.test; | ||
|
||
import com.google.errorprone.annotations.CheckReturnValue; | ||
import java.io.IOException; | ||
import java.net.URI; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.FileSystem; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Locale; | ||
import javax.tools.DiagnosticCollector; | ||
import javax.tools.JavaCompiler; | ||
import javax.tools.JavaFileObject; | ||
import javax.tools.SimpleJavaFileObject; | ||
import javax.tools.StandardJavaFileManager; | ||
import javax.tools.StandardLocation; | ||
import javax.tools.ToolProvider; | ||
import org.intellij.lang.annotations.Language; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class RuntimeCompiler { | ||
private static final Logger LOGGER = LoggerFactory.getLogger(RuntimeCompiler.class); | ||
private static final JavaCompiler COMPILER = ToolProvider.getSystemJavaCompiler(); | ||
|
||
private final DiagnosticCollector<JavaFileObject> diagnostics; | ||
private final StandardJavaFileManager manager; | ||
|
||
public RuntimeCompiler(FileSystem targetFS) { | ||
this.diagnostics = new DiagnosticCollector<>(); | ||
this.manager = COMPILER.getStandardFileManager(diagnostics, Locale.ROOT, StandardCharsets.UTF_8); | ||
try { | ||
manager.setLocationFromPaths(StandardLocation.CLASS_OUTPUT, List.of(targetFS.getPath("/"))); | ||
} catch (IOException e) { | ||
throw new RuntimeException("Failed to set root output location", e); | ||
} | ||
} | ||
|
||
public CompilationBuilder builder() { | ||
return new CompilationBuilder(); | ||
} | ||
|
||
public class CompilationBuilder { | ||
private final List<JavaFileObject> files = new ArrayList<>(); | ||
|
||
@CheckReturnValue | ||
public CompilationBuilder addClass(String name, @Language("java") String content) { | ||
if (!content.trim().startsWith("package ")) { | ||
List<String> nameByDot = new ArrayList<>(Arrays.asList(name.split("\\."))); | ||
nameByDot.removeLast(); | ||
|
||
content = ("package " + String.join(".", nameByDot) + ";\n" + content); | ||
} | ||
this.files.add(new JavaSourceFromString(name, content)); | ||
return this; | ||
} | ||
|
||
public void compile() { | ||
var task = COMPILER.getTask(null, manager, diagnostics, null, null, files); | ||
if (!task.call()) { | ||
diagnostics.getDiagnostics().forEach(diagnostic -> LOGGER.error("Failed to compile: {}", diagnostic)); | ||
throw new RuntimeException("Failed to compile class"); | ||
} | ||
} | ||
} | ||
|
||
public static class JavaSourceFromString extends SimpleJavaFileObject { | ||
private final String sourceCode; | ||
|
||
public JavaSourceFromString(String name, String sourceCode) { | ||
super(URI.create("string:///" + name.replace('.', '/') + Kind.SOURCE.extension), Kind.SOURCE); | ||
this.sourceCode = sourceCode; | ||
} | ||
|
||
@Override | ||
public CharSequence getCharContent(boolean ignoreEncodingErrors) { | ||
return sourceCode; | ||
} | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
loader/src/test/java/net/neoforged/fml/test/TestModFile.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package net.neoforged.fml.test; | ||
|
||
import com.electronwill.nightconfig.toml.TomlFormat; | ||
import com.electronwill.nightconfig.toml.TomlParser; | ||
import com.google.common.jimfs.Configuration; | ||
import com.google.common.jimfs.Jimfs; | ||
import com.google.errorprone.annotations.CheckReturnValue; | ||
import cpw.mods.jarhandling.JarContentsBuilder; | ||
import cpw.mods.jarhandling.SecureJar; | ||
import java.io.IOException; | ||
import java.nio.file.FileSystem; | ||
import net.neoforged.fml.loading.moddiscovery.ModFile; | ||
import net.neoforged.fml.loading.moddiscovery.ModFileInfo; | ||
import net.neoforged.fml.loading.moddiscovery.ModJarMetadata; | ||
import net.neoforged.fml.loading.moddiscovery.NightConfigWrapper; | ||
import net.neoforged.fml.loading.modscan.Scanner; | ||
import net.neoforged.neoforgespi.locating.ModFileDiscoveryAttributes; | ||
import net.neoforged.neoforgespi.locating.ModFileInfoParser; | ||
import org.intellij.lang.annotations.Language; | ||
|
||
public class TestModFile extends ModFile implements AutoCloseable { | ||
private static final TomlParser PARSER = TomlFormat.instance().createParser(); | ||
|
||
private final FileSystem fileSystem; | ||
private final RuntimeCompiler compiler; | ||
|
||
private TestModFile(SecureJar jar, FileSystem fileSystem, ModFileInfoParser parser) { | ||
super(jar, parser, new ModFileDiscoveryAttributes(null, null, null, null)); | ||
this.fileSystem = fileSystem; | ||
this.compiler = new RuntimeCompiler(fileSystem); | ||
} | ||
|
||
private static TestModFile buildFile(FileSystem fileSystem, ModFileInfoParser parser) { | ||
var jc = new JarContentsBuilder() | ||
.paths(fileSystem.getPath("/")) | ||
.build(); | ||
var metadata = new ModJarMetadata(jc); | ||
var sj = SecureJar.from(jc, metadata); | ||
var mod = new TestModFile(sj, fileSystem, parser); | ||
metadata.setModFile(mod); | ||
return mod; | ||
} | ||
|
||
@CheckReturnValue | ||
public RuntimeCompiler.CompilationBuilder classBuilder() { | ||
return compiler.builder(); | ||
} | ||
|
||
public void scan() { | ||
setScanResult(new Scanner(this).scan(), null); | ||
} | ||
|
||
@CheckReturnValue | ||
public static TestModFile newInstance(@Language("toml") String modsDotToml) { | ||
final var fs = Jimfs.newFileSystem(Configuration.unix() | ||
.toBuilder() | ||
.setWorkingDirectory("/") | ||
.build()); | ||
var wrapper = new NightConfigWrapper(PARSER.parse(modsDotToml)); | ||
return buildFile(fs, file -> new ModFileInfo((ModFile) file, wrapper, wrapper::setFile)); | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
getSecureJar().getRootPath().getFileSystem().close(); | ||
fileSystem.close(); | ||
} | ||
|
||
@Override | ||
public String getFileName() { | ||
return "dummy mod.jar"; | ||
} | ||
} |
Oops, something went wrong.