guard-rails-assets 0.0.4 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Gemfile CHANGED
@@ -2,4 +2,4 @@ source "http://rubygems.org"
2
2
 
3
3
  gemspec
4
4
 
5
- gem 'rake'
5
+ gem 'rake', :require => false
data/Gemfile.lock CHANGED
@@ -1,14 +1,15 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- guard-rails-assets (0.0.3)
4
+ guard-rails-assets (0.0.5)
5
5
  guard
6
+ rake
6
7
 
7
8
  GEM
8
9
  remote: http://rubygems.org/
9
10
  specs:
10
11
  diff-lcs (1.1.2)
11
- guard (0.4.2)
12
+ guard (0.5.1)
12
13
  thor (~> 0.14.6)
13
14
  rake (0.9.2)
14
15
  rspec (2.6.0)
data/README.md CHANGED
@@ -28,15 +28,17 @@ $ guard init rails-assets
28
28
  ## Rails 3.1
29
29
 
30
30
  The Rails 3.1 is a mandatory requirement, but is not enforeced via dependencies for now.
31
- The reason is that the assets are currently compiled via command line and thus this guard does not
32
- explicitly depend on Rails.
31
+ The reason is that the assets can currently be compiled using following "runners":
33
32
 
34
- Good thing about it is that assets will always be same as produced by Rails.
35
- Bad thing is that it is pretty slow (~10 seconds) because it starts Rails from ground zero.
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.
36
37
 
37
- *NOTE*: The guard runs the `rake assets:clean assets:precopile`.
38
- As of current Rails 3.1 edge that means that the assets will be deleted before they are compiled.
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.
39
40
 
41
+ The 2nd approach is good because it is much faster, but does not reload Rails environment (so you have to restart guard).
40
42
 
41
43
  ## Guardfile and Options
42
44
 
@@ -47,21 +49,28 @@ In addition to the standard configuration, this Guard has options to specify whe
47
49
  - `:reload` - compile assets when the guard quites (Ctrl-C) (not enabled by default)
48
50
  - `:all` - compile assets when running all the guards (Ctrl-/) (not enabled by default)
49
51
 
52
+ Also you can set the `:runner` option:
53
+
54
+ - `: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.
56
+
57
+
58
+
50
59
  For example:
51
60
 
52
61
 
53
62
  ```ruby
54
- # compile ONLY when something changes
55
- guard 'rails-assets', :run_on => :change do
63
+ # This is the default behaviour
64
+ guard 'rails-assets', :run_on => [:start, :change], :runner => :rails do
56
65
  watch(%r{^app/assets/.+$})
57
66
  end
58
67
 
59
- # compile when something changes and when starting
60
- guard 'rails-assets', :run_on => [:start, :change] do
68
+ # compile ONLY when something changes
69
+ guard 'rails-assets', :run_on => :change do
61
70
  watch(%r{^app/assets/.+$})
62
71
  end
63
72
 
64
- # This is the default behaviour
73
+ # compile when something changes and when starting
65
74
  guard 'rails-assets', :run_on => [:start, :change] do
66
75
  watch(%r{^app/assets/.+$})
67
76
  end
