Skip to content

Commit

Permalink
refactor template Justfile to use just modules (#168)
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuadavidthomas committed May 21, 2024
1 parent 1d4f187 commit 8ab75f1
Show file tree
Hide file tree
Showing 10 changed files with 505 additions and 301 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

- Adjusted the configuration of `django-storages` to use new style. At least, I think it's new. I cannot find in the library's CHANGELOG where it was added.
- `django-storages` installation options adjusted to specify the `[s3]` extra.
- Major refactor to the `Justfile`. Now utilizing the new modules introduced in version 1.19.0 of `just`.

### Removed

Expand Down
34 changes: 34 additions & 0 deletions src/django_twc_project/.just/copier.just
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
justfile := justfile_directory() + "/.just/copier.just"

[private]
default:
@just --list --justfile {{ justfile }}

[private]
fmt:
@just --fmt --justfile {{ justfile }}

# Create a copier answers file
[no-cd]
copy TEMPLATE_PATH DESTINATION_PATH=".":
$python -m copier copy --trust {{ TEMPLATE_PATH }} {{ DESTINATION_PATH }}

# Recopy the project from the original template
[no-cd]
recopy ANSWERS_FILE *ARGS:
$python -m copier recopy --trust --answers-file {{ ANSWERS_FILE }} {{ ARGS }}

# Loop through all answers files and recopy the project using copier
[no-cd]
@recopy-all *ARGS:
for file in `ls .copier/`; do just --unstable copier recopy .copier/$file "{{ ARGS }}"; done

# Update the project using a copier answers file
[no-cd]
update ANSWERS_FILE *ARGS:
$python -m copier update --trust --answers-file {{ ANSWERS_FILE }} {{ ARGS }}

# Loop through all answers files and update the project using copier
[no-cd]
@update-all *ARGS:
for file in `ls .copier/`; do just --unstable copier update .copier/$file "{{ ARGS }}"; done
39 changes: 39 additions & 0 deletions src/django_twc_project/.just/django.just
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
justfile := justfile_directory() + "/.just/django.just"

[private]
default:
@just --list --justfile {{ justfile }}

[private]
fmt:
@just --fmt --justfile {{ justfile }}

# Run a Django management command
manage *COMMAND:
@just docker command python -m manage {{ COMMAND }}

# Alias for makemigrations
alias mm := makemigrations

# Generate Django migrations
makemigrations *APPS:
@just dj manage makemigrations {{ APPS }}

# Run Django migrations
migrate *ARGS:
@just dj manage migrate {{ ARGS }}

# Open a Django shell using django-extensions shell_plus command
shell:
@just dj manage shell_plus

# Quickly create a superuser with the provided credentials
createsuperuser USERNAME="admin" PASSWORD="admin" EMAIL="admin@localhost":
@just docker run app "-e DJANGO_SUPERUSER_PASSWORD='{{ PASSWORD }}'" "/bin/bash -c 'python -m manage createsuperuser --noinput --username "{{ USERNAME }}" --email "{{ EMAIL }}"'"

# Graph models using django-extensions graph_models command
graph:
@just dj manage graph_models users \
--exclude-models AbstractUser \
--group-models \
--output ./docs/applications/images/users.svg
83 changes: 83 additions & 0 deletions src/django_twc_project/.just/docker.just
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
justfile := justfile_directory() + "/.just/docker.just"

[private]
default:
@just --list --justfile {{ justfile }}

[private]
fmt:
@just --fmt --justfile {{ justfile }}

# Build services using docker compose
[no-cd]
build *ARGS:
docker compose build {{ ARGS }}

# Build production stack using docker compose
[no-cd]
build-prod:
docker compose -f compose.yml -f compose.prod.yml build

# Stop and remove all containers, networks, images, and volumes
clean:
@just docker down --volumes --rmi local

# Run a command within the 'app' container
command *ARGS:
@just docker run app "" "/bin/bash -c '{{ ARGS }}'"

# Stop and remove all containers defined in docker compose
[no-cd]
down *ARGS:
docker compose down {{ ARGS }}

# Display the logs for containers, optionally using provided arguments (e.g., --follow)
[no-cd]
logs *ARGS:
docker compose logs {{ ARGS }}

# Display the running containers and their statuses
[no-cd]
ps:
docker compose ps

# Pull the latest versions of all images defined in docker compose
[no-cd]
pull:
docker compose pull

# Restart services, optionally targeting specific ones
[no-cd]
restart *ARGS:
docker compose restart {{ ARGS }}

# Run a command within the specified container with command-line flags
[no-cd]
run CONTAINER FLAGS *ARGS:
docker compose run --rm {{ FLAGS }} {{ CONTAINER }} {{ ARGS }}

# Start development stack using docker compose
start:
@just docker up --detach

# Start the full production stack using docker compose
start-prod:
@just docker up-prod --detach

# Stop services by calling the 'down' command
stop:
@just docker down

# Continuously follow the latest docker logs, optionally targeting specific containers
tail *ARGS:
@just docker logs --follow {{ ARGS }}

# Start development stack using docker compose, with optional arguments
[no-cd]
up *ARGS:
docker compose up {{ ARGS }}

# Start the full production stack using docker compose
[no-cd]
up-prod *ARGS:
docker compose -f compose.yml -f compose.prod.yml up {{ ARGS }}
43 changes: 43 additions & 0 deletions src/django_twc_project/.just/documentation.just
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
justfile := justfile_directory() + "/.just/documentation.just"

[private]
default:
@just --list --justfile {{ justfile }}

[private]
fmt:
@just --fmt --justfile {{ justfile }}

# Build documentation using Sphinx
[no-cd]
build LOCATION="docs/_build/html": cog
sphinx-build docs {{ LOCATION }}

# Install documentation dependencies
[no-cd]
install:
$python -m uv pip install --upgrade -r docs/requirements.txt

# Generate documentation requirements
[no-cd]
lock *ARGS:
$python -m uv pip compile {{ ARGS }} --generate-hashes docs/requirements.in --output-file docs/requirements.txt

# Serve documentation locally
[no-cd]
serve: cog
#!/usr/bin/env sh
if [ -f "/.dockerenv" ]; then
sphinx-autobuild docs docs/_build/html --host "0.0.0.0"
else
sphinx-autobuild docs docs/_build/html --host "localhost"
fi

# Generate and upgrade documentation dependencies
upgrade:
@just lock --upgrade

[private]
[no-cd]
cog:
cog -r docs/just.md
29 changes: 29 additions & 0 deletions src/django_twc_project/.just/node.just
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
justfile := justfile_directory() + "/.just/node.just"

[private]
default:
@just --list --justfile {{ justfile }}

[private]
fmt:
@just --fmt --justfile {{ justfile }}

# Install dependencies
[no-cd]
install *ARGS:
npm install

# Generate package-lock.json file
[no-cd]
lock:
npm install --package-lock-only

# Update dependencies
[no-cd]
update:
npm update

# Generate and upgrade dependencies
[no-cd]
upgrade:
npm upgrade
36 changes: 36 additions & 0 deletions src/django_twc_project/.just/postgres.just
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
justfile := justfile_directory() + "/.just/postgres.just"

[private]
default:
@just --list --justfile {{ justfile }}

[private]
fmt:
@just --fmt --justfile {{ justfile }}

DATABASE_URL := env_var("DATABASE_URL")

# Dump our local database to file
dump database_url=DATABASE_URL file='db.dump':
just docker run db "" \
pg_dump \
--dbname {{ database_url }} \
--file /app/{{ file }} \
--format=c \
--verbose

# Dump our production database to file
dump-prod file='production.dump':
set PROD_DATABASE_URL := "{{ env_var('PROD_DATABASE_URL') }}"
@just pg_dump ${PROD_DATABASE_URL} {{ file }}

# Restore database backup to our local database
restore database_url=DATABASE_URL file='db.dump':
just docker run db "" \
pg_restore \
--clean \
--dbname {{ database_url }} \
--if-exists \
--no-owner \
--verbose \
/app/{{ file }}
92 changes: 92 additions & 0 deletions src/django_twc_project/.just/project.just
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
justfile := justfile_directory() + "/.just/project.just"

[private]
default:
@just --list --justfile {{ justfile }}

[private]
fmt:
@just --fmt --justfile {{ justfile }}

# Remove all installed dependencies, caches, and generated files
[no-cd]
clean:
#!/usr/bin/env bash
set -euo pipefail
for dir in \
"$VIRTUAL_ENV" \
.mypy_cache \
.pytest_cache \
.ruff_cache \
htmlcov \
node_modules; do
[ -d "$dir" ] && rm -rf "$dir"
done
find . -name "__pycache__" -exec rm -rf {} +

# Create .env file from .env.example, if it doesn't exist
[no-cd]
envfile:
#!/usr/bin/env python
from pathlib import Path

envfile = Path("{{ justfile_directory() }}/.env")
envfile_example = Path("{{ justfile_directory() }}/.env.example")

if not envfile.exists():
envfile.write_text(envfile_example.read_text())

# Sync .env file back to .env.example
[no-cd]
envsync: envfile
#!/usr/bin/env python
from pathlib import Path

envfile = Path("{{ justfile_directory() }}/.env")
envfile_example = Path("{{ justfile_directory() }}/.env.example")

with envfile.open() as f:
lines = [line for line in f.readlines() if not line.endswith("# envsync: ignore\n")]
lines = [
line.split("=")[0] + "=\n" if line.endswith("# envsync: no-value\n") else line
for line in lines
]

lines.sort()
envfile_example.write_text("".join(lines))

[no-cd]
[private]
install-precommit: install-uv
$python -m uv pip install -c {{ justfile_directory() }}/requirements.txt pre-commit
$python -m pre_commit install --install-hooks

[no-cd]
[private]
install-uv:
$python -m pip install --upgrade uv

# Format justfile
just-fmt:
@just --fmt
@just copier fmt
@just dj fmt
@just docker fmt
@just docs fmt
@just node fmt
@just pg fmt
@just project fmt
@just py fmt

# Run pre-commit on all files
[no-cd]
lint: install-precommit
$python -m pre_commit run --all-files
@just project just-fmt

# Setup project scaffolding
setup:
@just project envfile
@just py venv
@just project install-uv
@just project install-precommit
Loading

0 comments on commit 8ab75f1

Please sign in to comment.