Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 8 additions & 13 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,17 @@ new: # Create new Lambda function template (usage: make new template=<name>)
uv run new -n $(template)

.PHONY: project
project: # Rename project (run once)
@if [ -d templates ]; then mv templates ${SOURCE}; fi
@sed -i '' 's/^::: templates\.app/::: ${SOURCE}\.app/' docs/reference/app.md
@sed -i '' 's/^repo_name: .*/repo_name: ${GITHUB}\/${NAME}/' mkdocs.yml
@sed -i '' 's/^repo_url: .*/repo_url: https:\/\/github.com\/${GITHUB}\/${NAME}/' mkdocs.yml
@sed -i '' 's/^source = \[.*\]/source = \["${SOURCE}"\]/' pyproject.toml
@sed -i '' 's/^name = ".*"/name = "${SOURCE}"/' pyproject.toml
@sed -i '' 's/^description = ".*"/description = "${DESCRIPTION}"/' pyproject.toml
@sed -i '' 's/^authors = \[.*\]/authors = \[{name = "${AUTHOR}", email = "${EMAIL}"}\]/' pyproject.toml
@sed -i '' 's/^# .*/# ${DESCRIPTION}/' docs/README.md
@sed -i '' 's/@.*/@${GITHUB}/' .github/CODEOWNERS
@sed -i '' 's/^github: \[.*\]/github: \[${GITHUB}\]/' .github/FUNDING.yml
project: uv # Rename project (run once)
@uv run rename \
--name '$(subst ','\'',$(NAME))' \
--description '$(subst ','\'',$(DESCRIPTION))' \
--author '$(subst ','\'',$(AUTHOR))' \
--email '$(subst ','\'',$(EMAIL))' \
--github '$(subst ','\'',$(GITHUB))'

.PHONY: uv
uv: # Install uv if not already installed
pipx install uv
@command -v uv >/dev/null 2>&1 || pipx install uv

venv: # Activate virtual environment
uv venv --clear
Expand Down
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ dependencies = [
]

[project.scripts]
new = "new:main"
new = "scripts.new:main"
rename = "scripts.rename:main"

[dependency-groups]
infra = [
Expand Down
Empty file added scripts/__init__.py
Comment thread
amrabed marked this conversation as resolved.
Empty file.
File renamed without changes.
82 changes: 82 additions & 0 deletions scripts/rename.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import os
import re
import shutil
from pathlib import Path

from click import ClickException, UsageError, command, echo, option


@command()
@option("--name", required=True, help="Project new name")
@option("--description", required=True, help="Project short description")
@option("--author", required=True, help="Author name")
@option("--email", required=True, help="Author email")
@option("--github", required=True, help="GitHub username")
def main(name: str, description: str, author: str, email: str, github: str):
# Validate name to prevent directory traversal or other injection
if not re.match(r"^[a-zA-Z0-9_-]+$", name):
raise UsageError(
f"Invalid project name '{name}'. Only alphanumeric characters, dashes, and underscores are allowed."
)

source = name.replace("-", "_").lower()

echo(f"Initializing project '{name}' (source: '{source}')...")

# 1. Rename templates directory
if os.path.isdir("templates"):
shutil.move("templates", source)
elif not os.path.isdir(source):
raise ClickException(f"Error: Neither 'templates' nor '{source}' directory found.")

# 2. File modifications
replacements = [
("mkdocs.yml", r"^repo_name: .*", f"repo_name: {github}/{name}"),
("mkdocs.yml", r"^repo_url: .*", f"repo_url: https://github.com/{github}/{name}"),
("pyproject.toml", r'^name = ".*"', f'name = "{name}"'),
("pyproject.toml", r'^description = ".*"', f'description = "{description}"'),
("pyproject.toml", r"^authors = \[.*\]", f'authors = [{{name = "{author}", email = "{email}"}}]'),
("pyproject.toml", r'^source = \["templates"\]', f'source = ["{source}"]'),
("docs/README.md", r"^# .*", f"# {description}"),
(".github/CODEOWNERS", r"@.*", f"@{github}"),
(".github/FUNDING.yml", r"^github: .*", f"github: {github}"),
]

for filepath, pattern, replacement in replacements:
path = Path(filepath)
if not path.exists():
echo(f"Warning: File {filepath} not found, skipping.")
continue

content = path.read_text()
new_content = re.sub(pattern, replacement, content, flags=re.MULTILINE)
path.write_text(new_content)
echo(f"Updated {filepath}")

# 3. Global search and replace for 'templates'
echo(f"Replacing 'templates.' and 'templates/' with '{source}.' and '{source}/'...")
for root, dirs, files in os.walk("."):
# Skip .git, .venv, and other hidden directories
dirs[:] = [d for d in dirs if not d.startswith(".") and d != "venv"]

for file in files:
if file.endswith((".py", ".md", ".yml", ".pyt", "Makefile")):
path = Path(root) / file
if path.name == "rename.py":
continue

content = path.read_text()
# Replace 'templates.' (imports), 'templates/' (paths), and '"templates"' (config strings)
new_content = content.replace("templates.", f"{source}.")
new_content = new_content.replace("templates/", f"{source}/")
new_content = new_content.replace('"templates"', f'"{source}"')

if new_content != content:
path.write_text(new_content)
echo(f"Updated references in {path}")

echo("Project initialization complete.")


if __name__ == "__main__":
main()