From 9219f1c57f4fe86847c22bc9c7959063d373ad4d Mon Sep 17 00:00:00 2001 From: Jared Sorge Date: Tue, 5 Nov 2024 07:01:28 -0800 Subject: [PATCH] Allow customizing the path to the package to deploy (#82) --- README.md | 12 +++++++----- bin/compile | 31 +++++++++++++++++++++++++------ bin/detect | 7 ++++++- bin/steps/swift-install | 14 +++++++------- bin/utils | 5 ----- 5 files changed, 45 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index 2b97df4d..3695a67f 100644 --- a/README.md +++ b/README.md @@ -26,8 +26,9 @@ You can also add it to upcoming builds of an existing application: $ heroku buildpacks:set vapor/vapor ``` -The buildpack will detect your app as Swift if it has a `Package.swift` file in -the root. +The buildpack will detect your app as Swift if it has a `Package.swift` file in the +repository. You can customize the path for the Swift package to deploy by setting the +`PACKAGE_DIR` config setting. ### Procfile @@ -51,7 +52,8 @@ web: Run --env=production --port=$PORT The buildpack defaults to Swift 6.0.2, but is compatible with Swift 5.9.1 - 6.0.1. -If you need to use a specific older version of the Swift toolchain from this range, you can pin the version number using a file called `.swift-version` in the root of the project folder, or by setting a `SWIFT_VERSION` configuration variable on Heroku, then deploying again. +If you need to use a specific older version of the Swift toolchain from this range, you can pin the version number using a file called `.swift-version` in the root of the project folder (or at the configured `PACKAGE_DIR` setting), or by +setting a `SWIFT_VERSION` configuration variable on Heroku, then deploying again. ```shell $ echo '5.10.1' > .swift-version @@ -147,12 +149,12 @@ Save the script as `bin/pre_compile` in the root of your project. GIT_PRIVATE_DOMAIN=`cat $ENV_DIR/GIT_PRIVATE_DOMAIN | tr -d '[[:space:]]'` GIT_PRIVATE_USER=`cat $ENV_DIR/GIT_PRIVATE_USER | tr -d '[[:space:]]'` GIT_PRIVATE_PASSWORD=`cat $ENV_DIR/GIT_PRIVATE_PASSWORD | tr -d '[[:space:]]'` - + # Create .netrc file with credentials echo "machine $GIT_PRIVATE_DOMAIN" >> "$HOME/.netrc" echo "login $GIT_PRIVATE_USER" >> "$HOME/.netrc" echo "password $GIT_PRIVATE_PASSWORD" >> "$HOME/.netrc" - + # Create cleanup script so the dyno does not see these values at runtime mkdir -p "$BUILD_DIR/.profile.d" echo "unset GIT_PRIVATE_DOMAIN" >> "$BUILD_DIR/.profile.d/netrc.sh" diff --git a/bin/compile b/bin/compile index 68b3395d..79b2fe65 100755 --- a/bin/compile +++ b/bin/compile @@ -7,10 +7,21 @@ set -o pipefail BIN_DIR=$(cd $(dirname $0); pwd) ROOT_DIR=$(dirname $BIN_DIR) -BUILD_DIR=$1 +APP_DIR=$1 CACHE_DIR=$2 ENV_DIR=$3 +VAR_PATH="$ENV_DIR/PACKAGE_DIR" + +if [[ -f "$VAR_PATH" ]]; then + package=$(cat "$VAR_PATH") + BUILD_DIR="$APP_DIR/$package" + echo "Using package directory at $package" +else + BUILD_DIR="$APP_DIR" + echo "No PACKAGE_DIR config variable is set. Using the repo root." +fi + SWIFT_VERSION="6.0.2" SWIFT_BUILD_CONFIGURATION="release" SWIFT_BUILD_FLAGS="" @@ -19,8 +30,11 @@ source "/etc/lsb-release" source "$BIN_DIR/utils" if [ -f "$BUILD_DIR/.swift-version" ]; then - SWIFT_VERSION=`cat $BUILD_DIR/.swift-version | tr -d '[[:space:]]'` - puts-step "Using Swift $SWIFT_VERSION (from .swift-version file)" + SWIFT_VERSION=$(cat $BUILD_DIR/.swift-version | tr -d '[[:space:]]') + puts-step "Using Swift $SWIFT_VERSION (from package .swift-version file)" +elif [ -f "$APP_DIR/.swift-version" ]; then + SWIFT_VERSION=$(cat $APP_DIR/.swift-version | tr -d '[[:space:]]') + puts-step "Using Swift $SWIFT_VERSION (from top-level .swift-version file)" elif [ -f "$ENV_DIR/SWIFT_VERSION" ]; then SWIFT_VERSION=`cat $ENV_DIR/SWIFT_VERSION | tr -d '[[:space:]]'` puts-step "Using Swift $SWIFT_VERSION (from SWIFT_VERSION config)" @@ -58,8 +72,13 @@ if [[ -z "$RUN_TESTS" ]]; then fi # Setup application environment -mkdir -p $BUILD_DIR/.profile.d +set-env() { + PROFILE_PATH="$APP_DIR/.profile.d/swift.sh" + echo "export $1=$2" >>$PROFILE_PATH +} + +mkdir -p $APP_DIR/.profile.d -set-env PATH '$HOME/.swift-bin:$PATH' -set-env LD_LIBRARY_PATH '$LD_LIBRARY_PATH:$HOME/.swift-lib' +set-env PATH '$HOME/$APP_DIR/.swift-bin:$PATH' +set-env LD_LIBRARY_PATH "$LD_LIBRARY_PATH:$HOME/.swift-lib:$APP_DIR/.swift-lib" set-env SWIFT_BACKTRACE "enable=yes,sanitize=yes,threads=all,images=all,interactive=no,output-to=stderr,symbolicate=fast,swift-backtrace=.swift-bin/$SWIFT_BACKTRACE_EXECUTABLE" diff --git a/bin/detect b/bin/detect index 6e0679c3..12084b42 100755 --- a/bin/detect +++ b/bin/detect @@ -1,7 +1,12 @@ #!/usr/bin/env bash # bin/detect -if [[ -f $1/Package.swift ]]; then +BUILD_DIR=$1 + +MANIFEST='Package.swift' +RESULTS=$(find "$BUILD_DIR" -name $MANIFEST) + +if [[ "$RESULTS" == *"$MANIFEST"* ]]; then echo "Swift" && exit 0 else echo "no" && exit 1 diff --git a/bin/steps/swift-install b/bin/steps/swift-install index a6e4fb29..4a85933d 100644 --- a/bin/steps/swift-install +++ b/bin/steps/swift-install @@ -2,20 +2,20 @@ DYNAMIC_LIBRARY_REGEX=".*\.so\(\.[0-9]+\)*\'" SWIFT_PREFIX="$(swiftenv prefix)" puts-step "Installing dynamic libraries" -mkdir -p $BUILD_DIR/.swift-lib -find -L .build/$SWIFT_BUILD_CONFIGURATION -regex "$DYNAMIC_LIBRARY_REGEX" -type f -exec cp -a {} $BUILD_DIR/.swift-lib \; || true 2>/dev/null +mkdir -p $APP_DIR/.swift-lib +find -L .build/$SWIFT_BUILD_CONFIGURATION -regex "$DYNAMIC_LIBRARY_REGEX" -type f -exec cp -a {} $APP_DIR/.swift-lib \; || true 2>/dev/null if [ ! -z "$SWIFT_DYNAMIC_STDLIB" ]; then - find -L $SWIFT_PREFIX/usr/lib/swift/linux -regex "$DYNAMIC_LIBRARY_REGEX" -type f -exec cp -a {} $BUILD_DIR/.swift-lib \; || true 2>/dev/null + find -L $SWIFT_PREFIX/usr/lib/swift/linux -regex "$DYNAMIC_LIBRARY_REGEX" -type f -exec cp -a {} $APP_DIR/.swift-lib \; || true 2>/dev/null fi puts-step "Installing binaries" -mkdir -p $BUILD_DIR/.swift-bin -find -L .build/$SWIFT_BUILD_CONFIGURATION ! -regex "$DYNAMIC_LIBRARY_REGEX" -type f -perm /a+x -exec cp -a {} $BUILD_DIR/.swift-bin \; || true 2>/dev/null -cp -a $SWIFT_PREFIX/usr/libexec/swift/linux/$SWIFT_BACKTRACE_EXECUTABLE $BUILD_DIR/.swift-bin +mkdir -p $APP_DIR/.swift-bin +find -L .build/$SWIFT_BUILD_CONFIGURATION ! -regex "$DYNAMIC_LIBRARY_REGEX" -type f -perm /a+x -exec cp -a {} $APP_DIR/.swift-bin \; || true 2>/dev/null +cp -a $SWIFT_PREFIX/usr/libexec/swift/linux/$SWIFT_BACKTRACE_EXECUTABLE $APP_DIR/.swift-bin puts-step "Installing resources" -find -L .build/$SWIFT_BUILD_CONFIGURATION/* -regex '.*\.resources$' -exec cp -a {} $BUILD_DIR/.swift-bin \; || true 2>/dev/null +find -L .build/$SWIFT_BUILD_CONFIGURATION/* -regex '.*\.resources$' -exec cp -a {} $APP_DIR/.swift-bin \; || true 2>/dev/null puts-step "Cleaning up after build" rm -rf .build diff --git a/bin/utils b/bin/utils index 5b0f9c61..0068e3dd 100644 --- a/bin/utils +++ b/bin/utils @@ -1,8 +1,3 @@ puts-step() { echo "-----> $@" } - -set-env() { - PROFILE_PATH="$BUILD_DIR/.profile.d/swift.sh" - echo "export $1=$2" >> $PROFILE_PATH -}