guard-rails-assets 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -3,3 +3,4 @@ pkg/*
3
3
  *.rbc
4
4
  *.swp
5
5
  .bundle
6
+ Gemfile.lock
data/Gemfile CHANGED
@@ -2,4 +2,4 @@ source "http://rubygems.org"
2
2
 
3
3
  gemspec
4
4
 
5
- gem 'rake', :require => false
5
+ gem 'pry', :group => [:development, :test]
data/README.md CHANGED
@@ -1,9 +1,9 @@
1
1
  # Guard::RailsAssets
2
2
 
3
3
 
4
- Guard::RailsAssets compiles the assets in Rails 3.1 application automatically when files are modified.
4
+ Guard::RailsAssets compiles the assets within Rails 3.1 application whenever those change.
5
5
 
6
- Tested on MRI Ruby 1.9.2 (please report if it works on your platform).
6
+ Tested on MRI 1.9.2 (please report if it works on your platform).
7
7
 
8
8
  If you have any questions please contact me [@dnagir](http://www.ApproachE.com).
9
9
 
@@ -11,8 +11,6 @@ If you have any questions please contact me [@dnagir](http://www.ApproachE.com).
11
11
 
12
12
  Please be sure to have [Guard](https://github.com/guard/guard) installed.
13
13
 
14
- Install the gem:
15
-
16
14
  Add it to your `Gemfile`, preferably inside the test and development group:
17
15
 
18
16
  ```ruby
@@ -22,27 +20,12 @@ gem 'guard-rails-assets'
22
20
  Add guard definition to your `Guardfile` by running:
23
21
 
24
22
  ```bash
25
- $ guard init rails-assets
23
+ $ bundle exec guard init rails-assets
26
24
  ```
27
25
 
28
- ## Rails 3.1
29
-
30
- The Rails 3.1 is a mandatory requirement, but is not enforeced via dependencies for now.
31
- The reason is that the assets can currently be compiled using following "runners":
32
-
33
- 1. rake command (CLI);
34
- 2. loading the actual Rails environment.
35
-
36
- In the 1st case - this Guard is not actually using Rails directly while in the 2nd - it loads it explicitly.
37
-
38
- Good thing about the 1st approach is that assets will always be same as produced by Rails.
39
- Bad thing is that it is pretty slow (~10 seconds) because it starts Rails from ground zero.
40
-
41
- The 2nd approach is good because it is much faster, but does not reload Rails environment (so you have to restart guard).
42
-
43
- ## Guardfile and Options
26
+ ## Options
44
27
 
45
- In addition to the standard configuration, this Guard has options to specify when exacly to precompile assets.
28
+ In addition to the guard configuration, `guard-rails-assets` has options to specify when exacly to precompile assets.
46
29
 
47
30
  - `:start` - compile assets when the guard starts (enabled by default)
48
31
  - `:change` - compile assets when watched files change (enabled by default)
@@ -52,8 +35,9 @@ In addition to the standard configuration, this Guard has options to specify whe
52
35
  Also you can set the `:runner` option:
53
36
 
54
37
  - `:cli` - compile assets using the rake task - the most correct method, but slow.
55
- - `:rails` - compile assets by loading rails environment (default) - fast, but does not pick up changes.
38
+ - `:rails` - compile assets by loading rails environment (default) - fast, but does not pick up changes. Additionally it relies on a single instance of your app to be loaded, so you can't have multiple guards with different rails configurations.
56
39
 
40
+ `:rails_env` option is available that allows you to specify the Rails environment to use (defaults to 'test').
57
41
 
58
42
 
59
43
  For example:
@@ -61,7 +45,7 @@ For example:
61
45
 
62
46
  ```ruby
63
47
  # This is the default behaviour
64
- guard 'rails-assets', :run_on => [:start, :change], :runner => :rails do
48
+ guard 'rails-assets', :run_on => [:start, :change], :runner => :rails, :rails_env => 'test' do
65
49
  watch(%r{^app/assets/.+$})
66
50
  end
67
51
 
@@ -15,6 +15,7 @@ Gem::Specification.new do |s|
15
15
 
16
16
  s.add_dependency 'guard'
17
17
  s.add_dependency 'rake'
18
+ s.add_dependency 'rails', '>= 3.1.0'
18
19
  s.add_development_dependency 'rspec'
19
20
 
20
21
  s.files = `git ls-files`.split("\n")
@@ -1,10 +1,11 @@
1
1
  module Guard
2
2
  class RailsAssets::CliRunner
3
- def initialize(options)
3
+ def initialize(options={})
4
+ @rails_env = (options[:rails_env] || 'test').to_s
4
5
  end
5
6
 
6
7
  def compile_assets
7
- system "bundle exec rake assets:clean assets:precompile"
8
+ system "RAILS_ENV=#{@rails_env} bundle exec rake assets:clean assets:precompile"
8
9
  end
9
10
  end
10
11
  end
@@ -2,63 +2,57 @@ require 'rake/dsl_definition'
2
2
  module Guard
3
3
 
4
4
  class RailsAssets::RailsRunner
5
+ @@rails_booted = false # Only one rails app is allowed, so make it a class var
6
+ @@rails_env = nil
5
7
 
6
- def initialize(options)
7
-
8
+ def initialize(options={})
9
+ @@rails_env = (options[:rails_env] || 'test').to_s unless @@rails_booted
8
10
  end
9
11
 
10
- # Methods to run the asset pipeline
11
- # Taken from - https://github.com/rails/rails/blob/master/actionpack/lib/sprockets/assets.rake
12
- module AssetPipeline
13
- extend self
14
- extend Rake::DSL
15
-
16
- def clean
17
- assets = Rails.application.config.assets
18
- public_asset_path = Rails.public_path + assets.prefix
19
- rm_rf public_asset_path, :secure => true
20
- end
21
-
22
- def precompile
23
- Sprockets::Helpers::RailsHelper
24
-
25
- assets = Rails.application.config.assets.precompile
26
- # Always perform caching so that asset_path appends the timestamps to file references.
27
- Rails.application.config.action_controller.perform_caching = true
28
- Rails.application.assets.precompile(*assets)
12
+ def self.apply_hacks
13
+ # TODO: Hack due to Rails 3.1 issue: https://github.com/rails/rails/issues/2663#issuecomment-1990121
14
+ ENV["RAILS_GROUPS"] ||= "assets"
15
+ ENV["RAILS_ENV"] ||= @@rails_env
16
+
17
+ # TODO: Now another hack: Rails replaces Rails.application.assets with Rails.applciation.assets.index
18
+ # (this happens when config.action_controller.perform_caching is true)
19
+ # It caches all the assets, so that the Rakse task can't be reused
20
+ require 'sprockets/environment'
21
+ Sprockets::Environment.class_eval do
22
+ def index; self; end # instead of Index.new(self)
29
23
  end
30
24
  end
31
25
 
32
- def boot_rails
33
- @rails_booted ||= begin
34
- require "#{Dir.pwd}/config/environment.rb"
35
- true
36
- end
26
+ # Methods to run the asset pipeline
27
+ # See as a reference https://github.com/rails/rails/blob/master/actionpack/lib/sprockets/assets.rake
28
+ def self.boot_rails
29
+ return if @@rails_booted
30
+ puts "Booting Rails for #{@@rails_env} environment."
31
+ apply_hacks
32
+ require 'rake'
33
+ require "#{Dir.pwd}/config/environment.rb"
34
+ app = ::Rails.application
35
+
36
+ app.assets.cache = nil # don't touch my FS pls. (can we use `app.config.assets.cache_store = false` instead)?
37
+ app.load_tasks
38
+ @@rails_booted = true
37
39
  end
38
40
 
39
- def run_compiler
40
- begin
41
- @failed = false
42
- AssetPipeline.clean
43
- AssetPipeline.precompile
44
- rescue => e
45
- puts "An error occurred compiling assets: #{e}"
46
- @failed = true
47
- end
48
- end
49
41
 
50
42
  # Runs the asset pipeline compiler.
51
43
  #
52
44
  # @return [ Boolean ] Whether the compilation was successful or not
53
45
  def compile_assets
54
- boot_rails
55
- run_compiler
56
-
57
- !failed?
58
- end
59
-
60
- def failed?
61
- @failed
46
+ self.class.boot_rails
47
+ return false unless @@rails_booted
48
+ begin
49
+ Rake::Task['assets:clean'].execute
50
+ Rake::Task['assets:precompile'].execute
51
+ true
52
+ rescue => e
53
+ puts "An error occurred compiling assets: #{e}"
54
+ false
55
+ end
62
56
  end
63
57
  end
64
58
  end
data/lib/guard/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  module Guard
2
2
  module RailsAssetsVersion
3
- VERSION = "0.0.6"
3
+ VERSION = "0.0.7"
4
4
  end
5
5
  end
@@ -2,11 +2,18 @@ require 'spec_helper'
2
2
 
3
3
  describe Guard::RailsAssets::CliRunner do
4
4
 
5
- subject { Guard::RailsAssets::CliRunner.new({}) }
6
-
7
5
  it 'should run the command' do
8
6
  subject.stub(:system)
9
- subject.should_receive(:system).with("bundle exec rake assets:clean assets:precompile")
7
+ subject.should_receive(:system).with("RAILS_ENV=test bundle exec rake assets:clean assets:precompile")
10
8
  subject.compile_assets
11
9
  end
10
+
11
+ context 'with production environment' do
12
+ subject { Guard::RailsAssets::CliRunner.new(:rails_env => :production) }
13
+ it 'should run the command' do
14
+ subject.stub(:system)
15
+ subject.should_receive(:system).with("RAILS_ENV=production bundle exec rake assets:clean assets:precompile")
16
+ subject.compile_assets
17
+ end
18
+ end
12
19
  end
@@ -2,70 +2,7 @@ require 'spec_helper'
2
2
 
3
3
  describe Guard::RailsAssets::RailsRunner do
4
4
 
5
- subject { Guard::RailsAssets::RailsRunner.new({}) }
6
-
7
- describe ".compile_assets" do
8
-
9
- let(:asset_pipeline) { Guard::RailsAssets::RailsRunner::AssetPipeline }
10
-
11
- before do
12
- described_class.class_eval do
13
- def boot_rails
14
- end
15
- end
16
- end
17
-
18
- context "successful compile" do
19
- before do
20
- asset_pipeline.stub(:clean)
21
- asset_pipeline.stub(:precompile)
22
- end
23
-
24
- it "cleans the assets" do
25
- asset_pipeline.should_receive(:clean)
26
- subject.compile_assets
27
- end
28
-
29
- it "runs the compiler" do
30
- asset_pipeline.should_receive(:precompile)
31
- subject.compile_assets
32
- end
33
-
34
- it "returns true" do
35
- subject.compile_assets.should be_true
36
- end
37
- end
38
-
39
- context "with a compilation error" do
40
-
41
- before do
42
- asset_pipeline.stub(:clean)
43
- asset_pipeline.should_receive(:precompile).and_raise(StandardError)
44
- @output = capture(:stdout) do
45
- @result = subject.compile_assets
46
- end
47
- end
48
-
49
- it "outputs the error" do
50
- @output.should include("An error occurred")
51
- end
52
-
53
- it "returns false" do
54
- @result.should be_false
55
- end
56
-
57
- context "on next successful compile" do
58
-
59
- it "works" do
60
- asset_pipeline.should_receive(:clean)
61
- asset_pipeline.should_receive(:precompile)
62
- subject.compile_assets.should be_true
63
- end
64
-
65
- end
66
-
67
- end
68
-
69
- end
5
+ it 'should be tested properly as a Rails engine'
70
6
 
7
+ it { should respond_to :compile_assets }
71
8
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: guard-rails-assets
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,12 +9,12 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-08-11 00:00:00.000000000 +10:00
12
+ date: 2011-09-04 00:00:00.000000000 +10:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: guard
17
- requirement: &18976180 !ruby/object:Gem::Requirement
17
+ requirement: &23672500 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ! '>='
@@ -22,10 +22,10 @@ dependencies:
22
22
  version: '0'
23
23
  type: :runtime
24
24
  prerelease: false
25
- version_requirements: *18976180
25
+ version_requirements: *23672500
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: rake
28
- requirement: &18975760 !ruby/object:Gem::Requirement
28
+ requirement: &23663340 !ruby/object:Gem::Requirement
29
29
  none: false
30
30
  requirements:
31
31
  - - ! '>='
@@ -33,10 +33,21 @@ dependencies:
33
33
  version: '0'
34
34
  type: :runtime
35
35
  prerelease: false
36
- version_requirements: *18975760
36
+ version_requirements: *23663340
37
+ - !ruby/object:Gem::Dependency
38
+ name: rails
39
+ requirement: &23662760 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ! '>='
43
+ - !ruby/object:Gem::Version
44
+ version: 3.1.0
45
+ type: :runtime
46
+ prerelease: false
47
+ version_requirements: *23662760
37
48
  - !ruby/object:Gem::Dependency
38
49
  name: rspec
39
- requirement: &18975340 !ruby/object:Gem::Requirement
50
+ requirement: &23661860 !ruby/object:Gem::Requirement
40
51
  none: false
41
52
  requirements:
42
53
  - - ! '>='
@@ -44,7 +55,7 @@ dependencies:
44
55
  version: '0'
45
56
  type: :development
46
57
  prerelease: false
47
- version_requirements: *18975340
58
+ version_requirements: *23661860
48
59
  description: guard-rails-assets automatically generates JavaScript, CSS, Image files
49
60
  using Rails assets pipelie
50
61
  email:
@@ -55,7 +66,6 @@ extra_rdoc_files: []
55
66
  files:
56
67
  - .gitignore
57
68
  - Gemfile
58
- - Gemfile.lock
59
69
  - README.md
60
70
  - Rakefile
61
71
  - guard-rails-assets.gemspec
@@ -83,12 +93,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
83
93
  - - ! '>='
84
94
  - !ruby/object:Gem::Version
85
95
  version: '0'
96
+ segments:
97
+ - 0
98
+ hash: 4449380752411900683
86
99
  required_rubygems_version: !ruby/object:Gem::Requirement
87
100
  none: false
88
101
  requirements:
89
102
  - - ! '>='
90
103
  - !ruby/object:Gem::Version
91
104
  version: '0'
105
+ segments:
106
+ - 0
107
+ hash: 4449380752411900683
92
108
  requirements: []
93
109
  rubyforge_project: guard-rails-assets
94
110
  rubygems_version: 1.6.2
data/Gemfile.lock DELETED
@@ -1,31 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- guard-rails-assets (0.0.5)
5
- guard
6
- rake
7
-
8
- GEM
9
- remote: http://rubygems.org/
10
- specs:
11
- diff-lcs (1.1.2)
12
- guard (0.5.1)
13
- thor (~> 0.14.6)
14
- rake (0.9.2)
15
- rspec (2.6.0)
16
- rspec-core (~> 2.6.0)
17
- rspec-expectations (~> 2.6.0)
18
- rspec-mocks (~> 2.6.0)
19
- rspec-core (2.6.4)
20
- rspec-expectations (2.6.0)
21
- diff-lcs (~> 1.1.2)
22
- rspec-mocks (2.6.0)
23
- thor (0.14.6)
24
-
25
- PLATFORMS
26
- ruby
27
-
28
- DEPENDENCIES
29
- guard-rails-assets!
30
- rake
31
- rspec