Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanseifert committed Jan 26, 2024
2 parents a0301b3 + 817be42 commit ef87060
Show file tree
Hide file tree
Showing 113 changed files with 879 additions and 873 deletions.
9 changes: 9 additions & 0 deletions changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@
xsi:schemaLocation="http://maven.apache.org/changes/1.0.0 https://maven.apache.org/plugins/maven-changes-plugin/xsd/changes-1.0.0.xsd">
<body>

<release version="1.17.0" date="2024-01-26">
<action type="update" dev="sseifert" issue="57">
Special handling for detecting ".cfg.json" file extensions.
</action>
<action type="remove" dev="sseifert" issue="59">
Remove Guava dependency.
</action>
</release>

<release version="1.16.4" date="2023-10-18">
<action type="fix" dev="sseifert" issue="47">
Increase SnakeYAML codepoint limit to 64MB (from default 3MB).
Expand Down
10 changes: 8 additions & 2 deletions generator/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<parent>
<groupId>io.wcm.devops.conga</groupId>
<artifactId>io.wcm.devops.conga.parent</artifactId>
<version>1.16.4</version>
<version>1.17.0</version>
<relativePath>../parent/pom.xml</relativePath>
</parent>

Expand All @@ -44,7 +44,7 @@
<dependency>
<groupId>io.wcm.devops.conga</groupId>
<artifactId>io.wcm.devops.conga.model</artifactId>
<version>1.16.4</version>
<version>1.17.0</version>
<scope>compile</scope>
</dependency>

Expand All @@ -54,6 +54,12 @@
<scope>compile</scope>
</dependency>

<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
<scope>compile</scope>
</dependency>

<dependency>
<groupId>com.github.jknack</groupId>
<artifactId>handlebars</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,14 @@
import static io.wcm.devops.conga.generator.ContextProperties.TENANTS_BY_ROLE;
import static io.wcm.devops.conga.generator.ContextProperties.TENANT_ROLES;
import static io.wcm.devops.conga.generator.ContextProperties.VERSION;
import static java.util.Map.entry;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.common.collect.ImmutableMap;

