#!/usr/bin/env bash set -euo pipefail usage() { echo "Stubs out files for new generators" && \ echo "usage:" && \ echo "$0 [options]" && \ echo " Options:" grep "[[:space:]].)\ #" $0 | tr -d "#" | sed -r 's/( \| \*)//' | sed -r 's/([a-z])\)/-\1/'; exit 0; } root=$(cd $(dirname "${BASH_SOURCE}") && pwd) gen_type=client tests=0 gen_name= [ $# -eq 0 ] && usage while getopts ":hcsdtn:" arg; do case ${arg} in n) # Required. Specify generator name. gen_name=${OPTARG} ;; c) # Create a client generator gen_type=client ;; s) # Create a server generator gen_type=server ;; d) # Create a documentation generator gen_type=documentation ;; t) # When specified, creates test file(s) for the generator. tests=1 ;; h | *) # Display help. usage exit 0 ;; esac done [ -z "${gen_name}" ] && usage lang_classname=$(echo "${gen_name}-${gen_type}Codegen" | sed -r 's/(^|_|-)([a-z])/\U\2/g') gen_name_camel=$(echo "${gen_name}" | sed -r 's/(^|_|-)([a-z])/\U\2/g' | sed 's/^./\L&/') codegen_type_enum=$(echo "${gen_type}" | sed -r 's/(.)/\U\1/g') # Step 1: Add Language Generator [ -f "${root}/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/${lang_classname}.java" ] && \ echo "${lang_classname} already exists" && exit 1; echo "Creating modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/${lang_classname}.java" cat > "${root}/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/${lang_classname}.java" <> "${root}/modules/swagger-codegen/src/main/resources/META-INF/services/io.swagger.codegen.CodegenConfig" # Step 3: Create resource files mkdir -p "${root}/modules/swagger-codegen/src/main/resources/${gen_name}-${gen_type}" echo "Creating modules/swagger-codegen/src/main/resources/${gen_name}-${gen_type}/README.md" && \ touch "${root}/modules/swagger-codegen/src/main/resources/${gen_name}-${gen_type}/README.md" echo "Creating modules/swagger-codegen/src/main/resources/${gen_name}-${gen_type}/model.mustache" && \ touch "${root}/modules/swagger-codegen/src/main/resources/${gen_name}-${gen_type}/model.mustache" echo "Creating /modules/swagger-codegen/src/main/resources/${gen_name}-${gen_type}/api.mustache" && \ touch "${root}/modules/swagger-codegen/src/main/resources/${gen_name}-${gen_type}/api.mustache" # Step 4: Create bash/batch scripts ## Windows batch file echo "Creating bin/windows/${gen_name}-${gen_type}-petstore.bat" cat > "${root}/bin/windows/${gen_name}-${gen_type}-petstore.bat"< "${root}/bin/${gen_name}-${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/swagger-codegen-cli/target/swagger-codegen-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} -XX:MaxPermSize=256M -Xmx1024M -DloggerPath=conf/log4j.properties" ags="\$@ generate -i modules/swagger-codegen/src/test/resources/2_0/petstore.yaml -l ${gen_name} -o samples/${gen_type}/petstore/${gen_name}" java \${JAVA_OPTS} -jar \${executable} \${ags} EOF # Step 5: (optional) Create Swagger Codegen test files if [ "1" -eq "${tests}" ]; then mkdir -p "${root}/modules/swagger-codegen/src/test/java/io/swagger/codegen/${gen_name_camel}" # Codegen echo "Creating modules/swagger-codegen/src/test/java/io/swagger/codegen/${gen_name_camel}/${lang_classname}Test.java" cat > "${root}/modules/swagger-codegen/src/test/java/io/swagger/codegen/${gen_name_camel}/${lang_classname}Test.java"< "${root}/modules/swagger-codegen/src/test/java/io/swagger/codegen/${gen_name_camel}/${lang_classname}ModelTest.java"< "${root}/modules/swagger-codegen/src/test/java/io/swagger/codegen/${gen_name_camel}/${lang_classname}OptionsTest.java"< "${root}/modules/swagger-codegen/src/test/java/io/swagger/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."