#!/bin/bash set -euo pipefail base_dir=$(dirname $BASH_SOURCE) . ${base_dir}/.env.colors . ${base_dir}/.exit_handler . ${base_dir}/.bash_helpers HEDGELOG_DIR=$(readlink -f ${base_dir}/../) HEDGELOG_VERSION_FILE="${HEDGELOG_DIR}/lib/hedgelog/version.rb" HEDGELOG_VERSION=${HEDGELOG_VERSION:-} VALID_VERSION_FORMAT="^[0-9]+.[0-9]+.[0-9]+(\.alpha.[0-9+])?$" RUBYGEMS_CRED_FILE=".gem/credentials" RUBYGEMS_USER_CRED_FILE="${HOME}/${RUBYGEMS_CRED_FILE}" RUBYGEMS_ROOT_CRED_FILE="/root/${RUBYGEMS_CRED_FILE}" RUBYGEMS_CRED_VOLUME="${RUBYGEMS_USER_CRED_FILE}:${RUBYGEMS_ROOT_CRED_FILE}" touch "${RUBYGEMS_USER_CRED_FILE}" chmod 600 "${RUBYGEMS_USER_CRED_FILE}" usage() { echo "USAGE: $(basename "$0")" echo echo " Input parameters can be specified via environmental variables:" echo " HEDGELOG_VERSION (default: ) must be in the format '${VALID_VERSION_FORMAT}'" echo echo " the following will update the hedgelog version to 4.8.1.alpha.2" echo " HEDGELOG_VERSION=4.8.1.alpha.2 $(basename "$0")" } validate() { [ "$*" == "" ] || finish "$(usage)" found_version_to_update='' STARTING_HEDGELOG_VERSION=$(get_gem_version "${HEDGELOG_VERSION_FILE}") if [ "${HEDGELOG_VERSION}" == "" ] then echo_light_white "No hedgelog version given. Leaving at [ ${STARTING_HEDGELOG_VERSION} ]" else found_version_to_update='true' validate_version_format "${HEDGELOG_VERSION}" || die "Version [ ${HEDGELOG_VERSION} ] must be in the format ${VALID_VERSION_FORMAT}" echo_light_white "This will update the hedgelog version from [ ${STARTING_HEDGELOG_VERSION} ] to [ ${HEDGELOG_VERSION} ]." fi if [ "${found_version_to_update}" == 'true' ] then echo confirm " Do you wish to continue? (y/n)" || finish "Exiting..." else echo echo_yellow "No versions given to update" echo exit 0 fi } validate_version_format() { local version=$1 echo "${version}" | grep -qE "${VALID_VERSION_FORMAT}" } main() { echo validate $* echo hedgelog_gem_update="" if [ "${HEDGELOG_VERSION}" != "" ] then hedgelog_gem_update="hedgelog=$(normalize_gem_version "${HEDGELOG_VERSION}")" publish_hedgelog "${HEDGELOG_VERSION}" fi # GEMS="${hedgelog_gem_update}" ${base_dir}/update_gems.sh # I'm doing this because I don't want persistent cred files on our dev servers which would give access to publish # gems - in case a dev server was ever compromised [[ -f ${RUBYGEMS_USER_CRED_FILE} ]] && rm -f ${RUBYGEMS_USER_CRED_FILE} echo echo_light_white "Hedgelog has successfully been upgraded from version [ ${STARTING_HEDGELOG_VERSION} ] to [ ${HEDGELOG_VERSION} ]" echo } publish_hedgelog() { local desired_version=$1 # Change the version in the file update_gem_version "${HEDGELOG_VERSION_FILE}" "${desired_version}" # Make sure hedgelog is up to date echo "Checking to see if the hedgelog gem needs to be published..." # versions="$(MSYS_NO_PATHCONV=1 docker-compose ${COMPOSE_FILES} run --no-deps -v "${RUBYGEMS_CRED_VOLUME}" --entrypoint /bin/sh client-ruby -c "gem query --all --pre --remote stbaldricks")" versions=`gem query --all --pre --remote hedgelog` if echo "${versions}" | grep -Eq "(\(| )${desired_version}(,|\))" then # Make sure the user expected this to have already been published confirm " Hedgelog gem version ${desired_version} has already been published. Is this expected? (y/n)" || finish "Exiting..." else # Publish the gem echo "Starting ruby container to build and publish gem" # MSYS_NO_PATHCONV=1 docker-compose ${COMPOSE_FILES} run --no-deps -v "${RUBYGEMS_CRED_VOLUME}" --entrypoint /bin/sh client-ruby -c "gem build stbaldricks.gemspec && gem push stbaldricks-${desired_version}.gem" gem build hedgelog.gemspec && gem push "hedgelog-${desired_version}.gem" fi echo } get_gem_version() { local version_file=$1 normalize_version "$(sed -nre "s@.*VERSION.*=.*('|\")(.*)('|\").*@\2@gp" ${version_file})" } update_gem_version() { local version_file=$1 local version=$2 local gem_version=$(normalize_gem_version "${version}") # Try to update the gem version in the file sed -i -re "s@(.*VERSION.*=.*)('|\")(.*)('|\")(.*)@\1\2${gem_version}\4\5@g" "${version_file}" # Verify the version is now what we wanted updated_version=$(get_gem_version "${version_file}") if [ "${updated_version}" != "${version}" ] then die "Unable to update version [ ${version} ] in [ ${version_file} ] (got ${updated_version})" fi git add "${version_file}" } normalize_version() { local version=$1 echo $version | sed -re "s@([0-9]).(alpha)@\1.\2@g" } normalize_gem_version() { local version=$1 echo $version | sed -re "s@([0-9]).(alpha)@\1.\2@g" } strip_alpha() { local starting_version=$1 echo ${starting_version} | sed -re 's@(.*).alpha.[0-9]+@\1@g' } export -f get_gem_version strip_alpha # Only run main if this script isn't currently being sourced $(return >/dev/null 2>&1) || main $*