#!/bin/bash
env_name_for() {
echo "dev-$1"
}
create_env() {
local os="$1"
local name
name=$(env_name_for "$os")
log "Creating environment $name (os=$os)"
"$EPKG_BIN" --assume-yes env create "$name" -c "$os" || error "Failed to create env $name"
bootstrap_shell "$name"
log "Updating package metadata for $name"
"$EPKG_BIN" -e "$name" update
}
bootstrap_shell() {
local name="$1"
log "Bootstrapping shell in $name"
if [ "$OS" = "msys2" ]; then
"$EPKG_BIN" -e "$name" --assume-yes --ignore-missing install bash
else
"$EPKG_BIN" -e "$name" --assume-yes --ignore-missing install busybox bash
fi
local host_os
host_os=$(uname -s)
if [ "$host_os" = "Darwin" ] || [ "$host_os" = "MINGW" ] || [ "$host_os" = "MSYS" ] || [ "$host_os" = "CYGWIN" ]; then
log "Creating busybox applet symlinks for VM (host=$host_os)"
local env_root="${EPKG_ENVS_DIR:-$HOME/.epkg/envs}/$name"
local bin_dir="$env_root/usr/bin"
if [ -f "$bin_dir/epkg" ]; then
for applet in mkdir ls cat cp mv rm rmdir echo printf sleep true false test pwd; do
if [ ! -e "$bin_dir/$applet" ]; then
ln -sf epkg "$bin_dir/$applet" 2>/dev/null || true
fi
done
fi
fi
}
remove_env() {
local os="$1"
local name
name=$(env_name_for "$os")
log "Removing environment $name (if exists)"
"$EPKG_BIN" --assume-yes env remove "$name" 2>/dev/null || true
local env_root="${EPKG_ENVS_DIR:-$HOME/.epkg/envs}/$name"
if [ -d "$env_root" ]; then
log "Removing leftover env directory $env_root"
rm -rf "$env_root"
fi
}
run_in_env() {
"$EPKG_BIN" -e "$ENV_NAME" run "$@"
}
run_with_timeout() {
local t="$1"
shift
case "$(uname -s)" in
CYGWIN*|MINGW*|MSYS*)
timeout "$t" "$@"
;;
Linux*)
timeout --foreground "$t" "$@"
;;
Darwin*)
perl -e 'alarm shift; exec @ARGV' "$t" "$@"
;;
*)
timeout --foreground "$t" "$@"
;;
esac
}
log() {
printf "%b[${OS:-dev-projects}]%b %b\n" "$GREEN" "$NC" "$*" >&2
}
error() {
printf "%b[ERROR]%b %b\n" "$RED" "$NC" "$*" >&2
exit 1
}
skip() {
printf "%b[SKIP]%b %b\n" "$YELLOW" "$NC" "$*" >&2
exit 0
}
parse_run_args() {
SELECT_OS=""
SELECT_TEST=""
while [ $# -gt 0 ]; do
case "$1" in
-o|--os)
[ $# -gt 1 ] || { echo "Missing value for $1" >&2; return 1; }
SELECT_OS="$2"
shift 2
;;
-t|--test)
[ $# -gt 1 ] || { echo "Missing value for $1" >&2; return 1; }
v="$2"; v="${v%.sh}"; v="${v##*/}"
SELECT_TEST="$v"
shift 2
;;
*)
break
;;
esac
done
return 0
}
should_run_os() {
local os="$1"
[ -z "$SELECT_OS" ] && return 0
[ "$os" = "$SELECT_OS" ] && return 0
return 1
}
should_run_test() {
local lang="$1"
[ -z "$SELECT_TEST" ] && return 0
[ "$lang" = "$SELECT_TEST" ] && return 0
return 1
}
list_lang_tests() {
local dir
dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/langs"
local f
for f in "$dir"/*.sh; do
[ -f "$f" ] || continue
[ -x "$f" ] || continue
echo "$(basename "$f" .sh)"
done
}
list_scene_tests() {
local dir
dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/scenes"
local f
for f in "$dir"/*.sh; do
[ -f "$f" ] || continue
[ -x "$f" ] || continue
echo "$(basename "$f" .sh)"
done
}