Skip to content

Commit

Permalink
Fix Windows test problem
Browse files Browse the repository at this point in the history
The Windows Freemarker failure has been fixed by restraining the
templates to the `changelog` directory.
  • Loading branch information
ppkarwasz committed Jan 30, 2023
1 parent 122169b commit df58dd7
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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("\\\\", "/");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down Expand Up @@ -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);

}

Expand All @@ -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<ChangelogEntry.Type, List<ChangelogEntry>> 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);
Expand All @@ -162,14 +169,19 @@ private static void exportRelease(
final Path outputDirectory,
final ChangelogRelease release,
final Map<ChangelogEntry.Type, List<ChangelogEntry>> 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<String, Object> 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) {
Expand All @@ -180,7 +192,8 @@ private static ChangelogRelease upcomingRelease(final int versionMajor) {
private static void exportIndex(
final Path outputDirectory,
final List<ChangelogRelease> changelogReleases,
final Path indexTemplateFile) {
final Path templateDirectory,
final String indexTemplateFile) {
final Object indexTemplateData = Collections.singletonMap(
"releases", IntStream
.range(0, changelogReleases.size())
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});

Expand Down

0 comments on commit df58dd7

Please sign in to comment.