Update update_changelog.sh
This commit is contained in:
@@ -1,9 +1,33 @@
|
|||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
set -euo pipefail
|
# -----------------------------------------------------------------------------
|
||||||
|
|
||||||
# scripts/update_changelog.sh
|
# Copyright (C) 2025 Moko Consulting <hello@mokoconsulting.tech>
|
||||||
#
|
#
|
||||||
# Purpose:
|
# This file is part of a Moko Consulting project.
|
||||||
|
#
|
||||||
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||||
|
#
|
||||||
|
# This program is free software; you can redistribute it and/or modify it under
|
||||||
|
# the terms of the GNU General Public License as published by the Free Software
|
||||||
|
# Foundation; either version 3 of the License, or (at your option) any later
|
||||||
|
# version.
|
||||||
|
#
|
||||||
|
# This program is distributed in the hope that it will be useful, but WITHOUT
|
||||||
|
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
||||||
|
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
||||||
|
#
|
||||||
|
# You should have received a copy of the GNU General Public License (./LICENSE.md).
|
||||||
|
# -----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
# FILE INFORMATION
|
||||||
|
# DEFGROUP: MokoStandards
|
||||||
|
# INGROUP: Generic.Script
|
||||||
|
# REPO: https://github.com/mokoconsulting-tech/MokoDefaults
|
||||||
|
# PATH: /scripts/update_changelog.sh
|
||||||
|
# VERSION: 01.00.00
|
||||||
|
# BRIEF: Insert a versioned CHANGELOG.md entry immediately after the main Changelog heading.
|
||||||
|
# NOTES
|
||||||
|
# # Purpose:
|
||||||
# - Apply the MokoWaaS-Brand CHANGELOG template entry for a given version.
|
# - Apply the MokoWaaS-Brand CHANGELOG template entry for a given version.
|
||||||
# - Insert a new header at the top of CHANGELOG.md, immediately after "# Changelog".
|
# - Insert a new header at the top of CHANGELOG.md, immediately after "# Changelog".
|
||||||
# - Avoid duplicates if an entry for the version already exists.
|
# - Avoid duplicates if an entry for the version already exists.
|
||||||
@@ -14,67 +38,87 @@ set -euo pipefail
|
|||||||
#
|
#
|
||||||
# Example:
|
# Example:
|
||||||
# ./scripts/update_changelog.sh 01.05.00
|
# ./scripts/update_changelog.sh 01.05.00
|
||||||
|
# =============================================================================
|
||||||
|
|
||||||
VERSION="${1:-}"
|
set -euo pipefail
|
||||||
|
|
||||||
if [[ -z "${VERSION}" ]]; then
|
|
||||||
echo "ERROR: Version argument is required. Usage: scripts/update_changelog.sh <VERSION>" >&2
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
CHANGELOG_FILE="CHANGELOG.md"
|
CHANGELOG_FILE="CHANGELOG.md"
|
||||||
|
|
||||||
DATE_UTC="$(date -u +"%Y-%m-%d")"
|
die() {
|
||||||
HEADER="## ${VERSION} - ${DATE_UTC}"
|
echo "ERROR: $*" 1>&2
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
|
||||||
if [[ ! -f "${CHANGELOG_FILE}" ]]; then
|
info() {
|
||||||
{
|
echo "INFO: $*"
|
||||||
echo "# Changelog"
|
}
|
||||||
echo
|
|
||||||
} > "${CHANGELOG_FILE}"
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Do not duplicate existing entries
|
require_cmd() {
|
||||||
if grep -qE "^##[[:space:]]+${VERSION}[[:space:]]+-[[:space:]]+" "${CHANGELOG_FILE}"; then
|
command -v "$1" >/dev/null 2>&1 || die "Missing required command: $1"
|
||||||
echo "CHANGELOG.md already contains an entry for ${VERSION}. No changes made."
|
}
|
||||||
|
|
||||||
|
validate_version() {
|
||||||
|
local v="$1"
|
||||||
|
[[ "$v" =~ ^[0-9]{2}\.[0-9]{2}\.[0-9]{2}$ ]] || die "Invalid version '$v'. Expected NN.NN.NN (example 03.01.00)."
|
||||||
|
}
|
||||||
|
|
||||||
|
main() {
|
||||||
|
require_cmd awk
|
||||||
|
require_cmd grep
|
||||||
|
require_cmd mktemp
|
||||||
|
require_cmd date
|
||||||
|
|
||||||
|
[[ $# -eq 1 ]] || die "Usage: $0 <VERSION>"
|
||||||
|
local version="$1"
|
||||||
|
validate_version "$version"
|
||||||
|
|
||||||
|
[[ -f "$CHANGELOG_FILE" ]] || die "Missing $CHANGELOG_FILE in repo root."
|
||||||
|
|
||||||
|
if ! grep -qE '^# Changelog[[:space:]]*$' "$CHANGELOG_FILE"; then
|
||||||
|
die "$CHANGELOG_FILE must contain a top level heading exactly: # Changelog"
|
||||||
|
fi
|
||||||
|
|
||||||
|
if grep -qE "^## \[$version\][[:space:]]" "$CHANGELOG_FILE"; then
|
||||||
|
info "CHANGELOG.md already contains an entry for version $version. No action taken."
|
||||||
exit 0
|
exit 0
|
||||||
fi
|
fi
|
||||||
|
|
||||||
TMP_FILE="${CHANGELOG_FILE}.tmp"
|
local stamp
|
||||||
|
stamp="$(date '+%Y-%m-%d')"
|
||||||
|
|
||||||
# Insert after the first '# Changelog' line.
|
local tmp
|
||||||
# If '# Changelog' is missing, prepend it.
|
tmp="$(mktemp)"
|
||||||
if grep -qE "^# Changelog$" "${CHANGELOG_FILE}"; then
|
trap 'rm -f "$tmp"' EXIT
|
||||||
awk -v header="${HEADER}" '
|
|
||||||
|
awk -v v="$version" -v d="$stamp" '
|
||||||
BEGIN { inserted=0 }
|
BEGIN { inserted=0 }
|
||||||
{
|
{
|
||||||
print
|
print $0
|
||||||
if (!inserted && $0 ~ /^# Changelog$/) {
|
if (inserted==0 && $0 ~ /^# Changelog[[:space:]]*$/) {
|
||||||
print ""
|
print ""
|
||||||
print header
|
print "## [" v "] " d
|
||||||
|
print "- Version bump."
|
||||||
print ""
|
print ""
|
||||||
inserted=1
|
inserted=1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
END {
|
END {
|
||||||
if (!inserted) {
|
if (inserted==0) {
|
||||||
# fallback: append at end
|
exit 3
|
||||||
print ""
|
|
||||||
print header
|
|
||||||
print ""
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
' "${CHANGELOG_FILE}" > "${TMP_FILE}"
|
' "$CHANGELOG_FILE" > "$tmp" || {
|
||||||
else
|
rc=$?
|
||||||
{
|
if [[ $rc -eq 3 ]]; then
|
||||||
echo "# Changelog"
|
die "Insertion point not found. Expected: # Changelog"
|
||||||
echo
|
fi
|
||||||
echo "${HEADER}"
|
die "Failed to update $CHANGELOG_FILE (awk exit code $rc)."
|
||||||
echo
|
}
|
||||||
cat "${CHANGELOG_FILE}"
|
|
||||||
} > "${TMP_FILE}"
|
|
||||||
fi
|
|
||||||
|
|
||||||
mv "${TMP_FILE}" "${CHANGELOG_FILE}"
|
mv "$tmp" "$CHANGELOG_FILE"
|
||||||
|
trap - EXIT
|
||||||
|
|
||||||
echo "Applied changelog header: ${HEADER}"
|
info "Inserted CHANGELOG.md entry for version $version on $stamp."
|
||||||
|
}
|
||||||
|
|
||||||
|
main "$@"
|
||||||
|
|||||||
Reference in New Issue
Block a user