-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgradle-bash-tools.sh
173 lines (145 loc) · 5.75 KB
/
gradle-bash-tools.sh
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
GRADLE_DEFAULT_FLAGS="--daemon"
_upsearch() # adapted from https://gist.github.com/lsiden/1577473
{
local file_to_search=$1
local curdir=`pwd`
while [[ "`pwd`" != '/' ]]; do
if [ -f "${file_to_search}" ]; then
pwd
cd "${curdir}"
return 0
fi
cd ..
done
cd "${curdir}"
return 1
}
_find_gradle_projectdir()
{
_upsearch "build.gradle"
}
_find_gradle_rootprojectdir()
{
_upsearch "settings.gradle"
}
_run_gradle()
{
local gradle_root_project_dir=`_find_gradle_rootprojectdir`
local gradle_project_dir=`_find_gradle_projectdir`
if [ -z "$gradle_project_dir" ]; then
echo "Gradle project directory not found" 1>&2
return 1
fi
# Use gradle wrapper if available
local gradle_command="${gradle_root_project_dir}/gradlew"
if [ ! -f "${gradle_command}" ]; then
gradle_command="${gradle_project_dir}/gradlew"
if [ ! -f "${gradle_command}" ]; then
gradle_command="gradle"
fi
fi
"${gradle_command}" -p "${gradle_project_dir}" "$@"
}
alias gr="_run_gradle ${GRADLE_DEFAULT_FLAGS}"
_gradle_complete()
{
local singledash='-? -h -a -b -c -D -d -g -I -i -m -P -p -q -S -s -t -u -v -x'
local doubledash='--help --no-rebuild --all --build-file --settings-file --console --continue --configure-on-demand --system-prop --debug --gradle-user-home --gui --init-script --info --dry-run --offline --project-prop --project-dir --parallel --max-workers --profile --project-cache-dir --quiet --recompile-scripts --refresh-dependencies --rerun-tasks --full-stacktrace --stacktrace --continuous --no-search-upwards --version --exclude-task --no-color --parallel-threads --daemon --foreground --no-daemon --stop'
# current word
local cur
_get_comp_words_by_ref -n : cur
case "${cur}" in
--*)
COMPREPLY=( $(compgen -S " " -W "${doubledash}" -- ${cur}) )
;;
-*)
COMPREPLY=( $(compgen -S " " -W "${singledash}" -- ${cur}) )
;;
*)
local gradle_root_project_dir=`_find_gradle_rootprojectdir`
if [ -z "${gradle_root_project_dir}" ]; then
# fallback to build.gradle
gradle_root_project_dir=`_find_gradle_projectdir`
if [ -z "${gradle_root_project_dir}" ]; then
return 1
fi
fi
local folderSha
if builtin command -v md5 > /dev/null; then
folderSha=$(md5 -q -s "${gradle_root_project_dir}")
elif builtin command -v md5sum > /dev/null ; then
folderSha=$(printf '%s' "${gradle_root_project_dir}" | md5sum | awk '{print $1}')
else
echo "Neither md5 nor md5sum were found in the PATH" 1>&2
return 1
fi
if [ ! -f "${HOME}/.gradle/bash/${folderSha}/gradle-autocomplete" ]; then
return 1
fi
if [ ! -f "${HOME}/.gradle/bash/${folderSha}/gradle-projects" ]; then
return 1
fi
local completionDir="${HOME}/.gradle/bash/${folderSha}"
local commands=$(cat "${completionDir}/gradle-autocomplete")
# file that maps project path to relative directory
# transform relative directory path to absolute path
local projectmappings=$(cat "${completionDir}/gradle-projects" \
| awk -F '=' "{print \$1\"=${gradle_root_project_dir}/\"\$2}")
# find current project path based on current directory, searching upward
local curdir=`pwd`
local foundmapping
# assumes subtrees comes after root
while [[ "`pwd`" != '/' ]]; do
foundmapping=$(printf '%s' "${projectmappings}" | grep -m 1 -F "`pwd`")
if [ ! -z "${foundmapping}" ]; then
break
fi
cd ..
done
cd "${curdir}"
# path to the project that our current directory corresponds to
local cdprojectpath=$(printf '%s' "$foundmapping" | awk -F '=' '{print $1}')
if [ "${cdprojectpath}" != ":" ]; then
cdprojectpath="${cdprojectpath}:"
fi
# cdprojectpath always ends with ":"
# simulate root project path if current command starts with ":"
case "${cur}" in
:*)
cdprojectpath=":"
# remove leading colons ::::: from cur (if there is more than one)
cur=$(printf '%s' "${cur}" | sed -E 's/^:*([^:]*.*)$/\1/')
esac
# cdprojectpath always ends with ":"
# cur does not start with ":"
# project path that we are selecting by $cur (everything before the last colon)
local currprojectpath=$(printf '%s' "$cur" | grep ':' | sed -E 's/^(.*):[^:]*$/\1/')
# currprojectpath does not start with ":" and never ends with ":"
# currprojectpath can also be empty
# set cur to everything behind last colon in cur
cur="${cur#$currprojectpath}"
cur="${cur#:}" # remove leading ":"
# construct project path based on path of current directory and path selected by cur
local totalpath
if [ "x${currprojectpath}" == "x" ]; then # if $currprojectpath is empty
totalpath="${cdprojectpath}"
else
totalpath="${cdprojectpath}${currprojectpath}:"
fi
# select subtree of commands that begins with totalpath
commands=$(printf '%s' "${commands}" | grep "^${totalpath}")
commands=$(printf '%s' "${commands}" | sed -E "s/^${totalpath}(.*)$/\1/")
# distinguish subprojects and tasks based on colon
local subprojects=$(printf '%s' "${commands}" | grep ':' | awk -F ':' '{print $1}' | uniq)
local tasks=$(printf '%s' "${commands}" | grep -v ':' | awk -F ':' '{print $1}')
declare -a subprojectsCompl
declare -a tasksCompl
local IFS=$'\t\n'
local subprojectsCompl=$(compgen -S ':' -o nospace -W "${subprojects}" -- ${cur})
local tasksCompl=$(compgen -S " " -W "${tasks}" -- ${cur})
COMPREPLY=( ${subprojectsCompl[@]} ${tasksCompl[@]} )
__ltrim_colon_completions "$cur"
;;
esac
}
complete -o nospace -F _gradle_complete gr