|
| 1 | +#!/usr/bin/env bash |
| 2 | +# |
| 3 | +# Regenerates and validates the metadata key registry (tika-metadata-schema). |
| 4 | +# |
| 5 | +# Run this after adding, renaming, or removing a Property or PassthroughPrefix |
| 6 | +# constant anywhere in tika-core or the standard parser bundle. It replaces the |
| 7 | +# multi-step manual sequence in .skills/metadata-schema.md with one command: |
| 8 | +# install the dependency modules, regenerate the three registry files, sanity |
| 9 | +# check the diff, then run the gate tests. |
| 10 | +# |
| 11 | +# Usage: |
| 12 | +# tika-metadata-schema/regen.sh [--skip-install] [--skip-tests] |
| 13 | +# |
| 14 | +# --skip-install skip the -am install step (only safe if no Property/ |
| 15 | +# PassthroughPrefix classes outside tika-metadata-schema |
| 16 | +# changed since the last install) |
| 17 | +# --skip-tests skip the final gate-test run, for a faster inner loop |
| 18 | +# |
| 19 | +# See tika-metadata-schema/README.md and .skills/metadata-schema.md for the |
| 20 | +# design and the traps this script exists to route around. |
| 21 | + |
| 22 | +set -euo pipefail |
| 23 | + |
| 24 | +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 25 | +REPO_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)" |
| 26 | +cd "$REPO_ROOT" |
| 27 | + |
| 28 | +MVN_REPO_OPT="-Dmaven.repo.local=$REPO_ROOT/.local_m2_repo" |
| 29 | + |
| 30 | +SKIP_INSTALL=0 |
| 31 | +SKIP_TESTS=0 |
| 32 | +for arg in "$@"; do |
| 33 | + case "$arg" in |
| 34 | + --skip-install) SKIP_INSTALL=1 ;; |
| 35 | + --skip-tests) SKIP_TESTS=1 ;; |
| 36 | + -h|--help) |
| 37 | + sed -n '2,20p' "$0" | sed 's/^# \{0,1\}//' |
| 38 | + exit 0 |
| 39 | + ;; |
| 40 | + *) |
| 41 | + echo "Unknown argument: $arg" >&2 |
| 42 | + exit 1 |
| 43 | + ;; |
| 44 | + esac |
| 45 | +done |
| 46 | + |
| 47 | +REGISTRY_DIR="tika-metadata-schema/src/main/resources/org/apache/tika/metadata" |
| 48 | +REGISTRY_FILES=( |
| 49 | + "$REGISTRY_DIR/metadata-keys.json" |
| 50 | + "$REGISTRY_DIR/metadata-open-namespaces.json" |
| 51 | + "$REGISTRY_DIR/metadata-key-fields.json" |
| 52 | +) |
| 53 | + |
| 54 | +if [ "$SKIP_INSTALL" -eq 0 ]; then |
| 55 | + echo "==> Installing tika-metadata-schema + its dependency modules (tika-core, standard parsers)" |
| 56 | + echo " so newly added Property/PassthroughPrefix classes are on the scan classpath." |
| 57 | + echo " (skip with --skip-install if you already did this)" |
| 58 | + ./mvnw -Pfast -DskipTests -pl tika-metadata-schema -am install "$MVN_REPO_OPT" |
| 59 | +fi |
| 60 | + |
| 61 | +echo "==> Recording committed key counts, to catch an incomplete classpath scan later" |
| 62 | +BEFORE_COUNTS=() |
| 63 | +for f in "${REGISTRY_FILES[@]}"; do |
| 64 | + if git cat-file -e "HEAD:$f" 2>/dev/null; then |
| 65 | + BEFORE_COUNTS+=("$(git show "HEAD:$f" | wc -l)") |
| 66 | + else |
| 67 | + BEFORE_COUNTS+=("0") |
| 68 | + fi |
| 69 | +done |
| 70 | + |
| 71 | +echo "==> Regenerating the registry (forked exec — see .skills/metadata-schema.md for why exec:java is unsafe)" |
| 72 | +./mvnw -pl tika-metadata-schema -Pregen-metadata-schema process-classes "$MVN_REPO_OPT" |
| 73 | + |
| 74 | +echo "==> Comparing key counts before/after (a large drop usually means classes failed to load):" |
| 75 | +for i in "${!REGISTRY_FILES[@]}"; do |
| 76 | + f="${REGISTRY_FILES[$i]}" |
| 77 | + before="${BEFORE_COUNTS[$i]}" |
| 78 | + after=$(wc -l < "$f") |
| 79 | + line=" $f: $before -> $after lines" |
| 80 | + if [ "$before" -gt 0 ] && [ "$after" -lt $((before * 90 / 100)) ]; then |
| 81 | + echo "$line *** WARNING: >10% drop, check --skip-install and module installs ***" |
| 82 | + else |
| 83 | + echo "$line" |
| 84 | + fi |
| 85 | +done |
| 86 | + |
| 87 | +echo "==> git diff of the registries (review before committing):" |
| 88 | +git --no-pager diff --stat -- "${REGISTRY_FILES[@]}" |
| 89 | + |
| 90 | +if [ "$SKIP_TESTS" -eq 0 ]; then |
| 91 | + echo "==> Running gate tests (MetadataSchemaTest, MetadataFieldTableTest, MetadataNoUnderscoreTest, MetadataCoverageTest, ...)" |
| 92 | + ./mvnw -pl tika-metadata-schema test "$MVN_REPO_OPT" |
| 93 | +fi |
| 94 | + |
| 95 | +echo "==> Done. Review the diff above, then commit the Property/PassthroughPrefix change and the regenerated JSON together." |
0 commit comments