-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrake_tab_completion_profile_time_analysis
executable file
·106 lines (90 loc) · 3.66 KB
/
rake_tab_completion_profile_time_analysis
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
# ----------------------
# Time Analysis
# ----------------------
# Function to measure the runtime of another function in microseconds
measure_runtime() {
local start_time end_time elapsed_time
# Start measuring time
# Note: MacOS date does not measure beyoned seconds, install gdate via coreutils with brew.
start_time=$([ "$(uname)" = "Darwin" ] && gdate +%s.%3N || date +%s.%3N)
# Execute the provided function or command
"$@"
# Calculate the elapsed time
end_time=$([ "$(uname)" = "Darwin" ] && gdate +%s.%3N || date +%s.%3N)
elapsed_time=$(echo "scale=63; $end_time - $start_time" | bc)
# Print the elapsed time
printf "\nElapsed time: %.6f seconds, for: %s\n" "$elapsed_time" "$1"
}
# ----------------------
# Rake Tab Completion
# ----------------------
# Validates the user's terminal is capable of running the script.
function validate_terminal_session() {
# Check if 'rake' command is available
if ! command -v rake &>/dev/null; then
echo "Error: 'rake' command not found. Make sure rake is installed and available in the PATH."
return 1
fi
# Check if 'md5sum' command is available
if ! command -v md5sum &>/dev/null; then
echo "Error: 'md5sum' command not found. Make sure md5sum is installed and available in the PATH."
return 1
fi
# Check 'date/gdate' command is available
if "$(uname)" = "Darwin"; then
if ! command -v gdate &>/dev/null; then
echo "Error: 'gdate' command not found. Make sure gdate is installed and available in the PATH."
return 1
fi
else
if ! command -v date &>/dev/null; then
echo "Error: 'date' command not found. Make sure date is installed and available in the PATH."
return 1
fi
fi
return 0;
}
# Finds the nearest Rakefile. In or above, not lateral or down.
function _rake_cache_path() {
local current_dir="$PWD"
# Search for Rakefile in the current directory and its parent directories.
while [ "$current_dir" != "/" ]; do
if [ -e "$current_dir/Rakefile" ]; then
echo "${current_dir}"
return 0
fi
current_dir="$(dirname "$current_dir")"
done
echo "No Rakefile found"
return 1
}
# Runs a silent rake task list and stores the output in a cache file.
function rake_task_cache_store() {
rake --tasks --silent > "$(_rake_cache_path)/.rake_t_cache"
}
# Runs a md5sum on the target Gemfile, Rakefile, and any extra rake filees in a rakelib directory. These values are stored adjacent to the rake tasks cache file.
function rake_md5_cache_store() {
md5sum $(_rake_cache_path)/{Gemfile,Rakefile} $(find $(_rake_cache_path)/rakelib -type f 2>/dev/null) > "$(_rake_cache_path)/.rake_md5_cache"
}
# Validates md5sums. On failure, regenerate the task and md5 cache files.
function validate_cache() {
measure_runtime validate_terminal_session
# Build cache if missing.
if [ ! -e "$(_rake_cache_path)/.rake_t_cache" ]; then
measure_runtime rake_task_cache_store
measure_runtime rake_md5_cache_store
else
md5sum --check "$(_rake_cache_path)/.rake_md5_cache" --status 2>/dev/null || rake_task_cache_store && rake_md5_cache_store
fi
}
# Modify the tab completion behavior.
export COMP_WORDBREAKS=${COMP_WORDBREAKS/\:/}
# Main tab completion modification function.
function _rakecomplete() {
# Validate/ Build cache files.
measure_runtime validate_cache
local tasks=$(awk '{print $2}' "$(_rake_cache_path)/.rake_t_cache")
COMPREPLY=($(compgen -W "${tasks}" -- "${COMP_WORDS[COMP_CWORD]}"))
return 0
}
complete -o default -o nospace -F _rakecomplete rake