-
Notifications
You must be signed in to change notification settings - Fork 25k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Wrap VersionPropertiesLoader in a BuildService to decouple build logic projects #78704
Merged
breskeby
merged 3 commits into
elastic:master
from
breskeby:decouple-build-tools-configuration
Oct 7, 2021
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
...ain/java/org/elasticsearch/gradle/internal/conventions/VersionPropertiesBuildService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
package org.elasticsearch.gradle.internal.conventions; | ||
|
||
import org.apache.commons.io.FileUtils; | ||
import org.gradle.api.GradleException; | ||
import org.gradle.api.JavaVersion; | ||
import org.gradle.api.file.RegularFileProperty; | ||
import org.gradle.api.provider.ProviderFactory; | ||
import org.gradle.api.services.BuildService; | ||
import org.gradle.api.services.BuildServiceParameters; | ||
import org.gradle.initialization.layout.BuildLayout; | ||
import org.gradle.initialization.layout.BuildLayoutFactory; | ||
|
||
import javax.inject.Inject; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.util.Properties; | ||
import java.util.function.Function; | ||
|
||
abstract class VersionPropertiesBuildService implements BuildService<VersionPropertiesBuildService.Params>, AutoCloseable { | ||
|
||
private final Properties properties; | ||
|
||
@Inject | ||
public VersionPropertiesBuildService(ProviderFactory providerFactory) { | ||
File infoPath = getParameters().getInfoPath().getAsFile().get(); | ||
try { | ||
File propertiesInputFile = new File(infoPath, "version.properties"); | ||
properties = VersionPropertiesLoader.loadBuildSrcVersion(propertiesInputFile, providerFactory); | ||
properties.computeIfAbsent("minimumJava", s -> resolveMinimumJavaVersion(infoPath)); | ||
} catch (IOException e) { | ||
throw new GradleException("Cannot load VersionPropertiesBuildService", e); | ||
} | ||
} | ||
|
||
private JavaVersion resolveMinimumJavaVersion(File infoPath) { | ||
final JavaVersion minimumJavaVersion; | ||
File minimumJavaInfoSource = new File(infoPath, "src/main/resources/minimumCompilerVersion"); | ||
try { | ||
String versionString = FileUtils.readFileToString(minimumJavaInfoSource); | ||
minimumJavaVersion = JavaVersion.toVersion(versionString); | ||
} catch (IOException e) { | ||
throw new GradleException("Cannot resolve minimum compiler version via VersionPropertiesBuildService", e); | ||
} | ||
return minimumJavaVersion; | ||
} | ||
|
||
public Properties getProperties() { | ||
return properties; | ||
} | ||
|
||
@Override | ||
public void close() throws Exception { | ||
} | ||
|
||
public interface Params extends BuildServiceParameters { | ||
RegularFileProperty getInfoPath(); | ||
} | ||
|
||
} |
38 changes: 38 additions & 0 deletions
38
.../src/main/java/org/elasticsearch/gradle/internal/conventions/VersionPropertiesPlugin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
package org.elasticsearch.gradle.internal.conventions; | ||
|
||
import org.gradle.api.Plugin; | ||
import org.gradle.api.Project; | ||
import org.gradle.api.provider.Provider; | ||
import org.gradle.initialization.layout.BuildLayout; | ||
|
||
import javax.inject.Inject; | ||
import java.io.File; | ||
|
||
public class VersionPropertiesPlugin implements Plugin<Project> { | ||
|
||
private BuildLayout buildLayout; | ||
|
||
@Inject | ||
VersionPropertiesPlugin(BuildLayout buildLayout) { | ||
this.buildLayout = buildLayout; | ||
} | ||
|
||
@Override | ||
public void apply(Project project) { | ||
mark-vieira marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Register the service if not done yet | ||
File infoPath = new File(buildLayout.getRootDirectory(), "build-tools-internal"); | ||
Provider<VersionPropertiesBuildService> serviceProvider = project.getGradle().getSharedServices() | ||
.registerIfAbsent("versions", VersionPropertiesBuildService.class, spec -> { | ||
spec.getParameters().getInfoPath().set(infoPath); | ||
}); | ||
project.getExtensions().add("versions", serviceProvider.forUseAtConfigurationTime().get().getProperties()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I definitely like this pattern of adding the service itself as a project extension 👍 |
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we move this to the bottom of the class definition?