From d3d1251faadf5b3a2192db488f39c53cec4f3af0 Mon Sep 17 00:00:00 2001 From: Markus Frosch <markus.frosch@icinga.com> Date: Mon, 9 Sep 2019 16:25:08 +0200 Subject: [PATCH] release: Add RPM implementation --- icinga-build-release | 81 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 79 insertions(+), 2 deletions(-) diff --git a/icinga-build-release b/icinga-build-release index 4a637a6..0c9cd83 100755 --- a/icinga-build-release +++ b/icinga-build-release @@ -1,10 +1,15 @@ #!/bin/bash -set -e +set -eo pipefail : "${GIT_REMOTE:=origin}" : "${DCH_DIST:=icinga}" +RPM_VERSION_RE='^(Version:\s+).*' +RPM_VERSION_REPLACE='\1' +RPM_REVISION_RE='^(Release:\s+).*' +RPM_REVISION_SUFFIX='%{?dist}' + error() { if [ -t 2 ]; then { @@ -25,6 +30,14 @@ fail() { help() { echo "Usage: $0 <version> [message]" echo + echo "Examples:" + echo " Only bump upstream version: icinga-build-release 1.7.0" + echo " Full release with tag and push: icinga-build-release 1.7.0-1" + echo +} + +rpm_date() { + LANG=C echo "$(date +%A | sed -r 's~([a-z]{3}).*~\1~i')" "$(date +'%b %d %Y')" } if [ $# -ge 1 ]; then @@ -106,7 +119,71 @@ if [ "$variant" = deb ]; then # Staging the change git add debian/changelog else # rpm - fail "RPM not yet implemented!" >&2 + spec_file="$(ls ./*.spec)" + + if ! current_version="$(grep -P "$RPM_VERSION_RE" "$spec_file" | cut -d: -f2- | awk '{$1=$1};1')"; then + fail "Could not find Version: in ${spec_file}" + fi + + # special cases for version + case "$current_version" in + '%{module_version}') + echo "Found %{module_version} as version and will replace this!" + RPM_VERSION_RE='^(%(global|define)\s+module_version\s+).*' + + current_version="$(grep -P "$RPM_VERSION_RE" "$spec_file" | awk '{print $3}')" + ;; + esac + + if ! current_revision="$(grep -P "$RPM_REVISION_RE" "$spec_file" | cut -d: -f2- | awk '{$1=$1};1')"; then + fail "Could not find Revision: in ${spec_file}" + fi + + # remove dist suffix + current_revision="$(echo "$current_revision" | sed -r 's~%\{\?dist\}~~')" + + # special cases for revision + case "$current_revision" in + '%{revision}') + echo "Found %{revision} as version and will replace this!" + RPM_REVISION_RE='^(%(global|define)\s+revision\s+).*' + RPM_REVISION_SUFFIX='' + + current_revision="$(grep -P "$RPM_REVISION_RE" "$spec_file" | awk '{print $3}')" + ;; + esac + + echo "Found current version as ${current_version} with revision ${current_revision}" + + new_version="$(echo "${version}" | cut -d- -f1)" + if [[ "${version}" != *-* ]]; then + new_revision=1 + else + new_revision="$(echo "${version}" | cut -d- -f2-)" + fi + + # Replace data in spec file + sed -ri \ + -e "s~${RPM_VERSION_RE}~${RPM_VERSION_REPLACE}${new_version}~" \ + -e "s~${RPM_REVISION_RE}~${RPM_VERSION_REPLACE}${new_revision}${RPM_REVISION_SUFFIX}~" \ + "$spec_file" + + # Check last changelog entry? + #last_changelog="$(grep -A1 %changelog "${spec_file}" | tail -n1)" + #if [[ "${last_changelog}" = *" - ${new_version}-${new_revision}" ]]; then + + # Add to changelog + new_changelog="$(printf '* %s %s <%s> - %s' \ + "$(rpm_date)" \ + "$(git config user.name)" \ + "$(git config user.email)" \ + "${new_version}-${new_revision}")" + + # Insert after %changelog + sed -i "/^%changelog/a ${new_changelog}\\n- ${message}\\n" "${spec_file}" + + # Stage the changes + git add "${spec_file}" fi # Show the user what changed -- GitLab