Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright 2018 OpenAPI-Generator Contributors (https://openapi-generator.tech)
* Copyright 2018 SmartBear Software
*
* Licensed 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
*
* https://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.openapitools.codegen.cmd;
import io.airlift.airline.Command;
import io.airlift.airline.Option;
import org.apache.commons.lang3.StringUtils;
import org.openapitools.codegen.CliOption;
import org.openapitools.codegen.CodegenConfig;
import org.openapitools.codegen.CodegenConfigLoader;
import org.openapitools.codegen.GeneratorNotFoundException;
import org.openapitools.codegen.meta.FeatureSet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Paths;
import java.util.*;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Function;
import java.util.stream.Collectors;
import static org.apache.commons.lang3.StringEscapeUtils.escapeHtml4;
import static org.apache.commons.lang3.StringUtils.isEmpty;
@SuppressWarnings({"unused","java:S106", "java:S1192"})
@Command(name = "config-help", description = "Config help for chosen lang")
public class ConfigHelp extends OpenApiGeneratorCommand {
private final Logger LOGGER = LoggerFactory.getLogger(ConfigHelp.class);
private static final String FORMAT_TEXT = "text";
private static final String FORMAT_MARKDOWN = "markdown";
private static final String FORMAT_YAMLSAMPLE = "yamlsample";
private static final int FEATURE_SET_DISPLAY_WIDTH= 20;
@Option(name = {"-g",
"--generator-name"}, title = "generator name", description = "generator to get config help for")
private String generatorName;
@Option(name = {
"--named-header"}, title = "named header", description = "Header includes the generator name, for clarity in output")
private Boolean namedHeader;
@Option(name = {"-o",
"--output"}, title = "output location", description = "Optionally write help to this location, otherwise default is standard output")
private String outputFile;
@Option(name = {"-f",
"--format"}, title = "output format", description = "Write output files in the desired format. Options are 'text', 'markdown' or 'yamlsample'. Default is 'text'.", allowedValues = {
FORMAT_TEXT, FORMAT_MARKDOWN, FORMAT_YAMLSAMPLE})
private String format;
@Option(name = {"--import-mappings"}, title = "import mappings", description = "displays the default import mappings (types and aliases, and what imports they will pull into the template)")
private Boolean importMappings;
@Option(name = {"--language-specific-primitive"}, title = "language specific primitives", description = "displays the language specific primitives (types which require no additional imports, or which may conflict with user defined model names)")
private Boolean languageSpecificPrimitives;
@Option(name = {"--reserved-words"}, title = "language specific reserved words", description = "displays the reserved words which may result in renamed model or property names")
private Boolean reservedWords;
@Option(name = {"--instantiation-types"}, title = "instantiation types", description = "displays types used to instantiate simple type/alias names")
private Boolean instantiationTypes;
@Option(name = {"--feature-set"}, title = "feature set", description = "displays feature set as supported by the generator")
private Boolean featureSets;
@Option(name = {
"--markdown-header"}, title = "markdown header", description = "When format=markdown, include this option to write out markdown headers (e.g. for docusaurus).")
private Boolean markdownHeader;
@Option(name = {"--full-details"}, title = "full generator details", description = "displays CLI options as well as other configs/mappings (implies --instantiation-types, --reserved-words, --language-specific-primitives, --import-mappings, --feature-set)")
private Boolean fullDetails;
private String newline = System.lineSeparator();
@Override
public void execute() {
if (isEmpty(generatorName)) {
LOGGER.error("[error] A generator name (--generator-name / -g) is required.");
System.exit(1);
}
if (Boolean.TRUE.equals(fullDetails)) {
instantiationTypes = Boolean.TRUE;
reservedWords = Boolean.TRUE;
languageSpecificPrimitives = Boolean.TRUE;
importMappings = Boolean.TRUE;
featureSets = Boolean.TRUE;
}
try {
StringBuilder sb = new StringBuilder();
CodegenConfig config = CodegenConfigLoader.forName(generatorName);
String desiredFormat = StringUtils.defaultIfBlank(format, FORMAT_TEXT);
switch (desiredFormat) {
case FORMAT_MARKDOWN:
generateMarkdownHelp(sb, config);
break;
case FORMAT_YAMLSAMPLE:
generateYamlSample(sb, config);
break;
case FORMAT_TEXT:
generatePlainTextHelp(sb, config);
break;
default:
LOGGER.warn("[warning] Unrecognized format option: {}", format);
break;
}
if (!isEmpty(outputFile)) {
File out = Paths.get(outputFile).toFile();
File parentFolder = out.getParentFile();
if (parentFolder != null && parentFolder.isDirectory()) {
//noinspection ResultOfMethodCallIgnored
parentFolder.mkdirs();
}
try (FileOutputStream fileOutputStream = new FileOutputStream(out);
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, StandardCharsets.UTF_8);
Writer writer = new BufferedWriter(outputStreamWriter)) {
writer.write(sb.toString());
}
} else {
System.out.print(sb.toString());
}
} catch (GeneratorNotFoundException e) {
LOGGER.error(e.getMessage());
LOGGER.error("[error] Check the spelling of the generator's name and try again.");
System.exit(1);
} catch (IOException e) {
LOGGER.error("Unexpected error", e);
}
}
private void generateMarkdownHelp(StringBuilder sb, CodegenConfig config) {
if (Boolean.TRUE.equals(markdownHeader)) {
sb.append("---").append(newline);
sb.append("title: Config Options for ").append(generatorName).append(newline);
sb.append("sidebar_label: ").append(generatorName).append(newline);
sb.append("---").append(newline);
sb.append(newline);
sb.append("These options may be applied as additional-properties (cli) or configOptions (plugins). Refer to [configuration docs](https://openapi-generator.tech/docs/configuration) for more details.");
sb.append(newline);
} else {
sb.append(newline);
sb.append("## CONFIG OPTIONS");
if (Boolean.TRUE.equals(namedHeader)) {
sb.append(" for ").append(generatorName).append("").append(newline);
}
}
sb.append(newline);
sb.append("| Option | Description | Values | Default |").append(newline);
sb.append("| ------ | ----------- | ------ | ------- |").append(newline);
Map langCliOptions = config.cliOptions()
.stream()
.collect(Collectors.toMap(CliOption::getOpt, Function.identity(), (a, b) -> {
throw new IllegalStateException(String.format(Locale.ROOT, "Duplicated options! %s and %s", a.getOpt(), b.getOpt()));
}, TreeMap::new));
langCliOptions.forEach((key, langCliOption) -> {
// start
sb.append("|");
// option
sb.append(escapeHtml4(key)).append("|");
// description
sb.append(escapeHtml4(langCliOption.getDescription())).append("|");
// values
Map enums = langCliOption.getEnum();
if (enums != null) {
sb.append("
");
for (Map.Entry entry : enums.entrySet()) {
sb.append("