Skip to content

Commit

Permalink
Added a bit of test
Browse files Browse the repository at this point in the history
  • Loading branch information
shartte committed Jul 21, 2024
1 parent fc899e7 commit 4eb2a55
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/main/java/net/neoforged/moddevgradle/tasks/JarJar.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.gradle.api.file.FileSystemOperations;
import org.gradle.api.model.ObjectFactory;
import org.gradle.api.tasks.InputFiles;
import org.gradle.api.tasks.Internal;
import org.gradle.api.tasks.Nested;
import org.gradle.api.tasks.OutputDirectory;
import org.gradle.api.tasks.SkipWhenEmpty;
Expand Down Expand Up @@ -56,27 +57,36 @@ public abstract class JarJar extends DefaultTask {
@OutputDirectory
public abstract DirectoryProperty getOutputDirectory();

@Internal
public abstract DirectoryProperty getBuildDirectory();

private final FileSystemOperations fileSystemOperations;

@Inject
public JarJar(FileSystemOperations fileSystemOperations) {
this.fileSystemOperations = fileSystemOperations;
this.getOutputDirectory().convention(getProject().getLayout().getBuildDirectory().dir("generated/" + getName()));
this.getBuildDirectory().convention(getProject().getLayout().getBuildDirectory());
}

@TaskAction
protected void run() {
List<ResolvedJarJarArtifact> includedJars = new ArrayList<>(getJarJarArtifacts().getResolvedArtifacts().get());
fileSystemOperations.delete(spec -> spec.delete(getOutputDirectory()));

var buildDir = getBuildDirectory().getAsFile().get().toPath();

var artifactFiles = new ArrayList<>(includedJars.stream().map(ResolvedJarJarArtifact::getFile).toList());
// Now we have to handle pure file collection dependencies that do not have artifact ids
for (var file : getInputFiles()) {
if (!artifactFiles.contains(file)) {
// This is only intended for libraries built by this project, so this is a shoddy check
if (!file.toPath().startsWith(buildDir)) {
throw new GradleException("Cannot embed file dependencies in the jar if they aren't in this projects build directory.");
}

// Create a hashcode to use as a version
var hashCode = hashFile(file);

// We only support file dependencies that are likely built by this project
includedJars.add(new ResolvedJarJarArtifact(
file,
file.getName(),
Expand Down
19 changes: 19 additions & 0 deletions testproject/jijtest/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,26 @@ plugins {
id 'net.neoforged.moddev'
}

sourceSets {
service
}

def serviceJar = tasks.register("serviceJar", Jar) {
from(sourceSets.service.output)
archiveClassifier = "service"
manifest {
attributes(
'FMLModType': "LIBRARY"
)
}
}

dependencies {
implementation files(sourceSets.service.output)
testImplementation 'org.junit.jupiter:junit-jupiter:5.10.2'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
jarJar(project(":coremod"))
jarJar(files(serviceJar))

jarJar("org.commonmark:commonmark") {
version {
Expand Down Expand Up @@ -35,6 +51,9 @@ neoForge {
coremod {
dependency project(":coremod")
}
service {
sourceSet sourceSets.service
}
}

unitTest {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package jijtestservice;

public class Service {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Manifest-Version: 1.0
Note: Dummy Manifest file only for in-dev runtime
FMLModType: LIBRARY
5 changes: 5 additions & 0 deletions testproject/jijtest/src/test/java/jijtest/CoreModTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,10 @@ public class CoreModTest {
void testPresenceOfCoreMod() throws Exception {
var field = assertDoesNotThrow(() -> ItemStack.class.getField("CORE_MOD_MARKER"));
assertTrue(field.getBoolean(null));

var obj = new jijtestservice.Service();
// ensures it is *not* a transforming classloader, meaning it was loaded in our parent layer
assertEquals("cpw.mods.cl.ModuleClassLoader", obj.getClass().getClassLoader().getClass().getName());
assertEquals("cpw.mods.modlauncher.TransformingClassLoader", getClass().getClassLoader().getClass().getName());
}
}

0 comments on commit 4eb2a55

Please sign in to comment.