2023-05-02 12:27:57 +02:00
|
|
|
# The version of the format of .rake_tasks. If the output of _rake_generate
|
|
|
|
# changes, incrementing this number will force it to regenerate
|
|
|
|
_rake_tasks_version=2
|
|
|
|
|
2014-02-13 18:27:00 +01:00
|
|
|
_rake_does_task_list_need_generating () {
|
2023-05-02 12:27:57 +02:00
|
|
|
_rake_tasks_missing || _rake_tasks_version_changed || _rakefile_has_changes || { _is_rails_app && _tasks_changed }
|
|
|
|
}
|
|
|
|
|
|
|
|
_rake_tasks_missing () {
|
|
|
|
[[ ! -f .rake_tasks ]]
|
|
|
|
}
|
|
|
|
|
|
|
|
_rake_tasks_version_changed () {
|
|
|
|
local -a file_version
|
|
|
|
file_version=`head -n 1 .rake_tasks | sed "s/^version\://"`
|
|
|
|
|
|
|
|
if ! [[ $file_version =~ '^[0-9]*$' ]]; then
|
|
|
|
return true
|
|
|
|
fi
|
|
|
|
|
|
|
|
[[ $file_version -ne $_rake_tasks_version ]]
|
|
|
|
}
|
|
|
|
|
|
|
|
_rakefile_has_changes () {
|
|
|
|
[[ Rakefile -nt .rake_tasks ]]
|
2016-08-13 20:47:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_is_rails_app () {
|
2016-08-21 19:51:08 +02:00
|
|
|
[[ -e "bin/rails" ]] || [[ -e "script/rails" ]]
|
2016-08-13 20:47:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_tasks_changed () {
|
2016-08-29 08:37:09 +02:00
|
|
|
local -a files
|
|
|
|
files=(lib/tasks lib/tasks/**/*(N))
|
2016-08-21 19:50:44 +02:00
|
|
|
|
2016-08-29 08:37:09 +02:00
|
|
|
for file in $files; do
|
|
|
|
if [[ "$file" -nt .rake_tasks ]]; then
|
2016-08-21 19:50:44 +02:00
|
|
|
return 0
|
|
|
|
fi
|
2016-08-13 20:47:43 +02:00
|
|
|
done
|
2016-08-21 19:50:44 +02:00
|
|
|
|
|
|
|
return 1
|
2014-02-13 18:27:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
_rake_generate () {
|
2023-12-20 20:52:27 +01:00
|
|
|
local rake_tasks_content="version:$_rake_tasks_version\n"
|
|
|
|
rake_tasks_content+=$(rake --silent --tasks --all \
|
2023-05-02 12:27:57 +02:00
|
|
|
| sed "s/^rake //" | sed "s/\:/\\\:/g" \
|
|
|
|
| sed "s/\[[^]]*\]//g" \
|
|
|
|
| sed "s/ *# /\:/" \
|
2023-12-20 20:52:27 +01:00
|
|
|
| sed "s/\:$//")
|
|
|
|
|
|
|
|
local rake_tasks_file="$(mktemp -t .rake_tasks.XXXXXX)"
|
|
|
|
echo $rake_tasks_content > $rake_tasks_file
|
|
|
|
|
|
|
|
mv $rake_tasks_file .rake_tasks
|
2014-02-13 18:27:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
_rake () {
|
2016-08-21 19:51:08 +02:00
|
|
|
if [[ -f Rakefile ]]; then
|
2014-02-13 18:27:00 +01:00
|
|
|
if _rake_does_task_list_need_generating; then
|
2016-08-21 19:51:08 +02:00
|
|
|
echo "\nGenerating .rake_tasks..." >&2
|
2014-02-13 18:27:00 +01:00
|
|
|
_rake_generate
|
|
|
|
fi
|
2023-05-02 12:27:57 +02:00
|
|
|
local -a rake_options
|
|
|
|
rake_options=("${(@f)$(cat .rake_tasks)}")
|
|
|
|
shift rake_options
|
|
|
|
_describe 'rake tasks' rake_options
|
2014-02-13 18:27:00 +01:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
compdef _rake rake
|
2016-08-21 19:51:08 +02:00
|
|
|
|
|
|
|
rake_refresh () {
|
|
|
|
[[ -f .rake_tasks ]] && rm -f .rake_tasks
|
|
|
|
|
|
|
|
echo "Generating .rake_tasks..." >&2
|
|
|
|
_rake_generate
|
|
|
|
cat .rake_tasks
|
|
|
|
}
|