Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ sourceSets {
}
```

> [!IMPORTANT]
> When processing templates, Pebble will trim newlines immediately after a template tag.
> This might not be desired when using formats like YAML or Java properties files, so Blossom offers a way to disable this behaviour:
>
> Add a `trimNewlines = false` line to the template set block and stripping will be disabled for any templates in the set.

Then place a file in the `src/main/resource-templates` folder:

`build-vars.properties`:
Expand Down
1 change: 1 addition & 0 deletions src/main/java/net/kyori/blossom/GenerateTemplates.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ void generate() throws IOException {

// general properties
spec.getHeader().set(this.getBaseSet().flatMap(TemplateSet::getHeader));
spec.getTrimNewlines().set(this.getBaseSet().flatMap(TemplateSet::getTrimNewlines));
spec.getSourceDirectories().from(this.getSourceDirectories());
spec.getIncludesDirectories().from(this.getIncludesDirectories());
spec.getDestinationDirectory().set(this.getOutputDir());
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/net/kyori/blossom/TemplateSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,18 @@ public interface TemplateSet extends Named {
@Override
@NotNull String getName();

/**
* Whether to strip trailing newlines from Pebble tokens that occur at the end of a line.
*
* <p>The default is {@code true}, which can cause strange behaviour in some file formats
* that are whitespace-sensitive.</p>
*
* @return whether to trim newlines
* @since 2.2.0
*/
@Input
@NotNull Property<Boolean> getTrimNewlines();

/**
* A collection of data files in YAML format.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import net.kyori.blossom.GenerateTemplates;
import net.kyori.blossom.ResourceTemplateSet;
import org.gradle.api.file.Directory;
import org.gradle.api.model.ObjectFactory;
import org.gradle.api.tasks.SourceSet;
import org.gradle.api.tasks.TaskProvider;

Expand All @@ -32,8 +33,8 @@
*/
public abstract class ResourceTemplateSetImpl extends TemplateSetImpl implements ResourceTemplateSet {
@Inject
public ResourceTemplateSetImpl(final String name) {
super(name);
public ResourceTemplateSetImpl(final ObjectFactory objects, final String name) {
super(objects, name);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.gradle.api.GradleException;
import org.gradle.api.file.Directory;
import org.gradle.api.file.SourceDirectorySet;
import org.gradle.api.model.ObjectFactory;
import org.gradle.api.tasks.SourceSet;
import org.gradle.api.tasks.TaskProvider;
import org.jetbrains.annotations.NotNull;
Expand All @@ -36,8 +37,8 @@ public abstract class SourceTemplateSetImpl extends TemplateSetImpl implements S
private transient TaskProvider<GenerateTemplates> pendingGenerateTask;

@Inject
public SourceTemplateSetImpl(final String name) {
super(name);
public SourceTemplateSetImpl(final ObjectFactory objects, final String name) {
super(objects, name);
}

@Override
Expand Down
26 changes: 14 additions & 12 deletions src/main/java/net/kyori/blossom/internal/TemplateSetImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,33 +32,30 @@

/**
* A directory of templates.
*
* <p>While it's perfectly possible to </p>
*/
public abstract class TemplateSetImpl implements TemplateSetInternal {
// shared
private final ConfigurableFileCollection dataFiles;
private final MapProperty<String, Object> properties;
private final NamedDomainObjectContainer<Variant> variants;
private final Property<String> header;
private final Property<Boolean> trimNewlines;
private transient final SourceDirectorySet includes;
private transient final SourceDirectorySet templates;
private final String name;

@Inject
public TemplateSetImpl(final String name) {
public TemplateSetImpl(final ObjectFactory objects, final String name) {
this.name = name;
this.dataFiles = this.getObjects().fileCollection();
this.properties = this.getObjects().mapProperty(String.class, Object.class);
this.variants = this.getObjects().domainObjectContainer(Variant.class, n -> this.getObjects().newInstance(VariantImpl.class, n));
this.header = this.getObjects().property(String.class);
this.includes = this.getObjects().sourceDirectorySet(name + "-template-includes", name + " template includes");
this.templates = this.getObjects().sourceDirectorySet(name + "-templates", name + " templates");
this.dataFiles = objects.fileCollection();
this.properties = objects.mapProperty(String.class, Object.class);
this.variants = objects.domainObjectContainer(Variant.class, n -> objects.newInstance(VariantImpl.class, n));
this.header = objects.property(String.class);
this.trimNewlines = objects.property(Boolean.class).convention(true);
this.includes = objects.sourceDirectorySet(name + "-template-includes", name + " template includes");
this.templates = objects.sourceDirectorySet(name + "-templates", name + " templates");
}

@Inject
protected abstract ObjectFactory getObjects();

@Override
public @NotNull String getName() {
return this.name;
Expand All @@ -81,6 +78,11 @@ public TemplateSetImpl(final String name) {
return this.header;
}

@Override
public @NotNull Property<Boolean> getTrimNewlines() {
return this.trimNewlines;
}

@Override
public @NotNull SourceDirectorySet getIncludes() {
return this.includes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ public interface Params extends WorkParameters {

Property<String> getHeader();

Property<Boolean> getTrimNewlines();

ConfigurableFileCollection getSourceDirectories();

ConfigurableFileCollection getIncludesDirectories();
Expand Down Expand Up @@ -92,7 +94,8 @@ public void execute() {
toPaths(params.getIncludesDirectories()),
toPaths(params.getSourceDirectories()),
params.getDestinationDirectory().get().getAsFile().toPath(),
params.getHeader().getOrNull()
params.getHeader().getOrNull(),
params.getTrimNewlines().getOrElse(false)
);
} catch (final IOException ex) {
throw new GradleException("Failed to process templates:" + ex.getMessage(), ex);
Expand Down
15 changes: 15 additions & 0 deletions src/test/java/net/kyori/blossom/ResourceTemplateSetTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,19 @@ void testResourceSingleSet(final TestContext ctx) throws IOException {
}
}

@BlossomFunctionalTest
void testTrimTrailing(final TestContext ctx) throws IOException {
SettingsFactory.writeSettings(ctx, "trimTrailing");
ctx.copyInput("build.gradle");
ctx.copyInput("meta.yaml.peb", "src/main/resource-templates/meta.yaml.peb");

final BuildResult result = ctx.build("generateTemplates"); // build templates
assertEquals(TaskOutcome.SUCCESS, result.task(":generateResourceTemplates").getOutcome());

ctx.assertOutputEquals(
"meta.yaml",
"build/generated/resources/blossom/main/resource/meta.yaml"
);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
@GradleParameters({"--warning-mode", "fail", "--stacktrace"}) // parameters for all variants
@TestVariant(gradleVersion = "7.6.4", maximumRuntimeVersion = 20)
@TestVariant(gradleVersion = "8.9", extraArguments = {"--configuration-cache"}) // last version with non-deprecated support for runtimes <17
@TestVariant(gradleVersion = "8.10", extraArguments = {"--configuration-cache"}, minimumRuntimeVersion = 17)
@TestVariant(gradleVersion = "8.14.2", extraArguments = {"--configuration-cache"}, minimumRuntimeVersion = 17)
@TestVariantResource(value = "/injected-gradle-versions", optional = true, minimumRuntimeVersion = 17) // newer Gradle versions deprecate running on JDK <17, and this is only for RC's
@Documented
@Retention(RetentionPolicy.RUNTIME)
Expand Down
18 changes: 18 additions & 0 deletions src/test/resources/net/kyori/blossom/trimTrailing/in/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
plugins {
id 'java'
id 'net.kyori.blossom'
}

version = "1.0.3"

sourceSets {
main {
blossom {
resources {
trimNewlines = false
property('name', project.name)
property('version', project.version.toString())
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
info:
name: {{ name }}
version: {{ version }}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
info:
name: trimTrailing
version: 1.0.3
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,15 @@ public void generate(
final Set<Path> includePaths,
final Set<Path> sourcePaths,
final Path outputDirectory,
final @Nullable String header
final @Nullable String header,
final boolean trimNewlines
) throws IOException {
// By default, resolves FS paths
// todo: restrict inputs to inputs and includes
final Loader<?> loader = this.makeLoader(sourcePaths, includePaths);
final PebbleEngine engine = new PebbleEngine.Builder()
.autoEscaping(false) // no html escaping
.newLineTrimming(trimNewlines) // can mess with line breaks in yaml files
.defaultLocale(Locale.ROOT)
.loader(loader)
// .cacheActive(false) // xX: overlap between file names and template names causes issues
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ void generate(
final Set<Path> includePaths,
final Set<Path> sourcePaths,
final Path outputDir,
final /* @Nullable */ String header
final /* @Nullable */ String header,
final boolean trimNewlines
) throws IOException;
}