Skip to content

Commit

Permalink
Add and use new output library
Browse files Browse the repository at this point in the history
  • Loading branch information
Malax committed Feb 26, 2025
1 parent db9c0d5 commit 6924eb9
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 39 deletions.
15 changes: 6 additions & 9 deletions bin/compile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 2 additions & 5 deletions bin/release
Original file line number Diff line number Diff line change
Expand Up @@ -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 "---"

Expand Down
10 changes: 4 additions & 6 deletions bin/test
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
10 changes: 4 additions & 6 deletions bin/test-compile
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
18 changes: 12 additions & 6 deletions lib/common.sh
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -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
}
Expand Down Expand Up @@ -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
Expand Down
25 changes: 18 additions & 7 deletions lib/maven.sh
Original file line number Diff line number Diff line change
@@ -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}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down
83 changes: 83 additions & 0 deletions lib/output.sh
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 6924eb9

Please sign in to comment.