forked from sergaks/y-scripts
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathy-whereis
executable file
·73 lines (63 loc) · 1.66 KB
/
y-whereis
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
#!/usr/bin/env bash
# Search for hybris platform folder based on current working directory
#
# source: https://github.com/sergaks/y-scripts
HYBRIS_PLATFORM_DIR="bin/platform"
function purge_hybris_pathes()
{
for f in "${pathes[@]}"; do
if [[ $f == *$HYBRIS_PLATFORM_DIR ]]; then
hybris_pathes+=("$f")
fi
done
}
function lookup_in_current_dir()
{
CURRENT_DIR=`pwd`
if [[ $CURRENT_DIR == *"platform"* ]]; then
pathes+=(`echo $CURRENT_DIR | sed -n 's/platform.*$/platform/p'`)
fi
}
function lookup_in_subdirectories()
{
while IFS= read -r -d $'\0'; do
pathes+=("$REPLY")
done < <(find . -maxdepth 4 -type d -name "platform" -print0)
}
function lookup_nearby()
{
PARENT=".."
for (( i = 0; i < 4; i++ )); do
while IFS= read -r -d $'\0'; do
pathes+=("$REPLY")
echo $REPLY + '+' + 'bin'
done < <(find $PARENT -maxdepth 4 -type d -name "platform" -print0)
PARENT+="/.."
purge_hybris_pathes
if [ ${#hybris_pathes[@]} -ne 0 ]; then
break
fi
done
}
# search for hybris platform path candidates
pathes=()
lookup_in_current_dir
lookup_in_subdirectories
# filter only pathes with hybris platform directory available
hybris_pathes=()
purge_hybris_pathes
# if nothing found search in adjacent folders as last resort
if [ ${#hybris_pathes[@]} -eq 0 ]; then
lookup_nearby
fi
if [ ${#hybris_pathes[@]} -eq 0 ]; then
>&2 echo "hybris isn't detected anywhere nearby current folder, please go to hybris folder"
exit 1
elif [ ${#hybris_pathes[@]} -eq 1 ]; then
echo "${hybris_pathes[0]}"
exit 0
else
>&2 echo "multiple hybris projects found, please go to correct one and execute the command again"
>&2 printf '%s\n' "${hybris_pathes[@]}"
exit 1
fi