@@ -72,7 +81,6 @@ end
72
81
  - Source hosted at [GitHub](https://github.com/dnagir/guard-rails-assets)
73
82
  - Report issues and feature requests to [GitHub Issues](https://github.com/dnagir/guard-rails-assets/issues)
74
83
 
75
-
76
84
  Pull requests are very welcome!
77
85
 
78
86
  ## Licensed under WTFPL
@@ -92,7 +100,3 @@ Pull requests are very welcome!
92
100
 
93
101
  0. You just DO WHAT THE FUCK YOU WANT TO.
94
102
  ```
95
-
96
-
97
-
98
-
@@ -14,6 +14,7 @@ Gem::Specification.new do |s|
14
14
  s.rubyforge_project = "guard-rails-assets"
15
15
 
16
16
  s.add_dependency 'guard'
17
+ s.add_dependency 'rake'
17
18
  s.add_development_dependency 'rspec'
18
19
 
19
20
  s.files = `git ls-files`.split("\n")
@@ -6,13 +6,17 @@ module Guard
6
6
  def initialize(watchers=[], options={})
7
7
  super
8
8
  @options = options || {}
9
+ @run_on = @options[:run_on] || [:start, :change]
10
+ @run_on = [@run_on] unless @run_on.respond_to?(:include?)
9
11
  end
10
12
 
11
13
  def start
14
+ runner.start if runner.respond_to? :start
12
15
  compile_assets if run_for? :start
13
16
  end
14
17
 
15
18
  def reload
19
+ runner.reload if runner.respond_to? :reload
16
20
  compile_assets if run_for? :reload
17
21
  end
18
22
 
@@ -25,20 +29,30 @@ module Guard
25
29
  end
26
30
 
27
31
  def compile_assets
28
- puts 'Compiling rails assets'
29
- result = system "bundle exec rake assets:clean assets:precompile"
32
+ puts "Compiling rails assets with #{runner.class.name}."
33
+ result = runner.compile_assets
34
+
30
35
  if result
31
36
  Notifier::notify 'Assets compiled'
37
+ puts 'Assets compiled.'
32
38
  else
33
39
  Notifier::notify 'see the details in the terminal', :title => "Can't compile assets", :image => :failed
40
+ puts 'Failed to compile assets.'
41
+ end
42
+ end
43
+
44
+ def runner
45
+ @runner ||= begin
46
+ runner_name = (@options[:runner] || :rails).to_s
47
+
48
+ require_relative "rails-assets/#{runner_name}_runner"
49
+ ::Guard::RailsAssets.const_get(runner_name.capitalize + 'Runner').new(@options)
34
50
  end
35
51
  end
36
52
 
37
53
  def run_for? command
38
- run_on = @options[:run_on]
39
- run_on = [:start, :change] if not run_on or run_on.to_s.empty?
40
- run_on = [run_on] unless run_on.respond_to?(:include?)
41
- run_on.include?(command)
54
+ @run_on.include?(command)
42
55
  end
43
56
  end
44
57
  end
58
+
@@ -0,0 +1,10 @@
1
+ module Guard
2
+ class RailsAssets::CliRunner
3
+ def initialize(options)
4
+ end
5
+
6
+ def compile_assets
7
+ system "bundle exec rake assets:clean assets:precompile"
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,64 @@
1
+ require 'rake/dsl_definition'
2
+ module Guard
3
+
4
+ class RailsAssets::RailsRunner
5
+
6
+ def initialize(options)
7
+
8
+ end
9
+
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)
29
+ end
30
+ end
31
+
32
+ def boot_rails
33
+ @rails_booted ||= begin
34
+ require "#{Dir.pwd}/config/environment.rb"
35
+ true
36
+ end
37
+ end
38
+
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
+
50
+ # Runs the asset pipeline compiler.
51
+ #
52
+ # @return [ Boolean ] Whether the compilation was successful or not
53
+ def compile_assets
54
+ boot_rails
55
+ run_compiler
56
+
57
+ !failed?
58
+ end
59
+
60
+ def failed?
61
+ @failed
62
+ end
63
+ end
64
+ end
data/lib/guard/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  module Guard
2
2
  module RailsAssetsVersion
3
- VERSION = "0.0.4"
3
+ VERSION = "0.0.5"
4
4
  end
5
5
  end
@@ -0,0 +1,12 @@
1
+ require 'spec_helper'
2
+
3
+ describe Guard::RailsAssets::CliRunner do
4
+
5
+ subject { Guard::RailsAssets::CliRunner.new({}) }
6
+
7
+ it 'should run the command' do
8
+ subject.stub(:system)
9
+ subject.should_receive(:system).with("bundle exec rake assets:clean assets:precompile")
10
+ subject.compile_assets
11
+ end
12
+ end
@@ -0,0 +1,71 @@
1
+ require 'spec_helper'
2
+
3
+ describe Guard::RailsAssets::RailsRunner do
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
70
+
71
+ end
@@ -2,55 +2,80 @@ require 'spec_helper'
2
2
  require 'guard/rails-assets'
3
3
 
4
4
  describe Guard::RailsAssets do
5
- let(:options) { {} }
6
- subject { Guard::RailsAssets.new(['watchers'], options) }
7
5
 
8
- describe '#start' do
9
- it_behaves_like 'guard command', :command => :start, :run => true
10
- end
11
6
 
12
- describe '#reload' do
13
- it_behaves_like 'guard command', :command => :reload, :run => false
14
- end
7
+ context 'with any runner' do
15
8
 
16
- describe '#run_all' do
17
- it_behaves_like 'guard command', :command => :run_all, :run => false
18
- end
9
+ let(:options) { {:runner => :cli} }
10
+ let(:runner) { mock('runner') }
11
+ subject { Guard::RailsAssets.new(['watchers'], options) }
19
12
 
20
- describe '#run_on_change' do
21
- it_behaves_like 'guard command', :command => :run_on_change, :run => true
22
- end
13
+ before do
14
+ Guard::RailsAssets::CliRunner.stub(:new).and_return runner
15
+ end
23
16
 
24
- describe 'run options' do
25
- it 'should allow array of symbols' do
26
- guard = Guard::RailsAssets.new(['watchers'], :run_on => [:start, :change])
27
- guard.run_for?(:start).should be_true
28
- guard.run_for?(:reload).should be_false
17
+ describe '#start' do
18
+ it_behaves_like 'guard command', :command => :start, :run => true
29
19
  end
30
20
 
31
- it 'should allow symbol' do
32
- guard = Guard::RailsAssets.new(['watchers'], :run_on => :start)
33
- guard.run_for?(:start).should be_true
34
- guard.run_for?(:reload).should be_false
21
+ describe '#reload' do
22
+ it_behaves_like 'guard command', :command => :reload, :run => false
35
23
  end
36
- end
37
24
 
38
- describe 'asset compilation using CLI' do
39
- def stub_system_with result
40
- subject.should_receive(:system).with("bundle exec rake assets:clean assets:precompile").and_return result
25
+ describe '#run_all' do
26
+ it_behaves_like 'guard command', :command => :run_all, :run => false
27
+ end
28
+
29
+ describe '#run_on_change' do
30
+ it_behaves_like 'guard command', :command => :run_on_change, :run => true
31
+ end
32
+
33
+ describe 'run options' do
34
+ it 'should allow array of symbols' do
35
+ guard = Guard::RailsAssets.new(['watchers'], :run_on => [:start, :change])
36
+ guard.run_for?(:start).should be_true
37
+ guard.run_for?(:reload).should be_false
38
+ end
39
+
40
+ it 'should allow symbol' do
41
+ guard = Guard::RailsAssets.new(['watchers'], :run_on => :start)
42
+ guard.run_for?(:start).should be_true
43
+ guard.run_for?(:reload).should be_false
44
+ end
45
+
41
46
  end
42
47
 
43
- it 'should notify on success' do
44
- stub_system_with true
45
- Guard::Notifier.should_receive(:notify).with('Assets compiled')
46
- subject.compile_assets
48
+ describe 'notifications' do
49
+ def stub_system_with result
50
+ runner.should_receive(:compile_assets).and_return result
51
+ end
52
+
53
+ it 'should notify on success' do
54
+ stub_system_with true
55
+ Guard::Notifier.should_receive(:notify).with('Assets compiled')
56
+ subject.compile_assets
57
+ end
58
+
59
+ it 'should notify on failure' do
60
+ stub_system_with false
61
+ Guard::Notifier.should_receive(:notify).with('see the details in the terminal', :title => "Can't compile assets", :image => :failed)
62
+ subject.compile_assets
63
+ end
47
64
  end
48
65
 
49
- it 'should notify on failure' do
50
- stub_system_with false
51
- subject.should_not_receive(:`) # don't obtain tree
52
- Guard::Notifier.should_receive(:notify).with('see the details in the terminal', :title => "Can't compile assets", :image => :failed)
53
- subject.compile_assets
66
+ end # context with any runner
67
+
68
+ describe 'picking a runner' do
69
+ it 'should use Rails runner by default' do
70
+ Guard::RailsAssets.new(['watchers']).runner.class.should == ::Guard::RailsAssets::RailsRunner
71
+ end
72
+
73
+ it 'should use CLI runner' do
74
+ Guard::RailsAssets.new(['watchers'], :runner => :cli).runner.class.should == ::Guard::RailsAssets::CliRunner
75
+ end
76
+
77
+ it 'should use RailsRunner' do
78
+ Guard::RailsAssets.new(['watchers'], :runner => :rails).runner.class.should == ::Guard::RailsAssets::RailsRunner
54
79
  end
55
80
  end
56
81
  end
data/spec/spec_helper.rb CHANGED
@@ -1,6 +1,9 @@
1
1
  require 'rspec'
2
2
  require 'guard/rails-assets'
3
3
  require 'support/shared_examples'
4
+ require 'support/stdout_helper'
5
+ require 'guard/rails-assets/cli_runner'
6
+ require 'guard/rails-assets/rails_runner'
4
7
 
5
8
  RSpec.configure do |config|
6
9
  config.color_enabled = true
@@ -15,5 +18,7 @@ RSpec.configure do |config|
15
18
  config.after(:each) do
16
19
  ENV["GUARD_ENV"] = nil
17
20
  end
21
+
22
+ config.include(Helpers)
18
23
 
19
24
  end
@@ -0,0 +1,17 @@
1
+ require 'stringio'
2
+
3
+ module Helpers
4
+
5
+ def capture(*streams)
6
+ streams.map! { |stream| stream.to_s }
7
+ begin
8
+ result = StringIO.new
9
+ streams.each { |stream| eval "$#{stream} = result" }
10
+ yield
11
+ ensure
12
+ streams.each { |stream| eval("$#{stream} = #{stream.upcase}") }
13
+ end
14
+ result.string
15
+ end
16
+
17
+ 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.4
4
+ version: 0.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,12 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-07-16 00:00:00.000000000Z
12
+ date: 2011-08-02 00:00:00.000000000 +10:00
13
+ default_executable:
13
14
  dependencies:
14
15
  - !ruby/object:Gem::Dependency
15
16
  name: guard
16
- requirement: &18715380 !ruby/object:Gem::Requirement
17
+ requirement: &15623720 !ruby/object:Gem::Requirement
17
18
  none: false
18
19
  requirements:
19
20
  - - ! '>='
@@ -21,10 +22,21 @@ dependencies:
21
22
  version: '0'
22
23
  type: :runtime
23
24
  prerelease: false
24
- version_requirements: *18715380
25
+ version_requirements: *15623720
26
+ - !ruby/object:Gem::Dependency
27
+ name: rake
28
+ requirement: &15623300 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: *15623300
25
37
  - !ruby/object:Gem::Dependency
26
38
  name: rspec
27
- requirement: &18714840 !ruby/object:Gem::Requirement
39
+ requirement: &15622880 !ruby/object:Gem::Requirement
28
40
  none: false
29
41
  requirements:
30
42
  - - ! '>='
@@ -32,7 +44,7 @@ dependencies:
32
44
  version: '0'
33
45
  type: :development
34
46
  prerelease: false
35
- version_requirements: *18714840
47
+ version_requirements: *15622880
36
48
  description: guard-rails-assets automatically generates JavaScript, CSS, Image files
37
49
  using Rails assets pipelie
38
50
  email:
@@ -48,11 +60,17 @@ files:
48
60
  - Rakefile
49
61
  - guard-rails-assets.gemspec
50
62
  - lib/guard/rails-assets.rb
63
+ - lib/guard/rails-assets/cli_runner.rb
64
+ - lib/guard/rails-assets/rails_runner.rb
51
65
  - lib/guard/rails-assets/templates/Guardfile
52
66
  - lib/guard/version.rb
67
+ - spec/guard/rails-assets/cli_runner_spec.rb
68
+ - spec/guard/rails-assets/rails_runner_spec.rb
53
69
  - spec/guard/rails-assets_spec.rb
54
70
  - spec/spec_helper.rb
55
71
  - spec/support/shared_examples.rb
72
+ - spec/support/stdout_helper.rb
73
+ has_rdoc: true
56
74
  homepage: http://github.com/dnagir/guard-rails-assets
57
75
  licenses: []
58
76
  post_install_message:
@@ -65,15 +83,21 @@ required_ruby_version: !ruby/object:Gem::Requirement
65
83
  - - ! '>='
66
84
  - !ruby/object:Gem::Version
67
85
  version: '0'
86
+ segments:
87
+ - 0
88
+ hash: 3181141652634130473
68
89
  required_rubygems_version: !ruby/object:Gem::Requirement
69
90
  none: false
70
91
  requirements:
71
92
  - - ! '>='
72
93
  - !ruby/object:Gem::Version
73
94
  version: '0'
95
+ segments:
96
+ - 0
97
+ hash: 3181141652634130473
74
98
  requirements: []
75
99
  rubyforge_project: guard-rails-assets
76
- rubygems_version: 1.8.5
100
+ rubygems_version: 1.6.2
77
101
  signing_key:
78
102
  specification_version: 3
79
103
  summary: Guard for compiling Rails assets