import io.wcm.devops.conga.generator.util.ObjectCloner;
import io.wcm.devops.conga.generator.util.VariableObjectTreeResolver;
import io.wcm.devops.conga.generator.util.VariableStringResolver;
Expand All @@ -55,20 +54,20 @@
*/
public final class ContextPropertiesBuilder {

static final Map<String, Object> EMPTY_CONTEXT_VARIABLES = ImmutableMap.<String, Object>builder()
.put(VERSION, "")
.put(ENVIRONMENT, "")
.put(NODES, Collections.emptyList())
.put(NODES_BY_ROLE, Collections.emptyMap())
.put(NODES_BY_ROLE_VARIANT, Collections.emptyMap())
.put(TENANTS, Collections.emptyMap())
.put(TENANTS_BY_ROLE, Collections.emptyMap())
.put(ROLE, "")
.put(ROLE_VARIANT, "")
.put(NODE, "")
.put(TENANT, "")
.put(TENANT_ROLES, Collections.emptyList())
.build();
static final Map<String, Object> EMPTY_CONTEXT_VARIABLES = Map.ofEntries(
entry(VERSION, ""),
entry(ENVIRONMENT, ""),
entry(NODES, Collections.emptyList()),
entry(NODES_BY_ROLE, Collections.emptyMap()),
entry(NODES_BY_ROLE_VARIANT, Collections.emptyMap()),
entry(TENANTS, Collections.emptyMap()),
entry(TENANTS_BY_ROLE, Collections.emptyMap()),
entry(ROLE, ""),
entry(ROLE_VARIANT, ""),
entry(NODE, ""),
entry(TENANT, ""),
entry(TENANT_ROLES, Collections.emptyList())
);

private ContextPropertiesBuilder() {
// static methods only
Expand All @@ -83,6 +82,7 @@ private ContextPropertiesBuilder() {
* @param variableStringResolver Variable string resolver
* @return Context variables map
*/
@SuppressWarnings("java:S3776") // ignore complexity
public static Map<String, Object> buildEnvironmentContextVariables(String environmentName,
Environment environment, String version,
VariableObjectTreeResolver variableObjectTreeResolver, VariableStringResolver variableStringResolver) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand All @@ -40,9 +41,6 @@

import com.github.jknack.handlebars.Handlebars;
import com.github.jknack.handlebars.Template;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import io.wcm.devops.conga.generator.export.NodeModelExport;
Expand Down Expand Up @@ -82,7 +80,7 @@
/**
* Generates file for one environment.
*/
class EnvironmentGenerator {
final class EnvironmentGenerator {

private final GeneratorOptions options;
private final String environmentName;
Expand Down Expand Up @@ -132,10 +130,10 @@ class EnvironmentGenerator {
ResourceLoader resourceLoader = new ResourceLoader(resourceClassLoader);

// prepare template and role directories
List<ResourceCollection> templateDirs = ImmutableList.of(
List<ResourceCollection> templateDirs = List.of(
resourceLoader.getResourceCollection(ResourceLoader.FILE_PREFIX + options.getTemplateDir()),
resourceLoader.getResourceCollection(ResourceLoader.CLASSPATH_PREFIX + GeneratorOptions.CLASSPATH_TEMPLATES_DIR));
List<ResourceCollection> roleDirs = ImmutableList.of(
List<ResourceCollection> roleDirs = List.of(
resourceLoader.getResourceCollection(ResourceLoader.FILE_PREFIX + options.getRoleDir()),
resourceLoader.getResourceCollection(ResourceLoader.CLASSPATH_PREFIX + GeneratorOptions.CLASSPATH_ROLES_DIR));

Expand All @@ -154,11 +152,11 @@ class EnvironmentGenerator {
this.handlebarsManager = new HandlebarsManager(templateDirs, this.pluginContextOptions);

this.defaultMultiplyPlugin = options.getPluginManager().get(NoneMultiply.NAME, MultiplyPlugin.class);
this.environmentContextProperties = ImmutableMap.copyOf(
this.environmentContextProperties = Collections.unmodifiableMap(
ContextPropertiesBuilder.buildEnvironmentContextVariables(environmentName, this.environment, options.getVersion(),
variableObjectTreeResolver, variableStringResolver));

this.dependencyVersions = options.getDependencyVersionBuilder() != null ? options.getDependencyVersionBuilder().apply(environment) : ImmutableList.of();
this.dependencyVersions = options.getDependencyVersionBuilder() != null ? options.getDependencyVersionBuilder().apply(environment) : List.of();

// prepare YAML representer
yamlRepresenter = new YamlRepresenter();
Expand All @@ -178,7 +176,7 @@ public void generate(String[] nodeNames) {
log.info("");
log.info("===== Environment '{}' =====", environmentName);

Set<String> nodeNamesIndex = ArrayUtils.isEmpty(nodeNames) ? Collections.emptySet() : ImmutableSet.copyOf(nodeNames);
Set<String> nodeNamesIndex = ArrayUtils.isEmpty(nodeNames) ? Collections.emptySet() : Set.of(nodeNames);
for (Node node : environment.getNodes()) {
if (isSelectedNode(node, nodeNamesIndex)) {
generateNode(node);
Expand Down Expand Up @@ -241,8 +239,7 @@ private void generateNode(Node node) {
mergedConfig.putAll(ContextPropertiesBuilder.buildCurrentContextVariables(node, nodeRole));

// collect role and tenant information for export model
ExportNodeRoleData exportNodeRoleData = exportModelGenerator.addRole(roleName, variants, mergedConfig,
role.getSensitiveConfigParameters());
ExportNodeRoleData exportNodeRoleData = exportModelGenerator.addRole(roleName, variants, mergedConfig);

// generate files
List<GeneratedFileContext> allFiles = new ArrayList<>();
Expand Down Expand Up @@ -321,6 +318,7 @@ private String getEscapingStrategy(RoleFile roleFile) {
.getName();
}

@SuppressWarnings("java:S107") // allow many parameters
private void multiplyFiles(Role role, RoleFile roleFile, Map<String, Object> config, File nodeDir, Template template,
String roleName, List<String> roleVariantNames, String templateName, List<GeneratedFileContext> generatedFiles) {
MultiplyPlugin multiplyPlugin = defaultMultiplyPlugin;
Expand Down Expand Up @@ -367,7 +365,10 @@ private void multiplyFiles(Role role, RoleFile roleFile, Map<String, Object> con
}
}

@SuppressWarnings("PMD.PreserveStackTrace")
@SuppressWarnings({
"PMD.PreserveStackTrace",
"java:S107" // allow many parameters
})
@SuppressFBWarnings("RV_RETURN_VALUE_IGNORED_BAD_PRACTICE")
private Collection<GeneratedFileContext> generateFile(RoleFile roleFile, String dir,
String fileName, String url, String symlinkTarget,
Expand All @@ -386,7 +387,12 @@ private Collection<GeneratedFileContext> generateFile(RoleFile roleFile, String

File file = new File(nodeDir, dir != null ? FilenameUtils.concat(dir, generatedFileName) : generatedFileName);
if (file.exists()) {
file.delete();
try {
Files.delete(file.toPath());
}
catch (IOException ex) {
throw new GeneratorException("Unable to delete: " + FileUtil.getCanonicalPath(file), ex);
}
}

FileGenerator fileGenerator = new FileGenerator(options, environmentName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
import org.slf4j.Logger;

import com.github.jknack.handlebars.Template;
import com.google.common.collect.ImmutableList;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import io.wcm.devops.conga.generator.plugins.fileheader.NoneFileHeader;
Expand Down Expand Up @@ -98,14 +97,13 @@ class FileGenerator {
static final String POSTPROCESSOR_KEY_FILE_HEADER = "postProcessor.fileHeader";
static final String POSTPROCESSOR_KEY_VALIDATORS = "postProcessor.validators";

//CHECKSTYLE:OFF
@SuppressWarnings({ "java:S107", "checkstyle:ParameterNumberCheck" }) // allow many parameters
FileGenerator(GeneratorOptions options, String environmentName,
String roleName, List<String> roleVariantNames, String templateName,
File nodeDir, File file, String url, String symlinkTarget,
RoleFile roleFile, Map<String, Object> config, Template template,
VariableMapResolver variableMapResolver, UrlFileManager urlFileManager, PluginContextOptions pluginContextOptions,
Collection<String> dependencyVersions) {
//CHECKSTYLE:ON
this.environmentName = environmentName;
this.roleName = roleName;
this.roleVariantNames = roleVariantNames;
Expand Down Expand Up @@ -207,6 +205,10 @@ private List<String> formatFileHeaderCommentLines(List<String> lines) {
* @return List of files that where generated directly or indirectly (by post processors).
*/
@SuppressFBWarnings({ "ST_WRITE_TO_STATIC_FROM_INSTANCE_METHOD", "RV_RETURN_VALUE_IGNORED_BAD_PRACTICE" })
@SuppressWarnings({
"java:S3776", // ignore complexity
"java:S2696" // static variable set by intention
})
public Collection<GeneratedFileContext> generate() throws IOException {
File dir = file.getParentFile();
if (!dir.exists()) {
Expand All @@ -215,7 +217,9 @@ public Collection<GeneratedFileContext> generate() throws IOException {

Collection<GeneratedFileContext> postProcessedFiles;
if (template != null) {
log.info("Generate file {}", getFilenameForLog(fileContext));
if (log.isInfoEnabled()) {
log.info("Generate file {}", getFilenameForLog(fileContext));
}

// generate with template
generateWithTemplate();
Expand All @@ -231,7 +235,9 @@ else if (StringUtils.isNotBlank(url)) {
// if copying from a local file try to create a symlink instead of coyping it
boolean symlinkCreated = false;
if (allowSymlinks && !symlinkCreationFailed && urlFileManager.isLocalFile(url) && !roleFile.isDeleteSource()) {
log.info("Symlink file {} from {}", getFilenameForLog(fileContext), url);
if (log.isInfoEnabled()) {
log.info("Symlink file {} from {}", getFilenameForLog(fileContext), url);
}
if (createSymlinkToLocalFile()) {
symlinkCreated = true;
}
Expand All @@ -242,7 +248,9 @@ else if (StringUtils.isNotBlank(url)) {

// generate by downloading/copying from URL
if (!symlinkCreated) {
log.info("Copy file {} from {}", getFilenameForLog(fileContext), url);
if (log.isInfoEnabled()) {
log.info("Copy file {} from {}", getFilenameForLog(fileContext), url);
}
copyFromUrlFile();
}

Expand Down Expand Up @@ -310,7 +318,7 @@ private boolean createSymlinkToLocalFile() throws IOException {
}
catch (IOException ex) {
// creates symbolic link failed - log warning and fallback to copying content
log.warn("Unable to create symbolic link: " + ex.getMessage());
log.warn("Unable to create symbolic link: {}", ex.getMessage());
return false;
}
}
Expand All @@ -333,7 +341,7 @@ private void createSymlinkToSymlinkTarget() throws IOException {
}
catch (IOException ex) {
// creates symbolic link failed - create text file with link instead (similar to git)
log.warn("Created link textfile instead of symbolic link: " + ex.getMessage());
log.warn("Created link textfile instead of symbolic link: {}", ex.getMessage());
FileUtils.write(linkPath.toFile(), relativizedPath.toString(), StandardCharsets.UTF_8);
}
}
Expand Down Expand Up @@ -387,7 +395,9 @@ private void applyFileHeader(FileContext fileItem, String pluginName) {
}

private void applyFileHeader(FileContext fileItem, FileHeaderPlugin plugin) {
log.debug(" Add {} file header to file {}", plugin.getName(), getFilenameForLog(fileItem));
if (log.isDebugEnabled()) {
log.debug(" Add {} file header to file {}", plugin.getName(), getFilenameForLog(fileItem));
}
plugin.apply(fileItem, fileHeaderContext);
}

Expand All @@ -398,7 +408,9 @@ private void applyValidation(FileContext fileItem, List<String> pluginNames) {
}

private void applyValidation(FileContext fileItem, ValidatorPlugin plugin) {
log.info(" Validate {} for file {}", plugin.getName(), getFilenameForLog(fileItem));
if (log.isInfoEnabled()) {
log.info(" Validate {} for file {}", plugin.getName(), getFilenameForLog(fileItem));
}
plugin.apply(fileItem, validatorContext);
}

Expand All @@ -423,7 +435,7 @@ private Collection<GeneratedFileContext> applyPostProcessor(FileContext fileItem
private void applyPostProcessor(Map<String, GeneratedFileContext> consolidatedFiles, PostProcessorPlugin plugin) {

// process all files from given map
ImmutableList.copyOf(consolidatedFiles.values()).stream()
List.copyOf(consolidatedFiles.values()).stream()
// do not apply post processor twice
.filter(fileItem -> !fileItem.getPostProcessors().contains(plugin.getName()))
.filter(fileItem -> plugin.accepts(fileItem.getFileContext(), postProcessorContext))
Expand All @@ -441,14 +453,14 @@ private void applyPostProcessor(Map<String, GeneratedFileContext> consolidatedFi
});

// remove items that do no longer exist
ImmutableList.copyOf(consolidatedFiles.values()).forEach(fileItem -> {
List.copyOf(consolidatedFiles.values()).forEach(fileItem -> {
if (!fileItem.getFileContext().getFile().exists()) {
consolidatedFiles.remove(fileItem.getFileContext().getCanonicalPath());
}
});

// apply post processor configured as implicit ALWAYS
consolidatedFiles.values().forEach(fileItem -> {
consolidatedFiles.values().forEach(fileItem ->
pluginManager.getAll(PostProcessorPlugin.class).stream()
.filter(implicitPlugin -> implicitPlugin.accepts(fileItem.getFileContext(), postProcessorContext))
.filter(implicitPlugin -> implicitPlugin.implicitApply(fileItem.getFileContext(), postProcessorContext) == ImplicitApplyOptions.ALWAYS)
Expand All @@ -465,11 +477,11 @@ private void applyPostProcessor(Map<String, GeneratedFileContext> consolidatedFi
}
generatedFileContext.postProcessor(implicitPlugin.getName());
});
});
});
})
);

// remove items that do no longer exist
ImmutableList.copyOf(consolidatedFiles.values()).forEach(fileItem -> {
List.copyOf(consolidatedFiles.values()).forEach(fileItem -> {
if (!fileItem.getFileContext().getFile().exists()) {
consolidatedFiles.remove(fileItem.getFileContext().getCanonicalPath());
}
Expand All @@ -478,7 +490,9 @@ private void applyPostProcessor(Map<String, GeneratedFileContext> consolidatedFi
}

private List<FileContext> applyPostProcessor(FileContext fileItem, PostProcessorPlugin plugin) {
log.info(" Post-process {} for file {}", plugin.getName(), getFilenameForLog(fileItem));
if (log.isInfoEnabled()) {
log.info(" Post-process {} for file {}", plugin.getName(), getFilenameForLog(fileItem));
}

List<FileContext> processedFiles = plugin.apply(fileItem, postProcessorContext);

Expand Down Expand Up @@ -510,7 +524,7 @@ private List<String> getPostProcessorValidators() {
return (List<String>)validators;
}
else {
return ImmutableList.of();
return List.of();
}
}

Expand Down
Loading

0 comments on commit ef87060

Please sign in to comment.