Update to use the new configuration cache safe GradleOperatingSystem#1338
Conversation
Generate changelog in
|
…:palantir/palantir-java-format into finlayw/migrate-to-gradleoperationsystem
| public static boolean isNativeImageConfigured(Project project, Provider<OperatingSystem> operatingSystem) { | ||
| return isNativeFlagEnabled(project) && isNativeImageSupported(operatingSystem); | ||
| } | ||
|
|
||
| private static boolean isNativeImageSupported() { | ||
| OperatingSystem os = OperatingSystem.get(); | ||
| private static boolean isNativeImageSupported(Provider<OperatingSystem> operatingSystem) { | ||
| OperatingSystem os = operatingSystem.get(); |
There was a problem hiding this comment.
we should just call operatingSystem inside here, it's a field.
There was a problem hiding this comment.
hmm, curious whether this get is avoidable where we are?
There was a problem hiding this comment.
I guess we could return a Provider<Boolean> but would we not have to realise that immediately above when we do:
!isNativeImageConfigured(rootProject, operatingSystem))
There was a problem hiding this comment.
I guess you could do:
isNativeImageConfigured(rootProject, operatingSystem).map(isConfigured -> {
if (!isConfigured) {
log.info("Skipping native image configuration as it is not supported on this platform");
return null;
}
....
but is that any better?
| transformSpec | ||
| .getFrom() | ||
| .attribute(ArtifactTypeDefinition.ARTIFACT_TYPE_ATTRIBUTE, getExtension(operatingSystem)); | ||
| .attribute(ArtifactTypeDefinition.ARTIFACT_TYPE_ATTRIBUTE, getExtension(operatingSystem.get())); |
There was a problem hiding this comment.
you're realising the operating system thing here, meaning it will always get called even if you're not going to resolve this configuration. You should use getFrom().attributeProvider(...)
There was a problem hiding this comment.
updated to:
attributeProvider(
ArtifactTypeDefinition.ARTIFACT_TYPE_ATTRIBUTE,
operatingSystem.map(NativeImageFormatProviderPlugin::getExtension));| @Nested | ||
| protected abstract GradleOperatingSystem getOs(); |
There was a problem hiding this comment.
I kinda hate having to pull this here, just to pass it in.
I have a slightly crazier idea that I think works a bit better:
Have a SpotlessInterop implement Action<JavaExtension>.
public abstract class SpotlessInterop implements Action<JavaExtension> {
@Nested
protected abstract GradleOperatingSystem getOs();
@Inject
protected abstract ConfigurationContainer getConfigurations();
public SpotlessInterop() {}
public void execute(JavaExtension java) {
java.addStep(spotlessJavaFormatStep());
}
private FormatterStep spotlessJavaFormatStep() {
// same as before but just use the methods instead of fields
}
}Usage then becomes:
SpotlessExtension spotlessExtension = project.getExtensions().getByType(SpotlessExtension.class);
// get from root project to have the right configurations
SpotlessInterop spotlessInterop = project.getRootProject().getObjects().newInstance(SpotlessInterop.class);
project.getPluginManager().withPlugin("java", _plugin -> {
SPOTLESS_PLUGINS.forEach(
spotlessPluginId -> project.getPluginManager().withPlugin(spotlessPluginId, _spotlessPlugin -> {
spotlessExtension.java(spotlessInterop);
}));
});Benefits being it's relatively self contained,
There was a problem hiding this comment.
I've converted to this format but needed to keep the project around to get the JavaFormatExtension and run isNativeImageConfigured - on the whole I do think it is neater though:
public abstract class SpotlessInterop implements Action<JavaExtension> {
private static final Logger logger = Logging.getLogger(SpotlessInterop.class);
private final Project project;
@Nested
protected abstract GradleOperatingSystem getOs();
@Inject
protected abstract ConfigurationContainer getConfigurations();
@Inject
public SpotlessInterop(Project project) {
this.project = project;
}
@Override
public void execute(JavaExtension java) {
java.addStep(spotlessJavaFormatStep());
}
private FormatterStep spotlessJavaFormatStep() {...}
kelvinou01
left a comment
There was a problem hiding this comment.
Nice! Excited to see this go in.
| return NativeImageFormatProviderPlugin.isNativeImageConfigured(rootProject) | ||
| private static Optional<Configuration> maybeGetNativeImplConfiguration( | ||
| Project rootProject, Provider<OperatingSystem> operatingSystem) { | ||
| return NativeImageFormatProviderPlugin.isNativeImageConfigured(rootProject, operatingSystem) |
There was a problem hiding this comment.
As discussed in person, we can make a @Nested NativeImageSupport type that contains these methods along with a @Nested GradleOperatingSystem and @Inject ProviderFactory to avoid having to pass this provider around everywhere
There was a problem hiding this comment.
so I've done this and made a NativeImageSupport type - currently it has a method boolean isNativeImageConfigured would it be better to make the is a Provider<Boolean> or does it not really matter?
| public abstract class PalantirJavaFormatIdeaPlugin implements Plugin<Project> { | ||
|
|
||
| @Nested | ||
| protected abstract NativeImageSupport getNativeImageConfigured(); |
| if (getNativeImageConfigured().isNativeImageConfigured()) { | ||
| task.getNativeImage().fileProvider(getNativeImplConfiguration(project)); |
There was a problem hiding this comment.
somehow my comment disappeared, or I never submitted, it, but similar thing here, task.getNativeImage().fileProvider() can take a provider
There was a problem hiding this comment.
so very similar to the attributeProvider thing you did
There was a problem hiding this comment.
I'm not sure if I've miss-understood this but getNativeImageConfigured().isNativeImageConfigured() returns a Boolean and not a Provider so I guess you could do something like:
task.getNativeImage()
.fileProvider(getNativeImplConfiguration(project).map(file -> {
if (getNativeImageSupport().isNativeImageConfigured()) {
return file;
}
return null;
}));but is that any better?
There was a problem hiding this comment.
actually that doesnt seem to work I get
Could not determine the dependencies of task ':formatDiff'.
> Failed to query the value of task ':formatDiff' property 'nativeImage'.
> Could not resolve all files for configuration ':palantirJavaFormatNative'.
> Cannot resolve external dependency com.palantir.javaformat:palantir-java-format-native:null because no repositories are defined.
Required by:
root project :
in the test
|
👍 |
1 similar comment
|
👍 |
|
I think there are probably nicer ways to get this in i.e. using attributes and module metadata, so we can really just resolve / run programs at the last possible second. I might throw something up at a later point, but in its current form it's pretty hard to avoid |
|
Released 2.70.0 |
Before this PR
palantir/gradle-baseline#3140 was being blocked by PJF using
OperatingSystemto run commands during the configuration phase via process builder. This break configuration cache and prevents merging.After this PR
palantir/gradle-utils#383 resolved this by introducing
GradleOperatingSystema configuration cache safe way of getting the operating system, this PR updates PJF to use the newGradleOperatingSystemclass fixing the issues allowing for baseline to merge==COMMIT_MSG==
Update to use the new configuration cache safe
GradleOperatingSystem==COMMIT_MSG==
Possible downsides?