|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors |
| 4 | +# SPDX-License-Identifier: AGPL-3.0-or-later |
| 5 | + |
| 6 | +set -Eeuo pipefail |
| 7 | + |
| 8 | +declare -r ROOT_DIRECTORY="$(readlink -f "$(dirname "$(dirname "${BASH_SOURCE[0]}")")")" |
| 9 | +declare -r COMPOSER_BIN="${COMPOSER_BIN:-$(which composer)}" |
| 10 | +declare -r BOX_BIN="${ROOT_DIRECTORY}/vendor/bin/box" |
| 11 | +declare -ri DEBUG="${DEBUG:-0}" |
| 12 | +declare -ri ALLOW_DIRTY="${ALLOW_DIRTY:-0}" |
| 13 | + |
| 14 | +if [[ ${DEBUG} -gt 1 ]]; then |
| 15 | + set -x |
| 16 | +fi |
| 17 | + |
| 18 | +function debug() { |
| 19 | + if [[ ${DEBUG} -lt 1 ]]; then |
| 20 | + return |
| 21 | + fi |
| 22 | + printf "%s\n" "${*}" |
| 23 | +} |
| 24 | + |
| 25 | +# Ignore changes on updater.phar |
| 26 | +git restore updater.phar |
| 27 | + |
| 28 | +# Build information |
| 29 | +declare -i BUILD_TIMESTAMP |
| 30 | +BUILD_TIMESTAMP=$(git rev-list --no-commit-header -n1 --format=%ct HEAD) |
| 31 | +declare BUILD_VERSION |
| 32 | +BUILD_VERSION=$(git describe --tags) |
| 33 | +declare -i DIRTY_BUILD |
| 34 | +if [[ -z "$(git status --porcelain)" ]]; then |
| 35 | + DIRTY_BUILD=0 |
| 36 | +else |
| 37 | + DIRTY_BUILD=1 |
| 38 | + BUILD_VERSION="${BUILD_VERSION} dirty" |
| 39 | +fi |
| 40 | + |
| 41 | +debug "Build version ${BUILD_VERSION} with timestamp ${BUILD_TIMESTAMP}" |
| 42 | + |
| 43 | +# Create version file |
| 44 | +cat >lib/Version.php <<EOF |
| 45 | +<?php |
| 46 | +
|
| 47 | +declare(strict_types=1); |
| 48 | +
|
| 49 | +namespace NC\Updater; |
| 50 | +
|
| 51 | +class Version { |
| 52 | + function get(): string { |
| 53 | + return '${BUILD_VERSION}'; |
| 54 | + } |
| 55 | +} |
| 56 | +EOF |
| 57 | + |
| 58 | +# Checks |
| 59 | +if [[ ${BUILD_TIMESTAMP} -lt 1 ]]; then |
| 60 | + echo 'Could not retrieve timestamp from latest git commit' |
| 61 | + exit 1 |
| 62 | +fi |
| 63 | +if [[ ${DIRTY_BUILD} -gt 0 ]]; then |
| 64 | + if [[ ${ALLOW_DIRTY} -lt 1 ]]; then |
| 65 | + echo 'Version name contains "dirty" suffix. Clean your repository or use ALLOW_DIRTY=1 to bypass.' |
| 66 | + exit 2 |
| 67 | + fi |
| 68 | + echo '⚠️ Dirty build' |
| 69 | +fi |
| 70 | + |
| 71 | +# Force Composer suffix |
| 72 | +debug "Use ComposerPhar_${BUILD_TIMESTAMP} suffix in composer" |
| 73 | +"${COMPOSER_BIN}" config autoloader-suffix "ComposerPhar_${BUILD_TIMESTAMP}" |
| 74 | +# Set build timestamp |
| 75 | +sed -i "s/@timestamp@/$(date '+%Y-%m-%d %H:%I:%S %Z' -d "@${BUILD_TIMESTAMP}")/" "${ROOT_DIRECTORY}/box.json" |
| 76 | + |
| 77 | +# Build |
| 78 | +debug "Start build" |
| 79 | +"${BOX_BIN}" -n compile --sort-compiled-files --composer-bin="${COMPOSER_BIN}" |
| 80 | + |
| 81 | +# Reset composer.json and version file |
| 82 | +debug "Reset changes" |
| 83 | +"${COMPOSER_BIN}" config autoloader-suffix --unset |
| 84 | +git restore box.json |
| 85 | +rm lib/Version.php |
0 commit comments