From 6f6d6efe748cab8a408fe68072bc221e34851d5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hartmut=20N=C3=B6renberg?= Date: Mon, 6 Apr 2026 13:00:14 +0200 Subject: [PATCH] chore: add HartOMat cleanup helper --- scripts/hartomat_cleanup_and_push.sh | 227 +++++++++++++++++++++++++++ 1 file changed, 227 insertions(+) create mode 100755 scripts/hartomat_cleanup_and_push.sh diff --git a/scripts/hartomat_cleanup_and_push.sh b/scripts/hartomat_cleanup_and_push.sh new file mode 100755 index 0000000..c1b9579 --- /dev/null +++ b/scripts/hartomat_cleanup_and_push.sh @@ -0,0 +1,227 @@ +#!/usr/bin/env bash +set -euo pipefail + +GITEA_URL="https://gitea.hartmut-noerenberg.com/Hartmut/HartOMat" +PUSH_GITEA=0 +PUSH_ALL=0 +SKIP_BUILD=0 + +usage() { + cat <<'EOF' +Usage: + ./scripts/hartomat_cleanup_and_push.sh [options] + +Options: + --push-gitea Add/update remote "gitea" and push current branch + tags + --push-all Push all branches + tags to remote "gitea" + --skip-build Skip frontend npm install/build + -h, --help Show this help + +Examples: + ./scripts/hartomat_cleanup_and_push.sh + ./scripts/hartomat_cleanup_and_push.sh --push-gitea + ./scripts/hartomat_cleanup_and_push.sh --push-all +EOF +} + +log() { + printf '\n[%s] %s\n' "$(date '+%H:%M:%S')" "$*" +} + +require_cmd() { + if ! command -v "$1" >/dev/null 2>&1; then + echo "Missing required command: $1" >&2 + exit 1 + fi +} + +ensure_repo_root() { + if ! REPO_ROOT="$(git rev-parse --show-toplevel 2>/dev/null)"; then + echo "This script must be run inside the git repository." >&2 + exit 1 + fi + cd "$REPO_ROOT" +} + +parse_args() { + while [ "$#" -gt 0 ]; do + case "$1" in + --push-gitea) + PUSH_GITEA=1 + ;; + --push-all) + PUSH_GITEA=1 + PUSH_ALL=1 + ;; + --skip-build) + SKIP_BUILD=1 + ;; + -h|--help) + usage + exit 0 + ;; + *) + echo "Unknown option: $1" >&2 + usage >&2 + exit 1 + ;; + esac + shift + done +} + +fix_permissions() { + log "Fixing ownership in repository" + sudo chown -R "$USER:$USER" "$REPO_ROOT" +} + +backup_backend_core_if_needed() { + if [ -f backend/core ]; then + local backup_dir backup_path + backup_dir="${HOME}/.cache/hartomat-cleanup" + mkdir -p "$backup_dir" + backup_path="${backup_dir}/backend-core-$(date '+%Y%m%d-%H%M%S')" + + log "Found regular file backend/core" + file backend/core || true + ls -lh backend/core || true + mv backend/core "$backup_path" + echo "Moved backend/core to $backup_path" + fi +} + +cleanup_artifacts() { + log "Removing generated caches and build artifacts" + find . -type d -name __pycache__ -prune -exec rm -rf {} + + find . -type d -name .pytest_cache -prune -exec rm -rf {} + + find . -type f -name '*.pyc' -delete + find . -type f -name '*.pyo' -delete + rm -rf frontend/node_modules frontend/dist + rm -f backend/celerybeat-schedule +} + +ensure_rollup_native() { + local libc pkg_suffix arch pkg version + + if [ ! -f frontend/node_modules/rollup/package.json ]; then + return + fi + + case "$(uname -m)" in + x86_64) + arch="x64" + ;; + *) + return + ;; + esac + + libc="gnu" + if command -v ldd >/dev/null 2>&1 && ldd --version 2>&1 | grep -qi musl; then + libc="musl" + fi + + pkg_suffix="linux-${arch}-${libc}" + pkg="@rollup/rollup-${pkg_suffix}" + version="$(node -p "require('./frontend/node_modules/rollup/package.json').version")" + + if [ -d "frontend/node_modules/@rollup/rollup-${pkg_suffix}" ]; then + return + fi + + log "Installing missing Rollup native package: ${pkg}@${version}" + ( + cd frontend + npm install --no-save "${pkg}@${version}" + ) +} + +rebuild_frontend() { + if [ "$SKIP_BUILD" -eq 1 ]; then + log "Skipping frontend build" + return + fi + + log "Installing frontend dependencies" + ( + cd frontend + npm install + ) + + ensure_rollup_native + + log "Building frontend" + ( + cd frontend + npm run build + ) + + log "Verifying HartOMat build output" + if [ ! -f frontend/dist/index.html ]; then + echo "Expected frontend/dist/index.html after build, but file was not created." >&2 + exit 1 + fi + + if ! grep -q 'HartOMat' frontend/dist/index.html; then + echo "Build succeeded, but frontend/dist/index.html does not contain 'HartOMat'." >&2 + exit 1 + fi +} + +configure_gitea_remote() { + log "Configuring git remote gitea" + if git remote get-url gitea >/dev/null 2>&1; then + git remote set-url gitea "$GITEA_URL" + else + git remote add gitea "$GITEA_URL" + fi + git remote -v | grep '^gitea[[:space:]]' || true +} + +push_gitea() { + local branch + branch="$(git branch --show-current)" + + if [ -z "$branch" ]; then + echo "Could not determine current branch." >&2 + exit 1 + fi + + if [ "$PUSH_ALL" -eq 1 ]; then + log "Pushing all branches and tags to gitea" + git push gitea --all + git push gitea --tags + else + log "Pushing current branch and tags to gitea" + git push -u gitea "$branch" + git push gitea --tags + fi +} + +main() { + parse_args "$@" + require_cmd git + require_cmd sudo + ensure_repo_root + fix_permissions + backup_backend_core_if_needed + cleanup_artifacts + if [ "$SKIP_BUILD" -eq 0 ]; then + require_cmd npm + fi + rebuild_frontend + + if [ "$PUSH_GITEA" -eq 1 ]; then + configure_gitea_remote + push_gitea + fi + + log "Finished" + echo "Repository: $REPO_ROOT" + echo "Branch: $(git branch --show-current)" + if git remote get-url gitea >/dev/null 2>&1; then + echo "Gitea remote: $(git remote get-url gitea)" + fi +} + +main "$@"