Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix included files #36

Merged
merged 4 commits into from
Sep 16, 2023
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
73 changes: 73 additions & 0 deletions src/test/java/net/kyori/blossom/IncludedMacrosTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* This file is part of blossom, licensed under the GNU Lesser General Public License.
*
* Copyright (c) 2023 KyoriPowered
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
* USA
*/
package net.kyori.blossom;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.util.Locale;
import java.util.Properties;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import net.kyori.blossom.test.BlossomDisplayNameGeneration;
import net.kyori.blossom.test.BlossomFunctionalTest;
import net.kyori.blossom.test.SettingsFactory;
import net.kyori.mammoth.test.TestContext;
import org.gradle.testkit.runner.BuildResult;
import org.gradle.testkit.runner.TaskOutcome;
import org.junit.jupiter.api.DisplayNameGeneration;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

@DisplayNameGeneration(BlossomDisplayNameGeneration.class)
class IncludedMacrosTest {
@BlossomFunctionalTest
void testIncludedMacros(final TestContext ctx) throws IOException {
SettingsFactory.writeSettings(ctx, "includedMacros");
ctx.copyInput("build.gradle");
ctx.copyInput("test.properties.peb", "src/main/resource-templates/test.properties.peb");
// test import with and without .peb extension specified
ctx.copyInput("macros.peb", "src/main/resource-macros/macros.peb");
ctx.copyInput("macros1.peb", "src/main/resource-macros/macros1.peb");

final BuildResult result = ctx.build("assemble"); // build a jar

assertEquals(TaskOutcome.SUCCESS, result.task(":generateResourceTemplates").getOutcome());

final var destPath = ctx.outputDirectory().resolve("build/libs/includedMacros.jar");
assertTrue(Files.isRegularFile(destPath), "The expected jar did not exist");

try (final var jar = new JarFile(destPath.toFile())) {
final JarEntry entry = jar.getJarEntry("test.properties");
assertNotNull(entry, "no test.properties in jar");
final Properties props = new Properties();
try (final InputStream is = jar.getInputStream(entry)) {
props.load(is);
}

final String expected = "abc123 abc123 abc123";
assertEquals(expected, props.getProperty("value"));
assertEquals(expected.toUpperCase(Locale.ROOT), props.getProperty("valueUpper"));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
plugins {
id 'java'
id 'net.kyori.blossom'
}

sourceSets {
main {
blossom {
resources {
property('property', 'abc123')
include('src/main/resource-macros')
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{% macro hello(value) %}
{{ value }} {{ value }} {{ value }}
{% endmacro %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{% macro helloUpper(value) %}
{{ value | upper }} {{ value | upper }} {{ value | upper }}
{% endmacro %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{% import "macros.peb" %}
{% import "macros1" %}
value={{ hello(property) }}

valueUpper={{ helloUpper(property) }}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
package net.kyori.blossom.internal.worker;

import io.pebbletemplates.pebble.PebbleEngine;
import io.pebbletemplates.pebble.loader.DelegatingLoader;
import io.pebbletemplates.pebble.loader.FileLoader;
import io.pebbletemplates.pebble.loader.Loader;
import io.pebbletemplates.pebble.template.PebbleTemplate;
import java.io.BufferedWriter;
Expand All @@ -34,12 +32,12 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.gradle.api.GradleException;
import org.gradle.api.InvalidUserDataException;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -109,25 +107,10 @@ public void generate(
}

private Loader<?> makeLoader(final Set<Path> sourcePaths, final Set<Path> includePaths) {
final List<Loader<?>> loaders = new ArrayList<>();
for (final Path sourceDir : sourcePaths) {
final Loader<?> sourceLoader = new FileLoader();
sourceLoader.setPrefix(sourceDir.toAbsolutePath().toString());
loaders.add(sourceLoader);
}

if (!includePaths.isEmpty()) {
for (final Path includesDir : includePaths) {
final Loader<?> includesLoader = new FileLoader();
includesLoader.setPrefix(includesDir.toAbsolutePath().toString());
}
}

switch (loaders.size()) {
case 0: throw new GradleException("No sources directories declared!");
case 1: return loaders.get(0);
default: return new DelegatingLoader(loaders);
}
return new MultiDirectoryLoader(
Stream.concat(sourcePaths.stream(), includePaths.stream()).collect(Collectors.toList()),
StandardCharsets.UTF_8
);
}

private Set<String> collectTemplateNames(final Set<Path> sourceDirs) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
* This file is part of blossom, licensed under the GNU Lesser General Public License.
*
* Copyright (c) 2023 KyoriPowered
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
* USA
*/
package net.kyori.blossom.internal.worker;

import io.pebbletemplates.pebble.error.LoaderException;
import io.pebbletemplates.pebble.loader.Loader;
import io.pebbletemplates.pebble.utils.PathUtils;
import java.io.File;
import java.io.IOException;
import java.io.Reader;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.stream.Collectors;
import org.jetbrains.annotations.Nullable;

final class MultiDirectoryLoader implements Loader<String> {
private final List<Path> directories;
private final Charset charset;

MultiDirectoryLoader(final List<Path> directories, final Charset charset) {
this.directories = List.copyOf(directories);
this.charset = charset;
}

@Override
public Reader getReader(final String templateName) {
final Path file = this.findFile(templateName);
if (file != null) {
try {
return Files.newBufferedReader(file, this.charset);
} catch (final IOException ex) {
throw new LoaderException(ex, "Could not load template \"" + templateName + "\" from file \"" + file + "\"");
}
}
throw new LoaderException(null, "Could not find template \"" + templateName + "\" in any of: " + this.directories.stream().map(Path::toString).collect(Collectors.joining("; ")));
}

private @Nullable Path findFile(final String templateName) {
for (final Path path : this.directories) {
@Nullable Path file = findFileIn(templateName, path);

if (file == null && !templateName.endsWith(".peb")) {
file = findFileIn(templateName + ".peb", path);
}

if (file != null) {
return file;
}
}
return null;
}

private static @Nullable Path findFileIn(final String templateName, final Path path) {
final Path file = path.resolve(templateName);
if (Files.isRegularFile(file)
// guard against escaping the directory
&& file.toAbsolutePath().startsWith(path.toAbsolutePath())) {
return file;
}
return null;
}

@Override
public void setSuffix(final String suffix) {
throw new UnsupportedOperationException("Not used by Blossom");
}

@Override
public void setPrefix(final String prefix) {
throw new UnsupportedOperationException("Not used by Blossom");
}

@Override
public void setCharset(final String charset) {
throw new UnsupportedOperationException("Not used by Blossom");
}

@Override
public String resolveRelativePath(final String relativePath, final String anchorPath) {
return PathUtils.resolveRelativePath(relativePath, anchorPath, File.separatorChar);
}

@Override
public String createCacheKey(final String templateName) {
return templateName;
}

@Override
public boolean resourceExists(final String templateName) {
final Path path = this.findFile(templateName);
return path != null && Files.isRegularFile(path);
}
}