Skip to content

Commit

Permalink
feat: update copier.yml
Browse files Browse the repository at this point in the history
  • Loading branch information
34j committed Mar 4, 2024
1 parent 3aa2a23 commit 05b1efd
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 35 deletions.
92 changes: 57 additions & 35 deletions copier.yml
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
# questions
project_name:
type: str
help: "Project Name (human readable version)"
default: '{{ "exec(''import sys'') or sys.argv[-1]" | eval | capitalize | replace("-", " ") }}'

project_short_description:
type: str
help: "A short description of the project"
default: "Python package for "

full_name:
type: str
help: "What's your name?"
default: "{{ '' | env ('NAME')}}"

email:
type: str
help: "Email address"
placeholder: "[email protected]"
default: "{{ '' | env('EMAIL') }}"

github_username:
type: str
help: "GitHub Username"

project_name:
type: str
help: "Project Name (human readable version)"
placeholder: "Python Package"
default: "{{ full_name | env('GITHUB_USER') }}"

project_slug:
type: str
Expand All @@ -27,11 +34,6 @@ package_name:
help: "The name of the main Python package (should be a valid Python identifier)"
default: "{{ project_slug.replace('-', '_') }}"

project_short_description:
type: str
help: "A short description of the project"
placeholder: "A super helpful small Python package."

open_source_license:
type: str
help: "The open source license to use"
Expand All @@ -44,69 +46,89 @@ open_source_license:
copyright_year:
type: str
help: "Copyright year(s)"
default: "2022"
default: "{% now 'utc', '%Y' %}"

documentation:
type: bool
help: "Generate documentation?"
default: yes

has_cli:
setup_venv:
type: bool
help: "Does the project have a CLI?"
default: no
help: "Setup a virtual environment?"
default: yes

cli_name:
venv_version:
type: str
help: "The name of the CLI"
default: "{{ project_slug }}"
when: "{{ has_cli }}"
help: "Python version for the virtual environment"
default: "3.11"
when: "{{ setup_venv }}"

run_poetry_install:
type: bool
help: "Run poetry install after {{ package_name }} generation?"
default: no
default: yes
when: "{{ setup_venv }}"

initial_commit:
type: bool
help: "Create an initial commit with the generated {{ package_name }}?"
default: no
default: yes

setup_github:
type: bool
help: "Setup GitHub repository (requires gh CLI)?"
default: no
default: yes
when: "{{ initial_commit }}"

setup_pre_commit:
type: bool
help: "Setup pre-commit hooks (requires pre-commit)?"
default: no
default: yes
when: "{{ setup_venv }}"

add_me_as_contributor:
type: bool
help: "Add me as a contributor?"
default: no
when: "{{ initial_commit }}"

open_with_vscode:
type: bool
help: "Open with VSCode?"
default: yes

# Copier metadata
_min_copier_version: "9.0.0"
_min_copier_version: "8.0.0"
_subdirectory: "project"
_tasks:
# In Windows, command prompt
# Remove license file if no license
- "{% if open_source_license == 'Not open source' %}rm LICENSE{% endif %}"
# Cleanup docs
- "{% if not documentation %}rm -rf docs .readthedocs.yml{% endif %}"
# Setup venv
- "{% if setup_venv %}{% if 'Windows' in ''|env('OS') %}py -{% else %}python{% endif %}{{ venv_version }} -m venv venv{% endif %}"
# Run poetry install
- "{% if run_poetry_install %}poetry install{% endif %}"
# Initial commit
- "{% if initial_commit %}git init{% endif %}"
- "{% if initial_commit %}git add .{% endif %}"
- "{% if initial_commit %}git commit -m 'chore: initial commit'{% endif %}"
# Setup GitHub
- "{% if setup_github %}gh repo create {{ github_username }}/{{ project_slug }} -d '{{ project_short_description }}' --public --remote=origin --source=. --push{% endif %}"
- "{% if setup_github %}gh repo edit --delete-branch-on-merge --enable-projects=false --enable-wiki=false{% endif %}"
- "{% if setup_github %}gh secret set GH_PAT -b 'changeme'{% endif %}"
- "{% if setup_github %}gh secret set CODECOV_TOKEN -b 'changeme'{% endif %}"
- "{% if 'Windows' in ''|env('OS') %}{% set venv_folder = 'venv\\\\scripts\\\\' %}{% else %}{% set venv_folder = 'venv/bin/' %}{% endif %}
{% if run_poetry_install %}{{ venv_folder }}python -m pip install -U pip setuptools wheel && {{ venv_folder }}pip install -U poetry pre-commit && {{ venv_folder }}python -m poetry install --with dev{% endif %}"
# git init
- "git init && git add ."
# Setup pre-commit
- "{% if setup_pre_commit %}pre-commit install{% endif %}"
- "{% if 'Windows' in ''|env('OS') %}{% set venv_folder = 'venv\\\\scripts\\\\' %}{% else %}{% set venv_folder = 'venv/bin/' %}{% endif %}
{% if setup_pre_commit %}{{ venv_folder }}pre-commit autoupdate && {{ venv_folder }}pre-commit install && {{ venv_folder }}pre-commit run -a || git add .{% endif %}"
# Initial commit
- '{% if initial_commit %}git commit -m "chore: initial commit"{% endif %}'
# Add me as a contributor
- "{% if add_me_as_contributor %}npx all-contributors-cli add {{ github_username }} code,ideas,doc{% endif %}"
# Setup GitHub
- '{% if setup_github %}bash .github/setup-github.bash {{ github_username }} {{ project_slug }} "{{ project_short_description }}"{% endif %}'
# Open with vscode
- "{% if open_with_vscode %}code .{% endif %}"
# Replace true with false in .copier-answers.yml except "documentation: true" for faster copier update
- "sed -i '/documentation: true/!s/true/false/g' .copier-answers.yml"
_jinja_extensions:
- jinja2_time.TimeExtension
- jinja2_eval.EvalExtension
- jinja2_shell.ShellExtension
- jinja2_env.EnvExtension
53 changes: 53 additions & 0 deletions project/.github/setup-github.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/bin/bash
owner=$1
repo=$2
shortDescription=$3
ownerRepo=$owner/$repo

