From 6924eb9fdd1c4c535a486569e6364fedae9546f7 Mon Sep 17 00:00:00 2001 From: Manuel Fuchs Date: Wed, 26 Feb 2025 12:09:30 +0100 Subject: [PATCH] Add and use new output library --- bin/compile | 15 ++++----- bin/release | 7 ++-- bin/test | 10 +++--- bin/test-compile | 10 +++--- lib/common.sh | 18 +++++++---- lib/maven.sh | 25 +++++++++++---- lib/output.sh | 83 ++++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 129 insertions(+), 39 deletions(-) create mode 100644 lib/output.sh diff --git a/bin/compile b/bin/compile index 741caf9..09e73db 100755 --- a/bin/compile +++ b/bin/compile @@ -3,19 +3,16 @@ set -e -BP_DIR=$( - cd "$(dirname "${0}")/".. - pwd -) +BUILDPACK_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && cd .. && pwd)" BUILD_DIR=$1 CACHE_DIR=$2 -ENV_DIR=$3 +ENV_DIR=$3s -source "${BP_DIR}/lib/util.sh" -source "${BP_DIR}/lib/common.sh" -source "${BP_DIR}/lib/maven.sh" -source "${BP_DIR}/lib/metadata.sh" +source "${BUILDPACK_DIR}/lib/util.sh" +source "${BUILDPACK_DIR}/lib/common.sh" +source "${BUILDPACK_DIR}/lib/maven.sh" +source "${BUILDPACK_DIR}/lib/metadata.sh" # Initialise the buildpack metadata store. # This is used to track state across builds (for cache invalidation and messaging when build diff --git a/bin/release b/bin/release index c1058d1..6213b0e 100755 --- a/bin/release +++ b/bin/release @@ -3,13 +3,10 @@ set -e -BP_DIR=$( - cd "$(dirname "${0}")"/.. - pwd -) +BUILDPACK_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && cd .. && pwd)" BUILD_DIR=$1 -source "${BP_DIR}/lib/frameworks.sh" +source "${BUILDPACK_DIR}/lib/frameworks.sh" echo "---" diff --git a/bin/test b/bin/test index 898666a..3fbd5aa 100755 --- a/bin/test +++ b/bin/test @@ -4,15 +4,13 @@ # fail fast set -e -BP_DIR=$( - cd "$(dirname "${0}")"/.. - pwd -) BUILD_DIR=$1 ENV_DIR=$2 -source "${BP_DIR}/lib/common.sh" -source "${BP_DIR}/lib/maven.sh" +BUILDPACK_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && cd .. && pwd)" + +source "${BUILDPACK_DIR}/lib/common.sh" +source "${BUILDPACK_DIR}/lib/maven.sh" export_env_dir "${ENV_DIR}" "." "JAVA_OPTS|JAVA_TOOL_OPTIONS" diff --git a/bin/test-compile b/bin/test-compile index a64193e..a7ec2c7 100755 --- a/bin/test-compile +++ b/bin/test-compile @@ -4,16 +4,14 @@ # fail fast set -e -BP_DIR=$( - cd "$(dirname "${0}")"/.. - pwd -) BUILD_DIR=$1 CACHE_DIR=$2 ENV_DIR=$3 -source "${BP_DIR}/lib/common.sh" -source "${BP_DIR}/lib/maven.sh" +BUILDPACK_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && cd .. && pwd)" + +source "${BUILDPACK_DIR}/lib/common.sh" +source "${BUILDPACK_DIR}/lib/maven.sh" export_env_dir "${ENV_DIR}" "." "JAVA_OPTS|JAVA_TOOL_OPTIONS" diff --git a/lib/common.sh b/lib/common.sh index 153bc69..5ed4edb 100644 --- a/lib/common.sh +++ b/lib/common.sh @@ -1,5 +1,9 @@ #!/usr/bin/env bash +BUILDPACK_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && cd .. && pwd)" + +source "${BUILDPACK_DIR}/lib/output.sh" + export DEFAULT_MAVEN_VERSION="3.9.4" install_maven() { @@ -11,14 +15,18 @@ install_maven() { mavenVersion=${definedMavenVersion:-$DEFAULT_MAVEN_VERSION} - status_pending "Installing Maven ${mavenVersion}" + output::step "Installing Maven ${mavenVersion}" local mavenUrl="https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/${mavenVersion}/apache-maven-${mavenVersion}-bin.tar.gz" if is_supported_maven_version "${mavenVersion}" "${mavenUrl}"; then download_maven "${mavenUrl}" "${mavenHome}" - status_done else - error_return "Error, you have defined an unsupported Maven version in the system.properties file. -The default supported version is ${DEFAULT_MAVEN_VERSION}" + output::error <<-EOF + ERROR: Unsupported Maven version + + You have defined an unsupported Maven version in the system.properties file. + The default supported version is ${DEFAULT_MAVEN_VERSION} + EOF + return 1 fi } @@ -92,8 +100,6 @@ install_jdk() { mkdir -p /tmp/jvm-common curl --fail --retry 3 --retry-connrefused --connect-timeout 5 --silent --location "${JVM_COMMON_BUILDPACK}" | tar xzm -C /tmp/jvm-common --strip-components=1 #shellcheck source=/dev/null - source /tmp/jvm-common/bin/util - #shellcheck source=/dev/null source /tmp/jvm-common/bin/java #shellcheck source=/dev/null source /tmp/jvm-common/opt/jdbc.sh diff --git a/lib/maven.sh b/lib/maven.sh index 7a2437c..66cb340 100644 --- a/lib/maven.sh +++ b/lib/maven.sh @@ -1,5 +1,9 @@ #!/usr/bin/env bash +BUILDPACK_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && cd .. && pwd)" + +source "${BUILDPACK_DIR}/lib/output.sh" + _mvn_java_opts() { local scope=${1} local home=${2} @@ -41,7 +45,11 @@ _mvn_settings_opt() { if [ -f "${settingsXml}" ]; then echo -n "-s ${settingsXml}" else - error "Could not download settings.xml from the URL defined in MAVEN_SETTINGS_URL!" + output::error <<-EOF + ERROR: Download failed + + Could not download settings.xml from the URL defined in MAVEN_SETTINGS_URL! + EOF return 1 fi elif [ -f "${home}/settings.xml" ]; then @@ -103,15 +111,18 @@ run_mvn() { local mvnOpts mvnOpts="$(_mvn_cmd_opts "${scope}")" - status "Executing Maven" - echo "$ ${mavenExe} ${mvnOpts}" | indent + output::step "Executing Maven" + echo "$ ${mavenExe} ${mvnOpts}" | output::indent # We rely on word splitting for mvn_settings_opt and mvnOpts: # shellcheck disable=SC2086 - if ! ${mavenExe} -DoutputFile=target/mvn-dependency-list.log -B ${mvn_settings_opt} ${mvnOpts} | indent; then - error "Failed to build app with Maven -We're sorry this build is failing! If you can't find the issue in application code, -please submit a ticket so we can help: https://help.heroku.com/" + if ! ${mavenExe} -DoutputFile=target/mvn-dependency-list.log -B ${mvn_settings_opt} ${mvnOpts} | output::indent; then + output::error <<-EOF + ERROR: Failed to build app with Maven + + We're sorry this build is failing! If you can't find the issue in application code, + please submit a ticket so we can help: https://help.heroku.com/ + EOF fi } diff --git a/lib/output.sh b/lib/output.sh new file mode 100644 index 0000000..cbc967a --- /dev/null +++ b/lib/output.sh @@ -0,0 +1,83 @@ +#!/usr/bin/env bash + +ANSI_BLUE='\033[1;34m' +ANSI_RED='\033[1;31m' +ANSI_YELLOW='\033[1;33m' +ANSI_RESET='\033[0m' + +# Output a single line step message to stdout. +# +# Usage: +# ``` +# output::step "Installing Python ..." +# ``` +function output::step() { + echo "-----> ${1}" +} + +# Indent passed stdout. Typically used to indent command output within a step. +# +# Usage: +# ``` +# pip install ... | output::indent +# ``` +function output::indent() { + sed --unbuffered "s/^/ /" +} + +# Output a styled multi-line notice message to stderr. +# +# Usage: +# ``` +# output::notice <<-EOF +# Note: The note summary. +# +# Detailed description. +# EOF +# ``` +function output::notice() { + local line + echo >&2 + while IFS= read -r line; do + echo -e "${ANSI_BLUE} ! ${line}${ANSI_RESET}" >&2 + done + echo >&2 +} + +# Output a styled multi-line warning message to stderr. +# +# Usage: +# ``` +# output::warning <<-EOF +# Warning: The warning summary. +# +# Detailed description. +# EOF +# ``` +function output::warning() { + local line + echo >&2 + while IFS= read -r line; do + echo -e "${ANSI_YELLOW} ! ${line}${ANSI_RESET}" >&2 + done + echo >&2 +} + +# Output a styled multi-line error message to stderr. +# +# Usage: +# ``` +# output::error <<-EOF +# Error: The error summary. +# +# Detailed description. +# EOF +# ``` +function output::error() { + local line + echo >&2 + while IFS= read -r line; do + echo -e "${ANSI_RED} ! ${line}${ANSI_RESET}" >&2 + done + echo >&2 +}