Last active
October 13, 2023 12:31
-
-
Save LucasRoesler/c7b245bcd9f33bc989e279ba9cdb9828 to your computer and use it in GitHub Desktop.
create-python-repo.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# source: https://gist.github.com/LucasRoesler/c7b245bcd9f33bc989e279ba9cdb9828 | |
# usage: | |
# create-repo [name] [owner] | |
# create-repo [name] # owner defaults to contiamo | |
# create-repo # name defaults to the current directory name | |
# name is the first arg, fallback to the the current directory name | |
NAME=${1:-$(basename $(pwd))} | |
OWNER=${2:-contiamo} | |
# convert name to lower case | |
NAME=$(echo $NAME | tr '[:upper:]' '[:lower:]') | |
# check if the gh cli is installed, complain if it is not found | |
if ! command -v gh &> /dev/null | |
then | |
echo "gh could not be found, please install from https://cli.github.com/" | |
exit | |
fi | |
echo "Creating repo $NAME" | |
# require user response | |
read -p "Press enter to continue" | |
# if current directory is a git repo, use it as the source | |
# otherwise, create a new repo | |
if [ -d .git ]; then | |
gh repo create --private -t all --disable-wiki --disable-issues --source=. --push | |
else | |
gh repo create $OWNER/$NAME --private -t all --disable-wiki --disable-issues --clone --add-readme --gitignore=Python | |
cd $NAME | |
fi | |
gh repo edit \ | |
--enable-projects=false \ | |
--delete-branch-on-merge \ | |
--enable-auto-merge \ | |
--enable-merge-commit=false \ | |
--allow-update-branch | |
## see https://github.com/cli/cli/issues/3528 | |
# get the repo ID of the "cli/cli" repository | |
repositoryId="$(gh api graphql -f query="{repository(owner:\"$OWNER\",name:\"$NAME\"){id}}" -q .data.repository.id)" | |
echo "Repository ID: $repositoryId" | |
# create a branch protection rule (untested!) | |
gh api graphql --silent -f query=' | |
mutation($repositoryId:ID!,$branch:String!,$requiredReviews:Int!) { | |
createBranchProtectionRule(input: { | |
repositoryId: $repositoryId | |
pattern: $branch | |
requiresApprovingReviews: true | |
requiredApprovingReviewCount: $requiredReviews | |
dismissesStaleReviews: true | |
requiresConversationResolution: true | |
requiresStatusChecks: true | |
}) { clientMutationId } | |
}' -f repositoryId="$repositoryId" -f branch=main -F requiredReviews=1 | |
# if the folder contains a pyproject.toml, exit 0 | |
if [ -f pyproject.toml ]; then | |
exit 0 | |
fi | |
# check if pyenv is installed, complain if it is not found | |
if ! command -v pyenv &> /dev/null | |
then | |
echo "pyenv could not be found, please install from https://github.com/pyenv/pyenv#installationpo" | |
exit | |
fi | |
# check if poetry is installed, complain if not found | |
if ! command -v poetry &> /dev/null | |
then | |
echo "poetry could not be found, please install from https://python-poetry.org/docs/" | |
exit | |
fi | |
PREFERRED_PYTHON_VERSION=${PREFERRED_PYTHON_VERSION:-"3.12"} | |
pyenv install -s $PREFERRED_PYTHON_VERSION | |
pyenv local $PREFERRED_PYTHON_VERSION | |
# get the current git user name and email in the format "name <email>" | |
git_user=$(git config user.name)" <"$(git config user.email)">" | |
poetry init --name="$NAME" --no-interaction \ | |
--author="$git_user" \ | |
--python="$PREFERRED_PYTHON_VERSION" \ | |
--dev-dependency=black@latest \ | |
--dev-dependency=pyright@latest \ | |
--dev-dependency=pytest@latest \ | |
--dev-dependency=ruff@latest \ | |
--dev-dependency=unimport@latest | |
# convert name to snake_case, converting hypen to underscore | |
snake_case_name=$(echo $NAME | sed -e 's/-/_/g') | |
mkdir -p $snake_case_name | |
mkdir -p tests | |
touch $snake_case_name/__init__.py | |
touch tests/__init__.py | |
# convert python PREFERRED_PYTHON_VERSION to a string like "py312" | |
# if it contains two dots, remove everything after the second dot | |
python_version=$(echo $PREFERRED_PYTHON_VERSION | sed -e 's/\./\n/g' | head -n 2 | tr '\n' '.' | sed -e 's/\.$//g') | |
cat <<EOF >> pyproject.toml | |
[tool.black] | |
# see https://black.readthedocs.io/en/stable/usage_and_configuration/index.html | |
line-length = 88 | |
target-version = ['py$python_version'] | |
[tool.ruff] | |
# See https://beta.ruff.rs/docs/configuration/ | |
select = ["E", "F", "I"] | |
include = ["*.py"] | |
ignore = ["E501"] | |
[tool.unimport] | |
sources = ["$snake_case_name", "tests"] | |
gitignore = true | |
remove = false | |
check = true | |
diff = false | |
include_star_import = false | |
ignore_init = true | |
[tool.pyright] | |
# see https://microsoft.github.io/pyright/#/configuration?id=pyright-configuration | |
include = ["$snake_case_name", "tests"] | |
exclude = [ | |
".venv/**", | |
"**/node_modules", | |
"**/__pycache__" | |
] | |
reportMissingImports = true | |
typeCheckingMode = "basic" | |
EOF | |
git add . | |
git commit -sm "feat: initial project bootstrap" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment