Skip to content

Commit

Permalink
Work around apparent bug in Gradle 6.1 DirectoryProperty#file (#411)
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamin-bader authored Sep 20, 2021
1 parent 1d6a0e7 commit 1951f2d
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,28 @@ class IntegrationSpec extends Specification {
"3.4.0" | "6.5.1" || 7435 | 926 | 3847
}

@Unroll
def "completes successfully using AGP #agpVersion and Gradle #gradleVersion"() {
given: "an integration test project"
def project = projectDir(agpVersion, gradleVersion)

when:
def result = GradleRunner.create()
.withGradleVersion(gradleVersion)
.withProjectDir(project)
.withArguments(":app:countDebugDexMethods", "--stacktrace")
.build()

then:
result.task(":app:countDebugDexMethods").outcome == TaskOutcome.SUCCESS

// These version combinations were known to fail at some point.
// This spec serves to guard against regression.
where:
agpVersion | gradleVersion | reportedIn
"4.0.0" | "6.1.1" | "https://github.com/KeepSafe/dexcount-gradle-plugin/issues/410"
}

@Unroll
def "counting AARs using AGP #agpVersion and Gradle #gradleVersion"() {
given: "an integration test project"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ protected void createTask(
String treePath = path.replace("outputs", "intermediates") + "/tree.compact.gz";

TaskProvider<LegacyGeneratePackageTreeTask> gen = getProject().getTasks().register(treeTaskName, LegacyGeneratePackageTreeTask.class, t -> {
t.setDescription("Generates dex method count for ${variant.name}.");
t.setDescription("Generates dex method count for " + variant.getName() + ".");
t.setGroup("Reporting");

t.getConfigProperty().set(getExt());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ public interface Params extends WorkParameters {
Property<PrintOptions> getPrintOptions();
}

private File outputDirectory = null;

@Override
public void execute() {
try {
PackageTree packageTree = generatePackageTree();

File outputDir = getParameters().getOutputDirectory().get().getAsFile();
FileUtils.deleteDirectory(outputDir);
FileUtils.forceMkdir(outputDir);
ensureCleanOutputDirectory();

writeIntermediateThriftFile(packageTree);
writeSummaryFile(packageTree);
Expand All @@ -72,6 +72,11 @@ public void execute() {
}
}

private void ensureCleanOutputDirectory() throws IOException {
FileUtils.deleteDirectory(getOutputDirectory());
FileUtils.forceMkdir(getOutputDirectory());
}

private void writeIntermediateThriftFile(PackageTree packageTree) throws IOException {
TreeGenOutput thrift = new TreeGenOutput.Builder()
.tree(PackageTree.toThrift(packageTree))
Expand All @@ -92,7 +97,7 @@ private void writeIntermediateThriftFile(PackageTree packageTree) throws IOExcep
}

private void writeSummaryFile(PackageTree packageTree) throws IOException {
File summaryFile = getParameters().getOutputDirectory().file("summary.csv").get().getAsFile();
File summaryFile = new File(getOutputDirectory(), "summary.csv");
FileUtils.forceMkdirParent(summaryFile);

String headers = "methods,fields,classes";
Expand All @@ -109,7 +114,7 @@ private void writeSummaryFile(PackageTree packageTree) throws IOException {
}

private void writeChartFiles(PackageTree packageTree) throws IOException {
File chartDirectory = getParameters().getOutputDirectory().dir("chart").get().getAsFile();
File chartDirectory = new File(getOutputDirectory(), "chart");
FileUtils.forceMkdir(chartDirectory);

PrintOptions options = getParameters().getPrintOptions().get()
Expand Down Expand Up @@ -140,13 +145,20 @@ private void writeChartFiles(PackageTree packageTree) throws IOException {
private void writeFullTree(PackageTree packageTree) throws IOException {
PrintOptions options = getParameters().getPrintOptions().get();
String fullCountFileName = getParameters().getOutputFileName().get() + options.getOutputFormat().getExtension();
File fullCountFile = getParameters().getOutputDirectory().file(fullCountFileName).get().getAsFile();
File fullCountFile = new File(getOutputDirectory(), fullCountFileName);

try (BufferedWriter bw = Files.newBufferedWriter(fullCountFile.toPath())) {
packageTree.print(bw, options.getOutputFormat(), options);
}
}

private File getOutputDirectory() {
if (outputDirectory == null) {
outputDirectory = getParameters().getOutputDirectory().get().getAsFile();
}
return outputDirectory;
}

protected abstract PackageTree generatePackageTree() throws IOException;

protected abstract String getInputRepresentation();
Expand Down

0 comments on commit 1951f2d

Please sign in to comment.