#!/bin/bash set -euo pipefail shopt -s nullglob root=$(cd "$(dirname "$0")/.." && pwd) check=false if [[ "${1:-}" == --check ]]; then check=true shift fi language_args=() if [[ "${1:-}" == --language && $# -ge 2 ]]; then language_args=(--language "$2") shift 2 fi if [[ $# -ne 2 || "${1:-}" == -* ]]; then printf 'Usage: %s [--check] [--language locale] resources-directory output-directory\n' "$0" >&2 exit 2 fi resources="$(cd "$1" && pwd)" output="$2" catalogs=("$resources"/*.xcstrings) if [[ ${#catalogs[@]} -eq 0 ]]; then printf 'No String Catalogs found in %s\n' "$resources" >&2 exit 1 fi mkdir -p "$output" output="$(cd "$output" && pwd)" staging="$(mktemp -d "$output/.staging.XXXXXX")" trap 'rm -rf -- "$staging"' EXIT trap 'exit 1' HUP INT TERM # A failed catalog must not leave the build's tables partially regenerated. for catalog in "${catalogs[@]}"; do prepared="$staging/$(basename "$catalog")" # Catalog entries without translations still need their source-language keys in the bundle. "$root/Scripts/parcel-dev.sh" prepare-catalog "$catalog" "$prepared" xcrun xcstringstool compile "$prepared" --output-directory "$staging" --serialization-format text ${language_args[@]+"${language_args[@]}"} done status=0 for generated in "$staging"/*.lproj/*.strings "$staging"/*.lproj/*.stringsdict; do relative="${generated#"$staging"/}" destination="$output/$relative" if "$check"; then if ! cmp -s "$generated" "$destination"; then printf 'Out-of-date generated resource: %s\n' "$destination" >&2 status=1 fi else mkdir -p "$(dirname "$destination")" cp "$generated" "$destination" fi done for existing in "$output"/*.lproj/*.strings "$output"/*.lproj/*.stringsdict; do relative="${existing#"$output"/}" if [[ ! -f "$staging/$relative" ]]; then if "$check"; then printf 'Unexpected generated resource: %s\n' "$existing" >&2 status=1 else rm -- "$existing" fi fi done if [[ $status -ne 0 ]]; then printf 'Run Scripts/generate-config-strings.sh with these directories to regenerate catalog outputs.\n' >&2 fi exit "$status"