genericdatabaseapiservice/generateAll.sh

142 lines
4.1 KiB
Bash
Executable File

#!/bin/bash
REMOVE="0"
BUILD="0"
EXECUTE="0"
STAGE1="0"
STAGE2="0"
STAGE2_PRE_CONTAINER="0"
KEEP="0"
while getopts rxbh12ck flag
do
case "${flag}" in
h)
echo "1 ... generator stage 1 (implementation generator)";
echo "2 ... generator stage 2 (openapi generator)";
echo "r ... remove output directory";
echo "k ... keep existing output directory";
echo "b ... build after generating";
echo "x ... execute after building";
echo "c ... run stage in pre-started container";
echo "h ... show help";
;;
b)
BUILD="1"
;;
r)
REMOVE="1"
;;
x)
EXECUTE="1"
;;
1)
STAGE1="1"
;;
2)
STAGE2="1"
;;
c)
STAGE2_PRE_CONTAINER="1"
;;
k)
KEEP="1"
;;
esac
done
if [ "$REMOVE" = "1" ]; then
echo "remove output directory"
rm -rf output
fi
# safety measure
if [ "$KEEP" = "0" -a -d output ]; then
echo "output directory already exist"
echo "remove manually and try again"
exit 1
fi
# PACKAGE_NAME will be loaded here
. ENV
if [ -f ENV.database ]; then
echo "database environment loaded"
. ENV.database
fi
if [ "$STAGE1" = "1" ]; then
echo "generate endpoint code from openapi.yaml"
python3.10 generate.py
fi
if [ "$STAGE2" = "1" ]; then
echo "generate server code and endpoint stubs from openapi.yaml"
if [ "$STAGE2_PRE_CONTAINER" = "0" ]; then
OPENAPI_GENERATOR_CMD="docker run -it --rm -v $PWD:/work -u $UID openapitools/openapi-generator-cli:v5.3.0"
WORK_DIR="/work"
else
OPENAPI_GENERATOR_CMD="docker-entrypoint.sh"
WORK_DIR="."
fi
$OPENAPI_GENERATOR_CMD \
generate -i $WORK_DIR/openapi.yaml -g aspnetcore -o $WORK_DIR/output \
--package-name $PACKAGE_NAME \
--additional-properties="packageVersion=0.0.1,aspnetCoreVersion=5.0,operationIsAsync=false,operationResultTask=true,\
generateBody=false,classModifier=abstract,operationModifier=abstract"
echo "patch DbService registering into generated startup code"
sed -i output/src/$PACKAGE_NAME/Startup.cs \
-e 's#\(using '$PACKAGE_NAME'.OpenApi;\)#\1\n\n// added by post-processor\nusing '$PACKAGE_NAME'.Services;\n#' \
-e 's#^\([[:space:]]*\)\(// Add framework services.\)#\1// added by post-processor\n\1services.AddSingleton<IDbInfoService, DbInfoService>();\n\1services.AddTransient<IDbService, DbService>();\n\n\1\2#' \
-e 's#\(c.RoutePrefix = "\)openapi\(";\)#\1'$ROUTE_PREFIX'/api/doc\2#' \
-e 's#\(c.SwaggerEndpoint("\)/openapi/2.0.0/openapi.json\(", "Generic Database API Service");\)#\1/'$ROUTE_PREFIX'/api/openapi/JSON\2#' \
-e 's#\(c.RouteTemplate = "\)openapi/{documentName}/openapi.json\(";\)#\1'$ROUTE_PREFIX'/api/openapi/JSON\2#' \
-e 's#\(app.UseHttpsRedirection();\)#// \1#'
echo "fix root redirect in index.html"
sed -i output/src/$PACKAGE_NAME/wwwroot/index.html \
-e 's#./openapi#/'$ROUTE_PREFIX'/api/doc#'
mkdir -p output/src/$PACKAGE_NAME/wwwroot/$ROUTE_PREFIX
cp output/src/$PACKAGE_NAME/wwwroot/index.html output/src/$PACKAGE_NAME/wwwroot/$ROUTE_PREFIX
echo "disable documentation warnings in generated code"
sed -i output/src/$PACKAGE_NAME/$PACKAGE_NAME.csproj \
-e 's#\(</PropertyGroup>\)#<NoWarn>1591</NoWarn>\n\1#'
echo "create directories for manually developed code"
mkdir output/src/$PACKAGE_NAME/Implementations
mkdir output/src/$PACKAGE_NAME/Services
echo "copy database service into source code try"
cp DbService.cs output/src/$PACKAGE_NAME/Services
if [ -f regular.cs ]; then
echo "copy endpoint code into source code tree"
cp regular.cs output/src/$PACKAGE_NAME/Implementations
cp OpenAPIDocCtrl.cs output/src/$PACKAGE_NAME/Controllers
else
echo "implementation not available, forgot to run stage 1?"
fi
fi
if [ "$BUILD" = "1" ]; then
echo "build service"
pushd output
pushd src/$PACKAGE_NAME
dotnet add package MySqlConnector --version 2.0.0
popd
sh build.sh
popd
fi
if [ "$EXECUTE" = "1" ]; then
echo "execute service"
dotnet run -p output/src/$PACKAGE_NAME/$PACKAGE_NAME.csproj
fi