# create repo
gh repo create $ownerRepo -d $shortDescription --public --remote=origin --source=. --push

# squash merge
gh repo edit --delete-branch-on-merge --enable-projects=false --enable-wiki=false --enable-merge-commit=false --enable-squash-merge --enable-rebase-merge=false

# set secrets
gh secret set PYPI_TOKEN -b $PYPI_TOKEN
gh secret set GH_PAT -b $GITHUB_TOKEN

# set workflow permissions
gh api --method PUT -H "Accept: application/vnd.github+json" -H "X-GitHub-Api-Version: 2022-11-28" "repos/$ownerRepo/actions/permissions/workflow" -f default_workflow_permissions="read" -F can_approve_pull_request_reviews=true

# set branch protection
# https://docs.github.com/ja/rest/branches/branch-protection?apiVersion=2022-11-28#update-branch-protection
echo "Setting branch protection rules for $ownerRepo"
curl -L \
-X PUT \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer $GITHUB_TOKEN" \
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/repos/$ownerRepo/branches/main/protection \
-d '{"required_status_checks":null,"enforce_admins":false,"required_pull_request_reviews":null,"restrictions":null,"required_linear_history":false,"allow_force_pushes":true,"allow_deletions":true,"block_creations":false,"required_conversation_resolution":false,"lock_branch":false,"allow_fork_syncing":true}'

# install GitHub Apps
# Raise if PYPACKAGE_TEMPLATE_INSTALLATION_IDS is not set
: ${PYPACKAGE_TEMPLATE_INSTALLATION_IDS:?"PYPACKAGE_TEMPLATE_INSTALLATION_IDS must be set. Set it to a comma separated list of installation ids, which could be found from the url of the 'Configure' page of the GitHub App. e.g. https://github.com/organizations/<Organization-name>/settings/installations/<ID>. See https://stackoverflow.com/questions/74462420/where-can-we-find-github-apps-installation-id for further details."}
echo "Installing GitHub Apps $PYPACKAGE_TEMPLATE_INSTALLATION_IDS"

# get installation ids for Renovate, pre-commit.ci and repository id
# AllContributors and Codecov can be globally installed
installationIds=$(echo $PYPACKAGE_TEMPLATE_INSTALLATION_IDS | tr "," "\n")
repositoryId=$(gh api "repos/$ownerRepo" --jq '.id')

# https://docs.github.com/ja/rest/apps/installations?apiVersion=2022-11-28#add-a-repository-to-an-app-installation
for installationId in $installationIds; do
gh api \
--method PUT \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"user/installations/$installationId/repositories/$repositoryId"
done

# to test this script, run
# mkdir -p testRepository && cd testRepository
# git init && echo "test" > test.txt && git add . && git commit -m "initial commit" && bash ../project/.github/setup-github.bash 34j test-pypackage-template "testing pypackage template script"
# cd .. && rm -rf testRepository

0 comments on commit 05b1efd

Please sign in to comment.