mirror of
https://gitee.com/dcren/initializr.git
synced 2025-12-21 10:59:59 +08:00
Add support for appending task attributes
This commit add support for appending task attributes rather than only setting them. This is useful for Kotlin's compiler arguments as these can be augmented by other plugins. Closes gh-1368
This commit is contained in:
@@ -16,6 +16,7 @@
|
||||
|
||||
package io.spring.initializr.generator.spring.code.kotlin;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import io.spring.initializr.generator.buildsystem.gradle.GradleTask;
|
||||
@@ -35,13 +36,17 @@ class GroovyDslKotlinGradleBuildCustomizer extends KotlinGradleBuildCustomizer {
|
||||
@Override
|
||||
protected void customizeKotlinOptions(KotlinProjectSettings settings, GradleTask.Builder compile) {
|
||||
compile.nested("kotlinOptions", (kotlinOptions) -> {
|
||||
String compilerArgs = settings.getCompilerArgs()
|
||||
.stream()
|
||||
.map((arg) -> "'" + arg + "'")
|
||||
.collect(Collectors.joining(", "));
|
||||
kotlinOptions.attribute("freeCompilerArgs", "[" + compilerArgs + "]");
|
||||
kotlinOptions.append("freeCompilerArgs", compilerArgsAsString(settings.getCompilerArgs()));
|
||||
kotlinOptions.attribute("jvmTarget", "'" + settings.getJvmTarget() + "'");
|
||||
});
|
||||
}
|
||||
|
||||
private String compilerArgsAsString(List<String> compilerArgs) {
|
||||
if (compilerArgs.size() == 1) {
|
||||
return "'" + compilerArgs.get(0) + "'";
|
||||
}
|
||||
String values = compilerArgs.stream().map((arg) -> "'" + arg + "'").collect(Collectors.joining(", "));
|
||||
return "[%s]".formatted(values);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
package io.spring.initializr.generator.spring.code.kotlin;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import io.spring.initializr.generator.buildsystem.gradle.GradleTask;
|
||||
@@ -35,13 +36,17 @@ class KotlinDslKotlinGradleBuildCustomizer extends KotlinGradleBuildCustomizer {
|
||||
@Override
|
||||
protected void customizeKotlinOptions(KotlinProjectSettings settings, GradleTask.Builder compile) {
|
||||
compile.nested("kotlinOptions", (kotlinOptions) -> {
|
||||
String compilerArgs = settings.getCompilerArgs()
|
||||
.stream()
|
||||
.map((arg) -> "\"" + arg + "\"")
|
||||
.collect(Collectors.joining(", "));
|
||||
kotlinOptions.attribute("freeCompilerArgs", "listOf(" + compilerArgs + ")");
|
||||
kotlinOptions.append("freeCompilerArgs", compilerArgsAsString(settings.getCompilerArgs()));
|
||||
kotlinOptions.attribute("jvmTarget", "\"" + settings.getJvmTarget() + "\"");
|
||||
});
|
||||
}
|
||||
|
||||
private String compilerArgsAsString(List<String> compilerArgs) {
|
||||
if (compilerArgs.size() == 1) {
|
||||
return "\"" + compilerArgs.get(0) + "\"";
|
||||
}
|
||||
String values = compilerArgs.stream().map((arg) -> "\"" + arg + "\"").collect(Collectors.joining(", "));
|
||||
return "listOf(%s)".formatted(values);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -16,8 +16,11 @@
|
||||
|
||||
package io.spring.initializr.generator.spring.code.kotlin;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import io.spring.initializr.generator.buildsystem.gradle.GradleBuild;
|
||||
import io.spring.initializr.generator.buildsystem.gradle.GradleTask;
|
||||
import io.spring.initializr.generator.buildsystem.gradle.GradleTask.Attribute;
|
||||
import org.assertj.core.groups.Tuple;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
@@ -51,6 +54,23 @@ class GroovyDslKotlinGradleBuildCustomizerTests {
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void kotlinCompilationTasksWithListOfCompilerArgsAreCustomizer() {
|
||||
GradleBuild build = new GradleBuild();
|
||||
KotlinProjectSettings kotlinProjectSettings = new SimpleKotlinProjectSettings("1.2.70", "11") {
|
||||
@Override
|
||||
public List<String> getCompilerArgs() {
|
||||
return List.of("-Xjsr305=strict", "-Xmx=128M");
|
||||
}
|
||||
};
|
||||
new GroovyDslKotlinGradleBuildCustomizer(kotlinProjectSettings).customize(build);
|
||||
assertThat(build.tasks().values()).singleElement().satisfies((task) -> {
|
||||
GradleTask kotlinOptions = task.getNested().get("kotlinOptions");
|
||||
assertThat(kotlinOptions.getAttributes())
|
||||
.contains(Attribute.append("freeCompilerArgs", "['-Xjsr305=strict', '-Xmx=128M']"));
|
||||
});
|
||||
}
|
||||
|
||||
private void assertKotlinOptions(GradleTask compileTask, String jvmTarget) {
|
||||
assertThat(compileTask.getAttributes()).isEmpty();
|
||||
assertThat(compileTask.getInvocations()).isEmpty();
|
||||
@@ -59,8 +79,8 @@ class GroovyDslKotlinGradleBuildCustomizerTests {
|
||||
assertThat(kotlinOptions.getInvocations()).hasSize(0);
|
||||
assertThat(kotlinOptions.getNested()).hasSize(0);
|
||||
assertThat(kotlinOptions.getAttributes()).hasSize(2);
|
||||
assertThat(kotlinOptions.getAttributes()).containsEntry("freeCompilerArgs", "['-Xjsr305=strict']")
|
||||
.containsEntry("jvmTarget", String.format("'%s'", jvmTarget));
|
||||
assertThat(kotlinOptions.getAttributes()).contains(Attribute.append("freeCompilerArgs", "'-Xjsr305=strict'"),
|
||||
Attribute.set("jvmTarget", String.format("'%s'", jvmTarget)));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -16,8 +16,11 @@
|
||||
|
||||
package io.spring.initializr.generator.spring.code.kotlin;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import io.spring.initializr.generator.buildsystem.gradle.GradleBuild;
|
||||
import io.spring.initializr.generator.buildsystem.gradle.GradleTask;
|
||||
import io.spring.initializr.generator.buildsystem.gradle.GradleTask.Attribute;
|
||||
import org.assertj.core.groups.Tuple;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
@@ -50,6 +53,23 @@ class KotlinDslKotlinGradleBuildCustomizerTests {
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void kotlinCompilationTasksWithListOfCompilerArgsAreCustomized() {
|
||||
GradleBuild build = new GradleBuild();
|
||||
KotlinProjectSettings kotlinProjectSettings = new SimpleKotlinProjectSettings("1.2.70", "11") {
|
||||
@Override
|
||||
public List<String> getCompilerArgs() {
|
||||
return List.of("-Xjsr305=strict", "-Xmx=128M");
|
||||
}
|
||||
};
|
||||
new KotlinDslKotlinGradleBuildCustomizer(kotlinProjectSettings).customize(build);
|
||||
assertThat(build.tasks().values()).singleElement().satisfies((task) -> {
|
||||
GradleTask kotlinOptions = task.getNested().get("kotlinOptions");
|
||||
assertThat(kotlinOptions.getAttributes())
|
||||
.contains(Attribute.append("freeCompilerArgs", "listOf(\"-Xjsr305=strict\", \"-Xmx=128M\")"));
|
||||
});
|
||||
}
|
||||
|
||||
private void assertKotlinOptions(GradleTask compileTask, String jvmTarget) {
|
||||
assertThat(compileTask.getAttributes()).isEmpty();
|
||||
assertThat(compileTask.getInvocations()).isEmpty();
|
||||
@@ -58,8 +78,8 @@ class KotlinDslKotlinGradleBuildCustomizerTests {
|
||||
assertThat(kotlinOptions.getInvocations()).hasSize(0);
|
||||
assertThat(kotlinOptions.getNested()).hasSize(0);
|
||||
assertThat(kotlinOptions.getAttributes()).hasSize(2);
|
||||
assertThat(kotlinOptions.getAttributes()).containsEntry("freeCompilerArgs", "listOf(\"-Xjsr305=strict\")")
|
||||
.containsEntry("jvmTarget", String.format("\"%s\"", jvmTarget));
|
||||
assertThat(kotlinOptions.getAttributes()).contains(Attribute.append("freeCompilerArgs", "\"-Xjsr305=strict\""),
|
||||
Attribute.set("jvmTarget", String.format("\"%s\"", jvmTarget)));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ dependencies {
|
||||
|
||||
tasks.withType(KotlinCompile) {
|
||||
kotlinOptions {
|
||||
freeCompilerArgs = ['-Xjsr305=strict']
|
||||
freeCompilerArgs += '-Xjsr305=strict'
|
||||
jvmTarget = '11'
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ dependencies {
|
||||
|
||||
tasks.withType<KotlinCompile> {
|
||||
kotlinOptions {
|
||||
freeCompilerArgs = listOf("-Xjsr305=strict")
|
||||
freeCompilerArgs += "-Xjsr305=strict"
|
||||
jvmTarget = "11"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ dependencies {
|
||||
|
||||
tasks.withType(KotlinCompile) {
|
||||
kotlinOptions {
|
||||
freeCompilerArgs = ['-Xjsr305=strict']
|
||||
freeCompilerArgs += '-Xjsr305=strict'
|
||||
jvmTarget = '1.8'
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ dependencies {
|
||||
|
||||
tasks.withType<KotlinCompile> {
|
||||
kotlinOptions {
|
||||
freeCompilerArgs = listOf("-Xjsr305=strict")
|
||||
freeCompilerArgs += "-Xjsr305=strict"
|
||||
jvmTarget = "1.8"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,7 +28,7 @@ dependencies {
|
||||
|
||||
tasks.withType(KotlinCompile) {
|
||||
kotlinOptions {
|
||||
freeCompilerArgs = ['-Xjsr305=strict']
|
||||
freeCompilerArgs += '-Xjsr305=strict'
|
||||
jvmTarget = '1.8'
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,7 +28,7 @@ dependencies {
|
||||
|
||||
tasks.withType<KotlinCompile> {
|
||||
kotlinOptions {
|
||||
freeCompilerArgs = listOf("-Xjsr305=strict")
|
||||
freeCompilerArgs += "-Xjsr305=strict"
|
||||
jvmTarget = "1.8"
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user