assets_booster 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (36) hide show
  1. data/.rspec +2 -0
  2. data/Rakefile +4 -0
  3. data/asset_booster.gemspec +6 -1
  4. data/lib/assets_booster/compiler/closure.rb +8 -6
  5. data/lib/assets_booster/compiler/dummy.rb +2 -5
  6. data/lib/assets_booster/compiler/jsmin.rb +138 -148
  7. data/lib/assets_booster/compiler/rainpress.rb +3 -3
  8. data/lib/assets_booster/compiler/uglify.rb +6 -4
  9. data/lib/assets_booster/merger/base.rb +26 -0
  10. data/lib/assets_booster/merger/css.rb +38 -43
  11. data/lib/assets_booster/merger/simple.rb +7 -11
  12. data/lib/assets_booster/package/base.rb +24 -14
  13. data/lib/assets_booster/package/javascript.rb +3 -7
  14. data/lib/assets_booster/package/stylesheet.rb +3 -7
  15. data/lib/assets_booster/packager.rb +102 -11
  16. data/lib/assets_booster/railtie.rb +5 -3
  17. data/lib/assets_booster/tasks/tasks.rake +9 -11
  18. data/lib/assets_booster/version.rb +1 -1
  19. data/lib/assets_booster/view_helper.rb +3 -6
  20. data/spec/compiler/base.rb +15 -0
  21. data/spec/compiler/closure_spec.rb +16 -0
  22. data/spec/compiler/dummy_spec.rb +16 -0
  23. data/spec/compiler/jsmin_spec.rb +16 -0
  24. data/spec/compiler/rainpress_spec.rb +16 -0
  25. data/spec/compiler/uglify_spec.rb +16 -0
  26. data/spec/merger/base.rb +31 -0
  27. data/spec/merger/css_spec.rb +138 -0
  28. data/spec/merger/simple_spec.rb +18 -0
  29. data/spec/package/base.rb +26 -0
  30. data/spec/package/javascript_spec.rb +16 -0
  31. data/spec/package/stylesheet_spec.rb +16 -0
  32. data/spec/packager_spec.rb +87 -0
  33. data/spec/spec_helper.rb +9 -0
  34. data/spec/view_helper_spec.rb +12 -0
  35. metadata +69 -7
  36. data/lib/assets_booster/configuration.rb +0 -104
@@ -1,29 +1,27 @@
1
- require "assets_booster/configuration"
2
1
  require "assets_booster/packager"
3
-
4
2
  namespace :assets_booster do
5
3
  desc 'Merge assets'
6
4
  task :merge do
7
- AssetsBooster::Packager.init
8
- AssetsBooster::Packager.merge_all
5
+ packager = AssetsBooster::Packager.new
6
+ packager.merge_all
9
7
  end
10
8
 
11
9
  desc 'Merge and compile assets'
12
10
  task :compile do
13
- AssetsBooster::Packager.init
14
- AssetsBooster::Packager.compile_all
11
+ packager = AssetsBooster::Packager.new
12
+ packager.compile_all
15
13
  end
16
14
 
17
15
  desc 'Delete all asset builds'
18
16
  task :delete do
19
- AssetsBooster::Packager.init
20
- AssetsBooster::Packager.delete_all
17
+ packager = AssetsBooster::Packager.new
18
+ packager.delete_all
21
19
  end
22
20
 
23
- desc 'Generate assets_booster.yml from existing assets'
21
+ desc 'Generate a configuration file for existing assets'
24
22
  task :setup do
25
- AssetsBooster::Packager.init
26
- AssetsBooster::Configuration.create
23
+ packager = AssetsBooster::Packager.new
24
+ packager.create_configuration
27
25
  end
28
26
  end
29
27
 
@@ -1,3 +1,3 @@
1
1
  module AssetsBooster
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -1,14 +1,11 @@
1
1
  module AssetsBooster
2
2
  module ViewHelper
