Skip to content

Instantly share code, notes, and snippets.

@saggie
Last active May 27, 2019 03:42
Show Gist options
  • Save saggie/90eae34c0805505fab86a93b4f26d367 to your computer and use it in GitHub Desktop.
Save saggie/90eae34c0805505fab86a93b4f26d367 to your computer and use it in GitHub Desktop.
Generating & deploying code by Swagger Codegen CLI
#!/bin/bash
#
# Usage: ./generate.sh <Maven repository URL>
# - e.g. ./generate.sh https://artifactory.example.com/artifactory/maven
#
# Pre-req: wget, java
#
set -eu
if [ $# -ne 1 ]
then
echo 'Usage: ./generate.sh <Maven repository URL>' 1>&2
exit 1
fi
readonly REPO_URL=${1}
readonly CLI_URL="${REPO_URL}/io/swagger/swagger-codegen-cli/2.3.1/swagger-codegen-cli-2.3.1.jar"
readonly SCRIPT_PATH=`readlink -f $0`
readonly SCRIPT_DIR=`dirname $SCRIPT_PATH`
readonly SCRIPT_PARENT_DIR=`dirname $SCRIPT_DIR`
readonly INPUT_FILE_NAME='rest_api_template.yaml'
readonly INPUT_FILE_PATH="${SCRIPT_DIR}/${INPUT_FILE_NAME}"
readonly OUTPUT_DIR_NAME='swagger-output'
readonly OUTPUT_DIR_PATH="${SCRIPT_DIR}/${OUTPUT_DIR_NAME}"
readonly PATH_TO_AUTOGEN_CODE='src/main/java/com/example/foo/bar/autogen'
readonly SRC_PATH_TO_DEPLOY="${SCRIPT_DIR}/${OUTPUT_DIR_NAME}/${PATH_TO_AUTOGEN_CODE}"
readonly DST_PATH_TO_DEPLOY="${SCRIPT_PARENT_DIR}/${PATH_TO_AUTOGEN_CODE}"
# Cleanup the output directory
function cleanup() {
echo 'Cleaning up an output directory...'
rm -rf $OUTPUT_DIR_PATH
}
cleanup
# Get 'swagger-codegen-cli.jar' if not exist
if [ ! -f ${SCRIPT_DIR}/swagger-codegen-cli.jar ]
then
echo "Getting a swagger-codegen CLI... (from ${CLI_URL})"
wget $CLI_URL -O ${SCRIPT_DIR}/swagger-codegen-cli.jar --quiet --no-check-certificate
fi
# Generate
echo 'Generating code...'
java -jar ${SCRIPT_DIR}/swagger-codegen-cli.jar generate \
-l java \
-i $INPUT_FILE_PATH \
-o $OUTPUT_DIR_PATH \
-c ${SCRIPT_DIR}/config.json \
-t ${SCRIPT_DIR}/templates \
# Deploy
echo 'Deploying code...'
mkdir -p ${DST_PATH_TO_DEPLOY}
cp -rf ${SRC_PATH_TO_DEPLOY}/api ${DST_PATH_TO_DEPLOY}
cp -rf ${SRC_PATH_TO_DEPLOY}/model ${DST_PATH_TO_DEPLOY}
cleanup
echo 'Completed.'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment