This is an automated email from the ASF dual-hosted git repository. sgoeschl pushed a commit to branch FREEMARKER-188 in repository https://gitbox.apache.org/repos/asf/freemarker-generator.git
commit 6bc2bca311a490659d94242374a3f7e9792e2d17 Author: Siegfried Goeschl <[email protected]> AuthorDate: Sun Sep 5 18:52:10 2021 +0200 FREEMARKER-188 [freemarker-generator] Support an output "generation" mode --- .../generator/base/FreeMarkerConstants.java | 9 ++ .../generator/base/output/OutputGenerator.java | 3 +- .../app/templates/freemarker-generator/info.ftl | 8 +- .../cli/config/OutputGeneratorsSupplier.java | 111 +++------------------ .../AggregatingOutputGenerator.java} | 21 ++-- .../config/output/GeneratingOutputGenerator.java | 39 ++++++++ .../cli/picocli/OutputGeneratorDefinition.java | 16 +++ .../cli/picocli/OutputGeneratorModeDefinition.java | 28 ++++++ .../picocli/TemplateOutputMapperDefinition.java | 25 +++++ 9 files changed, 149 insertions(+), 111 deletions(-) diff --git a/freemarker-generator-base/src/main/java/org/apache/freemarker/generator/base/FreeMarkerConstants.java b/freemarker-generator-base/src/main/java/org/apache/freemarker/generator/base/FreeMarkerConstants.java index 86a69ab..e4500ff 100644 --- a/freemarker-generator-base/src/main/java/org/apache/freemarker/generator/base/FreeMarkerConstants.java +++ b/freemarker-generator-base/src/main/java/org/apache/freemarker/generator/base/FreeMarkerConstants.java @@ -93,6 +93,15 @@ public class FreeMarkerConstants { public static final String FREEMARKER_USER_PARAMETERS = "freemarker.user.parameters"; } + public static class Mode { + + private Mode() { + } + + public static final String AGGREGATE = "aggregate"; + public static final String GENERATE = "generate"; + } + public static class SystemProperties { private SystemProperties() { diff --git a/freemarker-generator-base/src/main/java/org/apache/freemarker/generator/base/output/OutputGenerator.java b/freemarker-generator-base/src/main/java/org/apache/freemarker/generator/base/output/OutputGenerator.java index 5ce4cf4..c701151 100644 --- a/freemarker-generator-base/src/main/java/org/apache/freemarker/generator/base/output/OutputGenerator.java +++ b/freemarker-generator-base/src/main/java/org/apache/freemarker/generator/base/output/OutputGenerator.java @@ -26,7 +26,8 @@ import java.util.Map; import static java.util.Objects.requireNonNull; /** - * Information about loading templates and writing their output. + * Information about loading templates and writing their output. An instance + * describes a transformation of 0..N data sources to an output file. */ public class OutputGenerator { diff --git a/freemarker-generator-cli/src/app/templates/freemarker-generator/info.ftl b/freemarker-generator-cli/src/app/templates/freemarker-generator/info.ftl index c165be3..6e60ae2 100644 --- a/freemarker-generator-cli/src/app/templates/freemarker-generator/info.ftl +++ b/freemarker-generator-cli/src/app/templates/freemarker-generator/info.ftl @@ -25,10 +25,16 @@ Timestamp : ${.now} Output encoding : ${.output_encoding} Output format : ${.output_format} +FreeMarker Command-line Parameters +------------------------------------------------------------------------------ +<#list tools.system.getCommandLineArgs() as arg> + [#${arg?counter}] ${arg} +</#list> + FreeMarker Generator Template Loader Directories ------------------------------------------------------------------------------ <#list tools.system.getTemplateDirectories() as directory> -[#${directory?counter}] ${directory} + [#${directory?counter}] ${directory} </#list> FreeMarker Generator Data Model diff --git a/freemarker-generator-cli/src/main/java/org/apache/freemarker/generator/cli/config/OutputGeneratorsSupplier.java b/freemarker-generator-cli/src/main/java/org/apache/freemarker/generator/cli/config/OutputGeneratorsSupplier.java index 059a9ab..a8e819b 100644 --- a/freemarker-generator-cli/src/main/java/org/apache/freemarker/generator/cli/config/OutputGeneratorsSupplier.java +++ b/freemarker-generator-cli/src/main/java/org/apache/freemarker/generator/cli/config/OutputGeneratorsSupplier.java @@ -16,39 +16,30 @@ */ package org.apache.freemarker.generator.cli.config; -import org.apache.freemarker.generator.base.FreeMarkerConstants.Location; -import org.apache.freemarker.generator.base.datasource.DataSource; -import org.apache.freemarker.generator.base.datasource.DataSourceFactory; -import org.apache.freemarker.generator.base.datasource.DataSourcesSupplier; +import org.apache.freemarker.generator.base.FreeMarkerConstants.Mode; import org.apache.freemarker.generator.base.output.OutputGenerator; -import org.apache.freemarker.generator.base.template.TemplateTransformation; -import org.apache.freemarker.generator.base.template.TemplateTransformationsBuilder; -import org.apache.freemarker.generator.base.util.UriUtils; +import org.apache.freemarker.generator.cli.config.output.AggregatingOutputGenerator; +import org.apache.freemarker.generator.cli.config.output.GeneratingOutputGenerator; import org.apache.freemarker.generator.cli.picocli.OutputGeneratorDefinition; -import org.apache.freemarker.generator.cli.picocli.TemplateOutputDefinition; -import org.apache.freemarker.generator.cli.picocli.TemplateSourceDefinition; -import java.net.URI; -import java.util.ArrayList; import java.util.Collection; -import java.util.HashMap; import java.util.List; -import java.util.Map; import java.util.function.Supplier; import java.util.stream.Collectors; -import static java.nio.charset.StandardCharsets.UTF_8; -import static java.util.Objects.requireNonNull; -import static org.apache.freemarker.generator.base.FreeMarkerConstants.DEFAULT_GROUP; -import static org.apache.freemarker.generator.base.FreeMarkerConstants.Location.STDIN; -import static org.apache.freemarker.generator.base.mime.Mimetypes.MIME_TEXT_PLAIN; - +/** + * Supplies a list of list of <code>OutputGenerators</code> based on the user input. + */ public class OutputGeneratorsSupplier implements Supplier<List<OutputGenerator>> { private final Settings settings; + private final AggregatingOutputGenerator aggregatingOutputGenerator; + private final GeneratingOutputGenerator generatingOutputGenerator; public OutputGeneratorsSupplier(Settings settings) { this.settings = settings; + aggregatingOutputGenerator = new AggregatingOutputGenerator(settings); + generatingOutputGenerator = new GeneratingOutputGenerator(settings); } @Override @@ -60,83 +51,13 @@ public class OutputGeneratorsSupplier implements Supplier<List<OutputGenerator>> } private List<OutputGenerator> outputGenerator(OutputGeneratorDefinition definition) { - final List<OutputGenerator> result = new ArrayList<>(); - final TemplateSourceDefinition templateSourceDefinition = requireNonNull(definition.getTemplateSourceDefinition()); - final TemplateOutputDefinition templateOutputDefinition = definition.getTemplateOutputDefinition(); - final TemplateTransformationsBuilder builder = TemplateTransformationsBuilder.builder(); - - // set the template - if (templateSourceDefinition.isInteractiveTemplate()) { - builder.setInteractiveTemplate(templateSourceDefinition.interactiveTemplate); + final String mode = definition.getOutputGeneratorMode(); + if (Mode.AGGREGATE.equalsIgnoreCase(mode)) { + return aggregatingOutputGenerator.apply(definition); + } else if (Mode.GENERATE.equalsIgnoreCase(mode)) { + return generatingOutputGenerator.apply(definition); } else { - builder.setTemplateSource(templateSourceDefinition.template); - } - - // set encoding of loaded templates - builder.setTemplateEncoding(settings.getTemplateEncoding()); - - // set the writer - builder.setCallerSuppliedWriter(settings.getCallerSuppliedWriter()); - - // set template output - if (templateOutputDefinition != null && templateOutputDefinition.hasOutput()) { - builder.setOutput(templateOutputDefinition.outputs.get(0)); + throw new RuntimeException("Unknown output generator mode:" + mode); } - - // set the output encoding - builder.setOutputEncoding(settings.getOutputEncoding()); - - // set template filter - if (definition.hasTemplateSourceIncludes()) { - builder.addInclude(definition.getTemplateSourceFilterDefinition().templateIncludePatterns.get(0)); - } - - if (definition.hasTemplateSourceExcludes()) { - builder.addExclude(definition.getTemplateSourceFilterDefinition().templateExcludePatterns.get(0)); - } - - final List<TemplateTransformation> templateTransformations = builder.build(); - - for (TemplateTransformation templateTransformation : templateTransformations) { - final OutputGenerator outputGenerator = new OutputGenerator( - templateTransformation.getTemplateSource(), - templateTransformation.getTemplateOutput(), - dataSources(settings, definition), - dataModels(definition) - ); - result.add(outputGenerator); - } - - return result; - } - - private List<DataSource> dataSources(Settings settings, OutputGeneratorDefinition outputGeneratorDefinition) { - final ArrayList<DataSource> result = new ArrayList<>(); - - // Add optional data source from STDIN at the start of the list since - // this allows easy sequence slicing in FreeMarker. - if (settings.isReadFromStdin()) { - result.add(0, stdinDataSource()); - } - - final DataSourcesSupplier outputGeneratorDataSourcesSupplier = new DataSourcesSupplier( - outputGeneratorDefinition.getDataSources(), - settings.getSourceIncludePattern(), - settings.getSourceExcludePattern(), - settings.getInputEncoding() - ); - - result.addAll(outputGeneratorDataSourcesSupplier.get()); - - return result; - } - - private Map<String, Object> dataModels(OutputGeneratorDefinition outputGeneratorDefinition) { - return new DataModelSupplier(outputGeneratorDefinition.getDataModels()).get(); - } - - private static DataSource stdinDataSource() { - final URI uri = UriUtils.toUri(Location.SYSTEM, STDIN); - return DataSourceFactory.fromInputStream(STDIN, DEFAULT_GROUP, uri, System.in, MIME_TEXT_PLAIN, UTF_8, new HashMap<>()); } } diff --git a/freemarker-generator-cli/src/main/java/org/apache/freemarker/generator/cli/config/OutputGeneratorsSupplier.java b/freemarker-generator-cli/src/main/java/org/apache/freemarker/generator/cli/config/output/AggregatingOutputGenerator.java similarity index 89% copy from freemarker-generator-cli/src/main/java/org/apache/freemarker/generator/cli/config/OutputGeneratorsSupplier.java copy to freemarker-generator-cli/src/main/java/org/apache/freemarker/generator/cli/config/output/AggregatingOutputGenerator.java index 059a9ab..8959585 100644 --- a/freemarker-generator-cli/src/main/java/org/apache/freemarker/generator/cli/config/OutputGeneratorsSupplier.java +++ b/freemarker-generator-cli/src/main/java/org/apache/freemarker/generator/cli/config/output/AggregatingOutputGenerator.java @@ -14,7 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.apache.freemarker.generator.cli.config; +package org.apache.freemarker.generator.cli.config.output; import org.apache.freemarker.generator.base.FreeMarkerConstants.Location; import org.apache.freemarker.generator.base.datasource.DataSource; @@ -24,18 +24,18 @@ import org.apache.freemarker.generator.base.output.OutputGenerator; import org.apache.freemarker.generator.base.template.TemplateTransformation; import org.apache.freemarker.generator.base.template.TemplateTransformationsBuilder; import org.apache.freemarker.generator.base.util.UriUtils; +import org.apache.freemarker.generator.cli.config.DataModelSupplier; +import org.apache.freemarker.generator.cli.config.Settings; import org.apache.freemarker.generator.cli.picocli.OutputGeneratorDefinition; import org.apache.freemarker.generator.cli.picocli.TemplateOutputDefinition; import org.apache.freemarker.generator.cli.picocli.TemplateSourceDefinition; import java.net.URI; import java.util.ArrayList; -import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.function.Supplier; -import java.util.stream.Collectors; +import java.util.function.Function; import static java.nio.charset.StandardCharsets.UTF_8; import static java.util.Objects.requireNonNull; @@ -43,23 +43,16 @@ import static org.apache.freemarker.generator.base.FreeMarkerConstants.DEFAULT_G import static org.apache.freemarker.generator.base.FreeMarkerConstants.Location.STDIN; import static org.apache.freemarker.generator.base.mime.Mimetypes.MIME_TEXT_PLAIN; -public class OutputGeneratorsSupplier implements Supplier<List<OutputGenerator>> { +public class AggregatingOutputGenerator implements Function<OutputGeneratorDefinition, List<OutputGenerator>> { private final Settings settings; - public OutputGeneratorsSupplier(Settings settings) { + public AggregatingOutputGenerator(Settings settings) { this.settings = settings; } @Override - public List<OutputGenerator> get() { - return settings.getOutputGeneratorDefinitions().stream() - .map(this::outputGenerator) - .flatMap(Collection::stream) - .collect(Collectors.toList()); - } - - private List<OutputGenerator> outputGenerator(OutputGeneratorDefinition definition) { + public List<OutputGenerator> apply(OutputGeneratorDefinition definition) { final List<OutputGenerator> result = new ArrayList<>(); final TemplateSourceDefinition templateSourceDefinition = requireNonNull(definition.getTemplateSourceDefinition()); final TemplateOutputDefinition templateOutputDefinition = definition.getTemplateOutputDefinition(); diff --git a/freemarker-generator-cli/src/main/java/org/apache/freemarker/generator/cli/config/output/GeneratingOutputGenerator.java b/freemarker-generator-cli/src/main/java/org/apache/freemarker/generator/cli/config/output/GeneratingOutputGenerator.java new file mode 100644 index 0000000..c6d3ddd --- /dev/null +++ b/freemarker-generator-cli/src/main/java/org/apache/freemarker/generator/cli/config/output/GeneratingOutputGenerator.java @@ -0,0 +1,39 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.freemarker.generator.cli.config.output; + +import org.apache.freemarker.generator.base.output.OutputGenerator; +import org.apache.freemarker.generator.cli.config.Settings; +import org.apache.freemarker.generator.cli.picocli.OutputGeneratorDefinition; + +import java.util.Collections; +import java.util.List; +import java.util.function.Function; + +public class GeneratingOutputGenerator implements Function<OutputGeneratorDefinition, List<OutputGenerator>> { + + private final Settings settings; + + public GeneratingOutputGenerator(Settings settings) { + this.settings = settings; + } + + @Override + public List<OutputGenerator> apply(OutputGeneratorDefinition outputGeneratorDefinition) { + return Collections.emptyList(); + } +} diff --git a/freemarker-generator-cli/src/main/java/org/apache/freemarker/generator/cli/picocli/OutputGeneratorDefinition.java b/freemarker-generator-cli/src/main/java/org/apache/freemarker/generator/cli/picocli/OutputGeneratorDefinition.java index 532cf42..98d2347 100644 --- a/freemarker-generator-cli/src/main/java/org/apache/freemarker/generator/cli/picocli/OutputGeneratorDefinition.java +++ b/freemarker-generator-cli/src/main/java/org/apache/freemarker/generator/cli/picocli/OutputGeneratorDefinition.java @@ -16,6 +16,7 @@ */ package org.apache.freemarker.generator.cli.picocli; +import org.apache.freemarker.generator.base.FreeMarkerConstants.Mode; import picocli.CommandLine; import picocli.CommandLine.ArgGroup; import picocli.CommandLine.ParameterException; @@ -36,11 +37,17 @@ public class OutputGeneratorDefinition { public TemplateOutputDefinition templateOutputDefinition; @ArgGroup(exclusive = false) + public TemplateOutputMapperDefinition templateOutputMapperDefinition; + + @ArgGroup(exclusive = false) public DataSourceDefinition dataSourceDefinition; @ArgGroup(exclusive = false) public DataModelDefinition dataModelDefinition; + @ArgGroup(exclusive = false) + public OutputGeneratorModeDefinition outputGeneratorModeDefinition; + public void validate(CommandLine commandLine) { if (templateSourceDefinition == null) { throw new ParameterException(commandLine, "No template defined to be rendered"); @@ -99,6 +106,15 @@ public class OutputGeneratorDefinition { !getTemplateSourceFilterDefinition().templateExcludePatterns.isEmpty(); } + public String getOutputGeneratorMode() { + if (outputGeneratorModeDefinition != null && + outputGeneratorModeDefinition.outputGeneratorMode != null) { + return outputGeneratorModeDefinition.outputGeneratorMode; + } else { + return Mode.AGGREGATE; + } + } + private static boolean isFileSource(String source) { if (source.contains("file://")) { return true; diff --git a/freemarker-generator-cli/src/main/java/org/apache/freemarker/generator/cli/picocli/OutputGeneratorModeDefinition.java b/freemarker-generator-cli/src/main/java/org/apache/freemarker/generator/cli/picocli/OutputGeneratorModeDefinition.java new file mode 100644 index 0000000..ba865aa --- /dev/null +++ b/freemarker-generator-cli/src/main/java/org/apache/freemarker/generator/cli/picocli/OutputGeneratorModeDefinition.java @@ -0,0 +1,28 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.freemarker.generator.cli.picocli; + +import picocli.CommandLine.Option; + +/** + * Aggregate multiple data source into one output file or generate an output file per data source. + */ +public class OutputGeneratorModeDefinition { + + @Option(names = { "-M", "--output-mode" }, defaultValue = "aggregate", description = "Output mode: [generate|aggregate]") + public String outputGeneratorMode; +} diff --git a/freemarker-generator-cli/src/main/java/org/apache/freemarker/generator/cli/picocli/TemplateOutputMapperDefinition.java b/freemarker-generator-cli/src/main/java/org/apache/freemarker/generator/cli/picocli/TemplateOutputMapperDefinition.java new file mode 100644 index 0000000..1512a5d --- /dev/null +++ b/freemarker-generator-cli/src/main/java/org/apache/freemarker/generator/cli/picocli/TemplateOutputMapperDefinition.java @@ -0,0 +1,25 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.freemarker.generator.cli.picocli; + +import picocli.CommandLine.Option; + +public class TemplateOutputMapperDefinition { + + @Option(names = { "--output-mapper" }, description = "maps the name of the output file") + public String outputMapper; +}