3
- def should_merge?
4
- AssetPackage.merge_environments.include?(Rails.env)
5
- end
6
-
7
3
  def assets_booster_tag(type, *names)
8
4
  options = names.extract_options!
9
- packages = AssetsBooster::Packager.packages[type]
5
+ packager = AssetsBooster::Railtie.packager
6
+ packages = packager.packages[type]
10
7
  html = names.map do |name|
11
- methode, sources = packages[name].view_helper
8
+ methode, sources = packages[name].view_helper(packager)
12
9
  send(methode, sources, options)
13
10
  end*"\n"
14
11
  html.html_safe
@@ -0,0 +1,15 @@
1
+ require 'spec_helper'
2
+ module AssetsBooster
3
+ module Compiler
4
+ shared_examples_for "a compiler" do
5
+ subject{ described_class.new }
6
+
7
+ describe "name" do
8
+ it "should return a string" do
9
+ subject.name.should_not be_empty
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
15
+
@@ -0,0 +1,16 @@
1
+ require 'compiler/base'
2
+ require 'assets_booster/compiler/closure'
3
+ module AssetsBooster
4
+ module Compiler
5
+ describe Closure do
6
+ it_behaves_like "a compiler" do
7
+ describe "compile" do
8
+ it "should compile the source file" do
9
+ subject.compile("var a = 'test'; alert(a);").should == 'var a="test";alert(a);'
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
16
+
@@ -0,0 +1,16 @@
1
+ require 'compiler/base'
2
+ require 'assets_booster/compiler/dummy'
3
+ module AssetsBooster
4
+ module Compiler
5
+ describe Dummy do
6
+ it_behaves_like "a compiler" do
7
+ describe "compile" do
8
+ it "should compile the source file" do
9
+ subject.compile("var a = 'test'; alert(a);").should == "var a = 'test'; alert(a);"
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
16
+
@@ -0,0 +1,16 @@
1
+ require 'compiler/base'
2
+ require 'assets_booster/compiler/jsmin'
3
+ module AssetsBooster
4
+ module Compiler
5
+ describe JSMin do
6
+ it_behaves_like "a compiler" do
7
+ describe "compile" do
8
+ it "should compile the source file" do
9
+ subject.compile("var a = 'test'; alert(a);").should == "var a='test';alert(a);"
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
16
+
@@ -0,0 +1,16 @@
1
+ require 'compiler/base'
2
+ require 'assets_booster/compiler/rainpress'
3
+ module AssetsBooster
4
+ module Compiler
5
+ describe Rainpress do
6
+ it_behaves_like "a compiler" do
7
+ describe "compile" do
8
+ it "should compile the source file" do
9
+ subject.compile("{ color: #ff0000; }").should == "{color:red}"
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
16
+
@@ -0,0 +1,16 @@
1
+ require 'compiler/base'
2
+ require 'assets_booster/compiler/uglify'
3
+ module AssetsBooster
4
+ module Compiler
5
+ describe Uglify do
6
+ it_behaves_like "a compiler" do
7
+ describe "compile" do
8
+ it "should compile the source file" do
9
+ subject.compile("var a = 'test'; alert(a);").should == 'var a="test";alert(a)'
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
16
+
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+ require 'assets_booster/merger/base'
3
+ module AssetsBooster
4
+ module Merger
5
+ shared_examples_for "a merger" do
6
+ subject{ described_class.new([]) }
7
+
8
+ describe "name" do
9
+ it "should return a string" do
10
+ subject.name.should_not be_empty
11
+ end
12
+ end
13
+
14
+ describe "mtime" do
15
+ subject{ described_class.new(['a', 'b', 'c']) }
16
+ it "should return the last modification time of all sources" do
17
+ File.stub(:read){ "" }
18
+ File.stub(:mtime) do |asset|
19
+ case asset
20
+ when 'a' then 1
21
+ when 'b' then 3
22
+ when 'c' then 2
23
+ end
24
+ end
25
+ subject.mtime.should == 3
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
31
+
@@ -0,0 +1,138 @@
1
+ require 'merger/base'
2
+ require 'assets_booster/merger/css'
3
+ module AssetsBooster
4
+ module Merger
5
+ describe CSS do
6
+ it_behaves_like "a merger" do
7
+ describe "merge" do
8
+ subject{ described_class.new(['a.css', 'nested/b.css', 'c.css']) }
9
+
10
+ it "should merge sources and rewrite urls" do
11
+ File.stub(:read) do |source|
12
+ case source
13
+ when 'a.css'
14
+ "{background:url(gucki.png)}"
15
+ when 'nested/b.css'
16
+ "{background:url(gucki.png)}"
17
+ when 'c.css'
18
+ "{background:url(http://www.example.com/gucki.png)}"
19
+ end
20
+ end
21
+ subject.merge("target.css").should == "{background:url(gucki.png)}\n{background:url(nested/gucki.png)}\n{background:url(http://www.example.com/gucki.png)}"
22
+ end
23
+
24
+ it "should merge sources and respect import directives" do
25
+ File.stub(:read) do |source|
26
+ case source
27
+ when 'a.css'
28
+ "@import d.css"
29
+ when 'nested/b.css'
30
+ "@import url(http://www.example.com/test.css)"
31
+ when 'c.css'
32
+ "{color:#fff;}"
33
+ when 'd.css'
34
+ "{color:#f00;}"
35
+ end
36
+ end
37
+ subject.merge("target.css").should == "{color:#f00;}\n\n@import url(http://www.example.com/test.css)\n{color:#fff;}"
38
+ end
39
+ end
40
+
41
+ describe "absolute_url?" do
42
+ it "should detect absolute urls" do
43
+ [
44
+ ["http://www.example.com", true],
45
+ ["HTTP://www.example.com", true],
46
+ ["https://www.example.com", true],
47
+ ["/absolute.css", true],
48
+ ["relative.css", false],
49
+ ["another/relative.css", false],
50
+ ].each do |url, result|
51
+ subject.absolute_url?(url).should == result
52
+ end
53
+ end
54
+ end
55
+
56
+ describe "path_difference" do
57
+ it "should return the difference" do
58
+ [
59
+ ["", "", ""],
60
+ ["test", "", "test"],
61
+ ["test", "test", ""],
62
+ ["home/test", "", "home/test"],
63
+ ["home/test", "home", "test"],
64
+ ["/home/test", "/home", "test"],
65
+ ].each do |source, target, result|
66
+ subject.path_difference(source, target).should == result
67
+ end
68
+ end
69
+ it "should raise if source and target dont share a common base path" do
70
+ lambda{ subject.path_difference("/home/gucki/test", "/home/peter") }.should raise_error(ArgumentError)
71
+ end
72
+ end
73
+
74
+ describe "rewrite_urls" do
75
+ it "should rewrite url properties" do
76
+ subject.rewrite_urls("{color:#f00}", "/home/test", "/home/test").should == "{color:#f00}"
77
+ subject.rewrite_urls("{color:#f00; background:url(test.png)}", "nested", "").should == "{color:#f00; background:url(nested/test.png)}"
78
+ source_folder = "/home/test/nested"
79
+ target_folder = "/home/test"
80
+ [
81
+ [
82
+ "{color:#f00}",
83
+ "{color:#f00}",
84
+ ],
85
+ [
86
+ "{color:#f00; background:url(test.png)}",
87
+ "{color:#f00; background:url(nested/test.png)}",
88
+ ],
89
+ [
90
+ "{color:#f00; background:url(\"test.png\")}",
91
+ "{color:#f00; background:url(\"nested/test.png\")}",
92
+ ],
93
+ [
94
+ "{color:#f00; background:url('test.png')}",
95
+ "{color:#f00; background:url('nested/test.png')}",
96
+ ],
97
+ [
98
+ "{color:#f00; background:url('test file.png')}",
99
+ "{color:#f00; background:url('nested/test file.png')}",
100
+ ],
101
+ [
102
+ "{color:#f00; background:url(../test.png)}",
103
+ "{color:#f00; background:url(nested/../test.png)}",
104
+ ],
105
+ ].each do |input, output|
106
+ subject.rewrite_urls(input, source_folder, target_folder).should == output
107
+ end
108
+ end
109
+ end
110
+
111
+ describe "extract_url" do
112
+ it "should extract urls" do
113
+ [
114
+ ["'test.png'", ["test.png", "'"]],
115
+ ['"test.png"', ["test.png", '"']],
116
+ ["test.png", ["test.png", ""]],
117
+ ].each do |input, output|
118
+ subject.extract_url(input).should == output
119
+ end
120
+ end
121
+ end
122
+
123
+ describe "dirname" do
124
+ it "should return the folder of the given path" do
125
+ [
126
+ ["test.png", ""],
127
+ ["test/test.png", "test"],
128
+ ["/test/nested/test.png", "/test/nested"],
129
+ ].each do |input, output|
130
+ subject.dirname(input).should == output
131
+ end
132
+ end
133
+ end
134
+ end
135
+ end
136
+ end
137
+ end
138
+
@@ -0,0 +1,18 @@
1
+ require 'merger/base'
2
+ require 'assets_booster/merger/simple'
3
+ module AssetsBooster
4
+ module Merger
5
+ describe Simple do
6
+ it_behaves_like "a merger" do
7
+ describe "merge" do
8
+ subject{ described_class.new(['a', 'b', 'c']) }
9
+ it "should merge source files" do
10
+ File.stub(:read){ |source| source }
11
+ subject.merge("target.css").should == "a\nb\nc"
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
18
+
@@ -0,0 +1,26 @@
1
+ require 'spec_helper'
2
+ require 'assets_booster/package/base'
3
+ module AssetsBooster
4
+ module Package
5
+ shared_examples_for "a package" do
6
+ subject{ described_class.new("test", []) }
7
+
8
+ before do
9
+ Rails.stub(:root){ "/rails" }
10
+ end
11
+
12
+ describe "name" do
13
+ it "should return a string" do
14
+ subject.name.should_not be_empty
15
+ end
16
+ end
17
+
18
+ describe "view_helper_method" do
19
+ it "should return a symbol" do
20
+ subject.view_helper_method.should be_an_instance_of(Symbol)
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
26
+
@@ -0,0 +1,16 @@
1
+ require 'package/base'
2
+ require 'assets_booster/package/javascript'
3
+ module AssetsBooster
4
+ module Package
5
+ describe Javascript do
6
+ it_behaves_like "a package" do
7
+ describe "asset_path" do
8
+ it "should return a javascript filename" do
9
+ subject.asset_path("lala").should match(/javascripts\/lala\.js/)
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
16
+
@@ -0,0 +1,16 @@
1
+ require 'package/base'
2
+ require 'assets_booster/package/stylesheet'
3
+ module AssetsBooster
4
+ module Package
5
+ describe Stylesheet do
6
+ it_behaves_like "a package" do
7
+ describe "asset_path" do
8
+ it "should return a stylesheet filename" do
9
+ subject.asset_path("lala").should match(/stylesheets\/lala\.css/)
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
16
+
@@ -0,0 +1,87 @@
1
+ require 'spec_helper'
2
+ require 'assets_booster/packager'
3
+ module AssetsBooster
4
+ describe Packager do
5
+ before do
6
+ Rails.stub(:root){ "/rails" }
7
+ @messages = []
8
+ AssetsBooster.stub(:log){ |message| @messages << message }
9
+ end
10
+
11
+ describe "create_configuration" do
12
+ it "should abort if a configuration file already exists" do
13
+ File.should_receive(:exists?).with(subject.configuration_filename).and_return(true)
14
+ File.should_not_receive(:open).with(subject.configuration_filename, "w")
15
+ subject.create_configuration
16
+ @messages[0].should match(/already exists/)
17
+ end
18
+
19
+ it "should create a configration file from current assets" do
20
+ Dir.should_receive(:[]).with("/rails/public/javascripts/*.js").and_return(%w(/rails/public/javascripts/jquery.js /rails/public/javascripts/rails.js))
21
+ Dir.should_receive(:[]).with("/rails/public/stylesheets/*.css").and_return(%w(/rails/public/stylesheets/screen.css))
22
+ File.should_receive(:open).with(subject.configuration_filename, "w").and_yield(nil)
23
+ YAML.should_receive(:dump) do |config, file|
24
+ config['packages']['javascript']['base'].size.should == 2
25
+ config['packages']['stylesheet']['base'].size.should == 1
26
+ end
27
+ subject.create_configuration
28
+ @messages[0].should match(/created/)
29
+ end
30
+ end
31
+
32
+ describe "compile_all" do
33
+ it "should compile all assets of all packages" do
34
+ YAML.should_receive(:load_file).with(subject.configuration_filename).and_return(
35
+ 'packages' => {
36
+ 'javascript' => {
37
+ 'base' => %w(jquery rails),
38
+ },
39
+ 'stylesheet' => {
40
+ 'base' => %w(screen),
41
+ },
42
+ },
43
+ 'options' => {
44
+ 'javascript' => {
45
+ 'compiler' => "uglify",
46
+ },
47
+ 'stylesheet' => {
48
+ 'compiler' => "rainpress",
49
+ },
50
+ 'environments' => %w(test),
51
+ }
52
+ )
53
+
54
+ # merging css
55
+ File.should_receive(:read).with("/rails/public/stylesheets/screen.css").once.and_return("html { color: #f00; }")
56
+ file = double("File")
57
+ file.should_receive(:write).with("html { color: #f00; }").once
58
+ File.should_receive(:open).with("/rails/public/stylesheets/base_packaged.css", "w").twice.and_yield(file)
59
+ File.should_receive(:mtime).with("/rails/public/stylesheets/screen.css").once.and_return(13)
60
+ File.should_receive(:utime).with(13, 13, "/rails/public/stylesheets/base_packaged.css").twice
61
+
62
+ # compiling css
63
+ file.should_receive(:write).with("html{color:red}").once
64
+
65
+ # merging js
66
+ File.should_receive(:read).with("/rails/public/javascripts/jquery.js").once.and_return("var action = 'jquery'; alert('jquery')")
67
+ File.should_receive(:read).with("/rails/public/javascripts/rails.js").once.and_return("action = 4*3; alert('rails')")
68
+ file = double("File")
69
+ file.should_receive(:write).with("var action = 'jquery'; alert('jquery')\naction = 4*3; alert('rails')").once
70
+ File.should_receive(:open).with("/rails/public/javascripts/base_packaged.js", "w").twice.and_yield(file)
71
+ File.should_receive(:mtime).with("/rails/public/javascripts/jquery.js").once.and_return(13)
72
+ File.should_receive(:mtime).with("/rails/public/javascripts/rails.js").once.and_return(14)
73
+ File.should_receive(:utime).with(14, 14, "/rails/public/javascripts/base_packaged.js").twice
74
+
75
+ # compiling js
76
+ file.should_receive(:write).with("var action=\"jquery\";alert(\"jquery\"),action=12,alert(\"rails\")").once
77
+
78
+ subject.compile_all
79
+ @messages[0].should match(/Merging assets.*CSS Merger/)
80
+ @messages[1].should match(/Compiling.*Rainpress/)
81
+ @messages[3].should match(/Merging assets.*Simple Merger/)
82
+ @messages[4].should match(/Compiling.*UglifyJS/)
83
+ end
84
+ end
85
+ end
86
+ end
87
+