2021-11-22 19:07:46 +01:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
|
2021-11-23 14:00:39 +01:00
|
|
|
REMOVE="0"
|
|
|
|
BUILD="0"
|
|
|
|
EXECUTE="0"
|
2021-11-25 14:49:08 +01:00
|
|
|
STAGE1="0"
|
|
|
|
STAGE2="0"
|
2021-11-26 13:23:29 +01:00
|
|
|
STAGE2_PRE_CONTAINER="0"
|
|
|
|
KEEP="0"
|
|
|
|
while getopts rxbh12ck flag
|
2021-11-23 14:00:39 +01:00
|
|
|
do
|
|
|
|
case "${flag}" in
|
|
|
|
h)
|
2021-11-25 14:49:08 +01:00
|
|
|
echo "1 ... generator stage 1 (implementation generator)";
|
|
|
|
echo "2 ... generator stage 2 (openapi generator)";
|
2021-11-23 14:00:39 +01:00
|
|
|
echo "r ... remove output directory";
|
2021-11-26 13:23:29 +01:00
|
|
|
echo "k ... keep existing output directory";
|
2021-11-23 14:00:39 +01:00
|
|
|
echo "b ... build after generating";
|
|
|
|
echo "x ... execute after building";
|
2021-11-26 13:23:29 +01:00
|
|
|
echo "c ... run stage in pre-started container";
|
2021-11-23 14:00:39 +01:00
|
|
|
echo "h ... show help";
|
2021-11-25 14:49:08 +01:00
|
|
|
|
2021-11-23 14:00:39 +01:00
|
|
|
;;
|
|
|
|
b)
|
|
|
|
BUILD="1"
|
|
|
|
;;
|
|
|
|
r)
|
|
|
|
REMOVE="1"
|
|
|
|
;;
|
|
|
|
x)
|
|
|
|
EXECUTE="1"
|
|
|
|
;;
|
2021-11-25 14:49:08 +01:00
|
|
|
1)
|
|
|
|
STAGE1="1"
|
|
|
|
;;
|
|
|
|
2)
|
|
|
|
STAGE2="1"
|
|
|
|
;;
|
2021-11-26 13:23:29 +01:00
|
|
|
c)
|
|
|
|
STAGE2_PRE_CONTAINER="1"
|
|
|
|
;;
|
|
|
|
k)
|
|
|
|
KEEP="1"
|
|
|
|
;;
|
2021-11-23 14:00:39 +01:00
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
if [ "$REMOVE" = "1" ]; then
|
2021-11-23 14:48:31 +01:00
|
|
|
echo "remove output directory"
|
2021-11-23 14:00:39 +01:00
|
|
|
rm -rf output
|
|
|
|
fi
|
|
|
|
|
2021-11-22 19:07:46 +01:00
|
|
|
# safety measure
|
2021-11-26 13:23:29 +01:00
|
|
|
if [ "$KEEP" = "0" -a -d output ]; then
|
2021-11-22 19:07:46 +01:00
|
|
|
echo "output directory already exist"
|
|
|
|
echo "remove manually and try again"
|
2021-11-23 14:00:39 +01:00
|
|
|
exit 1
|
2021-11-22 19:07:46 +01:00
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
|
|
# PACKAGE_NAME will be loaded here
|
|
|
|
. ENV
|
|
|
|
|
2021-12-02 11:59:10 +01:00
|
|
|
if [ -f ENV.database ]; then
|
|
|
|
echo "database environment loaded"
|
|
|
|
. ENV.database
|
|
|
|
fi
|
|
|
|
|
2021-11-25 14:49:08 +01:00
|
|
|
if [ "$STAGE1" = "1" ]; then
|
|
|
|
echo "generate endpoint code from openapi.yaml"
|
|
|
|
python3.10 generate.py
|
|
|
|
fi
|
2021-11-22 19:07:46 +01:00
|
|
|
|
2021-11-25 14:49:08 +01:00
|
|
|
if [ "$STAGE2" = "1" ]; then
|
|
|
|
echo "generate server code and endpoint stubs from openapi.yaml"
|
2021-11-26 13:23:29 +01:00
|
|
|
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 \
|
2021-11-25 14:49:08 +01:00
|
|
|
--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#' \
|
2021-12-06 17:46:58 +01:00
|
|
|
-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#' \
|
2021-12-06 13:12:14 +01:00
|
|
|
-e 's#\(c.RoutePrefix = "\)openapi\(";\)#\1'$ROUTE_PREFIX'/api/doc\2#' \
|
2021-12-06 13:48:34 +01:00
|
|
|
-e 's#\(c.SwaggerEndpoint("\)/openapi/2.0.0/openapi.json\(", "Generic Database API Service");\)#\1/'$ROUTE_PREFIX'/api/openapi/JSON\2#' \
|
2021-12-06 13:12:14 +01:00
|
|
|
-e 's#\(c.RouteTemplate = "\)openapi/{documentName}/openapi.json\(";\)#\1'$ROUTE_PREFIX'/api/openapi/JSON\2#' \
|
2021-11-26 16:36:07 +01:00
|
|
|
-e 's#\(app.UseHttpsRedirection();\)#// \1#'
|
2021-11-25 14:49:08 +01:00
|
|
|
|
2021-12-06 13:12:14 +01:00
|
|
|
echo "fix root redirect in index.html"
|
|
|
|
sed -i output/src/$PACKAGE_NAME/wwwroot/index.html \
|
2021-12-06 16:34:38 +01:00
|
|
|
-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
|
2021-12-06 13:12:14 +01:00
|
|
|
|
2021-11-25 15:33:55 +01:00
|
|
|
echo "disable documentation warnings in generated code"
|
|
|
|
sed -i output/src/$PACKAGE_NAME/$PACKAGE_NAME.csproj \
|
|
|
|
-e 's#\(</PropertyGroup>\)#<NoWarn>1591</NoWarn>\n\1#'
|
|
|
|
|
2021-11-25 14:49:08 +01:00
|
|
|
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
|
2021-11-26 16:36:07 +01:00
|
|
|
cp OpenAPIDocCtrl.cs output/src/$PACKAGE_NAME/Controllers
|
2021-11-25 14:49:08 +01:00
|
|
|
else
|
|
|
|
echo "implementation not available, forgot to run stage 1?"
|
|
|
|
fi
|
|
|
|
fi
|
2021-11-22 19:07:46 +01:00
|
|
|
|
2021-11-23 14:00:39 +01:00
|
|
|
if [ "$BUILD" = "1" ]; then
|
|
|
|
echo "build service"
|
2021-11-26 13:23:29 +01:00
|
|
|
pushd output
|
2021-11-23 14:48:31 +01:00
|
|
|
pushd src/$PACKAGE_NAME
|
|
|
|
dotnet add package MySqlConnector --version 2.0.0
|
|
|
|
popd
|
2021-11-23 14:00:39 +01:00
|
|
|
sh build.sh
|
2021-11-26 13:23:29 +01:00
|
|
|
popd
|
2021-11-23 14:00:39 +01:00
|
|
|
fi
|
|
|
|
|
|
|
|
if [ "$EXECUTE" = "1" ]; then
|
|
|
|
echo "execute service"
|
2021-11-26 13:23:29 +01:00
|
|
|
dotnet run -p output/src/$PACKAGE_NAME/$PACKAGE_NAME.csproj
|
2021-11-23 14:00:39 +01:00
|
|
|
fi
|
2021-11-22 19:07:46 +01:00
|
|
|
|
|
|
|
|
2021-11-26 13:23:29 +01:00
|
|
|
|