1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209#!/bin/bash
set -eu
BUILDPACK_DIR="$(dirname "$(dirname "$0")")"
# see https://devcenter.heroku.com/articles/buildpack-api#bin-compile
BUILD_DIR=${1:-}
CACHE_DIR=${2:-}
cd "$BUILD_DIR"
mkdir -p "$BUILD_DIR/bin"
mkdir -p "$CACHE_DIR"
TYPST_VERSION="0.13.1"
OXIPNG_VERSION="9.1.5"
FIRA_SANS_VERSION="4.202"
NOTO_EMOJI_VERSION="2.048"
# Helper function to read a tool version from .tool-versions file
# Usage: read_tool_version <tool-name>
read_tool_version() {
local tool="$1"
local version
version=$(grep "^$tool " .tool-versions 2>/dev/null | awk '{print $2}')
if [ -z "$version" ]; then
echo "ERROR: Could not find '$tool' version in .tool-versions file" >&2
exit 1
fi
echo "$version"
}
### Install typst CLI
TYPST_ARCHIVE="$CACHE_DIR/typst-$TYPST_VERSION.tar.xz"
TYPST_BINARY="$BUILD_DIR/bin/typst"
if [ ! -x "$TYPST_BINARY" ]; then
echo "-----> Installing typst v$TYPST_VERSION"
if [ ! -f "$TYPST_ARCHIVE" ]; then
curl --proto '=https' --tlsv1.2 --location https://github.com/typst/typst/releases/download/v$TYPST_VERSION/typst-x86_64-unknown-linux-musl.tar.xz --output "$TYPST_ARCHIVE"
fi
tar -xf "$TYPST_ARCHIVE" -C /tmp
cp /tmp/typst-x86_64-unknown-linux-musl/typst "$TYPST_BINARY"
chmod +x "$TYPST_BINARY"
rm -rf /tmp/typst-x86_64-unknown-linux-musl
else
echo "-----> typst v$TYPST_VERSION already installed"
fi
### Install oxipng CLI
OXIPNG_ARCHIVE="$CACHE_DIR/oxipng-$OXIPNG_VERSION.tar.gz"
OXIPNG_BINARY="$BUILD_DIR/bin/oxipng"
if [ ! -x "$OXIPNG_BINARY" ]; then
echo "-----> Installing oxipng v$OXIPNG_VERSION"
if [ ! -f "$OXIPNG_ARCHIVE" ]; then
curl --proto '=https' --tlsv1.2 --location https://github.com/oxipng/oxipng/releases/download/v$OXIPNG_VERSION/oxipng-$OXIPNG_VERSION-x86_64-unknown-linux-musl.tar.gz --output "$OXIPNG_ARCHIVE"
fi
tar -xf "$OXIPNG_ARCHIVE" -C /tmp
cp /tmp/oxipng-$OXIPNG_VERSION-x86_64-unknown-linux-musl/oxipng "$OXIPNG_BINARY"
chmod +x "$OXIPNG_BINARY"
rm -rf /tmp/oxipng-$OXIPNG_VERSION-x86_64-unknown-linux-musl
else
echo "-----> oxipng v$OXIPNG_VERSION already installed"
fi
### Setup PATH for runtime
mkdir -p "$BUILD_DIR/.profile.d"
cat > "$BUILD_DIR/.profile.d/buildpack.sh" << 'EOF'
export PATH="/app/bin:$PATH"
EOF
### Setup fonts directory
FONTS_DIR="$BUILD_DIR/.fonts"
mkdir -p "$FONTS_DIR"
# Create fontconfig directory and config
mkdir -p "$BUILD_DIR/.config/fontconfig"
cat > "$BUILD_DIR/.config/fontconfig/fonts.conf" << 'EOF'
<?xml version="1.0"?>
<!DOCTYPE fontconfig SYSTEM "fonts.dtd">
<fontconfig>
<dir>~/.fonts</dir>
</fontconfig>
EOF
### Install fonts for OG image generation
if [ ! -f "$FONTS_DIR/FiraSans-Regular.ttf" ]; then
echo "-----> Installing Fira Sans font v$FIRA_SANS_VERSION"
# Download Fira Sans font files
FIRA_SANS_CACHE="$CACHE_DIR/fira-sans-$FIRA_SANS_VERSION"
if [ ! -d "$FIRA_SANS_CACHE" ]; then
mkdir -p "$FIRA_SANS_CACHE"
# Download from Mozilla's official repository
curl --proto '=https' --tlsv1.2 --location https://github.com/mozilla/Fira/archive/refs/tags/$FIRA_SANS_VERSION.zip --output "$FIRA_SANS_CACHE/fira-$FIRA_SANS_VERSION.zip"
unzip -q "$FIRA_SANS_CACHE/fira-$FIRA_SANS_VERSION.zip" -d "$FIRA_SANS_CACHE"
fi
# Copy TTF files to user font directory
cp "$FIRA_SANS_CACHE/Fira-$FIRA_SANS_VERSION/ttf/FiraSans"*.ttf "$FONTS_DIR/"
else
echo "-----> Fira Sans font v$FIRA_SANS_VERSION already installed"
fi
NOTO_CJK_FILE="$FONTS_DIR/NotoSansCJK-Regular.ttc"
if [ ! -f "$NOTO_CJK_FILE" ]; then
echo "-----> Installing Noto Sans CJK font"
curl --proto '=https' --tlsv1.2 --location "https://github.com/notofonts/noto-cjk/raw/refs/tags/Sans2.004/Sans/OTC/NotoSansCJK-Regular.ttc" --output "$NOTO_CJK_FILE"
else
echo "-----> Noto Sans CJK font already installed"
fi
NOTO_EMOJI_FILE="$FONTS_DIR/NotoColorEmoji.ttf"
if [ ! -f "$NOTO_EMOJI_FILE" ]; then
echo "-----> Installing Noto Color Emoji font v$NOTO_EMOJI_VERSION"
curl --proto '=https' --tlsv1.2 --location "https://github.com/googlefonts/noto-emoji/raw/refs/tags/v$NOTO_EMOJI_VERSION/fonts/NotoColorEmoji.ttf" --output "$NOTO_EMOJI_FILE"
else
echo "-----> Noto Color Emoji font v$NOTO_EMOJI_VERSION already installed"
fi
### Install Node.js & pnpm and build frontend
echo "-----> Parsing pnpm version from \`.tool-versions\` file"
PNPM_VERSION="$(read_tool_version pnpm)"
export PNPM_VERSION
export PNPM_HOME="$CACHE_DIR/pnpm"
echo "-----> Installing pnpm v$PNPM_VERSION"
# see https://github.com/pnpm/get.pnpm.io/pull/35 for why the install script is vendored
ENV="$CACHE_DIR/.shrc" SHELL="$(which sh)" sh "$BUILDPACK_DIR/install_pnpm.sh"
export PATH="$PNPM_HOME:$PATH"
echo "-----> Parsing Node.js version from \`.tool-versions\` file"
NODE_VERSION="$(read_tool_version nodejs)"
if [ ! -x "$PNPM_HOME/nodejs/$NODE_VERSION" ]; then
echo "-----> Installing Node.js v$NODE_VERSION"
pnpm env use --global "$NODE_VERSION"
else
echo "-----> Node.js v$NODE_VERSION already installed"
fi
echo "-----> pnpm install"
pnpm install
echo "-----> pnpm run build"
pnpm run build
echo "-----> pnpm run build (Svelte)"
cd "$BUILD_DIR/svelte"
pnpm run build
cd "$BUILD_DIR"
echo "-----> rm -rf node_modules"
rm -rf node_modules
### Install Rust and build backend
export RUSTUP_HOME="$CACHE_DIR/rustup"
export CARGO_HOME="$CACHE_DIR/cargo"
export CARGO_TARGET_DIR="$CACHE_DIR/target"
if [ ! -x "$CARGO_HOME/bin/rustup" ]; then
echo "-----> Installing rustup"
curl --proto '=https' --tlsv1.2 https://sh.rustup.rs | sh -s -- --default-toolchain none -y
else
echo "-----> rustup already installed"
fi
export PATH="$CARGO_HOME/bin:$PATH"
echo "-----> rustup set profile minimal"
rustup set profile minimal
echo "-----> rustup toolchain install"
rustup toolchain install
echo "-----> cargo build"
cargo build --release
echo "-----> Copying compiled binaries into \`./target/release\` folder"
mkdir -p target/release
find "$CARGO_TARGET_DIR/release" -maxdepth 1 -type f -executable -exec cp -a -t target/release {} \;
echo "-----> Cleaning up cache folder"
# We can remove this completely, as cargo will recreate this from `cache`
rm -rf "$CARGO_HOME/registry/src"
# Remove dependencies that haven't been touched in the past 20 days
if [ -d "$CARGO_TARGET_DIR/release/.fingerprint" ]; then
find "$CARGO_TARGET_DIR/release/.fingerprint" -maxdepth 1 -type d -mtime +20 -exec rm -rf {} \;
fi
if [ -d "$CARGO_TARGET_DIR/release/build" ]; then
find "$CARGO_TARGET_DIR/release/build" -maxdepth 1 -type d -mtime +20 -exec rm -rf {} \;
fi
if [ -d "$CARGO_TARGET_DIR/release/deps" ]; then
find "$CARGO_TARGET_DIR/release/deps" -maxdepth 1 -type d -mtime +20 -exec rm -rf {} \;
fi