genericdatabaseapiservice/generateAll.sh

85 lines
2.2 KiB
Bash
Raw Normal View History

#!/bin/bash
2021-11-23 14:00:39 +01:00
REMOVE="0"
BUILD="0"
EXECUTE="0"
while getopts rxbh flag
do
case "${flag}" in
h)
echo "r ... remove output directory";
echo "b ... build after generating";
echo "x ... execute after building";
echo "h ... show help";
;;
b)
BUILD="1"
;;
r)
REMOVE="1"
;;
x)
EXECUTE="1"
;;
esac
done
if [ "$REMOVE" = "1" ]; then
echo "remove output directory"
2021-11-23 14:00:39 +01:00
rm -rf output
fi
# safety measure
if [ -d output ]; then
echo "output directory already exist"
echo "remove manually and try again"
2021-11-23 14:00:39 +01:00
exit 1
fi
# PACKAGE_NAME will be loaded here
. ENV
2021-11-23 14:00:39 +01:00
echo "generate server code and endpoint stubs from openapi.yaml"
docker run -it --rm -v $PWD:/work -u $UID openapitools/openapi-generator:cli-v5.1.0 \
generate -i /work/openapi.yaml -g aspnetcore -o /work/output \
--package-name $PACKAGE_NAME \
2021-11-23 18:38:08 +01:00
--additional-properties="packageVersion=0.0.1,aspnetCoreVersion=5.0,operationIsAsync=true,\
generateBody=false,classModifier=abstract,operationModifier=abstract"
2021-11-23 14:00:39 +01:00
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.AddTransient<IDbService, DbService>();\n\n\1\2#'
2021-11-23 14:00:39 +01:00
echo "create directories for manually developed code"
mkdir output/src/$PACKAGE_NAME/Implementations
mkdir output/src/$PACKAGE_NAME/Services
2021-11-23 14:00:39 +01:00
echo "copy database service into source code try"
cp DbService.cs output/src/$PACKAGE_NAME/Services
2021-11-23 14:00:39 +01:00
echo "generate endpoint code from openapi.yaml"
python3.10 generate.py
echo "copy endpoint code into source code tree"
cp regular.cs output/src/$PACKAGE_NAME/Implementations
2021-11-23 14:00:39 +01:00
if [ "$BUILD" = "1" ]; then
echo "build service"
cd output
pushd src/$PACKAGE_NAME
dotnet add package MySqlConnector --version 2.0.0
popd
2021-11-23 14:00:39 +01:00
sh build.sh
fi
if [ "$EXECUTE" = "1" ]; then
echo "execute service"
dotnet run -p src/$PACKAGE_NAME/$PACKAGE_NAME.csproj
fi