d4t_formulas/docker-exec.sh

86 lines
1.9 KiB
Bash
Raw Normal View History

#!/bin/bash
set -e
2026-01-28 12:35:17 +00:00
detect_container(){
if [ "$DOCKER" != "" ]
then
return 0
elif command -v podman > /dev/null 2>&1
then
DOCKER=podman
elif command -v docker > /dev/null 2>&1
then
DOCKER=docker
else
echo "Error: no container manager detected (like 'docker' or 'podman'), please define DOCKER variable"
exit 2
fi
}
build_image(){
2026-01-28 12:35:17 +00:00
$DOCKER build -t d4rt-formulas-builder -f Dockerfile .
}
graphic_options(){
is_x11(){
[ "$XDG_SESSION_TYPE" = "x11" ] || [ "$DISPLAY" != "" ]
}
is_wayland(){
[ "$XDG_SESSION_TYPE" = "wayland" ] || [ "$WAYLAND_DISPLAY" != "" ]
}
if is_x11
then
echo "--env DISPLAY=$DISPLAY --volume /tmp/.X11-unix:/tmp/.X11-unix --security-opt=label=disable"
elif is_wayland
then
echo "--env=XDG_RUNTIME_DIR=/run/user/$(id -u) --volume=/run/user/$(id -u)/wayland:/run/user/$(id -u)/wayland --group-add=video"
else
echo "WARNING: no graphic environment" 1>&2
fi
}
exec_in_container(){
2026-01-28 12:35:17 +00:00
local SPIOPTIONS="--env AT_SPI_BUS=/run/user/$(id -u)/at-spi/bus_0 --volume=/run/user/$(id -u)/at-spi:/run/user/$(id -u)/at-spi --device=/dev/dri"
2026-01-28 15:29:18 +00:00
SPIOPTIONS=
2026-01-28 12:35:17 +00:00
local GRAPHICOPTIONS=$(graphic_options)
local BUILDCACHE=./.build-container-cache
mkdir -p $BUILDCACHE
2026-01-28 12:35:17 +00:00
$DOCKER run \
-it \
--init \
2026-01-28 12:35:17 +00:00
--rm \
$GRAPHICOPTIONS \
$SPIOPTIONS \
-v $BUILDCACHE:/cache:z \
-v .:/app:z \
-e FLUTTER_FLAVOR=prod \
d4rt-formulas-builder \
"$@"
}
2026-01-28 12:35:17 +00:00
main(){
detect_container
if [ "$1" = "build" ]; then
build_image
return $?
fi
2026-01-28 12:35:17 +00:00
if [ "$1" = "exec" ]; then
exec_in_container ${@:2}
return $?
fi
echo "Usage: $0 {build|exec <command>}"
return 1
}
2026-01-28 12:35:17 +00:00
main "$@"