-
-
Notifications
You must be signed in to change notification settings - Fork 941
Expand file tree
/
Copy pathjruby
More file actions
executable file
·145 lines (128 loc) · 3.64 KB
/
Copy pathjruby
File metadata and controls
executable file
·145 lines (128 loc) · 3.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/bin/sh
# -----------------------------------------------------------------------------
# jruby - Relaunches as jruby.sh from the same path.
#
# See jruby.sh for more information.
# -----------------------------------------------------------------------------
# Pure shell dirname
dir_name() {
local filename="$1" trail=
case $filename in
*/*[!/]*)
trail=${filename##*[!/]}
filename=${filename%%"$trail"}
REPLY=${filename%/*}
;;
*[!/]*)
trail=${filename##*[!/]}
REPLY="."
;;
*)
REPLY="/"
;;
esac
}
# Pure shell basename
base_name() {
local filename="$1" trail=
case $filename in
*/*[!/]*)
trail=${filename##*[!/]}
filename=${filename%%"$trail"}
REPLY=${filename##*/}
;;
*[!/]*)
trail=${filename##*[!/]}
REPLY=${filename%%"$trail"}
;;
*)
REPLY="/"
;;
esac
}
# Determine whether path is absolute and contains no relative segments or symlinks
path_is_canonical() {
local path=
for path; do
case $path in
([!/]*) return 1 ;;
(./*|../*) return 1 ;;
(*/.|*/..) return 1 ;;
(*/./*|*/../*) return 1 ;;
esac
while [ "$path" ]; do
[ -h "$path" ] && return 1
path="${path%/*}"
done
done
return 0
}
# Resolve directory to its canonical value
resolve_dir() {
# Some shells (dash, ksh) resolve relative paths by default before cd'ing, i.e.
# cd /foo/bar/../baz = cd /foo/baz
# This is fine unless bar is a symlink, in which case the second form is
# invalid. Passing -P to cd fixes this behaviour.
REPLY="$(cd -P -- "$1" && pwd)"
}
# Resolve symlink until it's not a symlink
resolve_file() {
local current="$1" target=
while [ -h "$current" ]; do
target="$(readlink "$current")" || return
case $target in
(/*) current="$target" ;;
# handle relative symlinks
(*) dir_name "$current"; current="$REPLY/$target" ;;
esac
done
REPLY="$current"
}
# Resolve path to its canonical value
resolve() {
local target="$1" base=
REPLY=
# Verify target actually exists (and isn't too deep in symlinks)
if ! [ -e "$target" ]; then
echo >&2 "Error: No such file or directory: $target"
return 1
fi
# Realpath is way faster than repeatedly calling readlink, so use it if possible
if command -v realpath >/dev/null; then
REPLY="$(realpath "$target")" && return
fi
# Take shortcut for directories
if [ -d "$target" ]; then
resolve_dir "$target" && return
fi
# Ensure $target is not a symlink
resolve_file "$target" || return
target="$REPLY"
# Resolve parent directory if it's not absolute
if ! path_is_canonical "$target"; then
dir_name "$target"
resolve_dir "$REPLY" || return
base="$REPLY"
base_name "$target"
target="$base/$REPLY"
fi
REPLY="$target"
}
# ----- Find JRuby launcher based on this executable's path -------------------
# Get the absolute path of the executable
if [ "${BASH-}" ]; then
# shellcheck disable=2128,3028
script_src=${BASH_SOURCE-}
else
script_src=$0
fi
dir_name "$script_src"
base_dir=$(cd -P -- "$REPLY" >/dev/null && pwd -P)
base_name "$script_src"
resolve "$base_dir/$REPLY"
self_path=$REPLY
# Get the directory of the target script
dir_name "$self_path"
jruby_bin=$REPLY
# Run the launcher (in the current shell as an optimization)
. "$jruby_bin/jruby.sh"