Spaces:
Runtime error
Runtime error
File size: 6,291 Bytes
babf125 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 | #!/usr/bin/env python3
"""
Automated cache-busting version bump utility for TreeTrack.
Updates:
- static/sw.js: const VERSION = <timestamp>
- static/index.html and static/map.html:
* inline currentVersion = '<version>'
* inline timestamp = '<timestamp>'
* script tag query params (?v=<version>&t=<timestamp>) for tree-track-app.js and map.js
Usage examples:
python scripts/bump_cache.py --now # bump timestamp only
python scripts/bump_cache.py --bump patch # bump version 1.2.3 -> 1.2.4 and timestamp
python scripts/bump_cache.py --set-version 5.1.2 # set specific version and bump timestamp
This script maintains state in version.json at repository root.
"""
import argparse
import json
import os
import re
import sys
import time
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
VERSION_FILE = ROOT / 'version.json'
FILES = {
'sw': ROOT / 'static' / 'sw.js',
'index': ROOT / 'static' / 'index.html',
'map': ROOT / 'static' / 'map.html',
}
SEMVER_RE = re.compile(r"^(\d+)\.(\d+)\.(\d+)$")
def read_text(p: Path) -> str:
try:
return p.read_text(encoding='utf-8')
except FileNotFoundError:
print(f"Error: file not found: {p}", file=sys.stderr)
sys.exit(1)
def write_text(p: Path, content: str) -> None:
p.write_text(content, encoding='utf-8', newline='\n')
def bump_semver(version: str, which: str) -> str:
m = SEMVER_RE.match(version)
if not m:
raise ValueError(f"Invalid semver: {version}")
major, minor, patch = map(int, m.groups())
if which == 'major':
major += 1
minor = 0
patch = 0
elif which == 'minor':
minor += 1
patch = 0
elif which == 'patch':
patch += 1
else:
raise ValueError(f"Unknown bump type: {which}")
return f"{major}.{minor}.{patch}"
def load_version() -> dict:
if VERSION_FILE.exists():
with VERSION_FILE.open('r', encoding='utf-8') as f:
data = json.load(f)
# minimal validation
if 'version' not in data or 'timestamp' not in data:
raise ValueError('version.json missing required fields')
return data
# default if not present
return {
'version': '0.1.0',
'timestamp': int(time.time()),
}
def save_version(version: str, timestamp: int) -> None:
with VERSION_FILE.open('w', encoding='utf-8') as f:
json.dump({'version': version, 'timestamp': timestamp}, f, indent=2)
f.write('\n')
def update_sw_js(content: str, timestamp: int) -> str:
# Replace: const VERSION = 1234567890;
new_content, n = re.subn(r"(const\s+VERSION\s*=\s*)(\d+)(\s*;)",
rf"\g<1>{timestamp}\g<3>", content)
if n == 0:
print('Warning: VERSION not updated in sw.js (pattern not found)')
return new_content
def update_html(content: str, version: str, timestamp: int, is_index: bool) -> str:
updated = content
# Update inline currentVersion = '<version>'
updated, n_ver = re.subn(r"(currentVersion\s*=\s*')[^']*(')", rf"\g<1>{version}\g<2>", updated)
if n_ver == 0:
print('Warning: currentVersion not updated (pattern not found)')
# Update inline timestamp = '<timestamp>'
updated, n_ts = re.subn(r"(timestamp\s*=\s*')[^']*(')", rf"\g<1>{timestamp}\g<2>", updated)
if n_ts == 0:
print('Warning: timestamp not updated (pattern not found)')
# Update script tag query params
# For index.html: tree-track-app.js?v=X&t=Y
# For map.html: /static/map.js?v=X&t=Y (we use same version for simplicity)
if is_index:
updated, n_tag = re.subn(r"(tree-track-app\.js\?v=)[^&']+(\&t=)[^'\"]+",
rf"\g<1>{version}\g<2>{timestamp}", updated)
if n_tag == 0:
print('Warning: tree-track-app.js cache params not updated (pattern not found)')
else:
updated, n_tag = re.subn(r"(/static/map\.js\?v=)[^&']+(\&t=)[^'\"]+",
rf"\g<1>{version}\g<2>{timestamp}", updated)
if n_tag == 0:
print('Warning: map.js cache params not updated (pattern not found)')
return updated
def main():
parser = argparse.ArgumentParser(description='Bump cache-busting version/timestamp across files')
group = parser.add_mutually_exclusive_group()
group.add_argument('--bump', choices=['major', 'minor', 'patch'], help='Semver bump type')
group.add_argument('--set-version', help='Set explicit version (x.y.z)')
parser.add_argument('--timestamp', type=int, help='Set explicit timestamp (epoch seconds)')
parser.add_argument('--now', action='store_true', help='Use current time as timestamp')
args = parser.parse_args()
state = load_version()
version = state['version']
timestamp = state['timestamp']
# Determine new version
if args.set_version:
if not SEMVER_RE.match(args.set_version):
print('Error: --set-version must be in x.y.z format', file=sys.stderr)
sys.exit(2)
version = args.set_version
elif args.bump:
version = bump_semver(version, args.bump)
# Determine new timestamp
if args.timestamp is not None:
timestamp = int(args.timestamp)
elif args.now or args.set_version or args.bump:
timestamp = int(time.time())
# Save version state
save_version(version, timestamp)
# Update files
sw_path = FILES['sw']
index_path = FILES['index']
map_path = FILES['map']
sw_content = read_text(sw_path)
index_content = read_text(index_path)
map_content = read_text(map_path)
sw_new = update_sw_js(sw_content, timestamp)
index_new = update_html(index_content, version, timestamp, is_index=True)
map_new = update_html(map_content, version, timestamp, is_index=False)
if sw_new != sw_content:
write_text(sw_path, sw_new)
print(f"Updated {sw_path}")
if index_new != index_content:
write_text(index_path, index_new)
print(f"Updated {index_path}")
if map_new != map_content:
write_text(map_path, map_new)
print(f"Updated {map_path}")
print(f"Done. version={version} timestamp={timestamp}")
if __name__ == '__main__':
main()
|