diff --git a/log4j-changelog/src/main/java/org/apache/logging/log4j/changelog/ChangelogFiles.java b/log4j-changelog/src/main/java/org/apache/logging/log4j/changelog/ChangelogFiles.java index df35b378..abca9530 100644 --- a/log4j-changelog/src/main/java/org/apache/logging/log4j/changelog/ChangelogFiles.java +++ b/log4j-changelog/src/main/java/org/apache/logging/log4j/changelog/ChangelogFiles.java @@ -56,6 +56,10 @@ public static Path indexTemplateFile(final Path changelogDirectory) { return changelogDirectory.resolve(".index.adoc.ftl"); } + public static String indexTemplateFile(final Path changelogDirectory, final Path baseDir) { + return baseDir.relativize(indexTemplateFile(changelogDirectory)).toString().replaceAll("\\\\", "/"); + } + public static Path releaseDirectory(final Path changelogDirectory, final String releaseVersion) { return changelogDirectory.resolve(releaseVersion); } @@ -68,4 +72,7 @@ public static Path releaseChangelogTemplateFile(final Path releaseDirectory) { return releaseDirectory.resolve(".changelog.adoc.ftl"); } + public static String releaseChangelogTemplateFile(final Path releaseDirectory, final Path baseDir) { + return baseDir.relativize(releaseChangelogTemplateFile(releaseDirectory)).toString().replaceAll("\\\\", "/"); + } } diff --git a/log4j-changelog/src/main/java/org/apache/logging/log4j/changelog/exporter/ChangelogExporter.java b/log4j-changelog/src/main/java/org/apache/logging/log4j/changelog/exporter/ChangelogExporter.java index 41d00217..96ac8640 100644 --- a/log4j-changelog/src/main/java/org/apache/logging/log4j/changelog/exporter/ChangelogExporter.java +++ b/log4j-changelog/src/main/java/org/apache/logging/log4j/changelog/exporter/ChangelogExporter.java @@ -60,12 +60,13 @@ public static void performExport(final ChangelogExporterArgs args) { for (int releaseIndex = 0; releaseIndex < releaseDirectories.size(); releaseIndex++) { final Path releaseDirectory = releaseDirectories.get(releaseIndex); final ChangelogRelease changelogRelease = changelogReleases.get(releaseIndex); - final Path releaseChangelogTemplateFile = ChangelogFiles.releaseChangelogTemplateFile(releaseDirectory); + final String releaseChangelogTemplateFile = ChangelogFiles.releaseChangelogTemplateFile(releaseDirectory, args.changelogDirectory); try { exportRelease( args.outputDirectory, releaseDirectory, changelogRelease, + args.changelogDirectory, releaseChangelogTemplateFile); } catch (final Exception error) { final String message = @@ -95,20 +96,25 @@ public static void performExport(final ChangelogExporterArgs args) { final Path upcomingReleaseDirectory = ChangelogFiles.unreleasedDirectory(args.changelogDirectory, upcomingReleaseVersionMajor); final ChangelogRelease upcomingRelease = upcomingRelease(upcomingReleaseVersionMajor); - final Path upcomingReleaseChangelogTemplateFile = - ChangelogFiles.releaseChangelogTemplateFile(upcomingReleaseDirectory); + final String upcomingReleaseChangelogTemplateFile = + ChangelogFiles.releaseChangelogTemplateFile(upcomingReleaseDirectory, args.changelogDirectory); System.out.format("exporting upcoming release directory: `%s`%n", upcomingReleaseDirectory); exportRelease( args.outputDirectory, upcomingReleaseDirectory, upcomingRelease, + args.changelogDirectory, upcomingReleaseChangelogTemplateFile); changelogReleases.add(upcomingRelease); }); // Export the release index - final Path changelogIndexTemplateFile = ChangelogFiles.indexTemplateFile(args.changelogDirectory); - exportIndex(args.outputDirectory, changelogReleases, changelogIndexTemplateFile); + final String changelogIndexTemplateFile = ChangelogFiles.indexTemplateFile(args.changelogDirectory, args.changelogDirectory); + exportIndex( + args.outputDirectory, + changelogReleases, + args.changelogDirectory, + changelogIndexTemplateFile); } @@ -135,10 +141,11 @@ private static void exportRelease( final Path outputDirectory, final Path releaseDirectory, final ChangelogRelease changelogRelease, - final Path releaseChangelogTemplateFile) { + final Path templateDirectory, + final String releaseChangelogTemplateFile) { final Map> changelogEntriesByType = readChangelogEntriesByType(releaseDirectory); try { - exportRelease(outputDirectory, changelogRelease, changelogEntriesByType, releaseChangelogTemplateFile); + exportRelease(outputDirectory, changelogRelease, changelogEntriesByType, templateDirectory, releaseChangelogTemplateFile); } catch (final IOException error) { final String message = String.format("failed exporting release from directory `%s`", releaseDirectory); throw new UncheckedIOException(message, error); @@ -162,14 +169,19 @@ private static void exportRelease( final Path outputDirectory, final ChangelogRelease release, final Map> entriesByType, - final Path releaseChangelogTemplateFile) + final Path templateDirectory, + final String releaseChangelogTemplateFile) throws IOException { final String releaseChangelogFileName = releaseChangelogFileName(release); final Path releaseChangelogFile = outputDirectory.resolve(releaseChangelogFileName); final Map releaseChangelogTemplateData = new LinkedHashMap<>(); releaseChangelogTemplateData.put("release", release); releaseChangelogTemplateData.put("entriesByType", entriesByType); - FreeMarkerUtils.render(releaseChangelogTemplateFile, releaseChangelogTemplateData, releaseChangelogFile); + FreeMarkerUtils.render( + templateDirectory, + releaseChangelogTemplateFile, + releaseChangelogTemplateData, + releaseChangelogFile); } private static ChangelogRelease upcomingRelease(final int versionMajor) { @@ -180,7 +192,8 @@ private static ChangelogRelease upcomingRelease(final int versionMajor) { private static void exportIndex( final Path outputDirectory, final List changelogReleases, - final Path indexTemplateFile) { + final Path templateDirectory, + final String indexTemplateFile) { final Object indexTemplateData = Collections.singletonMap( "releases", IntStream .range(0, changelogReleases.size()) @@ -196,7 +209,7 @@ private static void exportIndex( }) .collect(Collectors.toList())); final Path indexFile = outputDirectory.resolve("index.adoc"); - FreeMarkerUtils.render(indexTemplateFile, indexTemplateData, indexFile); + FreeMarkerUtils.render(templateDirectory, indexTemplateFile, indexTemplateData, indexFile); } private static String releaseChangelogFileName(final ChangelogRelease changelogRelease) { diff --git a/log4j-changelog/src/main/java/org/apache/logging/log4j/changelog/exporter/FreeMarkerUtils.java b/log4j-changelog/src/main/java/org/apache/logging/log4j/changelog/exporter/FreeMarkerUtils.java index 9deafcd3..eaaa6378 100644 --- a/log4j-changelog/src/main/java/org/apache/logging/log4j/changelog/exporter/FreeMarkerUtils.java +++ b/log4j-changelog/src/main/java/org/apache/logging/log4j/changelog/exporter/FreeMarkerUtils.java @@ -17,7 +17,6 @@ package org.apache.logging.log4j.changelog.exporter; import java.io.BufferedWriter; -import java.io.File; import java.io.IOException; import java.io.UncheckedIOException; import java.nio.file.Files; @@ -28,21 +27,23 @@ import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import freemarker.cache.FileTemplateLoader; -import freemarker.template.*; +import freemarker.template.Configuration; +import freemarker.template.DefaultObjectWrapper; +import freemarker.template.DefaultObjectWrapperBuilder; +import freemarker.template.Template; +import freemarker.template.TemplateExceptionHandler; final class FreeMarkerUtils { private FreeMarkerUtils() {} - private static final Configuration CONFIGURATION = createConfiguration(); - @SuppressFBWarnings("DMI_HARDCODED_ABSOLUTE_FILENAME") - private static Configuration createConfiguration() { + private static Configuration createConfiguration(final Path root) { final Configuration configuration = new Configuration(Configuration.VERSION_2_3_29); configuration.setDefaultEncoding(CharsetUtils.CHARSET_NAME); configuration.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER); try { - configuration.setTemplateLoader(new FileTemplateLoader(new File("/"))); + configuration.setTemplateLoader(new FileTemplateLoader(root.toFile())); } catch (final IOException error) { throw new UncheckedIOException(error); } @@ -58,9 +59,14 @@ private static Configuration createConfiguration() { } @SuppressFBWarnings("TEMPLATE_INJECTION_FREEMARKER") - static void render(final Path templateFile, final Object templateData, final Path outputFile) { + static void render( + final Path templateDirectory, + final String templateFile, + final Object templateData, + final Path outputFile) { try { - final Template template = CONFIGURATION.getTemplate(templateFile.toAbsolutePath().toString()); + final Configuration configuration = createConfiguration(templateDirectory); + final Template template = configuration.getTemplate(templateFile); final Path outputFileParent = outputFile.getParent(); if (outputFileParent != null) { Files.createDirectories(outputFileParent); diff --git a/log4j-changelog/src/test/java/org/apache/logging/log4j/changelog/FileTestUtils.java b/log4j-changelog/src/test/java/org/apache/logging/log4j/changelog/FileTestUtils.java index f7f491fa..d62ea229 100644 --- a/log4j-changelog/src/test/java/org/apache/logging/log4j/changelog/FileTestUtils.java +++ b/log4j-changelog/src/test/java/org/apache/logging/log4j/changelog/FileTestUtils.java @@ -44,7 +44,7 @@ static void assertDirectoryContentMatches(final Path actualPath, final Path expe final Path actualFilePath = actualContents.get(relativeFilePath); final Path expectedFilePath = expectedContents.get(relativeFilePath); if (!Files.isDirectory(actualFilePath) || !Files.isDirectory(expectedFilePath)) { - assertThat(actualFilePath).hasSameBinaryContentAs(expectedFilePath); + assertThat(actualFilePath).hasSameTextualContentAs(expectedFilePath); } });