Skip to content

Commit

Permalink
Allow customizing the path to the package to deploy (#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
jsorge authored Nov 5, 2024
1 parent 236f3a7 commit 9219f1c
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 24 deletions.
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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"
Expand Down
31 changes: 25 additions & 6 deletions bin/compile
Original file line number Diff line number Diff line change
Expand Up @@ -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=""
Expand All @@ -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)"
Expand Down Expand Up @@ -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"
7 changes: 6 additions & 1 deletion bin/detect
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
#!/usr/bin/env bash
# bin/detect <build-dir>

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
Expand Down
14 changes: 7 additions & 7 deletions bin/steps/swift-install
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 0 additions & 5 deletions bin/utils
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
puts-step() {
echo "-----> $@"
}

set-env() {
PROFILE_PATH="$BUILD_DIR/.profile.d/swift.sh"
echo "export $1=$2" >> $PROFILE_PATH
}

0 comments on commit 9219f1c

Please sign in to comment.