-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow arbitrary code snippets to be added in a Gradle build
This commit provides an escape hatch for the DSL that we currently don't support in a gradle build. Snippets can be added with a callback to an IndentingWriter. For convenience, imports can be added as well. Snippets are rendered at the end of the file. Closes gh-1079
- Loading branch information
Showing
7 changed files
with
306 additions
and
60 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
52 changes: 52 additions & 0 deletions
52
...erator/src/main/java/io/spring/initializr/generator/buildsystem/gradle/GradleSnippet.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,52 @@ | ||
/* | ||
* Copyright 2012-2022 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. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://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 io.spring.initializr.generator.buildsystem.gradle; | ||
|
||
import java.util.Set; | ||
import java.util.function.Consumer; | ||
|
||
import io.spring.initializr.generator.io.IndentingWriter; | ||
|
||
/** | ||
* A free-form {@code snippet} to add to a Gradle build. | ||
* | ||
* @author Stephane Nicoll | ||
*/ | ||
public class GradleSnippet { | ||
|
||
private final Set<String> importedTypes; | ||
|
||
private final Consumer<IndentingWriter> writer; | ||
|
||
GradleSnippet(Set<String> importedTypes, Consumer<IndentingWriter> writer) { | ||
this.importedTypes = Set.copyOf(importedTypes); | ||
this.writer = writer; | ||
} | ||
|
||
Set<String> getImportedTypes() { | ||
return this.importedTypes; | ||
} | ||
|
||
/** | ||
* Apply the snippet using the specified {@link IndentingWriter}. | ||
* @param indentingWriter the writer to use | ||
*/ | ||
public void apply(IndentingWriter indentingWriter) { | ||
this.writer.accept(indentingWriter); | ||
} | ||
|
||
} |
79 changes: 79 additions & 0 deletions
79
...c/main/java/io/spring/initializr/generator/buildsystem/gradle/GradleSnippetContainer.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,79 @@ | ||
/* | ||
* Copyright 2012-2022 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. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://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 io.spring.initializr.generator.buildsystem.gradle; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.function.Consumer; | ||
import java.util.stream.Stream; | ||
|
||
import io.spring.initializr.generator.io.IndentingWriter; | ||
|
||
/** | ||
* A container for {@linkplain GradleSnippet Gradle snippets}. | ||
* | ||
* @author Stephane Nicoll | ||
*/ | ||
public class GradleSnippetContainer { | ||
|
||
private final List<GradleSnippet> snippets = new ArrayList<>(); | ||
|
||
/** | ||
* Specify if this container is empty. | ||
* @return {@code true} if no snippet is registered | ||
*/ | ||
public boolean isEmpty() { | ||
return this.snippets.isEmpty(); | ||
} | ||
|
||
/** | ||
* Return the {@link GradleSnippet Gradle snippets} to apply. | ||
* @return the gradle snippets | ||
*/ | ||
public Stream<GradleSnippet> values() { | ||
return this.snippets.stream(); | ||
} | ||
|
||
/** | ||
* Return the fully qualified name of types to import. | ||
* @return the imported types | ||
*/ | ||
public Stream<String> importedTypes() { | ||
return this.snippets.stream().map(GradleSnippet::getImportedTypes).flatMap(Collection::stream); | ||
} | ||
|
||
/** | ||
* Register a {@code snippet} with the specified types to import and writer. | ||
* @param importedTypes the types to import | ||
* @param writer the writer to use. | ||
*/ | ||
public void add(Set<String> importedTypes, Consumer<IndentingWriter> writer) { | ||
this.snippets.add(new GradleSnippet(importedTypes, writer)); | ||
} | ||
|
||
/** | ||
* Register a {@code snippet} with no import. | ||
* @param writer the writer to use. | ||
*/ | ||
public void add(Consumer<IndentingWriter> writer) { | ||
add(Collections.emptySet(), writer); | ||
} | ||
|
||
} |
93 changes: 93 additions & 0 deletions
93
...c/test/java/io/spring/initializr/generator/buildsystem/gradle/GradleBuildWriterTests.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,93 @@ | ||
/* | ||
* Copyright 2012-2022 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. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://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 io.spring.initializr.generator.buildsystem.gradle; | ||
|
||
import java.util.Set; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** | ||
* Common tests for {@link GradleBuildWriter} implementations. | ||
* | ||
* @author Stephane Nicoll | ||
*/ | ||
public abstract class GradleBuildWriterTests { | ||
|
||
@Test | ||
void gradleBuildWithSnippet() { | ||
GradleBuild build = new GradleBuild(); | ||
build.snippets().add((writer) -> { | ||
writer.println("custom {"); | ||
writer.indented(() -> { | ||
writer.println("first = 1"); | ||
writer.println("second = 2"); | ||
}); | ||
writer.println("}"); | ||
}); | ||
assertThat(write(build)).contains(""" | ||
custom { | ||
first = 1 | ||
second = 2 | ||
} | ||
"""); | ||
} | ||
|
||
@Test | ||
void gradleBuildWithSnippetsAreSeparated() { | ||
GradleBuild build = new GradleBuild(); | ||
build.snippets().add((writer) -> { | ||
writer.println("custom {"); | ||
writer.indented(() -> { | ||
writer.println("first = 1"); | ||
writer.println("second = 2"); | ||
}); | ||
writer.println("}"); | ||
}); | ||
build.snippets().add((writer) -> { | ||
writer.println("another {"); | ||
writer.indented(() -> { | ||
writer.println("third = 3"); | ||
writer.println("fourth = 4"); | ||
}); | ||
writer.println("}"); | ||
}); | ||
|
||
assertThat(write(build)).contains(""" | ||
custom { | ||
first = 1 | ||
second = 2 | ||
} | ||
another { | ||
third = 3 | ||
fourth = 4 | ||
} | ||
"""); | ||
} | ||
|
||
@Test | ||
void gradleBuildWithSnippetAndImports() { | ||
GradleBuild build = new GradleBuild(); | ||
build.snippets().add(Set.of("com.example.CustomTask"), (writer) -> writer.println("custom { }")); | ||
assertThat(write(build)).containsOnlyOnce("import com.example.CustomTask"); | ||
} | ||
|
||
protected abstract String write(GradleBuild build); | ||
|
||
} |
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
Oops, something went wrong.