monitor 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #!/usr/bin/env bash
  2. original_ruby_strings=$rvm_ruby_strings
  3. original_ruby_string=$rvm_ruby_string
  4. source "$rvm_scripts_path/base"
  5. rvm_monitor_sleep="${rvm_monitor_sleep:-2}"
  6. array_length()
  7. {
  8. eval "echo \${#$1[*]}"
  9. }
  10. array_push()
  11. {
  12. array=$1
  13. shift
  14. eval "$array+=( \"\$@\" )"
  15. }
  16. timestamp()
  17. {
  18. __rvm_statf "%Y" "%m" $1
  19. }
  20. push_if_timestamp_changed()
  21. {
  22. \typeset file file_timestamp time
  23. file=$1
  24. file_timestamp=$(timestamp "$file")
  25. eval "time=\$${framework}_timestamp"
  26. if [[ "$file_timestamp" -gt $time ]]
  27. then
  28. array_push "changed_${framework}_files" $file
  29. fi
  30. }
  31. update_timestamp()
  32. {
  33. if [[ -d "${1}/" ]]
  34. then
  35. \command \touch "$rvm_path/${$}_${1}_timestamp"
  36. eval "${1}_timestamp=\$(timestamp \"$rvm_path/${$}_${1}_timestamp\")"
  37. fi
  38. }
  39. rvm_warn "rvm monitor is deprecated, take a look on autotest, guard, watchr or ruby-inotify"
  40. update_timestamp "test"
  41. update_timestamp "spec"
  42. while : ; do
  43. changed_test_files=() ; changed_spec_files=() ; changed_code_files=()
  44. for file in lib/**/*.rb lib/*.rb app/**/*.rb app/*.rb ; do
  45. if [[ -f "$file" ]] ; then push_if_timestamp_changed $file "code" ; fi
  46. done
  47. for framework in test spec ; do
  48. if [[ -d "$framework/" ]] ; then
  49. for file in ${framework}/**/*_${framework}.rb ${framework}/*_${framework}.rb ; do
  50. if [[ -f "$file" ]] ; then
  51. push_if_timestamp_changed $file $framework
  52. fi
  53. done
  54. if [[ "$(array_length "changed_${framework}_files")" -gt 0 ]] ; then
  55. rvm_ruby_strings=$original_ruby_strings
  56. rvm_ruby_string=$original_ruby_string
  57. if [[ "spec" == "$framework" ]] ; then
  58. rvm_action="spec"
  59. rvm_ruby_args="spec/spec_helper.rb ${changed_spec_files[*]}"
  60. "$rvm_scripts_path/set" $rvm_action $rvm_ruby_args
  61. elif [[ "test" == "$framework" ]] ; then
  62. rvm_action="ruby"
  63. rvm_ruby_args=" -r$(echo "${changed_test_files[*]}" | __rvm_sed 's/ / -r/g') test/test_helper.rb"
  64. "$rvm_scripts_path/set" $rvm_action $rvm_ruby_args
  65. fi
  66. update=1
  67. fi
  68. if [[ "$(array_length "changed_code_files")" -gt 0 ]] ; then
  69. rvm_ruby_strings=$original_ruby_strings
  70. rvm_ruby_string=$original_ruby_string
  71. if [[ "spec" == "$framework" ]] ; then
  72. rvm_action="spec"
  73. rvm_ruby_args="spec/"
  74. "$rvm_scripts_path/set" $rvm_action $rvm_ruby_args
  75. elif [[ "test" == "$framework" ]] ; then
  76. rvm_action="rake"
  77. rvm_ruby_args="test"
  78. "$rvm_scripts_path/set" "$rvm_action" $rvm_ruby_args
  79. fi
  80. update=1
  81. fi
  82. fi
  83. if [[ "$update" -eq 1 ]] ; then
  84. update_timestamp $framework
  85. fi
  86. done
  87. unset update changed_test_files changed_spec_files
  88. sleep $rvm_monitor_sleep
  89. done