generate_resource_manifest.py (2.4 KB)


 1 #!/usr/bin/env python3
 2 """
 3 Generate resource manifest for PWA caching
 4 Scans the images/ and resources/ directories and creates a JSON manifest file
 5 """
 6 
 7 import os
 8 import json
 9 from pathlib import Path
10 
11 def generate_resource_manifest():
12     """Generate a JSON manifest of all resources in the images/ and resources/ directories"""
13     
14     # Define supported image extensions
15     image_extensions = {'.png', '.jpg', '.jpeg', '.gif', '.svg', '.webp', '.ico', '.bmp'}
16     
17     # Get the repo root (two levels up from resources/scripts/)
18     repo_root = Path(__file__).parent.parent.parent
19     resources_dir = repo_root / 'resources'
20     images_dir = resources_dir / 'images'
21     
22     # Collect all image files
23     image_files = []
24     if images_dir.exists():
25         for file_path in images_dir.iterdir():
26             if file_path.is_file() and file_path.suffix.lower() in image_extensions:
27                 # Create relative path from web root (GitHub Pages subdirectory)
28                 relative_path = f"/web_workshop/resources/images/{file_path.name}"
29                 image_files.append(relative_path)
30     
31     # Collect ALL resource files (no extension filtering)
32     resource_files = []
33     if resources_dir.exists():
34         for file_path in resources_dir.rglob('*'):
35             if file_path.is_file():
36                 # Create relative path from web root (GitHub Pages subdirectory)
37                 relative_path = f"/web_workshop/resources/{file_path.relative_to(resources_dir).as_posix()}"
38                 resource_files.append(relative_path)
39     
40     # Sort for consistent output
41     image_files.sort()
42     resource_files.sort()
43     
44     # Create manifest object
45     manifest = {
46         "images": image_files,
47         "resources": resource_files,
48         "generated_at": "auto-generated by GitHub Actions"
49     }
50     
51     # Write manifest file
52     manifest_path = resources_dir / 'resource-manifest.json'
53     with open(manifest_path, 'w', encoding='utf-8') as f:
54         json.dump(manifest, f, indent=2, sort_keys=True)
55     
56     print(f"Generated manifest with {len(image_files)} images and {len(resource_files)} resources:")
57     for img in image_files:
58         print(f"  - {img}")
59     for res in resource_files:
60         print(f"  - {res}")
61     
62     return manifest
63 
64 if __name__ == "__main__":
65     manifest = generate_resource_manifest()
66     print(f"\nManifest saved to: resources/resource-manifest.json")