Skip to content

Instantly share code, notes, and snippets.

@arashmad
Last active December 25, 2024 09:19
Show Gist options
  • Save arashmad/fb1f58c88710594df3ecdc5b3387f03b to your computer and use it in GitHub Desktop.
Save arashmad/fb1f58c88710594df3ecdc5b3387f03b to your computer and use it in GitHub Desktop.
A shell script to generate custom repository from fastapi-poetry-boilerplate repository
# #!/bin/bash
convert_to_snake_sep() {
echo "$1" | tr -s ' ' ' ' | tr '[:upper:]' '[:lower:]' | sed 's/-/_/g'
}
# Getting source directory
echo "Specify the path to the boilerplate directory (e.g. /home/projects/fastapi-poetry-boilerplate)":
read src_dir
if [ ! -d "$src_dir" ]; then
echo "❌ Error: Source directory '$src_dir' does not exist." >&2
exit 1
fi
# Getting destination directory
echo "Specify the path to the new generated boilerplate directory (e.g. /home/projects)":
read dest_dir
if [ ! -d "$dest_dir" ]; then
echo "❌ Error: Destination directory '$dest_dir' does not exist." >&2
exit 1
fi
if [ "$src_dir" = "$dest_dir" ]; then
echo "❌ Error: Source and destination directories are the same." >&2
exit 1
fi
# Getting project slug
echo "specify the project slug (e.g. my-api-service)":
# Check slug name pattern
read project_slug
if [[ ! "$project_slug" =~ ^[a-z-]+$ ]]; then
echo "❌ Error: Invalid <project_slug>. Only lowercase English letters and dashes are allowed."
exit 1
fi
project_slug_dash_case=$project_slug
project_slug_snake_case=$(convert_to_snake_sep "$project_slug")
# Copying the boilerplate directory to the new directory
dest_dir=$dest_dir/$project_slug_dash_case
if [ -d "$dest_dir" ]; then
echo "❌ Error: Instance of the boilerplate directory '$dest_dir' already exists." >&2
exit 1
fi
cp -r $src_dir $dest_dir
# Change the directory to the new directory
cd $dest_dir
# Cleaning the projecy directory
rm -rf .git
rm -rf .venv
rm .coverage
rm poetry.lock
rm coverage.xml
find "$(pwd)" -type d -name ".pytest_cache" -exec rm -rf {} +
find "$(pwd)" -type d -name "__pycache__" -exec rm -rf {} +
# Renaming defult name with the project slug and aligning the files/folder names
find "$(pwd)" -type f -exec sed -i "s/fastapi-poetry-boilerplate/${project_slug_dash_case}/g" {} +
find "$(pwd)" -type f -exec sed -i "s/fastapi_poetry_boilerplate/${project_slug_snake_case}/g" {} +
mv fastapi_poetry_boilerplate $project_slug_snake_case
# Create a new virtual environment
echo $(pwd)
poetry install
poetry shell
echo "✅ python virtual environment < $project_slug_snake_case > has been created."
# Test the instance
make lint
make test
# Finish
echo "✅ The instance was created under the directory '$dest_dir' and tested."
echo ""
echo "================================================================"
echo "👉 Project slug: $project_slug_dash_case".
echo "👉 Using <poetry shell> you can activte it."
echo "👉 Using <git init> command you can initialize the git repository."
echo """👉
git init -b main
git remote add origin <your-reposity-url>
git add .
git commit -m "First commit"
git push -u origin main
"""
echo "Have Fun with it 🚀"
echo "================================================================\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment