clean rails tmp file,log file and others file.then tar it!


/ Published in: Ruby
Save to your folder(s)



Copy this code and paste it in your HTML
  1. namespace :project do
  2. desc "clean log,upload file, and tar file"
  3. task :build => :environment do |t|
  4. if get_answer "You really want to clean logs?(yes or no)"
  5. puts "cleaning log......."
  6. Rake::Task["log:clear"].invoke
  7. puts "cleaning log.......done"
  8. else
  9. puts "skip cleaning log"
  10. end
  11.  
  12. if get_answer "You really want to clean all temp file?(yes or no)"
  13. puts "cleaning tmp files....."
  14. Rake::Task["tmp:clear"].invoke
  15. puts "cleaning tmp files.....done"
  16. else
  17. puts "skip cleaning log"
  18. end
  19.  
  20. # clean upload files
  21. if get_answer "You really want to clean all uploaded files?(yes or no)"
  22. puts "cleaning uploaded files....."
  23. path = File.join(RAILS_ROOT,"public","upload")
  24. puts path
  25. require 'find'
  26. Find.find(path) do |path|
  27. File.delete(path) if File.file? path
  28. end
  29. puts "cleaning uploaded files.....done"
  30. os =RUBY_PLATFORM
  31. if os.include?("mswin32")
  32. puts " tar doesn't support win32 platform"
  33. exit
  34. else
  35. puts "tar files......."
  36. filename = "#{File.basename(RAILS_ROOT)}.#{Time.now.to_i}.tar.gz"
  37. `cd #{RAILS_ROOT} && cd ../ && tar -zcvf ./#{filename} #{File.basename(RAILS_ROOT)}/`
  38. puts "tar files.......done"
  39. end
  40. else
  41. puts "skip clean uploaded files"
  42. end
  43.  
  44. end
  45.  
  46. def get_answer word
  47. answer='no'
  48. while true
  49. print word
  50. answer = STDIN.gets.chomp!
  51. break if %w[yes no].include? answer
  52. end
  53. if answer=="yes"
  54. true
  55. else
  56. false
  57. end
  58.  
  59. end
  60. end

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.