diff --git a/bin/generate-samples.sh b/bin/generate-samples.sh index 5205b22c745c..d497a554f3ee 100755 --- a/bin/generate-samples.sh +++ b/bin/generate-samples.sh @@ -6,21 +6,61 @@ declare cwd="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" declare root="$(cd "$cwd" && cd ../ && pwd)" declare executable="${root}/modules/openapi-generator-cli/target/openapi-generator-cli.jar" -echo "# START SCRIPT: $0" -echo "This script generates all configs under bin/configs by default." -echo "You may generate a targeted script or set of scripts using glob patterns." -echo "For example: $0 bin/configs/java-*" -echo "" -echo "Please press CTRL+C to stop or the script will continue in 5 seconds." - -sleep 5 if [ ! -f "$executable" ]; then (cd "${root}" && mvn -B --no-snapshot-updates clean package -DskipTests=true -Dmaven.javadoc.skip=true -Djacoco.skip=true) fi -export JAVA_OPTS="${JAVA_OPTS} -server -Duser.timezone=UTC" +export JAVA_OPTS="${JAVA_OPTS} -ea -server -Duser.timezone=UTC" -configs=${@:-"${root}"/bin/configs/*.yaml} +files=() +args=() +end_option=false +while [[ $# -gt 0 ]]; do + key="$1" + if [ "--" == "$key" ]; then + end_option=true + else + if [[ "$end_option" = true ]]; then + args+=("$1") + else + files+=("$1") + fi + fi + shift +done + +header="# START SCRIPT: $0 +This script generates all configs under bin/configs by default. +You may generate a targeted script or set of scripts using glob patterns. + +For example: + $0 bin/configs/java-* + +You may generate a single config with additional options if you use -- to +separate the single config file from the generator arguments. + +For example: + $0 bin/configs/java-vertx.yaml -- --global-property debugModels=true + +" + +echo "$header" + +if [[ ${#files[@]} -eq 1 && "${files[0]}" != *'*'* ]]; then + # shellcheck disable=SC2086 + # shellcheck disable=SC2068 + java ${JAVA_OPTS} -jar "$executable" generate -c ${files[0]} ${args[@]} +else + echo "Please press CTRL+C to stop or the script will continue in 5 seconds." + + sleep 5 + + if [ ${#files[@]} -eq 0 ]; then + files=("${root}"/bin/configs/*.yaml) + fi + + # shellcheck disable=SC2086 + # shellcheck disable=SC2068 + java ${JAVA_OPTS} -jar "$executable" batch --includes-base-dir "${root}" --fail-fast -- ${files[@]} +fi -# shellcheck disable=SC2086 -java $JAVA_OPTS -jar "$executable" batch --includes-base-dir "${root}" --fail-fast -- $configs diff --git a/bin/utils/ensure-up-to-date b/bin/utils/ensure-up-to-date index efe99bd76a59..037d1f554e86 100755 --- a/bin/utils/ensure-up-to-date +++ b/bin/utils/ensure-up-to-date @@ -8,11 +8,8 @@ declare root="$(cd "$cwd" && cd ../../ && pwd)" declare executable="${root}/modules/openapi-generator-cli/target/openapi-generator-cli.jar" echo "# START SCRIPT: $0" - echo "IMPORTANT: this script should be run by the CI (e.g. Shippable) to ensure that the 'samples/' folder is up to date." -echo "Please press CTRL+C to stop or the script will continue in 5 seconds." - -sleep 5 +echo "" "${root}/bin/generate-samples.sh" diff --git a/modules/openapi-generator-cli/src/main/java/org/openapitools/codegen/cmd/Generate.java b/modules/openapi-generator-cli/src/main/java/org/openapitools/codegen/cmd/Generate.java index 3387a4047487..12794d295537 100644 --- a/modules/openapi-generator-cli/src/main/java/org/openapitools/codegen/cmd/Generate.java +++ b/modules/openapi-generator-cli/src/main/java/org/openapitools/codegen/cmd/Generate.java @@ -29,6 +29,7 @@ import java.util.List; import java.util.stream.Stream; +import org.apache.commons.lang3.StringUtils; import org.openapitools.codegen.*; import org.openapitools.codegen.config.CodegenConfigurator; import org.slf4j.Logger; @@ -52,8 +53,8 @@ public class Generate extends OpenApiGeneratorCommand { description = "where to write the generated files (current dir by default)") private String output = ""; - @Option(name = {"-i", "--input-spec"}, title = "spec file", required = true, - description = "location of the OpenAPI spec, as URL or file (required)") + @Option(name = {"-i", "--input-spec"}, title = "spec file", + description = "location of the OpenAPI spec, as URL or file (required if not loaded via config using -c)") private String spec; @Option(name = {"-t", "--template-dir"}, title = "template directory", @@ -264,6 +265,10 @@ public void execute() { if (configFile != null && configFile.length() > 0) { // attempt to load from configFile configurator = CodegenConfigurator.fromFile(configFile); + } else if (StringUtils.isEmpty(spec)) { + // if user doesn't pass configFile and does not pass spec, we can fail immediately because one of these two is required to run. + System.err.println("[error] Required option '-i' is missing"); + System.exit(1); } // if a config file wasn't specified, or we were unable to read it @@ -295,11 +300,9 @@ public void execute() { configurator.setInputSpec(spec); } + // Generator name should not be validated here, as it's validated in toClientOptInput if (isNotEmpty(generatorName)) { configurator.setGeneratorName(generatorName); - } else { - System.err.println("[error] A generator name (--generator-name / -g) is required."); - System.exit(1); } if (isNotEmpty(output)) { diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/config/CodegenConfigurator.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/config/CodegenConfigurator.java index ddb41ce7b8a6..6b46f0d61097 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/config/CodegenConfigurator.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/config/CodegenConfigurator.java @@ -466,7 +466,7 @@ public CodegenConfigurator setVerbose(boolean verbose) { @SuppressWarnings("WeakerAccess") public Context toContext() { - Validate.notEmpty(generatorName, "language/generatorName must be specified"); + Validate.notEmpty(generatorName, "generator name must be specified"); Validate.notEmpty(inputSpec, "input spec must be specified"); if (isEmpty(templatingEngineName)) {