|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Rename the placeholder scaffold to your plugin's package name. |
| 3 | +# Run once on a fresh clone of the template, then delete this script. |
| 4 | +set -euo pipefail |
| 5 | + |
| 6 | +usage() { |
| 7 | + cat <<'EOF' |
| 8 | +Usage: bin/customize.sh <package-name> [options] |
| 9 | +
|
| 10 | +Renames the placeholder/ package to <package-name> and updates all |
| 11 | +references in pyproject.toml, .releaserc.yaml, and Python imports. |
| 12 | +
|
| 13 | +Arguments: |
| 14 | + <package-name> Lowercase Python identifier (e.g. acme, my_org) |
| 15 | +
|
| 16 | +Options: |
| 17 | + --author "Your Name" Set authors[].name in pyproject.toml |
| 18 | + --email you@example.com Set authors[].email in pyproject.toml |
| 19 | + --description "..." Set project.description in pyproject.toml |
| 20 | + (must not contain the '|' character) |
| 21 | + --repository-url URL Set repositoryUrl in .releaserc.yaml |
| 22 | + -h, --help Show this message |
| 23 | +
|
| 24 | +After running, verify with: |
| 25 | + uv venv && source .venv/bin/activate && uv sync --all-extras && tox |
| 26 | +EOF |
| 27 | +} |
| 28 | + |
| 29 | +if [[ $# -eq 0 ]]; then |
| 30 | + usage |
| 31 | + exit 2 |
| 32 | +fi |
| 33 | + |
| 34 | +PACKAGE="" |
| 35 | +AUTHOR="" |
| 36 | +EMAIL="" |
| 37 | +DESCRIPTION="" |
| 38 | +REPOSITORY_URL="" |
| 39 | + |
| 40 | +while [[ $# -gt 0 ]]; do |
| 41 | + case "$1" in |
| 42 | + -h|--help) usage; exit 0 ;; |
| 43 | + --author) AUTHOR="${2:-}"; shift 2 ;; |
| 44 | + --email) EMAIL="${2:-}"; shift 2 ;; |
| 45 | + --description) DESCRIPTION="${2:-}"; shift 2 ;; |
| 46 | + --repository-url) REPOSITORY_URL="${2:-}"; shift 2 ;; |
| 47 | + --*) echo "Error: unknown option: $1" >&2; usage >&2; exit 2 ;; |
| 48 | + *) |
| 49 | + if [[ -z "$PACKAGE" ]]; then |
| 50 | + PACKAGE="$1" |
| 51 | + shift |
| 52 | + else |
| 53 | + echo "Error: unexpected positional argument: $1" >&2 |
| 54 | + usage >&2 |
| 55 | + exit 2 |
| 56 | + fi |
| 57 | + ;; |
| 58 | + esac |
| 59 | +done |
| 60 | + |
| 61 | +if [[ -z "$PACKAGE" ]]; then |
| 62 | + echo "Error: <package-name> is required." >&2 |
| 63 | + usage >&2 |
| 64 | + exit 2 |
| 65 | +fi |
| 66 | + |
| 67 | +if [[ "$PACKAGE" == "placeholder" ]]; then |
| 68 | + echo "Error: package name cannot be 'placeholder'." >&2 |
| 69 | + exit 2 |
| 70 | +fi |
| 71 | + |
| 72 | +if ! [[ "$PACKAGE" =~ ^[a-z][a-z0-9_]*$ ]]; then |
| 73 | + echo "Error: package name must be lowercase letters/digits/underscores, starting with a letter." >&2 |
| 74 | + echo "Got: $PACKAGE" >&2 |
| 75 | + exit 2 |
| 76 | +fi |
| 77 | + |
| 78 | +if [[ "$DESCRIPTION" == *"|"* ]]; then |
| 79 | + echo "Error: --description must not contain the '|' character." >&2 |
| 80 | + exit 2 |
| 81 | +fi |
| 82 | + |
| 83 | +REPO_ROOT="$(cd "$(dirname "$0")/.." && pwd)" |
| 84 | +cd "$REPO_ROOT" |
| 85 | + |
| 86 | +if [[ ! -d placeholder ]]; then |
| 87 | + echo "Error: placeholder/ directory not found at $REPO_ROOT." >&2 |
| 88 | + echo "This template appears to be already customized (or you ran the script from the wrong directory)." >&2 |
| 89 | + exit 1 |
| 90 | +fi |
| 91 | + |
| 92 | +# Portable in-place sed: works on both GNU and BSD sed. |
| 93 | +sed_inplace() { |
| 94 | + local pattern="$1" |
| 95 | + local file="$2" |
| 96 | + sed -i.bak "$pattern" "$file" |
| 97 | + rm -f "${file}.bak" |
| 98 | +} |
| 99 | + |
| 100 | +echo "==> Renaming placeholder/ to ${PACKAGE}/" |
| 101 | +mv placeholder "$PACKAGE" |
| 102 | + |
| 103 | +echo "==> Updating pyproject.toml" |
| 104 | +sed_inplace "s/^name = \"placeholder-my-plugin\"\$/name = \"${PACKAGE}-my-plugin\"/" pyproject.toml |
| 105 | +sed_inplace "s/^include = \[\"placeholder\\*\"\]\$/include = [\"${PACKAGE}*\"]/" pyproject.toml |
| 106 | +sed_inplace "s/^testpaths = \[\"placeholder\", \"tests\"\]\$/testpaths = [\"${PACKAGE}\", \"tests\"]/" pyproject.toml |
| 107 | + |
| 108 | +if [[ -n "$AUTHOR" || -n "$EMAIL" ]]; then |
| 109 | + AUTHOR_NAME="${AUTHOR:-Your Name placeholder}" |
| 110 | + AUTHOR_EMAIL="${EMAIL:-placeholder@placeholder.com}" |
| 111 | + sed_inplace "s|^authors = .*|authors = [{ name = \"${AUTHOR_NAME}\", email = \"${AUTHOR_EMAIL}\" }]|" pyproject.toml |
| 112 | +fi |
| 113 | + |
| 114 | +if [[ -n "$DESCRIPTION" ]]; then |
| 115 | + sed_inplace "s|^description = .*|description = \"${DESCRIPTION}\"|" pyproject.toml |
| 116 | +fi |
| 117 | + |
| 118 | +echo "==> Updating .releaserc.yaml" |
| 119 | +sed_inplace "s|chore(release mloda-plugin-template):|chore(release ${PACKAGE}-my-plugin):|" .releaserc.yaml |
| 120 | +if [[ -n "$REPOSITORY_URL" ]]; then |
| 121 | + sed_inplace "s|^repositoryUrl: .*|repositoryUrl: \"${REPOSITORY_URL}\"|" .releaserc.yaml |
| 122 | +fi |
| 123 | + |
| 124 | +echo "==> Updating Python imports under ${PACKAGE}/" |
| 125 | +find "$PACKAGE" -type f -name '*.py' -print0 | while IFS= read -r -d '' f; do |
| 126 | + sed_inplace "s/from placeholder\\./from ${PACKAGE}./g; s/import placeholder\\./import ${PACKAGE}./g" "$f" |
| 127 | +done |
| 128 | + |
| 129 | +echo "==> Checking for stale 'placeholder' references" |
| 130 | +STALE="$(grep -rn 'placeholder' "$PACKAGE" pyproject.toml .releaserc.yaml || true)" |
| 131 | +if [[ -n "$STALE" ]]; then |
| 132 | + echo "Error: stale 'placeholder' references found in customized files:" >&2 |
| 133 | + printf '%s\n' "$STALE" >&2 |
| 134 | + exit 1 |
| 135 | +fi |
| 136 | + |
| 137 | +cat <<EOF |
| 138 | +
|
| 139 | +Customization complete. |
| 140 | +
|
| 141 | +Next steps: |
| 142 | + 1. Verify the build: |
| 143 | + uv venv && source .venv/bin/activate && uv sync --all-extras && tox |
| 144 | + 2. Remove template-only files: |
| 145 | + rm CONTRIBUTING.md bin/customize.sh |
| 146 | + 3. Delete the '## First-time setup' section from CLAUDE.md and AGENTS.md. |
| 147 | +EOF |
0 commit comments