Skip to content

Commit

Permalink
Disable build scans when properties task may have run
Browse files Browse the repository at this point in the history
Closes gh-69
  • Loading branch information
wilkinsona committed Jan 16, 2024
1 parent ecce1e1 commit 19d02ba
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020-2023 the original author or authors.
* Copyright 2020-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -76,32 +76,42 @@ private void apply(Project project) {

private void configureBuildScanConventions(BuildScanExtension buildScan, StartParameter startParameter,
File rootDir) {
if (!startParameter.isNoBuildScan()) {
ProcessOperationsProcessRunner processRunner = new ProcessOperationsProcessRunner(
new WorkingDirectoryProcessOperations(this.processOperations, rootDir));
if (startParameter.isBuildScan()) {
new AnonymousPublicationBuildScanConventions(processRunner) {

@Override
protected String getJdkVersion() {
String toolchainVersion = startParameter.getProjectProperties().get("toolchainVersion");
return (toolchainVersion != null) ? toolchainVersion : super.getJdkVersion();
}

}.execute(buildScan);
}
else {
new BuildScanConventions(processRunner) {
if (startParameter.isNoBuildScan() || containsPropertiesTask(startParameter)) {
return;
}
ProcessOperationsProcessRunner processRunner = new ProcessOperationsProcessRunner(
new WorkingDirectoryProcessOperations(this.processOperations, rootDir));
if (startParameter.isBuildScan()) {
new AnonymousPublicationBuildScanConventions(processRunner) {

@Override
protected String getJdkVersion() {
String toolchainVersion = startParameter.getProjectProperties().get("toolchainVersion");
return (toolchainVersion != null) ? toolchainVersion : super.getJdkVersion();
}

}.execute(buildScan);
}
else {
new BuildScanConventions(processRunner) {

@Override
protected String getJdkVersion() {
String toolchainVersion = startParameter.getProjectProperties().get("toolchainVersion");
return (toolchainVersion != null) ? toolchainVersion : super.getJdkVersion();
}
@Override
protected String getJdkVersion() {
String toolchainVersion = startParameter.getProjectProperties().get("toolchainVersion");
return (toolchainVersion != null) ? toolchainVersion : super.getJdkVersion();
}

}.execute(buildScan);
}
}

}.execute(buildScan);
private boolean containsPropertiesTask(StartParameter startParameter) {
for (String taskName : startParameter.getTaskNames()) {
if (taskName.equals("properties") || taskName.endsWith(":properties")) {
return true;
}
}
return false;
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020-2023 the original author or authors.
* Copyright 2020-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -66,6 +66,24 @@ void givenGradle6WhenThePluginIsAppliedAndBuildScansAreDisabledThenBuildScanConv
assertThat(result.getOutput()).contains("Capture task input files: false");
}

@Test
void givenGradle6WhenThePluginIsAppliedAndPropertiesTaskIsExecutedThenBuildScanConventionsAreNotApplied(
@TempDir File projectDir) {
prepareGradle6Project(projectDir);
BuildResult result = build(projectDir, "6.0.1", "properties", "verifyBuildScanConfig", "--no-scan");
assertThat(result.getOutput()).contains("Build scan server: null");
assertThat(result.getOutput()).contains("Capture task input files: false");
}

@Test
void givenGradle6MulitProjectBuildWhenThePluginIsAppliedAndPropertiesTaskIsExecutedThenBuildScanConventionsAreNotApplied(
@TempDir File projectDir) {
prepareGradle6MultiProject(projectDir);
BuildResult result = build(projectDir, "6.0.1", "sub:properties", "sub:verifyBuildScanConfig", "--no-scan");
assertThat(result.getOutput()).contains("Build scan server: null");
assertThat(result.getOutput()).contains("Capture task input files: false");
}

@Test
void givenGradle6WhenThePluginIsAppliedAndScanIsSpecifiedThenServerIsNotCustomized(@TempDir File projectDir) {
prepareGradle6Project(projectDir);
Expand Down Expand Up @@ -99,6 +117,15 @@ void givenGradle5WhenThePluginIsAppliedAndBuildScansAreDisabledThenBuildScanConv
assertThat(result.getOutput()).contains("Capture task input files: false");
}

@Test
void givenGradle5WhenThePluginIsAppliedAndPropertiesTaskIsExecutedThenBuildScanConventionsAreNotApplied(
@TempDir File projectDir) {
prepareGradle5Project(projectDir);
BuildResult result = build(projectDir, "5.6.4", "properties", "verifyBuildScanConfig", "--no-scan");
assertThat(result.getOutput()).contains("Build scan server: null");
assertThat(result.getOutput()).contains("Capture task input files: false");
}

@Test
void givenGradle5WhenThePluginIsAppliedAndScanIsSpecifiedThenServerIsNotCustomized(@TempDir File projectDir) {
prepareGradle5Project(projectDir);
Expand Down Expand Up @@ -147,7 +174,34 @@ private void prepareGradle6Project(File projectDir) {
});
}

private void prepareGradle6MultiProject(File projectDir) {
write(new File(projectDir, "gradle.properties"), (writer) -> writer.println("org.gradle.caching=true"));
write(new File(projectDir, "settings.gradle"), (writer) -> {
writer.println("plugins {");
writer.println(" id 'com.gradle.enterprise'");
writer.println(" id 'io.spring.ge.conventions' version '" + version() + "'");
writer.println("}");
writer.println("include 'sub'");
writer.println("gradle.afterProject { project -> project.ext['settings'] = settings }");
});
write(new File(new File(projectDir, "sub"), "build.gradle"), (writer) -> {
writer.println("task verifyBuildScanConfig {");
writer.println(" doFirst {");
writer.println(" println \"Build scan server: ${buildScan.server}\"");
writer.println(" println \"Capture task input files: ${buildScan.captureTaskInputFiles}\"");
writer.println(" }");
writer.println("}");
writer.println("task verifyBuildCacheConfig {");
writer.println(" doFirst {");
writer
.println(" println \"Build cache remote: ${project.ext['settings'].buildCache?.remote?.url}\"");
writer.println(" }");
writer.println("}");
});
}

private void write(File file, Consumer<PrintWriter> consumer) {
file.getParentFile().mkdirs();
try (PrintWriter writer = new PrintWriter(new FileWriter(file))) {
consumer.accept(writer);
}
Expand Down

0 comments on commit 19d02ba

Please sign in to comment.