-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New plugin: 'com.palantir.baseline-enable-preview-flag' (#1549)
Add the new line `apply plugin: 'com.palantir.baseline-enable-preview-flag'` to your subprojects block to enable the usage of unreleased java features (e.g. records, switch expressions, var keyword etc). Note, this plugin is a no-op on any project where you have a low sourceCompatibility.
- Loading branch information
Showing
6 changed files
with
327 additions
and
5 deletions.
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
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,8 @@ | ||
type: feature | ||
feature: | ||
description: |- | ||
Add the `apply plugin: 'com.palantir.baseline-enable-preview-flag'` to your subprojects block to enable the usage of unreleased java features (e.g. records, switch expressions, var keyword etc). | ||
Note, this plugin is a no-op on any project where you have a low sourceCompatibility. | ||
links: | ||
- https://github.com/palantir/gradle-baseline/pull/1549 |
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
97 changes: 97 additions & 0 deletions
97
...aseline-java/src/main/groovy/com/palantir/baseline/plugins/BaselineEnablePreviewFlag.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,97 @@ | ||
/* | ||
* (c) Copyright 2020 Palantir Technologies Inc. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.palantir.baseline.plugins; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
import org.gradle.api.JavaVersion; | ||
import org.gradle.api.Plugin; | ||
import org.gradle.api.Project; | ||
import org.gradle.api.plugins.JavaPluginConvention; | ||
import org.gradle.api.provider.Provider; | ||
import org.gradle.api.tasks.JavaExec; | ||
import org.gradle.api.tasks.compile.JavaCompile; | ||
import org.gradle.api.tasks.javadoc.Javadoc; | ||
import org.gradle.api.tasks.testing.Test; | ||
import org.gradle.external.javadoc.CoreJavadocOptions; | ||
import org.gradle.process.CommandLineArgumentProvider; | ||
|
||
public final class BaselineEnablePreviewFlag implements Plugin<Project> { | ||
|
||
private static final String FLAG = "--enable-preview"; | ||
|
||
@Override | ||
public void apply(Project project) { | ||
// The idea behind registering a single 'extra property' is that other plugins (like | ||
// sls-packaging) can easily detect this and also also add the --enable-preview jvm arg | ||
Provider<Boolean> enablePreview = project.provider(() -> { | ||
JavaVersion jvmExecutingGradle = JavaVersion.current(); | ||
JavaPluginConvention javaConvention = project.getConvention().findPlugin(JavaPluginConvention.class); | ||
if (javaConvention == null) { | ||
return false; | ||
} | ||
|
||
return javaConvention.getSourceCompatibility() == jvmExecutingGradle; | ||
}); | ||
|
||
project.getExtensions().getExtraProperties().set("enablePreview", enablePreview); | ||
|
||
project.getPlugins().withId("java", _unused -> { | ||
project.getTasks().withType(JavaCompile.class, t -> { | ||
List<CommandLineArgumentProvider> args = t.getOptions().getCompilerArgumentProviders(); | ||
args.add(new MaybeEnablePreview(enablePreview)); // mutation is gross, but it's the gradle convention | ||
}); | ||
project.getTasks().withType(Test.class, t -> { | ||
t.getJvmArgumentProviders().add(new MaybeEnablePreview(enablePreview)); | ||
}); | ||
project.getTasks().withType(JavaExec.class, t -> { | ||
t.getJvmArgumentProviders().add(new MaybeEnablePreview(enablePreview)); | ||
}); | ||
|
||
// sadly we have to use afterEvaluate because the Javadoc task doesn't support passing in providers | ||
project.afterEvaluate(_unused2 -> { | ||
if (enablePreview.get()) { | ||
JavaVersion sourceCompat = project.getConvention() | ||
.getPlugin(JavaPluginConvention.class) | ||
.getSourceCompatibility(); | ||
project.getTasks().withType(Javadoc.class, t -> { | ||
CoreJavadocOptions options = (CoreJavadocOptions) t.getOptions(); | ||
|
||
// Yes truly javadoc wants a single leading dash, other javac wants a double leading dash. | ||
// We also have to use these manual string options because they don't have first-class methods | ||
// yet (e.g. https://github.com/gradle/gradle/issues/12898) | ||
options.addBooleanOption("-enable-preview", true); | ||
options.addStringOption("source", sourceCompat.getMajorVersion()); | ||
}); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
private static class MaybeEnablePreview implements CommandLineArgumentProvider { | ||
private final Provider<Boolean> shouldEnable; | ||
|
||
MaybeEnablePreview(Provider<Boolean> shouldEnable) { | ||
this.shouldEnable = shouldEnable; | ||
} | ||
|
||
@Override | ||
public Iterable<String> asArguments() { | ||
return shouldEnable.get() ? Collections.singletonList(FLAG) : Collections.emptyList(); | ||
} | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...in/resources/META-INF/gradle-plugins/com.palantir.baseline-enable-preview-flag.properties
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 @@ | ||
implementation-class=com.palantir.baseline.plugins.BaselineEnablePreviewFlag |
174 changes: 174 additions & 0 deletions
174
...e-java/src/test/groovy/com/palantir/baseline/plugins/BaselineEnablePreviewFlagTest.groovy
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,174 @@ | ||
/* | ||
* (c) Copyright 2020 Palantir Technologies Inc. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.palantir.baseline.plugins | ||
|
||
|
||
import nebula.test.IntegrationSpec | ||
import nebula.test.functional.ExecutionResult | ||
import org.gradle.api.JavaVersion | ||
import spock.lang.IgnoreIf | ||
|
||
@IgnoreIf({JavaVersion.current() < JavaVersion.VERSION_14}) | ||
// this class uses records, which were introduced in java 14 | ||
class BaselineEnablePreviewFlagTest extends IntegrationSpec { | ||
|
||
def setupSingleProject(File dir) { | ||
new File(dir, "build.gradle") << ''' | ||
apply plugin: 'java-library' | ||
apply plugin: 'com.palantir.baseline-enable-preview-flag' | ||
apply plugin: 'application' | ||
application { | ||
mainClass = 'foo.Foo' | ||
} | ||
'''.stripIndent() | ||
|
||
writeSourceFileContainingRecord(dir); | ||
} | ||
|
||
private writeSourceFileContainingRecord(File dir) { | ||
writeJavaSourceFile(''' | ||
package foo; | ||
public class Foo { | ||
/** Hello this is some javadoc. */ | ||
public record Coordinate(int x, int y) {} | ||
public static void main(String... args) { | ||
System.out.println("Hello, world: " + new Coordinate(1, 2)); | ||
} | ||
} | ||
'''.stripIndent(), dir) | ||
} | ||
|
||
def 'compiles'() { | ||
when: | ||
setupSingleProject(projectDir) | ||
buildFile << ''' | ||
tasks.classes.doLast { | ||
println "COMPILED:" + new File(sourceSets.main.java.outputDir, "foo").list() | ||
} | ||
''' | ||
ExecutionResult executionResult = runTasks('classes', '-is') | ||
|
||
then: | ||
executionResult.getStandardOutput().contains('Foo$Coordinate.class') | ||
executionResult.getStandardOutput().contains('Foo.class') | ||
} | ||
|
||
def 'javadoc'() { | ||
when: | ||
setupSingleProject(projectDir) | ||
|
||
then: | ||
ExecutionResult executionResult = runTasks('javadoc', '-is') | ||
assert executionResult.getSuccess() ?: executionResult.getStandardOutput() | ||
} | ||
|
||
def 'runs'() { | ||
when: | ||
setupSingleProject(projectDir) | ||
ExecutionResult executionResult = runTasks('run', '-is') | ||
|
||
then: | ||
executionResult.getStandardOutput().contains("Hello, world: Coordinate[x=1, y=2]") | ||
} | ||
|
||
def 'testing works'() { | ||
when: | ||
setupSingleProject(projectDir) | ||
buildFile << ''' | ||
repositories { mavenCentral() } | ||
dependencies { | ||
testImplementation 'junit:junit:4.13.1' | ||
} | ||
''' | ||
|
||
file('src/test/java/foo/FooTest.java') << ''' | ||
package foo; | ||
public final class FooTest { | ||
@org.junit.Ignore("silly junit4 thinks this 'class' actually contains tests") | ||
record Whatever(int x, int y) {} | ||
@org.junit.Test | ||
public void whatever() { | ||
Foo.main(); | ||
System.out.println("Hello, world: " + new Whatever(1, 2)); | ||
} | ||
} | ||
'''.stripIndent() | ||
ExecutionResult executionResult = runTasks('test', '-is') | ||
|
||
then: | ||
executionResult.getStandardOutput().contains("Hello, world: Coordinate[x=1, y=2]") | ||
executionResult.getStandardOutput().contains("Hello, world: Whatever[x=1, y=2]") | ||
} | ||
|
||
def 'multiproject'() { | ||
when: | ||
buildFile << ''' | ||
subprojects { | ||
apply plugin: 'java-library' | ||
apply plugin: 'com.palantir.baseline-enable-preview-flag' | ||
} | ||
''' | ||
|
||
File java14Dir = addSubproject("my-java-14", ''' | ||
apply plugin: 'application' | ||
application { | ||
mainClass = 'foo.Foo' | ||
} | ||
dependencies { | ||
implementation project(':my-java-14-preview') | ||
} | ||
''') | ||
writeJavaSourceFile(''' | ||
package bar; | ||
public class Bar { | ||
public static void main(String... args) { | ||
foo.Foo.main(); | ||
} | ||
} | ||
''', java14Dir); | ||
|
||
File java14PreviewDir = addSubproject("my-java-14-preview") | ||
writeSourceFileContainingRecord(java14PreviewDir) | ||
|
||
|
||
then: | ||
ExecutionResult executionResult = runTasks('run', '-is') | ||
executionResult.getStandardOutput().contains 'Hello, world: Coordinate[x=1, y=2]' | ||
} | ||
|
||
def 'does not always apply'() { | ||
when: | ||
File java8Dir = addSubproject('my-java-8-api', "sourceCompatibility = 8") | ||
buildFile << ''' | ||
subprojects { | ||
apply plugin: 'java-library' | ||
apply plugin: 'com.palantir.baseline-enable-preview-flag' | ||
} | ||
''' | ||
writeSourceFileContainingRecord(java8Dir) | ||
|
||
ExecutionResult result = runTasks('my-java-8-api:classes', '-is') | ||
|
||
then: | ||
result.getStandardError().contains("use --enable-preview to enable records") | ||
} | ||
} | ||
|