Browse Source

x: be more flexible when finding run.sh

Signed-off-by: Alejandro Mery <amery@jpi.io>
master
Alejandro Mery 4 years ago
parent
commit
8f4771e208
1 changed files with 55 additions and 19 deletions
  1. +55
    -19
      files/bin/x

+ 55
- 19
files/bin/x

@ -2,28 +2,64 @@
set -eu set -eu
# find root of the "workspace"
find_repo_workspace_root() {
if [ -d "$1/.repo" ]; then
echo "$1"
elif [ "${1:-/}" != / ]; then
find_repo_workspace_root "${1%/*}"
fi
brute_find() {
local op="$1" cue="$2" dir="$3"
while true; do
if [ $op "$dir/$cue" ]; then
echo "$dir"
elif [ / != "${dir:-/}" ]; then
dir="${dir%/*}"
continue
fi
return
done
} }
WS="$(find_repo_workspace_root "$PWD")"
if [ -z "$WS" ]; then
# find root of .git repository
WS="$(git rev-parse --show-superproject-working-tree 2> /dev/null || true)"
[ -d "$WS" ] || WS="$(git rev-parse --show-toplevel 2> /dev/null || true)"
fi
find_run_sh() {
local dir="${1:-$PWD}"
local try_repo=true
local ws=
if [ -d "$WS" ]; then
if [ -x "$WS/run.sh" ]; then
exec "$WS/run.sh" "$@"
fi
fi
while true; do
[ "$PWD" = "$dir" ] || cd "$dir"
# find root of repo workspace
if $try_repo; then
ws="$(brute_find -d .repo "$PWD")"
[ -d "$ws" ] || try_repo=false
fi
# or find root of .git repository
if [ ! -d "$ws" ]; then
ws="$(git rev-parse --show-superproject-working-tree 2> /dev/null || true)"
[ -d "$ws" ] || ws="$(git rev-parse --show-toplevel 2> /dev/null || true)"
fi
if [ ! -d "$ws" ]; then
# no repo? find anything then
dir="$(brute_find -x run.sh "$PWD")"
[ -z "$dir" ] || echo "$dir/run.sh"
elif [ -x "$ws/run.sh" ]; then
# found at repo's root
echo "$ws/run.sh"
elif [ / != "${ws:-/}" ]; then
# check on the parent's then
dir="${ws%/*}"
ws=
continue
fi
return
done
}
if [ "$#" -gt 0 ]; then
run_sh="$(find_run_sh)"
if [ -x "$run_sh" ]; then
exec "$run_sh" "$@"
elif [ "$#" -gt 0 ]; then
exec "$@" exec "$@"
fi fi

Loading…
Cancel
Save