Skip to content

Commit 0a3f1df

Browse files
Copilotjsburckhardt
andcommitted
feat: update bat feature to install from GitHub releases instead of apt package
Co-authored-by: jsburckhardt <[email protected]>
1 parent 811c5c5 commit 0a3f1df

2 files changed

Lines changed: 99 additions & 26 deletions

File tree

src/bat/devcontainer-feature.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"version": {
88
"type": "string",
99
"default": "latest",
10-
"description": "Version of bat to install (currently installs from Ubuntu package repository)."
10+
"description": "Version of bat to install from GitHub releases."
1111
}
1212
}
1313
}

src/bat/install.sh

Lines changed: 98 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,120 @@
11
#!/usr/bin/env bash
22

3+
# Variables
4+
REPO_OWNER="sharkdp"
5+
REPO_NAME="bat"
6+
BINARY_NAME="bat"
37
BAT_VERSION="${VERSION:-"latest"}"
8+
GITHUB_API_REPO_URL="https://api.github.com/repos/${REPO_OWNER}/${REPO_NAME}/releases"
49

510
set -e
611

712
if [ "$(id -u)" -ne 0 ]; then
8-
echo -e 'Script must be run as root. Use sudo, su, or add "USER root" to your Dockerfile before running this script.'
9-
exit 1
13+
echo -e 'Script must be run as root. Use sudo, su, or add "USER root" to your Dockerfile before running this script.'
14+
exit 1
1015
fi
1116

17+
# Clean up
18+
rm -rf /var/lib/apt/lists/*
19+
1220
# Checks if packages are installed and installs them if not
1321
check_packages() {
14-
if ! dpkg -s "$@" >/dev/null 2>&1; then
15-
if [ "$(find /var/lib/apt/lists/* | wc -l)" = "0" ]; then
16-
echo "Running apt-get update..."
17-
apt-get update -y
18-
fi
19-
apt-get -y install --no-install-recommends "$@"
20-
fi
22+
if ! dpkg -s "$@" >/dev/null 2>&1; then
23+
if [ "$(find /var/lib/apt/lists/* | wc -l)" = "0" ]; then
24+
echo "Running apt-get update..."
25+
apt-get update -y
26+
fi
27+
apt-get -y install --no-install-recommends "$@"
28+
fi
2129
}
2230

23-
echo "Installing bat..."
31+
# Make sure we have curl and jq
32+
check_packages curl jq ca-certificates
2433

25-
# For simplicity and reliability, install bat from Ubuntu repositories
26-
# This provides a stable version that works well in devcontainer environments
27-
check_packages bat
34+
# Function to get the latest version from GitHub API
35+
get_latest_version() {
36+
curl -s "${GITHUB_API_REPO_URL}/latest" | jq -r ".tag_name"
37+
}
2838

29-
# Verify installation
30-
if command -v batcat >/dev/null 2>&1; then
31-
echo "batcat installed successfully!"
32-
batcat --version
33-
# Create a symlink so users can use 'bat' command
34-
ln -sf /usr/bin/batcat /usr/local/bin/bat
35-
echo "Created symlink: /usr/local/bin/bat -> /usr/bin/batcat"
36-
elif command -v bat >/dev/null 2>&1; then
37-
echo "bat installed successfully!"
38-
bat --version
39+
# Check if a version is passed as an argument
40+
if [ -z "$BAT_VERSION" ] || [ "$BAT_VERSION" == "latest" ]; then
41+
# No version provided, get the latest version
42+
BAT_VERSION=$(get_latest_version)
43+
echo "No version provided or 'latest' specified, installing the latest version: $BAT_VERSION"
3944
else
40-
echo "ERROR: bat installation failed - neither bat nor batcat found"
41-
exit 1
45+
echo "Installing version from environment variable: $BAT_VERSION"
46+
fi
47+
48+
# Determine the OS and architecture
49+
OS=$(uname -s | tr '[:upper:]' '[:lower:]')
50+
ARCH=$(uname -m)
51+
52+
case "$ARCH" in
53+
x86_64)
54+
ARCH="x86_64"
55+
;;
56+
i686)
57+
ARCH="i686"
58+
;;
59+
aarch64)
60+
ARCH="aarch64"
61+
;;
62+
armv7l)
63+
ARCH="arm-unknown"
64+
;;
65+
*)
66+
echo "Unsupported architecture: $ARCH"
67+
exit 1
68+
;;
69+
esac
70+
71+
case "$OS" in
72+
linux)
73+
OS="unknown-linux-gnu"
74+
;;
75+
darwin)
76+
OS="apple-darwin"
77+
;;
78+
*)
79+
echo "Unsupported OS: $OS"
80+
exit 1
81+
;;
82+
esac
83+
84+
# Construct the download URL
85+
DOWNLOAD_URL="https://github.com/${REPO_OWNER}/${REPO_NAME}/releases/download/${BAT_VERSION}/bat-${BAT_VERSION}-${ARCH}-${OS}.tar.gz"
86+
87+
# Create a temporary directory for the download
88+
TMP_DIR=$(mktemp -d)
89+
cd "$TMP_DIR" || exit
90+
91+
echo "Downloading bat from $DOWNLOAD_URL"
92+
curl -sSL "$DOWNLOAD_URL" -o "bat.tar.gz"
93+
94+
# Extract the tarball
95+
echo "Extracting bat..."
96+
tar -xzf "bat.tar.gz"
97+
98+
# Find the extracted directory (it should match the archive name pattern)
99+
EXTRACTED_DIR=$(find . -name "bat-${BAT_VERSION}-*" -type d | head -1)
100+
if [ -z "$EXTRACTED_DIR" ]; then
101+
echo "ERROR: Could not find extracted bat directory"
102+
exit 1
42103
fi
43104

105+
# Move the binary to /usr/local/bin
106+
echo "Installing bat..."
107+
mv "${EXTRACTED_DIR}/bat" /usr/local/bin/
108+
109+
# Cleanup
110+
cd - || exit
111+
rm -rf "$TMP_DIR"
112+
44113
# Clean up
45114
rm -rf /var/lib/apt/lists/*
46115

116+
# Verify installation
117+
echo "Verifying installation..."
118+
bat --version
119+
47120
echo "Done!"

0 commit comments

Comments
 (0)