Files
Styleguide/scripts/sync_styleguide_to_webapp_aktienberater.sh

221 lines
6.2 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_CSS_REL="public/assets/styleguide.upstream.css"
PORTAL_BUILT_CSS_REL="public/assets/styles.css"
PORTAL_META_REL="public/assets/styleguide.upstream.meta.json"
PORTAL_STYLEGUIDE_DOCS_REL="docs/styleguide"
PORTAL_BUILD_SCRIPT_REL="scripts/styleguide/build_styles.sh"
COMMIT_IN_PORTAL="false"
usage() {
cat <<USAGE
Usage:
$(basename "$0") [--commit-portal]
Options:
--commit-portal Create commit in portal repo after sync.
-h, --help Show this help.
USAGE
}
while [[ $# -gt 0 ]]; do
case "$1" in
--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
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"
}
build_portal_css() {
local portal_key="$1"
local source_file="$2"
local output_file="$3"
case "$portal_key" in
vsf)
awk '
BEGIN { skip=0 }
/^:root\[data-portal="naurua"\] \{/ { skip=1; next }
skip && /^}/ { skip=0; next }
skip { next }
{ print }
' "$source_file" > "$output_file"
;;
naurua)
awk '
BEGIN { in_override=0 }
/^:root\[data-portal="naurua"\] \{/ { print ":root {"; in_override=1; next }
in_override && /^}/ { print "}"; in_override=0; next }
{ print }
' "$source_file" > "$output_file"
;;
*)
echo "Unknown portal: $portal_key" >&2
exit 1
;;
esac
}
sync_portal() {
local portal_key="$1"
local portal_repo_path="$2"
local portal_css_path="$portal_repo_path/$PORTAL_CSS_REL"
local portal_built_css_path="$portal_repo_path/$PORTAL_BUILT_CSS_REL"
local portal_meta_path="$portal_repo_path/$PORTAL_META_REL"
local portal_styleguide_docs_path="$portal_repo_path/$PORTAL_STYLEGUIDE_DOCS_REL"
local portal_build_script_path="$portal_repo_path/$PORTAL_BUILD_SCRIPT_REL"
local tmp_portal_css
if [[ ! -d "$portal_repo_path/.git" ]]; then
echo "Portal repo is not a git repository: $portal_repo_path" >&2
exit 1
fi
mkdir -p "$(dirname "$portal_css_path")"
mkdir -p "$portal_styleguide_docs_path"
tmp_portal_css="$(mktemp)"
build_portal_css "$portal_key" "$TMP_UPSTREAM_CSS" "$tmp_portal_css"
cp "$tmp_portal_css" "$portal_css_path"
rm -f "$tmp_portal_css"
rsync -a --delete \
--exclude ".git/" \
--exclude ".codex/" \
--exclude ".DS_Store" \
--exclude "AGENTS.md" \
"$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
case "$portal_key" in
vsf)
if [[ ! -f "$portal_build_script_path" ]]; then
echo "Portal build script not found: $portal_build_script_path" >&2
exit 1
fi
bash "$portal_build_script_path"
;;
naurua)
cp "$portal_css_path" "$portal_built_css_path"
;;
esac
if [[ "$COMMIT_IN_PORTAL" == "true" ]]; then
git -C "$portal_repo_path" add -A \
"$PORTAL_CSS_REL" \
"$PORTAL_BUILT_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_key portal synced and pushed."
else
echo "No changes to commit in $portal_key portal repo."
fi
else
echo "$portal_key portal files updated locally (no commit requested)."
fi
echo "Synced $SOURCE_CSS -> $portal_css_path"
echo "Built portal stylesheet: $portal_built_css_path"
echo "Metadata written: $portal_meta_path"
echo "Mirrored styleguide docs: $portal_styleguide_docs_path"
}
assert_no_unimported_styles() {
local source_file="$1"
local source_root="$2"
local styles_dir="$source_root/styles"
local missing_imports=()
if [[ ! -d "$styles_dir" ]]; then
return 0
fi
while IFS= read -r css_file; do
local rel_path="./styles/$(basename "$css_file")"
if ! grep -Eq "^[[:space:]]*@import[[:space:]]+\"$rel_path\"[[:space:]]*;" "$source_file"; then
missing_imports+=("$rel_path")
fi
done < <(find "$styles_dir" -maxdepth 1 -type f -name '*.css' | sort)
if [[ ${#missing_imports[@]} -gt 0 ]]; then
echo "Unimported CSS module files detected in styles/:" >&2
for file in "${missing_imports[@]}"; do
echo " - $file" >&2
done
echo "Add missing @import lines in $source_file before syncing." >&2
exit 1
fi
}
TMP_UPSTREAM_CSS="$(mktemp)"
trap 'rm -f "$TMP_UPSTREAM_CSS"' EXIT
assert_no_unimported_styles "$SOURCE_CSS" "$STYLEGUIDE_REPO_ROOT"
flatten_css "$SOURCE_CSS" "$STYLEGUIDE_REPO_ROOT" "$TMP_UPSTREAM_CSS"
sync_portal "vsf" "/Users/mathias/Documents/Dokumente Chouchou/Codebases/WebApp_Aktienberater"
sync_portal "naurua" "/Users/mathias/Documents/Dokumente Chouchou/Codebases/erp_naurua"