|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Regenerates providers/index.json from the current providers/*.json files. |
| 3 | +# Run this whenever you add, remove, or bump the version of a recipe. |
| 4 | +# The updated_at timestamp is only changed when the recipe list or versions change. |
| 5 | +set -euo pipefail |
| 6 | + |
| 7 | +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 8 | +PROVIDERS_DIR="$SCRIPT_DIR/../providers" |
| 9 | +OUTPUT="$PROVIDERS_DIR/index.json" |
| 10 | + |
| 11 | +python3 - "$PROVIDERS_DIR" "$OUTPUT" <<'PYEOF' |
| 12 | +import json, os, sys, glob |
| 13 | +from datetime import timezone, datetime |
| 14 | +
|
| 15 | +providers_dir = sys.argv[1] |
| 16 | +output_path = sys.argv[2] |
| 17 | +
|
| 18 | +files = sorted(glob.glob(os.path.join(providers_dir, '*.json'))) |
| 19 | +
|
| 20 | +recipes = [] |
| 21 | +for f in files: |
| 22 | + basename = os.path.basename(f) |
| 23 | + if basename == 'index.json': |
| 24 | + continue |
| 25 | + with open(f) as fh: |
| 26 | + data = json.load(fh) |
| 27 | + recipes.append({ |
| 28 | + 'id': data['id'], |
| 29 | + 'file': basename, |
| 30 | + 'version': data.get('version', '1.0.0'), |
| 31 | + }) |
| 32 | +
|
| 33 | +# Read existing index to preserve updated_at when nothing changed |
| 34 | +existing_recipes = [] |
| 35 | +existing_updated_at = None |
| 36 | +if os.path.exists(output_path): |
| 37 | + with open(output_path) as fh: |
| 38 | + existing = json.load(fh) |
| 39 | + existing_recipes = existing.get('recipes', []) |
| 40 | + existing_updated_at = existing.get('updated_at') |
| 41 | +
|
| 42 | +if recipes == existing_recipes and existing_updated_at: |
| 43 | + updated_at = existing_updated_at |
| 44 | +else: |
| 45 | + updated_at = datetime.now(timezone.utc).strftime('%Y-%m-%dT%H:%M:%SZ') |
| 46 | +
|
| 47 | +index = { |
| 48 | + 'schema_version': '1', |
| 49 | + 'updated_at': updated_at, |
| 50 | + 'recipes': recipes, |
| 51 | +} |
| 52 | +
|
| 53 | +with open(output_path, 'w') as fh: |
| 54 | + json.dump(index, fh, indent=2) |
| 55 | + fh.write('\n') |
| 56 | +
|
| 57 | +print(f"Generated {output_path} with {len(recipes)} recipes.") |
| 58 | +PYEOF |
0 commit comments