#!/usr/bin/env bash set -euo pipefail set -o noclobber usage() { cat <&2 usage >&2 exit 1 fi } [ $# -eq 0 ] && usage while getopts ":hcsdtn:" arg; do case ${arg} in n) # Required. Specify generator name, should be kebab-cased. gen_name=${OPTARG} ;; c) # Create a client generator checkPreviousGenType gen_type=client ;; s) # Create a server generator checkPreviousGenType gen_type=server ;; d) # Create a documentation generator checkPreviousGenType gen_type=documentation ;; h) # Create a schema generator checkPreviousGenType gen_type=schema ;; f) # Create a config generator checkPreviousGenType gen_type=config ;; t) # When specified, creates test file(s) for the generator. tests=1 ;; h | *) # Display help. usage exit 0 ;; esac done if [ -z "$gen_type" ]; then echo "[error] You may set a generator type" >&2 usage >&2 exit 1 fi [ -z "${gen_name}" ] && usage titleCase() { if [ "$os" == "darwin" ]; then echo $1 | tr '-' ' ' | tr '_' ' ' | ruby -e "print STDIN.gets.split.map(&:capitalize).join(' ')" | tr -d ' ' else read -ra words <<< $(echo $1 | tr '-' ' ' | tr '_' ' ') echo "${words[@]^}" | tr -d ' ' fi } kebabCase() { echo $1 | tr '_' ' ' | tr ' ' '-' | tr '[:upper:]' '[:lower:]' } kebabCasePath() { echo $1 | tr '_' ' ' | tr ' ' '-' | tr '-' '/' | tr '[:upper:]' '[:lower:]' } kebabCasePathWin() { echo $1 | tr '_' ' ' | tr ' ' '-' | tr '-' '\\' | tr '[:upper:]' '[:lower:]' } kebabCasePkg() { echo $1 | tr '_' ' ' | tr ' ' '-' | tr '-' '.' | tr '[:upper:]' '[:lower:]' } upperCase() { echo $1 | tr '[[:lower:]]' '[[:upper:]]' } declare lang_classname=$(titleCase "${gen_name}-${gen_type}-Codegen") declare gen_name_camel=$(kebabCase "${gen_name}") declare gen_name_camel_path=$(kebabCasePath "${gen_name}") declare gen_name_camel_pathwin=$(kebabCasePathWin "${gen_name}") declare gen_name_camel_pkg=$(kebabCasePkg "${gen_name}") declare codegen_type_enum=$(upperCase "${gen_type}") # Step 1: Add Language Generator [ -f "${root}/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/${lang_classname}.java" ] && \ echo "${lang_classname} already exists" && exit 1; echo "Creating modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/${lang_classname}.java" cat > "${root}/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/${lang_classname}.java" <> "${root}/modules/openapi-generator/src/main/resources/META-INF/services/org.openapitools.codegen.CodegenConfig" # Step 3: Create resource files mkdir -p "${root}/modules/openapi-generator/src/main/resources/${gen_name_camel}-${gen_type}" echo "Creating modules/openapi-generator/src/main/resources/${gen_name_camel}-${gen_type}/README.mustache" && \ touch "${root}/modules/openapi-generator/src/main/resources/${gen_name_camel}-${gen_type}/README.mustache" echo "Creating modules/openapi-generator/src/main/resources/${gen_name_camel}-${gen_type}/model.mustache" && \ touch "${root}/modules/openapi-generator/src/main/resources/${gen_name_camel}-${gen_type}/model.mustache" echo "Creating modules/openapi-generator/src/main/resources/${gen_name_camel}-${gen_type}/api.mustache" && \ touch "${root}/modules/openapi-generator/src/main/resources/${gen_name_camel}-${gen_type}/api.mustache" # Step 4: Create bash/batch scripts ## Windows batch file echo "Creating bin/windows/${gen_name_camel}-${gen_type}-petstore.bat" cat > "${root}/bin/windows/${gen_name_camel}-${gen_type}-petstore.bat"< "${root}/bin/${gen_name_camel}-${gen_type}-petstore.sh"< \(.*\)$') if expr "\$link" : '/.*' > /dev/null; then SCRIPT="\$link" else SCRIPT=\$(dirname "\$SCRIPT")/"\$link" fi done if [ ! -d "\${APP_DIR}" ]; then APP_DIR=\$(dirname "\$SCRIPT")/.. APP_DIR=\$(cd "\${APP_DIR}"; pwd) fi executable="./modules/openapi-generator-cli/target/openapi-generator-cli.jar" if [ ! -f "\$executable" ] then mvn clean package fi # if you've executed sbt assembly previously it will use that instead. export JAVA_OPTS="\${JAVA_OPTS} -Xmx1024M -DloggerPath=conf/log4j.properties" ags="\$@ generate -i modules/openapi-generator/src/test/resources/2_0/petstore.yaml -g ${gen_name_camel} -o samples/${gen_type}/petstore/${gen_name_camel_path}" java \${JAVA_OPTS} -jar \${executable} \${ags} EOF chmod u+x "${root}/bin/${gen_name_camel}-${gen_type}-petstore.sh" # Step 5: (optional) Create OpenAPI Generator test files if [ "1" -eq "${tests}" ]; then mkdir -p "${root}/modules/openapi-generator/src/test/java/org/openapitools/codegen/${gen_name_camel_path}" # Codegen echo "Creating modules/openapi-generator/src/test/java/org/openapitools/codegen/${gen_name_camel_path}/${lang_classname}Test.java" cat > "${root}/modules/openapi-generator/src/test/java/org/openapitools/codegen/${gen_name_camel_path}/${lang_classname}Test.java"< "${root}/modules/openapi-generator/src/test/java/org/openapitools/codegen/${gen_name_camel_path}/${lang_classname}ModelTest.java"< "${root}/modules/openapi-generator/src/test/java/org/openapitools/codegen/${gen_name_camel_path}/${lang_classname}OptionsTest.java"< "${root}/modules/openapi-generator/src/test/java/org/openapitools/codegen/options/${lang_classname}OptionsProvider.java"< createOptions() { ImmutableMap.Builder builder = new ImmutableMap.Builder(); return builder .put(${lang_classname}.PROJECT_NAME, PROJECT_NAME_VALUE) .build(); } @Override public boolean isServer() { return false; } } EOF fi echo "Finished."