Files
Styleguide/scripts/sync_styleguide_to_webapp_aktienberater.sh
T

137 lines
3.7 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
STYLEGUIDE_REPO_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
SOURCE_CSS="$STYLEGUIDE_REPO_ROOT/styleguide.css"
VERSION_FILE="$STYLEGUIDE_REPO_ROOT/VERSION"
PORTAL_REPO_PATH="/Users/mathias/Documents/Dokumente Chouchou/Codebases/WebApp_Aktienberater"
PORTAL_CSS_REL="public/assets/styleguide.upstream.css"
PORTAL_META_REL="public/assets/styleguide.upstream.meta.json"
PORTAL_STYLEGUIDE_DOCS_REL="docs/styleguide"
COMMIT_IN_PORTAL="false"
usage() {
cat <<USAGE
Usage:
$(basename "$0") [--portal-repo <path>] [--commit-portal]
Options:
--portal-repo <path> Optional override for portal repository root.
--commit-portal Create commit in portal repo after sync.
-h, --help Show this help.
USAGE
}
while [[ $# -gt 0 ]]; do
case "$1" in
--portal-repo)
PORTAL_REPO_PATH="${2:-}"
shift 2
;;
--commit-portal)
COMMIT_IN_PORTAL="true"
shift
;;
-h|--help)
usage
exit 0
;;
*)
echo "Unknown argument: $1" >&2
usage
exit 1
;;
esac
done
if [[ ! -f "$SOURCE_CSS" ]]; then
echo "Source CSS not found: $SOURCE_CSS" >&2
exit 1
fi
if [[ ! -f "$VERSION_FILE" ]]; then
echo "Version file not found: $VERSION_FILE" >&2
exit 1
fi
if [[ ! -d "$PORTAL_REPO_PATH/.git" ]]; then
echo "Portal repo is not a git repository: $PORTAL_REPO_PATH" >&2
exit 1
fi
PORTAL_CSS_PATH="$PORTAL_REPO_PATH/$PORTAL_CSS_REL"
PORTAL_META_PATH="$PORTAL_REPO_PATH/$PORTAL_META_REL"
PORTAL_STYLEGUIDE_DOCS_PATH="$PORTAL_REPO_PATH/$PORTAL_STYLEGUIDE_DOCS_REL"
mkdir -p "$(dirname "$PORTAL_CSS_PATH")"
mkdir -p "$PORTAL_STYLEGUIDE_DOCS_PATH"
flatten_css() {
local source_file="$1"
local source_root="$2"
local output_file="$3"
: > "$output_file"
while IFS= read -r line; do
if [[ "$line" =~ ^[[:space:]]*@import[[:space:]]+\"([^\"]+)\"[[:space:]]*\;[[:space:]]*$ ]]; then
local import_path="${BASH_REMATCH[1]}"
local import_abs="$source_root/$import_path"
if [[ ! -f "$import_abs" ]]; then
echo "Imported CSS not found: $import_abs" >&2
exit 1
fi
cat "$import_abs" >> "$output_file"
printf "\n" >> "$output_file"
else
printf "%s\n" "$line" >> "$output_file"
fi
done < "$source_file"
}
TMP_UPSTREAM_CSS="$(mktemp)"
trap 'rm -f "$TMP_UPSTREAM_CSS"' EXIT
flatten_css "$SOURCE_CSS" "$STYLEGUIDE_REPO_ROOT" "$TMP_UPSTREAM_CSS"
cp "$TMP_UPSTREAM_CSS" "$PORTAL_CSS_PATH"
rsync -a --delete \
--exclude ".git/" \
--exclude ".codex/" \
--exclude ".DS_Store" \
--exclude "AGENTS.md" \
--exclude "scripts/" \
"$STYLEGUIDE_REPO_ROOT/" \
"$PORTAL_STYLEGUIDE_DOCS_PATH/"
STYLEGUIDE_VERSION="$(tr -d '[:space:]' < "$VERSION_FILE")"
STYLEGUIDE_COMMIT="$(git -C "$STYLEGUIDE_REPO_ROOT" rev-parse --short HEAD)"
SYNCED_AT="$(date -u +"%Y-%m-%dT%H:%M:%SZ")"
cat > "$PORTAL_META_PATH" <<META
{
"styleguideVersion": "$STYLEGUIDE_VERSION",
"styleguideCommit": "$STYLEGUIDE_COMMIT",
"syncedAtUtc": "$SYNCED_AT",
"sourceRepo": "$STYLEGUIDE_REPO_ROOT",
"mirroredDocsPath": "$PORTAL_STYLEGUIDE_DOCS_REL"
}
META
if [[ "$COMMIT_IN_PORTAL" == "true" ]]; then
git -C "$PORTAL_REPO_PATH" add -A "$PORTAL_CSS_REL" "$PORTAL_META_REL" "$PORTAL_STYLEGUIDE_DOCS_REL"
if ! git -C "$PORTAL_REPO_PATH" diff --cached --quiet; then
git -C "$PORTAL_REPO_PATH" commit -m "Sync styleguide $STYLEGUIDE_VERSION"
git -C "$PORTAL_REPO_PATH" push
echo "Portal repo synced and pushed."
else
echo "No changes to commit in portal repo."
fi
else
echo "Portal files updated locally (no commit requested)."
fi
echo "Synced $SOURCE_CSS -> $PORTAL_CSS_PATH"
echo "Metadata written: $PORTAL_META_PATH"
echo "Mirrored styleguide docs: $PORTAL_STYLEGUIDE_DOCS_PATH"