Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ci][cli] Allow invoking generate-samples.sh with single file + args #6609

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 52 additions & 12 deletions bin/generate-samples.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 1 addition & 4 deletions bin/utils/ensure-up-to-date
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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",
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down