-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrake_tab_completion_profile
More file actions
executable file
·156 lines (120 loc) · 3.32 KB
/
rake_tab_completion_profile
File metadata and controls
executable file
·156 lines (120 loc) · 3.32 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
146
147
148
149
150
151
152
153
154
155
# ----------------------
# Rake Tab Completion
# ----------------------
if ! command -v rake &>/dev/null; then
# If rake isn't present, don't try to enable completion.
return 0 2>/dev/null || exit 0
fi
if ! command -v md5sum &>/dev/null; then
# If md5sum isn't present, don't try to enable completion.
return 0 2>/dev/null || exit 0
fi
__RAKE_STAT_STYLE=""
if stat --version &>/dev/null; then
__RAKE_STAT_STYLE="gnu"
else
__RAKE_STAT_STYLE="bsd"
fi
# Cache validity window (in seconds): 8 hours
__RAKE_CACHE_MAX_AGE=28800 # 8 * 60 * 60
__RAKE_ROOT=""
_rake_cache_path() {
# Reuse previously found root if still under it
if [[ -n "$__RAKE_ROOT" && "$PWD" == "$__RAKE_ROOT"* ]]; then
printf '%s\n' "$__RAKE_ROOT"
return 0
fi
local dir="$PWD"
while [[ "$dir" != "/" ]]; do
if [[ -f "$dir/Rakefile" ]]; then
__RAKE_ROOT="$dir"
printf '%s\n' "$__RAKE_ROOT"
return 0
fi
dir="$(dirname "$dir")"
done
# No Rakefile found; clear cache so we don't reuse stale
__RAKE_ROOT=""
return 1
}
__rake_mtime() {
local f="$1"
[[ -e "$f" ]] || { echo 0; return 1; }
if [[ "$__RAKE_STAT_STYLE" == "gnu" ]]; then
stat -c %Y "$f"
else
stat -f %m "$f"
fi
}
_rake_task_cache_store() {
local root="$1"
local tcache="$root/.rake_t_cache"
( cd "$root" && rake --tasks --silent ) > "${tcache}.tmp" && mv "${tcache}.tmp" "$tcache"
}
_rake_md5_cache_store() {
local root="$1"
local mcache="$root/.rake_md5_cache"
local files=()
[[ -f "$root/Gemfile" ]] && files+=("$root/Gemfile")
[[ -f "$root/Rakefile" ]] && files+=("$root/Rakefile")
if [[ -d "$root/rakelib" ]]; then
while IFS= read -r f; do
files+=("$f")
done < <(find "$root/rakelib" -type f 2>/dev/null)
fi
if ((${#files[@]} == 0)); then
: > "$mcache"
return 0
fi
md5sum "${files[@]}" > "${mcache}.tmp" && mv "${mcache}.tmp" "$mcache"
}
_rake_rebuild_cache() {
local root="$1"
_rake_task_cache_store "$root"
_rake_md5_cache_store "$root"
}
_validate_rake_cache() {
local root
root="$(_rake_cache_path)" || return 1
local tcache="$root/.rake_t_cache"
local mcache="$root/.rake_md5_cache"
if [[ ! -f "$tcache" || ! -f "$mcache" ]]; then
_rake_rebuild_cache "$root"
return
fi
local now
now=$(date +%s)
local t_mtime m_mtime
t_mtime=$(__rake_mtime "$tcache")
m_mtime=$(__rake_mtime "$mcache")
if (( t_mtime > 0 && m_mtime > 0 )); then
local t_age=$(( now - t_mtime ))
local m_age=$(( now - m_mtime ))
if (( t_age <= __RAKE_CACHE_MAX_AGE && m_age <= __RAKE_CACHE_MAX_AGE )); then
return
fi
fi
if ! md5sum --check "$mcache" --status 2>/dev/null; then
_rake_rebuild_cache "$root"
fi
}
rake_tasks_refresh() {
local root
root="$(_rake_cache_path)" || { echo "No Rakefile found in this directory or parents."; return 1; }
echo "Rebuilding rake task cache for: $root"
_rake_rebuild_cache "$root"
echo "Done."
}
export COMP_WORDBREAKS=${COMP_WORDBREAKS/\:/}
_rakecomplete() {
local root
root="$(_rake_cache_path)" || return 0 # No Rakefile, no completion
_validate_rake_cache
local tcache="$root/.rake_t_cache"
[[ -f "$tcache" ]] || return 0
local tasks
tasks=$(awk '{print $2}' "$tcache")
COMPREPLY=($(compgen -W "$tasks" -- "${COMP_WORDS[COMP_CWORD]}"))
return 0
}
complete -o default -o nospace -F _rakecomplete rake