Skip to content

Commit

Permalink
Using Rake task for assets, everything gets reloaded every time
Browse files Browse the repository at this point in the history
  • Loading branch information
dnagir committed Sep 2, 2011
1 parent 4096407 commit a4e905e
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 64 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Expand Up @@ -2,4 +2,4 @@ source "http://rubygems.org"

gemspec

gem 'rake', :require => false
gem 'pry', :group => [:development, :test]
95 changes: 92 additions & 3 deletions Gemfile.lock
@@ -1,17 +1,93 @@
PATH
remote: .
specs:
guard-rails-assets (0.0.5)
guard-rails-assets (0.0.6)
guard
rails (>= 3.1.0)
rake

GEM
remote: http://rubygems.org/
specs:
actionmailer (3.1.0)
actionpack (= 3.1.0)
mail (~> 2.3.0)
actionpack (3.1.0)
activemodel (= 3.1.0)
activesupport (= 3.1.0)
builder (~> 3.0.0)
erubis (~> 2.7.0)
i18n (~> 0.6)
rack (~> 1.3.2)
rack-cache (~> 1.0.3)
rack-mount (~> 0.8.2)
rack-test (~> 0.6.1)
sprockets (~> 2.0.0)
activemodel (3.1.0)
activesupport (= 3.1.0)
bcrypt-ruby (~> 3.0.0)
builder (~> 3.0.0)
i18n (~> 0.6)
activerecord (3.1.0)
activemodel (= 3.1.0)
activesupport (= 3.1.0)
arel (~> 2.2.1)
tzinfo (~> 0.3.29)
activeresource (3.1.0)
activemodel (= 3.1.0)
activesupport (= 3.1.0)
activesupport (3.1.0)
multi_json (~> 1.0)
arel (2.2.1)
bcrypt-ruby (3.0.0)
builder (3.0.0)
coderay (0.9.8)
diff-lcs (1.1.2)
guard (0.5.1)
erubis (2.7.0)
guard (0.6.3)
thor (~> 0.14.6)
hike (1.2.1)
i18n (0.6.0)
mail (2.3.0)
i18n (>= 0.4.0)
mime-types (~> 1.16)
treetop (~> 1.4.8)
method_source (0.6.0)
ruby_parser (>= 2.0.5)
mime-types (1.16)
multi_json (1.0.3)
polyglot (0.3.2)
pry (0.9.3)
coderay (>= 0.9.8)
method_source (>= 0.6.0)
ruby_parser (>= 2.0.5)
slop (~> 1.9.0)
rack (1.3.2)
rack-cache (1.0.3)
rack (>= 0.4)
rack-mount (0.8.3)
rack (>= 1.0.0)
rack-ssl (1.3.2)
rack
rack-test (0.6.1)
rack (>= 1.0)
rails (3.1.0)
actionmailer (= 3.1.0)
actionpack (= 3.1.0)
activerecord (= 3.1.0)
activeresource (= 3.1.0)
activesupport (= 3.1.0)
bundler (~> 1.0)
railties (= 3.1.0)
railties (3.1.0)
actionpack (= 3.1.0)
activesupport (= 3.1.0)
rack-ssl (~> 1.3.2)
rake (>= 0.8.7)
rdoc (~> 3.4)
thor (~> 0.14.6)
rake (0.9.2)
rdoc (3.9.4)
rspec (2.6.0)
rspec-core (~> 2.6.0)
rspec-expectations (~> 2.6.0)
Expand All @@ -20,12 +96,25 @@ GEM
rspec-expectations (2.6.0)
diff-lcs (~> 1.1.2)
rspec-mocks (2.6.0)
ruby_parser (2.1.0)
sexp_processor (~> 3.0)
sexp_processor (3.0.5)
slop (1.9.1)
sprockets (2.0.0)
hike (~> 1.2)
rack (~> 1.0)
tilt (!= 1.3.0, ~> 1.1)
thor (0.14.6)
tilt (1.3.3)
treetop (1.4.10)
polyglot
polyglot (>= 0.3.1)
tzinfo (0.3.29)

PLATFORMS
ruby

DEPENDENCIES
guard-rails-assets!
rake
pry
rspec
1 change: 1 addition & 0 deletions guard-rails-assets.gemspec
Expand Up @@ -15,6 +15,7 @@ Gem::Specification.new do |s|

s.add_dependency 'guard'
s.add_dependency 'rake'
s.add_dependency 'rails', '>= 3.1.0'
s.add_development_dependency 'rspec'

s.files = `git ls-files`.split("\n")
Expand Down
80 changes: 20 additions & 60 deletions lib/guard/rails-assets/rails_runner.rb
Expand Up @@ -2,80 +2,40 @@
module Guard

class RailsAssets::RailsRunner
@@rails_booted = false # Only one rails app is allowed, so make it a class var

def initialize(options)

end

# Methods to run the asset pipeline
# Taken from - https://github.com/rails/rails/blob/master/actionpack/lib/sprockets/assets.rake
module AssetPipeline
extend self
extend Rake::DSL

def clean
assets = ::Rails.application.config.assets
public_asset_path = File.join(::Rails.public_path, assets.prefix)
rm_rf public_asset_path, :secure => true
end

def precompile
Sprockets::Helpers::RailsHelper
::ActionView::Base

config = ::Rails.application.config
env = ::Rails.application.assets
target = ::Rails.root.join("public#{config.assets.prefix}")

config.assets.precompile.each do |path|
env.each_logical_path do |logical_path|
if path.is_a?(::Regexp)
next unless path.match(logical_path)
else
next unless ::File.fnmatch(path.to_s, logical_path)
end

if asset = env.find_asset(logical_path)
filename = target.join(asset.pathname.basename)
mkdir_p filename.dirname
asset.write_to(filename)
asset.write_to("#{filename}.gz") if filename.to_s =~ /\.(css|js)$/
end
end
end
end
end

# See as a reference https://github.com/rails/rails/blob/master/actionpack/lib/sprockets/assets.rake
def boot_rails
@rails_booted ||= begin
require "#{Dir.pwd}/config/environment.rb"
true
end
return if @@rails_booted
puts "------------BOOTING RAILS"
require "#{Dir.pwd}/config/environment.rb"
app = ::Rails.application
puts "--- CACHE=#{app.assets.cache}"
app.assets.cache = nil
app.load_tasks
@@rails_booted = true
puts "-- BOOTED after?=#{@@rails_booted}"
end

def run_compiler
begin
@failed = false
AssetPipeline.clean
AssetPipeline.precompile
rescue => e
puts "An error occurred compiling assets: #{e}"
@failed = true
end
end

# Runs the asset pipeline compiler.
#
# @return [ Boolean ] Whether the compilation was successful or not
def compile_assets
boot_rails
run_compiler

!failed?
end

def failed?
@failed
return false unless @@rails_booted
begin
Rake::Task['assets:clean'].execute
Rake::Task['assets:precompile'].execute
true
rescue => e
puts "An error occurred compiling assets: #{e}"
false
end
end
end
end

0 comments on commit a4e905e

Please sign in to comment.