guard-sprockets2 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/README.md +1 -0
- data/lib/guard/sprockets2/compiler.rb +73 -0
- data/lib/guard/sprockets2/version.rb +1 -1
- data/lib/guard/sprockets2.rb +5 -45
- data/spec/guard/sprockets2/compiler_spec.rb +52 -4
- metadata +25 -24
data/README.md
CHANGED
@@ -23,6 +23,7 @@ Configure guard for your environment. The following options are available:
|
|
23
23
|
- `precompile` - Optional. An array of regex's or strings which match files
|
24
24
|
that need compiling. Defaults to `[ /\w+\.(?!js|css).+/, /application.(css|js)$/ ]`
|
25
25
|
- `digest` - Optional. Whether to include the digest in the filename. Defaults to true.
|
26
|
+
- `gz` - Optional. Whether to compile a gzipped version of each file. Defaults to true.
|
26
27
|
|
27
28
|
Example Rails and Sinatra apps can be found in the examples directory.
|
28
29
|
|
@@ -0,0 +1,73 @@
|
|
1
|
+
module Guard
|
2
|
+
class Sprockets2::Compiler
|
3
|
+
def initialize(options = {})
|
4
|
+
configure(options)
|
5
|
+
@target = Pathname.new(@assets_path)
|
6
|
+
end
|
7
|
+
|
8
|
+
def clean
|
9
|
+
FileUtils.rm_rf @assets_path, :secure => true
|
10
|
+
end
|
11
|
+
|
12
|
+
def compile
|
13
|
+
@sprockets.send(:expire_index!)
|
14
|
+
success = true
|
15
|
+
@precompile.each do |path|
|
16
|
+
@sprockets.each_logical_path do |logical_path|
|
17
|
+
next unless path_matches?(path, logical_path)
|
18
|
+
|
19
|
+
if asset = @sprockets.find_asset(logical_path)
|
20
|
+
success = compile_asset(asset)
|
21
|
+
break unless success
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
success
|
26
|
+
end
|
27
|
+
|
28
|
+
protected
|
29
|
+
|
30
|
+
def compile_asset(asset)
|
31
|
+
filename = @digest ? @target.join(asset.digest_path) : @target.join(asset.logical_path)
|
32
|
+
|
33
|
+
FileUtils.mkdir_p filename.dirname
|
34
|
+
asset.write_to(filename)
|
35
|
+
asset.write_to("#{filename}.gz") if @gz && filename.to_s =~ /\.(css|js)$/
|
36
|
+
true
|
37
|
+
rescue => e
|
38
|
+
puts unless ENV["GUARD_ENV"] == "test"
|
39
|
+
UI.error e.message.gsub(/^Error: /, '')
|
40
|
+
false
|
41
|
+
end
|
42
|
+
|
43
|
+
def path_matches?(path, logical_path)
|
44
|
+
if path.is_a?(Regexp)
|
45
|
+
path.match(logical_path)
|
46
|
+
else
|
47
|
+
File.fnmatch(path.to_s, logical_path)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def configure(options)
|
52
|
+
@sprockets = options[:sprockets]
|
53
|
+
@assets_path = options[:assets_path]
|
54
|
+
@precompile = options[:precompile]
|
55
|
+
@digest = options[:digest]
|
56
|
+
@gz = options[:gz]
|
57
|
+
set_defaults
|
58
|
+
end
|
59
|
+
|
60
|
+
def set_defaults
|
61
|
+
if defined?(Rails)
|
62
|
+
@sprockets ||= Rails.application.assets
|
63
|
+
@assets_path ||= File.join(Rails.public_path, Rails.application.config.assets.prefix)
|
64
|
+
@precompile ||= Rails.application.config.assets.precompile
|
65
|
+
else
|
66
|
+
@assets_path ||= "#{Dir.pwd}/public/assets"
|
67
|
+
@precompile ||= [ /\w+\.(?!js|css).+/, /application.(css|js)$/ ]
|
68
|
+
end
|
69
|
+
@digest = true if @digest.nil?
|
70
|
+
@gz = true if @gz.nil?
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
data/lib/guard/sprockets2.rb
CHANGED
@@ -5,6 +5,8 @@ require 'sprockets'
|
|
5
5
|
|
6
6
|
module Guard
|
7
7
|
class Sprockets2 < Guard
|
8
|
+
autoload :Compiler, 'guard/sprockets2/compiler'
|
9
|
+
|
8
10
|
def initialize(watchers = [], options = {})
|
9
11
|
super
|
10
12
|
@compiler = Compiler.new(options)
|
@@ -22,58 +24,16 @@ module Guard
|
|
22
24
|
compile_assets
|
23
25
|
end
|
24
26
|
|
25
|
-
class Compiler
|
26
|
-
def initialize(options = {})
|
27
|
-
@sprockets = options[:sprockets]
|
28
|
-
@assets_path = options[:assets_path]
|
29
|
-
@precompile = options[:precompile]
|
30
|
-
@digest = options[:digest]
|
31
|
-
@digest = true if @digest.nil?
|
32
|
-
if defined?(Rails)
|
33
|
-
@sprockets ||= Rails.application.assets
|
34
|
-
@assets_path ||= File.join(Rails.public_path, Rails.application.config.assets.prefix)
|
35
|
-
@precompile ||= Rails.application.config.assets.precompile
|
36
|
-
else
|
37
|
-
@assets_path ||= "#{Dir.pwd}/public/assets"
|
38
|
-
@precompile ||= [ /\w+\.(?!js|css).+/, /application.(css|js)$/ ]
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
def clean
|
43
|
-
FileUtils.rm_rf @assets_path, :secure => true
|
44
|
-
end
|
45
|
-
|
46
|
-
def compile
|
47
|
-
target = Pathname.new(@assets_path)
|
48
|
-
@precompile.each do |path|
|
49
|
-
@sprockets.each_logical_path do |logical_path|
|
50
|
-
if path.is_a?(Regexp)
|
51
|
-
next unless path.match(logical_path)
|
52
|
-
else
|
53
|
-
next unless File.fnmatch(path.to_s, logical_path)
|
54
|
-
end
|
55
|
-
|
56
|
-
if asset = @sprockets.find_asset(logical_path)
|
57
|
-
filename = @digest ? target.join(asset.digest_path) : target.join(asset.logical_path)
|
58
|
-
|
59
|
-
FileUtils.mkdir_p filename.dirname
|
60
|
-
asset.write_to(filename)
|
61
|
-
asset.write_to("#{filename}.gz") if filename.to_s =~ /\.(css|js)$/
|
62
|
-
end
|
63
|
-
end
|
64
|
-
end
|
65
|
-
end
|
66
|
-
end
|
67
|
-
|
68
27
|
protected
|
69
28
|
|
70
29
|
def compile_assets
|
71
30
|
@compiler.clean
|
72
31
|
print "Compiling assets... " unless ENV["GUARD_ENV"] == "test"
|
32
|
+
successful = true
|
73
33
|
time_taken = time do
|
74
|
-
@compiler.compile
|
34
|
+
successful = @compiler.compile
|
75
35
|
end
|
76
|
-
UI.info "completed in #{time_taken} seconds"
|
36
|
+
UI.info "completed in #{time_taken} seconds" if successful
|
77
37
|
end
|
78
38
|
|
79
39
|
def time(&block)
|
@@ -6,6 +6,10 @@ describe Guard::Sprockets2::Compiler do
|
|
6
6
|
File.open(path, "wb") {|f| f.write data }
|
7
7
|
end
|
8
8
|
|
9
|
+
def write_hello_coffee(code)
|
10
|
+
write_file(assets_path.join("hello.coffee").to_s, code)
|
11
|
+
end
|
12
|
+
|
9
13
|
let(:tmp) { Pathname.new(File.expand_path("../../../../tmp", __FILE__)) }
|
10
14
|
let(:sprockets) { Sprockets::Environment.new(tmp.to_s) }
|
11
15
|
let(:assets_path) { tmp.join("assets") }
|
@@ -18,19 +22,50 @@ describe Guard::Sprockets2::Compiler do
|
|
18
22
|
sprockets.append_path(assets_path)
|
19
23
|
write_file(assets_path.join("application.js").to_s, "//= require_tree .")
|
20
24
|
end
|
21
|
-
|
22
25
|
|
23
26
|
context 'preconfigured' do
|
24
27
|
subject { Guard::Sprockets2::Compiler.new(:sprockets => sprockets, :assets_path => compiled_path.to_s) }
|
25
28
|
|
26
29
|
it "compiles assets" do
|
27
|
-
|
30
|
+
write_hello_coffee("console.log 'hello'")
|
28
31
|
subject.compile
|
29
32
|
asset = sprockets.find_asset("application.js")
|
30
33
|
app_js_path = compiled_path.join(asset.digest_path)
|
31
34
|
|
32
35
|
app_js_path.should exist
|
33
36
|
app_js_path.read.should include("console.log('hello')")
|
37
|
+
|
38
|
+
app_js_gz_path = compiled_path.join("#{asset.digest_path}.gz")
|
39
|
+
app_js_gz_path.should exist
|
40
|
+
end
|
41
|
+
|
42
|
+
it "returns true" do
|
43
|
+
write_hello_coffee("console.log 'hello'")
|
44
|
+
subject.compile.should be_true
|
45
|
+
end
|
46
|
+
|
47
|
+
context 'with an error' do
|
48
|
+
before do
|
49
|
+
write_hello_coffee("console.log 'hello''")
|
50
|
+
@result = subject.compile
|
51
|
+
end
|
52
|
+
|
53
|
+
it "returns false" do
|
54
|
+
@result.should be_false
|
55
|
+
end
|
56
|
+
|
57
|
+
it "works when the error has been fixed" do
|
58
|
+
write_hello_coffee("console.log 'hello'")
|
59
|
+
expect {
|
60
|
+
subject.compile
|
61
|
+
}.not_to raise_error(Sprockets::CircularDependencyError)
|
62
|
+
|
63
|
+
asset = sprockets.find_asset("application.js")
|
64
|
+
app_js_path = compiled_path.join(asset.digest_path)
|
65
|
+
|
66
|
+
app_js_path.should exist
|
67
|
+
app_js_path.read.should include("console.log('hello')")
|
68
|
+
end
|
34
69
|
end
|
35
70
|
end
|
36
71
|
|
@@ -38,7 +73,7 @@ describe Guard::Sprockets2::Compiler do
|
|
38
73
|
subject { Guard::Sprockets2::Compiler.new(:sprockets => sprockets, :assets_path => compiled_path.to_s, :digest => false) }
|
39
74
|
|
40
75
|
it "compiles assets without the digest" do
|
41
|
-
|
76
|
+
write_hello_coffee("console.log 'hello'")
|
42
77
|
subject.compile
|
43
78
|
app_js_path = compiled_path.join('application.js')
|
44
79
|
|
@@ -47,9 +82,22 @@ describe Guard::Sprockets2::Compiler do
|
|
47
82
|
end
|
48
83
|
end
|
49
84
|
|
85
|
+
context 'with gz false' do
|
86
|
+
subject { Guard::Sprockets2::Compiler.new(:sprockets => sprockets, :assets_path => compiled_path.to_s, :gz => false) }
|
87
|
+
|
88
|
+
it "compiles assets without the digest" do
|
89
|
+
write_hello_coffee("console.log 'hello'")
|
90
|
+
subject.compile
|
91
|
+
asset = sprockets.find_asset("application.js")
|
92
|
+
app_js_gz_path = compiled_path.join("#{asset.digest_path}.gz")
|
93
|
+
|
94
|
+
app_js_gz_path.should_not exist
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
50
98
|
context 'with rails loaded' do
|
51
99
|
before do
|
52
|
-
|
100
|
+
write_hello_coffee("console.log 'hello2'")
|
53
101
|
module Rails
|
54
102
|
end
|
55
103
|
Rails.stub(:public_path => tmp)
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: guard-sprockets2
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-09-
|
12
|
+
date: 2011-09-20 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: guard
|
16
|
-
requirement: &
|
16
|
+
requirement: &70258732026820 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70258732026820
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: sprockets
|
27
|
-
requirement: &
|
27
|
+
requirement: &70258732025400 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ~>
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '2.0'
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70258732025400
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: rspec
|
38
|
-
requirement: &
|
38
|
+
requirement: &70258732024460 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: '0'
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70258732024460
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: coffee-script
|
49
|
-
requirement: &
|
49
|
+
requirement: &70258732023200 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ! '>='
|
@@ -54,10 +54,10 @@ dependencies:
|
|
54
54
|
version: '0'
|
55
55
|
type: :development
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *70258732023200
|
58
58
|
- !ruby/object:Gem::Dependency
|
59
59
|
name: uglifier
|
60
|
-
requirement: &
|
60
|
+
requirement: &70258732022200 !ruby/object:Gem::Requirement
|
61
61
|
none: false
|
62
62
|
requirements:
|
63
63
|
- - ! '>='
|
@@ -65,10 +65,10 @@ dependencies:
|
|
65
65
|
version: '0'
|
66
66
|
type: :development
|
67
67
|
prerelease: false
|
68
|
-
version_requirements: *
|
68
|
+
version_requirements: *70258732022200
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: sass
|
71
|
-
requirement: &
|
71
|
+
requirement: &70258732021360 !ruby/object:Gem::Requirement
|
72
72
|
none: false
|
73
73
|
requirements:
|
74
74
|
- - ! '>='
|
@@ -76,10 +76,10 @@ dependencies:
|
|
76
76
|
version: '0'
|
77
77
|
type: :development
|
78
78
|
prerelease: false
|
79
|
-
version_requirements: *
|
79
|
+
version_requirements: *70258732021360
|
80
80
|
- !ruby/object:Gem::Dependency
|
81
81
|
name: sinatra
|
82
|
-
requirement: &
|
82
|
+
requirement: &70258732020600 !ruby/object:Gem::Requirement
|
83
83
|
none: false
|
84
84
|
requirements:
|
85
85
|
- - ! '>='
|
@@ -87,10 +87,10 @@ dependencies:
|
|
87
87
|
version: '0'
|
88
88
|
type: :development
|
89
89
|
prerelease: false
|
90
|
-
version_requirements: *
|
90
|
+
version_requirements: *70258732020600
|
91
91
|
- !ruby/object:Gem::Dependency
|
92
92
|
name: cucumber
|
93
|
-
requirement: &
|
93
|
+
requirement: &70258732019960 !ruby/object:Gem::Requirement
|
94
94
|
none: false
|
95
95
|
requirements:
|
96
96
|
- - ! '>='
|
@@ -98,10 +98,10 @@ dependencies:
|
|
98
98
|
version: '0'
|
99
99
|
type: :development
|
100
100
|
prerelease: false
|
101
|
-
version_requirements: *
|
101
|
+
version_requirements: *70258732019960
|
102
102
|
- !ruby/object:Gem::Dependency
|
103
103
|
name: aruba
|
104
|
-
requirement: &
|
104
|
+
requirement: &70258732019300 !ruby/object:Gem::Requirement
|
105
105
|
none: false
|
106
106
|
requirements:
|
107
107
|
- - ! '>='
|
@@ -109,10 +109,10 @@ dependencies:
|
|
109
109
|
version: '0'
|
110
110
|
type: :development
|
111
111
|
prerelease: false
|
112
|
-
version_requirements: *
|
112
|
+
version_requirements: *70258732019300
|
113
113
|
- !ruby/object:Gem::Dependency
|
114
114
|
name: rails
|
115
|
-
requirement: &
|
115
|
+
requirement: &70258732018100 !ruby/object:Gem::Requirement
|
116
116
|
none: false
|
117
117
|
requirements:
|
118
118
|
- - ~>
|
@@ -120,7 +120,7 @@ dependencies:
|
|
120
120
|
version: '3.1'
|
121
121
|
type: :development
|
122
122
|
prerelease: false
|
123
|
-
version_requirements: *
|
123
|
+
version_requirements: *70258732018100
|
124
124
|
description: ''
|
125
125
|
email:
|
126
126
|
- steve@hodgkiss.me
|
@@ -191,6 +191,7 @@ files:
|
|
191
191
|
- features/support/env.rb
|
192
192
|
- guard-sprockets2.gemspec
|
193
193
|
- lib/guard/sprockets2.rb
|
194
|
+
- lib/guard/sprockets2/compiler.rb
|
194
195
|
- lib/guard/sprockets2/templates/Guardfile
|
195
196
|
- lib/guard/sprockets2/version.rb
|
196
197
|
- spec/guard/sprockets2/compiler_spec.rb
|
@@ -210,7 +211,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
210
211
|
version: '0'
|
211
212
|
segments:
|
212
213
|
- 0
|
213
|
-
hash:
|
214
|
+
hash: -482744400968965211
|
214
215
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
215
216
|
none: false
|
216
217
|
requirements:
|
@@ -219,7 +220,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
219
220
|
version: '0'
|
220
221
|
segments:
|
221
222
|
- 0
|
222
|
-
hash:
|
223
|
+
hash: -482744400968965211
|
223
224
|
requirements: []
|
224
225
|
rubyforge_project: guard-sprockets2
|
225
226
|
rubygems_version: 1.8.6
|