rake-fast.plugin.zsh 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. # The version of the format of .rake_tasks. If the output of _rake_generate
  2. # changes, incrementing this number will force it to regenerate
  3. _rake_tasks_version=2
  4. _rake_does_task_list_need_generating () {
  5. _rake_tasks_missing || _rake_tasks_version_changed || _rakefile_has_changes || { _is_rails_app && _tasks_changed }
  6. }
  7. _rake_tasks_missing () {
  8. [[ ! -f .rake_tasks ]]
  9. }
  10. _rake_tasks_version_changed () {
  11. local -a file_version
  12. file_version=`head -n 1 .rake_tasks | sed "s/^version\://"`
  13. if ! [[ $file_version =~ '^[0-9]*$' ]]; then
  14. return true
  15. fi
  16. [[ $file_version -ne $_rake_tasks_version ]]
  17. }
  18. _rakefile_has_changes () {
  19. [[ Rakefile -nt .rake_tasks ]]
  20. }
  21. _is_rails_app () {
  22. [[ -e "bin/rails" ]] || [[ -e "script/rails" ]]
  23. }
  24. _tasks_changed () {
  25. local -a files
  26. files=(lib/tasks lib/tasks/**/*(N))
  27. for file in $files; do
  28. if [[ "$file" -nt .rake_tasks ]]; then
  29. return 0
  30. fi
  31. done
  32. return 1
  33. }
  34. _rake_generate () {
  35. local rake_tasks_content="version:$_rake_tasks_version\n"
  36. rake_tasks_content+=$(rake --silent --tasks --all \
  37. | sed "s/^rake //" | sed "s/\:/\\\:/g" \
  38. | sed "s/\[[^]]*\]//g" \
  39. | sed "s/ *# /\:/" \
  40. | sed "s/\:$//")
  41. local rake_tasks_file="$(mktemp -t .rake_tasks.XXXXXX)"
  42. echo $rake_tasks_content > $rake_tasks_file
  43. mv $rake_tasks_file .rake_tasks
  44. }
  45. _rake () {
  46. if [[ -f Rakefile ]]; then
  47. if _rake_does_task_list_need_generating; then
  48. echo "\nGenerating .rake_tasks..." >&2
  49. _rake_generate
  50. fi
  51. local -a rake_options
  52. rake_options=("${(@f)$(cat .rake_tasks)}")
  53. shift rake_options
  54. _describe 'rake tasks' rake_options
  55. fi
  56. }
  57. compdef _rake rake
  58. rake_refresh () {
  59. [[ -f .rake_tasks ]] && rm -f .rake_tasks
  60. echo "Generating .rake_tasks..." >&2
  61. _rake_generate
  62. cat .rake_tasks
  63. }