Skip to content

Commit

Permalink
Ensure that generated resources are cleaned up correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
richardTingle committed Oct 27, 2024
1 parent 37a9523 commit 9ca449e
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.io.IOException;
import java.nio.file.Files;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class AssetsFunctionalTest extends FunctionalTestBase{
Expand Down Expand Up @@ -59,7 +60,9 @@ void localAssetConstants() throws IOException{
runner.withProjectDir(projectDir);
BuildResult result = runner.build();

String content = Files.readString(getGeneratedJavaFile("com/myproject/assets/Assets.java").toPath());
File assetsFile = getGeneratedJavaFile("com/myproject/assets/Assets.java");

String content = Files.readString(assetsFile.toPath());

String expectedMaterialsSection = """
public static class MatDefs{
Expand Down Expand Up @@ -95,6 +98,15 @@ public static String child(String child){

assertTrue(content.contains(expectedMaterialsSection), content);
assertTrue(content.contains(expectedTexturesSection), content);

GradleRunner runnerClean = GradleRunner.create();
runnerClean.forwardOutput();
runnerClean.withPluginClasspath();
runnerClean.withArguments("clean", "--stacktrace");
runnerClean.withProjectDir(projectDir);
BuildResult result2 = runnerClean.build();

assertFalse(assetsFile.exists());
}

/**
Expand Down Expand Up @@ -320,7 +332,9 @@ void jarAssetConstantsFlatFileListingWithFilter() throws IOException{
runner.withProjectDir(projectDir);
runner.build();

String content = Files.readString(getGeneratedResourcesFile("com_onemillionworlds_typedmaterials_assets.txt").toPath());
File assetsFile = getGeneratedResourcesFile("com_onemillionworlds_typedmaterials_assets.txt");

String content = Files.readString(assetsFile.toPath());

String expectedAssetsSection = """
::jme3-core-3.6.1-stable
Expand All @@ -333,5 +347,13 @@ void jarAssetConstantsFlatFileListingWithFilter() throws IOException{

assertTrue(content.contains(expectedAssetsSection), content);

GradleRunner runnerClean = GradleRunner.create();
runnerClean.forwardOutput();
runnerClean.withPluginClasspath();
runnerClean.withArguments("clean", "--stacktrace");
runnerClean.withProjectDir(projectDir);
BuildResult result2 = runnerClean.build();

assertFalse(assetsFile.exists());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,17 @@ public void apply(Project project) {
task.delete(project.file(extension.getGeneratedSourcesDir()));
});

project.getTasks().register("cleanTypedMaterialResources", Delete.class, task -> {
task.setGroup("typedMaterials");
task.delete(project.file(extension.getGeneratedResourcesDir()));
});

if(project.getTasks().findByName("clean") != null){
project.getTasks().named("clean").configure(compileJava -> compileJava.dependsOn("cleanTypedMaterials"));
project.getTasks().named("clean").configure(cleanTask -> {
cleanTask.dependsOn("cleanTypedMaterials");
cleanTask.dependsOn("cleanTypedMaterialResources");
});

}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,12 +192,15 @@ public void localMaterialsSearch(String outputPackage, String materialsDirectory

/**
* The directory where the generated sources will be placed relative to the module root.
* Default is "src/main/generated/java"
*/
public Property<String> getGeneratedSourcesDir(){
return generatedSourcesDir;
}

public Property<String> getGeneratedResourcesDir(){
return generatedResourcesDir;
}

public Property<String> getMaterialFactoryClass(){
return materialFactoryClass;
}
Expand Down

0 comments on commit 9ca449e

Please sign in to comment.