assets.rake 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. require "fileutils"
  2. namespace :assets do
  3. def ruby_rake_task(task, fork = true)
  4. env = ENV['RAILS_ENV'] || 'production'
  5. groups = ENV['RAILS_GROUPS'] || 'assets'
  6. args = [$0, task,"RAILS_ENV=#{env}","RAILS_GROUPS=#{groups}"]
  7. args << "--trace" if Rake.application.options.trace
  8. if $0 =~ /rake\.bat\Z/i
  9. Kernel.exec $0, *args
  10. else
  11. fork ? ruby(*args) : Kernel.exec(FileUtils::RUBY, *args)
  12. end
  13. end
  14. # We are currently running with no explicit bundler group
  15. # and/or no explicit environment - we have to reinvoke rake to
  16. # execute this task.
  17. def invoke_or_reboot_rake_task(task)
  18. if ENV['RAILS_GROUPS'].to_s.empty? || ENV['RAILS_ENV'].to_s.empty?
  19. ruby_rake_task task
  20. else
  21. Rake::Task[task].invoke
  22. end
  23. end
  24. desc "Compile all the assets named in config.assets.precompile"
  25. task :precompile do
  26. invoke_or_reboot_rake_task "assets:precompile:all"
  27. end
  28. namespace :precompile do
  29. def internal_precompile(digest=nil)
  30. unless Rails.application.config.assets.enabled
  31. warn "Cannot precompile assets if sprockets is disabled. Please set config.assets.enabled to true"
  32. exit
  33. end
  34. # Ensure that action view is loaded and the appropriate
  35. # sprockets hooks get executed
  36. _ = ActionView::Base
  37. config = Rails.application.config
  38. config.assets.compile = true
  39. config.assets.digest = digest unless digest.nil?
  40. config.assets.digests = {}
  41. env = Rails.application.assets
  42. target = File.join(Rails.public_path, config.assets.prefix)
  43. compiler = Sprockets::StaticCompiler.new(env,
  44. target,
  45. config.assets.precompile,
  46. :manifest_path => config.assets.manifest,
  47. :digest => config.assets.digest,
  48. :manifest => digest.nil?)
  49. compiler.compile
  50. end
  51. task :all do
  52. Rake::Task["assets:precompile:primary"].invoke
  53. # We need to reinvoke in order to run the secondary digestless
  54. # asset compilation run - a fresh Sprockets environment is
  55. # required in order to compile digestless assets as the
  56. # environment has already cached the assets on the primary
  57. # run.
  58. ruby_rake_task("assets:precompile:nondigest", false) if Rails.application.config.assets.digest
  59. end
  60. task :primary => ["assets:environment", "tmp:cache:clear"] do
  61. internal_precompile
  62. end
  63. task :nondigest => ["assets:environment", "tmp:cache:clear"] do
  64. internal_precompile(false)
  65. end
  66. end
  67. desc "Remove compiled assets"
  68. task :clean do
  69. invoke_or_reboot_rake_task "assets:clean:all"
  70. end
  71. namespace :clean do
  72. task :all => ["assets:environment", "tmp:cache:clear"] do
  73. config = Rails.application.config
  74. public_asset_path = File.join(Rails.public_path, config.assets.prefix)
  75. rm_rf public_asset_path, :secure => true
  76. end
  77. end
  78. task :environment do
  79. if Rails.application.config.assets.initialize_on_precompile
  80. Rake::Task["environment"].invoke
  81. else
  82. Rails.application.initialize!(:assets)
  83. Sprockets::Bootstrap.new(Rails.application).run
  84. end
  85. end
  86. end