make-git-episode00-demo.sh
bash
sha256:5472be4fece32b606c308fad9d57295ce327955fb2b87b8beec6ea1626050473
Add published YouTube URL to Episode 00
Sonnet 5
3 hours ago
| 1 | #!/usr/bin/env bash |
| 2 | set -euo pipefail |
| 3 | |
| 4 | DEMO_DIR="${1:-muse-git-demo}" |
| 5 | |
| 6 | setup() { |
| 7 | rm -rf "$DEMO_DIR" |
| 8 | mkdir -p "$DEMO_DIR" |
| 9 | cd "$DEMO_DIR" |
| 10 | |
| 11 | git init -q |
| 12 | |
| 13 | cat > calculator.py <<'PY' |
| 14 | def calculate_total(items): |
| 15 | return sum(item["price"] for item in items) |
| 16 | |
| 17 | if __name__ == "__main__": |
| 18 | cart = [{"price": 10}, {"price": 15}] |
| 19 | print(calculate_total(cart)) |
| 20 | PY |
| 21 | |
| 22 | mkdir -p app |
| 23 | for i in $(seq -w 1 20); do |
| 24 | cat > "app/module_${i}.py" <<PY |
| 25 | from calculator import calculate_total |
| 26 | |
| 27 | def report_${i}(items): |
| 28 | return calculate_total(items) |
| 29 | PY |
| 30 | done |
| 31 | |
| 32 | python3 - <<'PY' |
| 33 | from pathlib import Path |
| 34 | |
| 35 | notes = [60, 64, 67, 72] |
| 36 | track = bytearray() |
| 37 | track += bytes([0x00, 0xFF, 0x51, 0x03, 0x07, 0xA1, 0x20]) |
| 38 | |
| 39 | for note in notes: |
| 40 | track += bytes([0x00, 0x90, note, 100]) |
| 41 | track += bytes([0x60, 0x80, note, 64]) |
| 42 | |
| 43 | track += bytes([0x00, 0xFF, 0x2F, 0x00]) |
| 44 | |
| 45 | header = b"MThd" + (6).to_bytes(4, "big") + (0).to_bytes(2, "big") + (1).to_bytes(2, "big") + (96).to_bytes(2, "big") |
| 46 | chunk = b"MTrk" + len(track).to_bytes(4, "big") + track |
| 47 | Path("melody.mid").write_bytes(header + chunk) |
| 48 | PY |
| 49 | |
| 50 | git add . |
| 51 | git commit -qm "Initial demo state" |
| 52 | |
| 53 | echo |
| 54 | echo "Created: $(pwd)" |
| 55 | echo "Baseline commit:" |
| 56 | git --no-pager log --oneline -1 |
| 57 | } |
| 58 | |
| 59 | python_change() { |
| 60 | cd "$DEMO_DIR" |
| 61 | python3 - <<'PY' |
| 62 | from pathlib import Path |
| 63 | p = Path("calculator.py") |
| 64 | s = p.read_text() |
| 65 | s = s.replace( |
| 66 | 'return sum(item["price"] for item in items)', |
| 67 | 'return round(sum(item["price"] for item in items), 2)' |
| 68 | ) |
| 69 | p.write_text(s) |
| 70 | PY |
| 71 | } |
| 72 | |
| 73 | rename_change() { |
| 74 | cd "$DEMO_DIR" |
| 75 | python3 - <<'PY' |
| 76 | from pathlib import Path |
| 77 | for p in [Path("calculator.py"), *sorted(Path("app").glob("*.py"))]: |
| 78 | s = p.read_text().replace("calculate_total", "compute_total") |
| 79 | p.write_text(s) |
| 80 | PY |
| 81 | } |
| 82 | |
| 83 | midi_change() { |
| 84 | cd "$DEMO_DIR" |
| 85 | python3 - <<'PY' |
| 86 | from pathlib import Path |
| 87 | p = Path("melody.mid") |
| 88 | data = bytearray(p.read_bytes()) |
| 89 | mapping = {60: 62, 64: 65, 67: 69, 72: 74} |
| 90 | for i in range(1, len(data)): |
| 91 | if data[i-1] in (0x90, 0x80) and data[i] in mapping: |
| 92 | data[i] = mapping[data[i]] |
| 93 | p.write_bytes(data) |
| 94 | PY |
| 95 | } |
| 96 | |
| 97 | reset_demo() { |
| 98 | cd "$DEMO_DIR" |
| 99 | git reset --hard -q HEAD |
| 100 | git clean -fdq |
| 101 | } |
| 102 | |
| 103 | case "${2:-setup}" in |
| 104 | setup) setup ;; |
| 105 | python-change) python_change ;; |
| 106 | rename-change) rename_change ;; |
| 107 | midi-change) midi_change ;; |
| 108 | reset) reset_demo ;; |
| 109 | *) |
| 110 | echo "Usage: $0 [demo-dir] {setup|python-change|rename-change|midi-change|reset}" |
| 111 | exit 1 |
| 112 | ;; |
| 113 | esac |
File History
1 commit
sha256:5472be4fece32b606c308fad9d57295ce327955fb2b87b8beec6ea1626050473
Add published YouTube URL to Episode 00
Sonnet 5
3 hours ago