Great clarification!
You want each aspect (error handling, observability, caching) to be a separate patch against the base "happy path" source, not cumulative (i.e., not building on top of each other).
This means:
- The base is always the same.
- Each aspect patch is a diff from the base, not from the previous aspect.
This is a perfect use case for Pijul's patch model!
Below is a script that does this by using branches (called "channels" in Pijul) for each aspect, then records the patch, and finally returns to the base channel.
#!/bin/bash
set -euo pipefail
# Check if Pijul is installed
if ! command -v pijul &> /dev/null; then
echo "Error: Pijul is not installed. Please install Pijul first (https://pijul.org/)."
exit 1
fi
REPO_DIR="aspect-oriented-pijul-demo"
rm -rf "$REPO_DIR"
mkdir "$REPO_DIR"
cd "$REPO_DIR"
pijul init
# -----------------------------------------------------------------------------
# Step 1: Base "happy path" implementation
# -----------------------------------------------------------------------------
cat > app.py << 'EOF'
def add(a, b):
return a + b
if __name__ == "__main__":
result = add(2, 3)
print(f"Result: {result}")
EOF
pijul add app.py
pijul record -m "base: happy path implementation (add function)"
# -----------------------------------------------------------------------------
# Step 2: Aspect 1 - Error handling (on its own channel)
# -----------------------------------------------------------------------------
pijul channel new error-handling
pijul channel switch error-handling
cat > app.py << 'EOF'
def add(a, b):
# Validate inputs
if not isinstance(a, (int, float)) or not isinstance(b, (int, float)):
raise TypeError("Inputs must be numbers (int or float)")
return a + b
if __name__ == "__main__":
result = add(2, 3)
print(f"Result: {result}")
EOF
pijul record -m "aspect: error handling (input validation for add function)"
# -----------------------------------------------------------------------------
# Step 3: Aspect 2 - Observability (on its own channel)
# -----------------------------------------------------------------------------
pijul channel switch main
pijul channel new observability
pijul channel switch observability
cat > app.py << 'EOF'
def add(a, b):
return a + b
if __name__ == "__main__":
import logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
logger.info("Starting calculation...")
result = add(2, 3)
logger.info(f"Calculation completed. Result: {result}")
print(f"Result: {result}")
EOF
pijul record -m "aspect: observability (logging for execution flow)"
# -----------------------------------------------------------------------------
# Step 4: Aspect 3 - Caching (on its own channel)
# -----------------------------------------------------------------------------
pijul channel switch main
pijul channel new caching
pijul channel switch caching
cat > app.py << 'EOF'
from functools import lru_cache
@lru_cache(maxsize=128)
def add(a, b):
return a + b
if __name__ == "__main__":
result = add(2, 3)
print(f"Result: {result}")
EOF
pijul record -m "aspect: caching (LRU cache for add function results)"
# -----------------------------------------------------------------------------
# Final output
# -----------------------------------------------------------------------------
echo -e "\nRepository setup complete! Channels and their patches:"
pijul channel list
echo -e "\nTo explore the repo:"
echo "cd $REPO_DIR"
echo "pijul channel list # See all channels (base and aspects)"
echo "pijul channel switch <name> # Switch to a channel (e.g., error-handling)"
echo "pijul log # View patches in the current channel"
How it works:
- The main channel contains only the base code.
- Each aspect is created on its own channel, branched from the base, and contains only the base patch + the aspect patch.
- You can switch between channels to see the effect of each aspect in isolation.
This script demonstrates how Pijul can manage cross-cutting concerns as independent, non-additive patches against a common base!