Browse code

Added a gradle build tool plugin

* Enhanced gradle plugin to parse tasks from a quick execution of the tool
* Added a duplicated function for gradlew completion that uses gradlew internally. Builds like Gradle itself (built with Gradle) are sensitive to being run with a matching gradlew version.
* Fixed broken caching of gradle task names. Added - and -- argument completions.

Matthew Git McCullough authored on 14/06/2011 at 21:01:28
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,119 @@
0
+#!zsh
1
+##############################################################################
2
+# A descriptive listing of core Gradle commands 
3
+############################################################################
4
+function _gradle_core_commands() {
5
+    local ret=1 state
6
+    _arguments ':subcommand:->subcommand' && ret=0
7
+
8
+    case $state in
9
+      subcommand)
10
+        subcommands=(
11
+          "properties:Display all project properties"
12
+          "tasks:Calculate and display all tasks"
13
+          "dependencies:Calculate and display all dependencies"
14
+          "projects:Discover and display all sub-projects"
15
+          "build:Build the project"
16
+          "help:Display help"
17
+        )
18
+        _describe -t subcommands 'gradle subcommands' subcommands && ret=0
19
+    esac
20
+
21
+    return ret
22
+}
23
+
24
+function _gradle_arguments() {
25
+    _arguments -C \
26
+    '-a[Do not rebuild project dependencies]' \
27
+    '-h[Help]' \
28
+    '-D[System property]' \
29
+    '-d[Log at the debug level]' \
30
+    '--gui[Launches the Gradle GUI app]' \
31
+    '--stop[Stop the Gradle daemon]' \
32
+    '--daemon[Use the Gradle daemon]' \
33
+    '--no-daemon[Do not use the Gradle daemon]' \
34
+    '--no-opt[Do not perform any task optimization]' \
35
+    '-i[Log at the info level]' \
36
+    '-m[Dry run]' \
37
+    '-P[Set a project property]' \
38
+    '--profile[Profile the build time]' \
39
+    '-q[Log at the quiet level (only show errors)]' \
40
+    '-v[Print the Gradle version info]' \
41
+    '-x[Specify a task to be excluded]' \
42
+    '*::command:->command' \
43
+    && return 0
44
+}
45
+
46
+
47
+##############################################################################
48
+# Are we in a directory containing a build.gradle file?
49
+############################################################################
50
+function in_gradle() {
51
+    if [[ -f build.gradle ]]; then
52
+        echo 1
53
+    fi
54
+}
55
+
56
+############################################################################
57
+# Define the stat_cmd command based on platform behavior
58
+##########################################################################
59
+stat -f%m . > /dev/null 2>&1
60
+if [ "$?" = 0 ]; then
61
+	stat_cmd=(stat -f%m)
62
+else
63
+	stat_cmd=(stat -L --format=%Y)
64
+fi
65
+
66
+############################################################################## Examine the build.gradle file to see if its
67
+# timestamp has changed, and if so, regen
68
+# the .gradle_tasks cache file
69
+############################################################################
70
+_gradle_does_task_list_need_generating () {
71
+  if [ ! -f .gradletasknamecache ]; then return 0;
72
+  else
73
+    accurate=$($stat_cmd .gradletasknamecache)
74
+    changed=$($stat_cmd build.gradle)
75
+    return $(expr $accurate '>=' $changed)
76
+  fi
77
+}
78
+
79
+
80
+##############################################################################
81
+# Discover the gradle tasks by running "gradle tasks --all"
82
+############################################################################
83
+_gradle_tasks () {
84
+  if [ in_gradle ]; then
85
+    _gradle_arguments
86
+    if _gradle_does_task_list_need_generating; then
87
+     gradle tasks --all | grep "^[ ]*[a-zA-Z0-9]*\ -\ " | sed "s/ - .*$//" | sed "s/[\ ]*//" > .gradletasknamecache
88
+    fi
89
+    compadd -X "==== Gradle Tasks ====" `cat .gradletasknamecache`
90
+  fi
91
+}
92
+
93
+_gradlew_tasks () {
94
+  if [ in_gradle ]; then
95
+    _gradle_arguments
96
+    if _gradle_does_task_list_need_generating; then
97
+     gradlew tasks --all | grep "^[ ]*[a-zA-Z0-9]*\ -\ " | sed "s/ - .*$//" | sed "s/[\ ]*//" > .gradletasknamecache
98
+    fi
99
+    compadd -X "==== Gradlew Tasks ====" `cat .gradletasknamecache`
100
+  fi
101
+}
102
+
103
+
104
+##############################################################################
105
+# Register the completions against the gradle and gradlew commands
106
+############################################################################
107
+compdef _gradle_tasks gradle
108
+compdef _gradlew_tasks gradlew
109
+
110
+
111
+##############################################################################
112
+# Open questions for future improvements:
113
+# 1) Should 'gradle tasks' use --all or just the regular set?
114
+# 2) Should gradlew use the same approach as gradle?
115
+# 3) Should only the " - " be replaced with a colon so it can work
116
+#     with the richer descriptive method of _arguments?
117
+#     gradle tasks | grep "^[a-zA-Z0-9]*\ -\ " | sed "s/ - /\:/"
118
+#############################################################################