From 4f1449ec937d1aa302d09fc2df927e17079fbb05 Mon Sep 17 00:00:00 2001 From: nyyu Date: Mon, 7 Apr 2025 11:27:22 +0200 Subject: [PATCH] chore: cleanup build script --- build.sh | 134 +++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 86 insertions(+), 48 deletions(-) diff --git a/build.sh b/build.sh index cd86d6e..a64dd20 100755 --- a/build.sh +++ b/build.sh @@ -1,17 +1,45 @@ #!/bin/bash set -euo pipefail -trap 'echo "Error on line $LINENO"' ERR - -. .util.sh # Constants readonly REQUIRED_ENV_VARS=(BUILD_DIR REPO_DIR GIT_USER_EMAIL GIT_USER_NAME CI_REPO_CLONE_URL GIT_USER GIT_TOKEN) +readonly REQUIRED_COMMANDS=(makepkg repo-add git pacman) + +# Colors for logging +readonly RED='\033[0;31m' +readonly GREEN='\033[0;32m' +readonly YELLOW='\033[1;33m' +readonly NC='\033[0m' # No Color + +trap 'echo -e "${RED}Error on line $LINENO${NC}"' ERR + +. .util.sh log() { local level=$1 shift - echo "[$(date '+%Y-%m-%d %H:%M:%S')] ${level}: $*" + local color="${NC}" + case "${level}" in + "ERROR") color="${RED}" ;; + "WARN") color="${YELLOW}" ;; + "INFO") color="${GREEN}" ;; + esac + echo -e "[$(date '+%Y-%m-%d %H:%M:%S')] ${color}${level}${NC}: $*" +} + +check_dependencies() { + local missing_cmds=() + for cmd in "${REQUIRED_COMMANDS[@]}"; do + if ! command -v "${cmd}" >/dev/null 2>&1; then + missing_cmds+=("${cmd}") + fi + done + + if [[ ${#missing_cmds[@]} -gt 0 ]]; then + log "ERROR" "Missing required commands: ${missing_cmds[*]}" + exit 1 + fi } validate_env_vars() { @@ -29,8 +57,8 @@ validate_env_vars() { } cleanup_build_dir() { + git clean -fdx if [[ -d "${BUILD_DIR}" ]]; then - log "INFO" "Cleaning build directory" rm -rf "${BUILD_DIR:?}"/* else log "INFO" "Creating build directory" @@ -40,7 +68,8 @@ cleanup_build_dir() { build() { local upd=${1:-false} - local retries=3 + local name ver rel epoch + cleanup_build_dir if grep -q 'git+' PKGBUILD && ! grep -q '#tag=' PKGBUILD; then @@ -48,58 +77,59 @@ build() { log "ERROR" "makepkg failed during preparation" return 1 fi - makepkg --printsrcinfo >.SRCINFO + if ! makepkg --printsrcinfo >.SRCINFO; then + log "ERROR" "Failed to generate .SRCINFO" + return 1 + fi [[ "${upd}" == true ]] && update_pkg fi - local name ver rel epoch name=$(grep -m1 'pkgname' .SRCINFO | cut -d'=' -f2 | tr -d ' ') ver=$(grep -m1 'pkgver' .SRCINFO | cut -d'=' -f2 | tr -d ' ') rel=$(grep -m1 'pkgrel' .SRCINFO | cut -d'=' -f2 | tr -d ' ') epoch=$(grep -m1 'epoch' .SRCINFO | cut -d'=' -f2 | tr -d ' ') [[ -n "${epoch}" ]] && epoch=${epoch}: - # Check if package exists in official repos if pacman -Si "${name}" 2>/dev/null | grep -v nyyu | grep -q -E '^Repository'; then - log "WARN" "Found ${name} in arch repo" + log "WARN" "Found ${name} in the Arch repo" fi - # Build package if it doesn't exist or needs updating if ! compgen -G "${REPO_DIR}/${name}-${epoch}${ver}-${rel}-*.pkg.tar.zst" >/dev/null; then log "INFO" "Building package ${name}-${epoch}${ver}-${rel}" - - while ((retries > 0)); do - if makepkg --nodeps --skippgpcheck --noconfirm --sign || \ - makepkg -C -s --skippgpcheck --noconfirm --sign || \ - makepkg -C -s --skippgpcheck --nocheck --noconfirm --sign; then - - # Copy and add to repo - for pkg in *.pkg.tar.zst; do - if [[ -f "${pkg}" ]]; then - cp "${pkg}"{,.sig} "${REPO_DIR}/" - repo-add -R -s "${REPO_DIR}/nyyu.db.tar.zst" "${REPO_DIR}/${pkg}" - fi - done - - if ! sudo pacman -Syu --noconfirm; then - log "WARN" "Failed to update system packages" + + if ! { makepkg --nodeps --skippgpcheck --noconfirm --sign || + makepkg -C -s --skippgpcheck --noconfirm --sign || + makepkg -C -s --skippgpcheck --nocheck --noconfirm --sign; }; then + log "ERROR" "All build attempts failed for ${name}" + return 1 + fi + + for pkg in *.pkg.tar.zst; do + if [[ -f "${pkg}" ]]; then + if ! cp "${pkg}"{,.sig} "${REPO_DIR}/"; then + log "ERROR" "Failed to copy ${pkg} to repo" + return 1 + fi + if ! repo-add -R -s "${REPO_DIR}/nyyu.db.tar.zst" "${REPO_DIR}/${pkg}"; then + log "ERROR" "Failed to add ${pkg} to repo database" + return 1 fi - return 0 fi - - ((retries--)) - log "WARN" "Build failed, $retries retries remaining" - sleep 5 done - - log "ERROR" "Failed to build package ${name}-${epoch}${ver}-${rel} after all retries" - return 1 + + if ! sudo pacman -Syu --noconfirm; then + log "WARN" "Failed to update system packages" + fi fi + return 0 } process_directory() { local dir=$1 - cd "${dir}" || { log "ERROR" "Failed to enter directory ${dir}"; return 1; } + if ! pushd "${dir}" >/dev/null; then + log "ERROR" "Failed to enter directory ${dir}" + return 1 + fi if [[ -f PKGBUILD ]]; then log "INFO" "Processing ${dir}" @@ -114,26 +144,24 @@ process_directory() { update_pkg build true fi - cd .. + + popd >/dev/null } process_directories() { local exit_status=0 local dirs=("$@") - for dir in "${dirs[@]}"; do if ! process_directory "$dir"; then log "ERROR" "Failed to process directory ${dir}" exit_status=1 fi done - return $exit_status } find_and_remove_deleted_packages() { local last_commit del=() - if git cat-file -e "${CI_PREV_COMMIT_SHA:-}" 2>/dev/null; then last_commit=${CI_PREV_COMMIT_SHA} else @@ -144,7 +172,6 @@ find_and_remove_deleted_packages() { fi log "INFO" "Finding removed packages from ${last_commit}" - mapfile -t del < <( { git --no-pager diff "${last_commit}"..HEAD aur.txt | tail -n +4 | grep -E '^-' | cut -c2- @@ -155,7 +182,7 @@ find_and_remove_deleted_packages() { if [[ ${#del[@]} -gt 0 ]]; then for pkg in "${del[@]}"; do log "INFO" "Removing package ${pkg}" - if ! repo-remove -s "${REPO_DIR}/nyyu.db.tar.zst" "${pkg}"; then + if ! repo-remove -s "${REPO_DIR}/nyyu.db.tar.zst" "${pkg}"; then log "WARN" "Failed to remove ${pkg} from repo" fi if ! rm -vf "${REPO_DIR}/${pkg}"*; then @@ -184,25 +211,36 @@ process_aur_packages() { while IFS= read -r pkg; do [[ -z "$pkg" ]] && continue + log "INFO" "Processing ${pkg}" - if ! git clone https://aur.archlinux.org/"${pkg}".git --depth 1; then + if ! git clone "https://aur.archlinux.org/${pkg}.git" --depth 1; then log "ERROR" "Failed to clone repository for package ${pkg}" continue fi - - (cd "${pkg}" && build false) || log "ERROR" "Failed to build ${pkg}" - done < aur.txt + + if ! pushd "${pkg}" >/dev/null; then + log "ERROR" "Failed to enter directory for package ${pkg}" + continue + fi + + if ! build false; then + log "ERROR" "Failed to build ${pkg}" + fi + + popd >/dev/null + rm -rf "${pkg}" + done