Skip to content

Commit

Permalink
Fix generator to work on Windows
Browse files Browse the repository at this point in the history
- Tests should work on Windows and use consistent separator.
- Simplify test logic and de-duplicate assertions.
- Rename project generator to build generator for consistency.
  • Loading branch information
tresat committed Nov 11, 2024
1 parent ee9ed7e commit 863ec47
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 44 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package org.gradle.api.experimental.android;

import org.gradle.api.Plugin;
import org.gradle.api.experimental.buildinit.StaticProjectGenerator;
import org.gradle.api.experimental.buildinit.StaticProjectSpec;
import org.gradle.api.experimental.buildinit.StaticBuildGenerator;
import org.gradle.api.experimental.buildinit.StaticBuildSpec;
import org.gradle.api.initialization.Settings;
import org.gradle.buildinit.specs.internal.BuildInitSpecRegistry;

Expand All @@ -14,10 +14,10 @@ public abstract class AndroidEcosystemInitPlugin implements Plugin<Settings> {
@Override
public void apply(Settings settings) {
// TODO: Add something for generating AGP-based templates
getBuildInitSpecRegistry().register(StaticProjectGenerator.class, List.of(
new StaticProjectSpec("android-application", "Android application with Android libraries"),
new StaticProjectSpec("android-application-basic-activity", "Android Application with a basic Activity"),
new StaticProjectSpec("android-application-empty-activity", "Android Application with an empty Activity")
getBuildInitSpecRegistry().register(StaticBuildGenerator.class, List.of(
new StaticBuildSpec("android-application", "Android application with Android libraries"),
new StaticBuildSpec("android-application-basic-activity", "Android Application with a basic Activity"),
new StaticBuildSpec("android-application-empty-activity", "Android Application with an empty Activity")
));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package org.gradle.util

import org.apache.commons.io.FileUtils
import spock.lang.Specification

class ResourceLoaderIntegrationTest extends Specification {
File outputDir
import static org.gradle.test.util.TestAssertions.assertDirContainsExactly

def setup() {
outputDir = new File("build/tmp/integTest/output").tap { deleteDir() }
class ResourceLoaderIntegrationTest extends Specification {
private File outputDir = new File("build/tmp/integTest/output").tap {
deleteDir()
mkdirs()
}

def "can load resource from jar file"() {
Expand All @@ -18,12 +18,6 @@ class ResourceLoaderIntegrationTest extends Specification {
resourceLoader.extractDirectoryFromResources("templates/java-library", outputDir)

then:
assertOutputIs(['build.gradle.dcl', 'src/main/java/com/example/lib/Library.java'])
}

private void assertOutputIs(List<String> expectedRelativePaths) {
def actualPaths = FileUtils.listFiles(outputDir, null, true)*.path.sort()
def expectedPaths = expectedRelativePaths.collect { "${outputDir.toPath()}/$it".toString() }.sort()
assert actualPaths == expectedPaths
assertDirContainsExactly(outputDir, ['build.gradle.dcl', 'src/main/java/com/example/lib/Library.java'])
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,23 @@
import org.gradle.buildinit.specs.BuildInitGenerator;
import org.gradle.util.ResourceLoader;

import java.io.File;

///**
// * A {@link BuildInitGenerator} that generates a project from a static template packaged
// * A {@link BuildInitGenerator} that generates a Gradle build from a static template packaged
// * as resources files in the {@link #TEMPLATES_ROOT} directory.
// */
@SuppressWarnings("UnstableApiUsage")
public abstract class StaticProjectGenerator implements BuildInitGenerator {
public abstract class StaticBuildGenerator implements BuildInitGenerator {
private static final String TEMPLATES_ROOT = "templates";

@Override
public void generate(BuildInitConfig config, Directory projectDir) {
if (!(config.getBuildSpec() instanceof StaticProjectSpec projectSpec)) {
if (!(config.getBuildSpec() instanceof StaticBuildSpec projectSpec)) {
throw new IllegalArgumentException("Unknown project type: " + config.getBuildSpec().getDisplayName() + " (" + config.getBuildSpec().getClass().getName() + ")");
}

String templatePath = TEMPLATES_ROOT + "/" + projectSpec.getType();
String templatePath = TEMPLATES_ROOT + File.separatorChar + projectSpec.getType();
ResourceLoader resourceLoader = new ResourceLoader();

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@
import java.util.List;

/**
* An {@link BuildInitSpec} that represents a project that can be generated from a static template
* using the {@link StaticProjectGenerator}.
* An {@link BuildInitSpec} that represents a build that can be generated from a static template
* using the {@link StaticBuildGenerator}.
* <p>
* The relative path to the root template directory from the {@code /templates`} directory in the
* root of the resources dir (and classpath in the library jar) should be the same as the type.
*/
@SuppressWarnings("UnstableApiUsage")
public final class StaticProjectSpec implements BuildInitSpec {
public final class StaticBuildSpec implements BuildInitSpec {
private final String type;
private final String displayName;

public StaticProjectSpec(String type, String displayName) {
public StaticBuildSpec(String type, String displayName) {
this.type = type;
this.displayName = displayName;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ private static void copyDirectoryFromJar(String relativePath, File destDir, JarU
JarEntry entry = iterator.next();
String entryName = entry.getName();

if (entryName.startsWith(relativePath + File.separator)) {
if (entryName.startsWith(relativePath + "/") || entryName.startsWith(relativePath + "\\")) {
String entrySuffix = entryName.substring(relativePath.length());
File destFile = new File(destDir, entrySuffix);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
package org.gradle.util

import org.apache.commons.io.FileUtils

import spock.lang.Specification

import static org.gradle.test.util.TestAssertions.assertDirContainsExactly

class ResourceLoaderTest extends Specification {
private File outputDir = new File("output").tap {
deleteDir()
mkdirs()
}

def "can extract resource directory"() {
given:
File output = new File("output").tap { mkdirs() }
ResourceLoader resourceLoader = new ResourceLoader()

when:
resourceLoader.extractDirectoryFromResources("templates/java-library", output)
resourceLoader.extractDirectoryFromResources("templates/java-library", outputDir)

then:
FileUtils.listFiles(output, null, true)*.path.sort() == ['output/build.gradle.dcl', 'output/src/main/java/com/example/lib/Library.java']
assertDirContainsExactly(outputDir, ['build.gradle.dcl', 'src/main/java/com/example/lib/Library.java'])
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.gradle.test.util

import org.gradle.internal.impldep.org.apache.commons.io.FileUtils

/**
* Static util class containing test assertions.
*/
class TestAssertions {
private TestAssertions() { /* not instantiable */ }

static void assertDirContainsExactly(File outputDir, List<String> expectedRelativePaths) {
def actualPaths = FileUtils.listFiles(outputDir, null, true)*.path.sort()
def expectedPaths = expectedRelativePaths.collect { new File(outputDir, it).path }.sort()
assert actualPaths == expectedPaths
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package org.gradle.api.experimental.jvm;

import org.gradle.api.Plugin;
import org.gradle.api.experimental.buildinit.StaticProjectGenerator;
import org.gradle.api.experimental.buildinit.StaticProjectSpec;
import org.gradle.api.experimental.buildinit.StaticBuildGenerator;
import org.gradle.api.experimental.buildinit.StaticBuildSpec;
import org.gradle.api.initialization.Settings;
import org.gradle.buildinit.specs.internal.BuildInitSpecRegistry;

Expand All @@ -16,8 +16,8 @@ public abstract class JvmEcosystemInitPlugin implements Plugin<Settings> {

@Override
public void apply(Settings settings) {
getBuildInitSpecRegistry().register(StaticProjectGenerator.class, List.of(
new StaticProjectSpec("java-application", "Declarative Java Application Project")
getBuildInitSpecRegistry().register(StaticBuildGenerator.class, List.of(
new StaticBuildSpec("java-application", "Declarative Java Application Project")
));
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
package org.gradle.api.experimental.kmp;

import org.gradle.api.Plugin;
import org.gradle.api.experimental.buildinit.StaticProjectGenerator;
import org.gradle.api.experimental.buildinit.StaticProjectSpec;
import org.gradle.api.experimental.jvm.JvmEcosystemConventionsPlugin;
import org.gradle.api.experimental.kotlin.StandaloneKotlinJvmApplicationPlugin;
import org.gradle.api.experimental.kotlin.StandaloneKotlinJvmLibraryPlugin;
import org.gradle.api.experimental.buildinit.StaticBuildGenerator;
import org.gradle.api.experimental.buildinit.StaticBuildSpec;
import org.gradle.api.initialization.Settings;
import org.gradle.api.internal.plugins.software.RegistersSoftwareTypes;
import org.gradle.buildinit.specs.internal.BuildInitSpecRegistry;

import javax.inject.Inject;
Expand All @@ -18,8 +14,8 @@
public abstract class KmpEcosystemInitPlugin implements Plugin<Settings> {
@Override
public void apply(Settings settings) {
getBuildInitSpecRegistry().register(StaticProjectGenerator.class, List.of(
new StaticProjectSpec("kotlin-application", "Declarative Kotlin (JVM) Application Project")
getBuildInitSpecRegistry().register(StaticBuildGenerator.class, List.of(
new StaticBuildSpec("kotlin-application", "Declarative Kotlin (JVM) Application Project")
));
}

Expand Down

0 comments on commit 863ec47

Please sign in to comment.