Skip to content

Commit 289b51b

Browse files
Add a tool for package detection
1 parent fc0041e commit 289b51b

File tree

2 files changed

+56
-7
lines changed

2 files changed

+56
-7
lines changed

Earthfile

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@ BASE:
5252
FUNCTION
5353
COPY --chmod=755 tools/__tool /usr/local/bin/__tool
5454
RUN __tool __init
55-
# Basic requirements to even function:
55+
# Basic requirements:
56+
IF __can_install epel-release # test -f /etc/redhat-release && ! test -f /etc/fedora-release
57+
RUN __install epel-release
58+
END
5659
RUN (curl --version || __install curl)
5760

5861
# Obtain uv
@@ -103,7 +106,7 @@ INSTALL_DEPS:
103106
RUN __install zip unzip pkg-config git
104107
ELSE
105108
RUN __install libfmt-dev libssl-dev
106-
IF apt-cache show libboost-url-dev 2>&1 > /dev/null
109+
IF __can_install libboost-url-dev
107110
# Install the default version, if available
108111
RUN __install libboost-url-dev libboost-container-dev
109112
ELSE
@@ -155,7 +158,7 @@ INSTALL_DEPS:
155158
COPY_SRC:
156159
FUNCTION
157160
COPY --dir CMakeLists.txt vcpkg*.json etc/ src/ tools/ include/ etc/ \
158-
tests/ Makefile pyproject.toml uv.lock \
161+
tests/ docs/ Makefile pyproject.toml uv.lock \
159162
.
160163

161164
BUILD:

tools/__tool

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,57 @@ __install() {
142142
fi
143143
}
144144

145+
__can_install() {
146+
# Refresh local package databases
147+
if test -f /etc/debian_version; then
148+
apt-get -y update
149+
elif test -f /etc/alpine-release; then
150+
apk update
151+
elif test -f /etc/arch-release; then
152+
pacman --sync --refresh --quiet --noconfirm || :
153+
fi
154+
# If more than one package, test each one individually:
155+
if test $# -ne 1; then
156+
for pkg in "$@"; do
157+
__can_install_1 "$pkg" || return
158+
done
159+
return 0
160+
fi
161+
__can_install_1 "$1"
162+
}
163+
164+
__can_install_1() {
165+
local package="$1"
166+
# Package checks:
167+
if test -f /etc/debian_version; then
168+
__silent apt-cache show -- "$package"
169+
return
170+
elif test -f /etc/redhat-release; then
171+
# "Yum" may be an alias for dnf, but this still works:
172+
__silent yum info "$package"
173+
return
174+
elif test -f /etc/arch-release; then
175+
__silent pacman --sync --info -- "$package"
176+
return
177+
elif test -f /etc/alpine-release; then
178+
__silent apk info -- "$package"
179+
return
180+
else
181+
echo "$0: We don't know how to manage packages on this system" 1>&2
182+
return 1
183+
fi
184+
}
185+
186+
__silent() {
187+
"$@" > /dev/null 2>&1
188+
}
189+
145190
__init() {
146-
__alias __alias __tool __alias
147-
__alias __install __tool __install
148-
__alias __bool __tool __bool
149-
__alias __boolstr __tool __boolstr
191+
__alias __alias __tool __alias
192+
__alias __install __tool __install
193+
__alias __bool __tool __bool
194+
__alias __boolstr __tool __boolstr
195+
__alias __can_install __tool __can_install
150196
}
151197

152198
cmd=$1

0 commit comments

Comments
 (0)