rails_asset_packager 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,90 @@
1
+ require 'yaml'
2
+ require File.dirname(__FILE__) + '/../rails_asset_packager/asset_package'
3
+
4
+ namespace :asset do
5
+ namespace :packager do
6
+
7
+ desc "Merge and compress assets"
8
+ task :build_all do
9
+ RailsAssetPackager::AssetPackage.build_all
10
+ end
11
+
12
+ desc "Delete all asset builds"
13
+ task :delete_all do
14
+ RailsAssetPackager::AssetPackage.delete_all
15
+ end
16
+
17
+ desc "Generate asset_packages.yml from existing assets"
18
+ task :create_yml do
19
+ RailsAssetPackager::AssetPackage.create_yml
20
+ end
21
+
22
+ end
23
+
24
+ namespace :cache do
25
+ S3_CONFIG = YAML.load_file(File.join(Rails.root, "config/s3.yml"))[Rails.env] rescue nil || {}
26
+ ASSET_PKG_CONFIG = YAML.load_file(File.join(Rails.root, "config/asset_packages.yml")) rescue nil || {}
27
+
28
+ desc "start aws"
29
+ task :aws do
30
+ require 'aws/s3'
31
+
32
+ AWS::S3::Base.establish_connection!(
33
+ :access_key_id => S3_CONFIG['access_key_id'],
34
+ :secret_access_key => S3_CONFIG['secret_access_key']
35
+ )
36
+ end
37
+
38
+ desc "update s3"
39
+ task :s3 => :aws do
40
+ prefix = ''
41
+
42
+ files = []
43
+
44
+ ASSET_PKG_CONFIG.keys.each do |set|
45
+ if set == 'javascripts' || set == 'stylesheets'
46
+ ext = (set == 'javascripts') ? "js" : "css"
47
+
48
+ ASSET_PKG_CONFIG[set].each do |val|
49
+ val.keys.each do |val|
50
+ files << "./public/#{set}/#{val}_packaged.#{ext}"
51
+ end
52
+ end
53
+ elsif set == 'uploads'
54
+ ASSET_PKG_CONFIG[set].each do |val|
55
+ if File.directory?("./public/#{val}")
56
+ files = files + Dir.glob("./public/#{val}/**/*.*")
57
+ end
58
+ end
59
+
60
+ files.uniq!
61
+ end
62
+ end
63
+
64
+ files.each do |f|
65
+ next if File.directory?(f)
66
+ puts f
67
+
68
+ key = f.gsub(/\.\/public/, prefix)
69
+ puts " -> %s" % key
70
+
71
+ AWS::S3::S3Object.store(
72
+ key,
73
+ File.open(f),
74
+ S3_CONFIG['bucket'],
75
+ :access => :public_read,
76
+ 'Cache-Control' => 'max-age=315360000',
77
+ 'x-amz-storage-class' => 'REDUCED_REDUNDANCY'
78
+ )
79
+ end
80
+ end
81
+
82
+ desc "cache assets and update s3 for production"
83
+ task :production do
84
+ Rake::Task['asset:packager:delete_all'].invoke
85
+ Rake::Task['asset:packager:build_all'].invoke
86
+ Rake::Task['asset:cache:s3'].invoke
87
+
88
+ end
89
+ end
90
+ end
data/rails/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'rails_asset_packager'
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "rails_asset_packager/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "rails_asset_packager"
7
+ s.version = RailsAssetPackager::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Scott Becker","Katherine G Pe","James Thompson"]
10
+ s.email = ["","","james@plainprograms.com"]
11
+ s.homepage = "http://rubygems.org/gems/rails_asset_packager"
12
+ s.summary = %q{Javascript and CSS asset compression for Rails}
13
+ s.description = %q{A tool to compress Javascript and CSS assets for production Rals applications.}
14
+
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
+ s.require_paths = ["lib"]
19
+
20
+ s.add_dependency "aws-s3", "~> 0.6.2"
21
+ end
@@ -0,0 +1,102 @@
1
+ $:.unshift(File.dirname(__FILE__) + '/../lib')
2
+
3
+ ENV['RAILS_ENV'] = "development"
4
+ require File.dirname(__FILE__) + '/../../../../config/environment'
5
+ require 'test/unit'
6
+ require 'rubygems'
7
+ require 'mocha'
8
+
9
+ require 'action_controller/test_process'
10
+
11
+ ActionController::Base.logger = nil
12
+ ActionController::Routing::Routes.reload rescue nil
13
+
14
+ class AssetPackageHelperDevelopmentTest < Test::Unit::TestCase
15
+ include ActionController::Assertions::DomAssertions
16
+ include ActionController::TestCase::Assertions
17
+ include ActionView::Helpers::TagHelper
18
+ include ActionView::Helpers::AssetTagHelper
19
+ include RailsAssetPackager::AssetPackageHelper
20
+
21
+ def setup
22
+ RailsAssetPackager::AssetPackage.asset_base_path = "#{Rails.root}/vendor/plugins/asset_packager/test/assets"
23
+ RailsAssetPackager::AssetPackage.asset_packages_yml = YAML.load_file("#{Rails.root}/vendor/plugins/asset_packager/test/asset_packages.yml")
24
+
25
+ RailsAssetPackager::AssetPackage.any_instance.stubs(:log)
26
+
27
+ @controller = Class.new do
28
+ def request
29
+ @request ||= ActionController::TestRequest.new
30
+ end
31
+ end.new
32
+ end
33
+
34
+ def build_js_expected_string(*sources)
35
+ sources.map {|s| javascript_include_tag(s) }.join("\n")
36
+ end
37
+
38
+ def build_css_expected_string(*sources)
39
+ sources.map {|s| stylesheet_link_tag(s) }.join("\n")
40
+ end
41
+
42
+ def test_js_basic
43
+ assert_dom_equal build_js_expected_string("prototype"),
44
+ javascript_include_merged("prototype")
45
+ end
46
+
47
+ def test_js_multiple_packages
48
+ assert_dom_equal build_js_expected_string("prototype", "foo"),
49
+ javascript_include_merged("prototype", "foo")
50
+ end
51
+
52
+ def test_js_unpackaged_file
53
+ assert_dom_equal build_js_expected_string("prototype", "foo", "not_part_of_a_package"),
54
+ javascript_include_merged("prototype", "foo", "not_part_of_a_package")
55
+ end
56
+
57
+ def test_js_multiple_from_same_package
58
+ assert_dom_equal build_js_expected_string("prototype", "effects", "controls", "not_part_of_a_package", "foo"),
59
+ javascript_include_merged("prototype", "effects", "controls", "not_part_of_a_package", "foo")
60
+ end
61
+
62
+ def test_js_by_package_name
63
+ assert_dom_equal build_js_expected_string("prototype", "effects", "controls", "dragdrop"),
64
+ javascript_include_merged(:base)
65
+ end
66
+
67
+ def test_js_multiple_package_names
68
+ assert_dom_equal build_js_expected_string("prototype", "effects", "controls", "dragdrop", "foo", "bar", "application"),
69
+ javascript_include_merged(:base, :secondary)
70
+ end
71
+
72
+ def test_css_basic
73
+ assert_dom_equal build_css_expected_string("screen"),
74
+ stylesheet_link_merged("screen")
75
+ end
76
+
77
+ def test_css_multiple_packages
78
+ assert_dom_equal build_css_expected_string("screen", "foo", "subdir/bar"),
79
+ stylesheet_link_merged("screen", "foo", "subdir/bar")
80
+ end
81
+
82
+ def test_css_unpackaged_file
83
+ assert_dom_equal build_css_expected_string("screen", "foo", "not_part_of_a_package", "subdir/bar"),
84
+ stylesheet_link_merged("screen", "foo", "not_part_of_a_package", "subdir/bar")
85
+ end
86
+
87
+ def test_css_multiple_from_same_package
88
+ assert_dom_equal build_css_expected_string("screen", "header", "not_part_of_a_package", "foo", "bar", "subdir/foo", "subdir/bar"),
89
+ stylesheet_link_merged("screen", "header", "not_part_of_a_package", "foo", "bar", "subdir/foo", "subdir/bar")
90
+ end
91
+
92
+ def test_css_by_package_name
93
+ assert_dom_equal build_css_expected_string("screen", "header"),
94
+ stylesheet_link_merged(:base)
95
+ end
96
+
97
+ def test_css_multiple_package_names
98
+ assert_dom_equal build_css_expected_string("screen", "header", "foo", "bar", "subdir/foo", "subdir/bar"),
99
+ stylesheet_link_merged(:base, :secondary, "subdir/styles")
100
+ end
101
+
102
+ end
@@ -0,0 +1,142 @@
1
+ $:.unshift(File.dirname(__FILE__) + '/../lib')
2
+
3
+ require File.dirname(__FILE__) + '/../../../../config/environment'
4
+ require 'test/unit'
5
+ require 'rubygems'
6
+ require 'mocha'
7
+
8
+ require 'action_controller/test_process'
9
+
10
+ ActionController::Base.logger = nil
11
+ ActionController::Routing::Routes.reload rescue nil
12
+
13
+ class AssetPackageHelperProductionTest < Test::Unit::TestCase
14
+ include ActionController::Assertions::DomAssertions
15
+ include ActionController::TestCase::Assertions
16
+ include ActionView::Helpers::TagHelper
17
+ include ActionView::Helpers::AssetTagHelper
18
+ include RailsAssetPackager::AssetPackageHelper
19
+
20
+ cattr_accessor :packages_built
21
+
22
+ def setup
23
+ RailsAssetPackager::AssetPackage.asset_base_path = "#{Rails.root}/vendor/plugins/asset_packager/test/assets"
24
+ RailsAssetPackager::AssetPackage.asset_packages_yml = YAML.load_file("#{Rails.root}/vendor/plugins/asset_packager/test/asset_packages.yml")
25
+
26
+ RailsAssetPackager::AssetPackage.any_instance.stubs(:log)
27
+ self.stubs(:should_merge?).returns(true)
28
+
29
+ @controller = Class.new do
30
+ def request
31
+ @request ||= ActionController::TestRequest.new
32
+ end
33
+ end.new
34
+
35
+ build_packages_once
36
+ end
37
+
38
+ def build_packages_once
39
+ unless @@packages_built
40
+ RailsAssetPackager::AssetPackage.build_all
41
+ @@packages_built = true
42
+ end
43
+ end
44
+
45
+ def build_js_expected_string(*sources)
46
+ sources.map {|s| javascript_include_tag(s) }.join("\n")
47
+ end
48
+
49
+ def build_css_expected_string(*sources)
50
+ sources.map {|s| stylesheet_link_tag(s) }.join("\n")
51
+ end
52
+
53
+ def test_js_basic
54
+ current_file = RailsAssetPackager::AssetPackage.find_by_source("javascripts", "prototype").current_file
55
+ assert_dom_equal build_js_expected_string(current_file),
56
+ javascript_include_merged("prototype")
57
+ end
58
+
59
+ def test_js_multiple_packages
60
+ current_file1 = RailsAssetPackager::AssetPackage.find_by_source("javascripts", "prototype").current_file
61
+ current_file2 = RailsAssetPackager::AssetPackage.find_by_source("javascripts", "foo").current_file
62
+
63
+ assert_dom_equal build_js_expected_string(current_file1, current_file2),
64
+ javascript_include_merged("prototype", "foo")
65
+ end
66
+
67
+ def test_js_unpackaged_file
68
+ current_file1 = RailsAssetPackager::AssetPackage.find_by_source("javascripts", "prototype").current_file
69
+ current_file2 = RailsAssetPackager::AssetPackage.find_by_source("javascripts", "foo").current_file
70
+
71
+ assert_dom_equal build_js_expected_string(current_file1, current_file2, "not_part_of_a_package"),
72
+ javascript_include_merged("prototype", "foo", "not_part_of_a_package")
73
+ end
74
+
75
+ def test_js_multiple_from_same_package
76
+ current_file1 = RailsAssetPackager::AssetPackage.find_by_source("javascripts", "prototype").current_file
77
+ current_file2 = RailsAssetPackager::AssetPackage.find_by_source("javascripts", "foo").current_file
78
+
79
+ assert_dom_equal build_js_expected_string(current_file1, "not_part_of_a_package", current_file2),
80
+ javascript_include_merged("prototype", "effects", "controls", "not_part_of_a_package", "foo")
81
+ end
82
+
83
+ def test_js_by_package_name
84
+ package_name = RailsAssetPackager::AssetPackage.find_by_target("javascripts", "base").current_file
85
+ assert_dom_equal build_js_expected_string(package_name),
86
+ javascript_include_merged(:base)
87
+ end
88
+
89
+ def test_js_multiple_package_names
90
+ package_name1 = RailsAssetPackager::AssetPackage.find_by_target("javascripts", "base").current_file
91
+ package_name2 = RailsAssetPackager::AssetPackage.find_by_target("javascripts", "secondary").current_file
92
+ assert_dom_equal build_js_expected_string(package_name1, package_name2),
93
+ javascript_include_merged(:base, :secondary)
94
+ end
95
+
96
+ def test_css_basic
97
+ current_file = RailsAssetPackager::AssetPackage.find_by_source("stylesheets", "screen").current_file
98
+ assert_dom_equal build_css_expected_string(current_file),
99
+ stylesheet_link_merged("screen")
100
+ end
101
+
102
+ def test_css_multiple_packages
103
+ current_file1 = RailsAssetPackager::AssetPackage.find_by_source("stylesheets", "screen").current_file
104
+ current_file2 = RailsAssetPackager::AssetPackage.find_by_source("stylesheets", "foo").current_file
105
+ current_file3 = RailsAssetPackager::AssetPackage.find_by_source("stylesheets", "subdir/bar").current_file
106
+
107
+ assert_dom_equal build_css_expected_string(current_file1, current_file2, current_file3),
108
+ stylesheet_link_merged("screen", "foo", "subdir/bar")
109
+ end
110
+
111
+ def test_css_unpackaged_file
112
+ current_file1 = RailsAssetPackager::AssetPackage.find_by_source("stylesheets", "screen").current_file
113
+ current_file2 = RailsAssetPackager::AssetPackage.find_by_source("stylesheets", "foo").current_file
114
+
115
+ assert_dom_equal build_css_expected_string(current_file1, current_file2, "not_part_of_a_package"),
116
+ stylesheet_link_merged("screen", "foo", "not_part_of_a_package")
117
+ end
118
+
119
+ def test_css_multiple_from_same_package
120
+ current_file1 = RailsAssetPackager::AssetPackage.find_by_source("stylesheets", "screen").current_file
121
+ current_file2 = RailsAssetPackager::AssetPackage.find_by_source("stylesheets", "foo").current_file
122
+ current_file3 = RailsAssetPackager::AssetPackage.find_by_source("stylesheets", "subdir/bar").current_file
123
+
124
+ assert_dom_equal build_css_expected_string(current_file1, "not_part_of_a_package", current_file2, current_file3),
125
+ stylesheet_link_merged("screen", "header", "not_part_of_a_package", "foo", "bar", "subdir/foo", "subdir/bar")
126
+ end
127
+
128
+ def test_css_by_package_name
129
+ package_name = RailsAssetPackager::AssetPackage.find_by_target("stylesheets", "base").current_file
130
+ assert_dom_equal build_css_expected_string(package_name),
131
+ stylesheet_link_merged(:base)
132
+ end
133
+
134
+ def test_css_multiple_package_names
135
+ package_name1 = RailsAssetPackager::AssetPackage.find_by_target("stylesheets", "base").current_file
136
+ package_name2 = RailsAssetPackager::AssetPackage.find_by_target("stylesheets", "secondary").current_file
137
+ package_name3 = RailsAssetPackager::AssetPackage.find_by_target("stylesheets", "subdir/styles").current_file
138
+ assert_dom_equal build_css_expected_string(package_name1, package_name2, package_name3),
139
+ stylesheet_link_merged(:base, :secondary, "subdir/styles")
140
+ end
141
+
142
+ end
@@ -0,0 +1,91 @@
1
+ require File.dirname(__FILE__) + '/../../../../config/environment'
2
+ require 'test/unit'
3
+ require 'mocha'
4
+
5
+ class AssetPackagerTest < Test::Unit::TestCase
6
+ include RailsAssetPackager
7
+
8
+ def setup
9
+ RailsAssetPackager::AssetPackage.asset_base_path = "#{Rails.root}/vendor/plugins/asset_packager/test/assets"
10
+ RailsAssetPackager::AssetPackage.asset_packages_yml = YAML.load_file("#{Rails.root}/vendor/plugins/asset_packager/test/asset_packages.yml")
11
+
12
+ RailsAssetPackager::AssetPackage.any_instance.stubs(:log)
13
+ RailsAssetPackager::AssetPackage.build_all
14
+ end
15
+
16
+ def teardown
17
+ RailsAssetPackager::AssetPackage.delete_all
18
+ end
19
+
20
+ def test_find_by_type
21
+ js_asset_packages = RailsAssetPackager::AssetPackage.find_by_type("javascripts")
22
+ assert_equal 2, js_asset_packages.length
23
+ assert_equal "base", js_asset_packages[0].target
24
+ assert_equal ["prototype", "effects", "controls", "dragdrop"], js_asset_packages[0].sources
25
+ end
26
+
27
+ def test_find_by_target
28
+ package = RailsAssetPackager::AssetPackage.find_by_target("javascripts", "base")
29
+ assert_equal "base", package.target
30
+ assert_equal ["prototype", "effects", "controls", "dragdrop"], package.sources
31
+ end
32
+
33
+ def test_find_by_source
34
+ package = RailsAssetPackager::AssetPackage.find_by_source("javascripts", "controls")
35
+ assert_equal "base", package.target
36
+ assert_equal ["prototype", "effects", "controls", "dragdrop"], package.sources
37
+ end
38
+
39
+ def test_delete_and_build
40
+ RailsAssetPackager::AssetPackage.delete_all
41
+ js_package_names = Dir.new("#{RailsAssetPackager::AssetPackage.asset_base_path}/javascripts").entries.delete_if { |x| ! (x =~ /\A\w+_packaged.js/) }
42
+ css_package_names = Dir.new("#{RailsAssetPackager::AssetPackage.asset_base_path}/stylesheets").entries.delete_if { |x| ! (x =~ /\A\w+_packaged.css/) }
43
+ css_subdir_package_names = Dir.new("#{RailsAssetPackager::AssetPackage.asset_base_path}/stylesheets/subdir").entries.delete_if { |x| ! (x =~ /\A\w+_packaged.css/) }
44
+
45
+ assert_equal 0, js_package_names.length
46
+ assert_equal 0, css_package_names.length
47
+ assert_equal 0, css_subdir_package_names.length
48
+
49
+ RailsAssetPackager::AssetPackage.build_all
50
+ js_package_names = Dir.new("#{RailsAssetPackager::AssetPackage.asset_base_path}/javascripts").entries.delete_if { |x| ! (x =~ /\A\w+_packaged.js/) }.sort
51
+ css_package_names = Dir.new("#{RailsAssetPackager::AssetPackage.asset_base_path}/stylesheets").entries.delete_if { |x| ! (x =~ /\A\w+_packaged.css/) }.sort
52
+ css_subdir_package_names = Dir.new("#{RailsAssetPackager::AssetPackage.asset_base_path}/stylesheets/subdir").entries.delete_if { |x| ! (x =~ /\A\w+_packaged.css/) }.sort
53
+
54
+ assert_equal 2, js_package_names.length
55
+ assert_equal 2, css_package_names.length
56
+ assert_equal 1, css_subdir_package_names.length
57
+ assert js_package_names[0].match(/\Abase_packaged.js\z/)
58
+ assert js_package_names[1].match(/\Asecondary_packaged.js\z/)
59
+ assert css_package_names[0].match(/\Abase_packaged.css\z/)
60
+ assert css_package_names[1].match(/\Asecondary_packaged.css\z/)
61
+ assert css_subdir_package_names[0].match(/\Astyles_packaged.css\z/)
62
+ end
63
+
64
+ def test_js_names_from_sources
65
+ package_names = RailsAssetPackager::AssetPackage.targets_from_sources("javascripts", ["prototype", "effects", "noexist1", "controls", "foo", "noexist2"])
66
+ assert_equal 4, package_names.length
67
+ assert package_names[0].match(/\Abase_packaged\z/)
68
+ assert_equal package_names[1], "noexist1"
69
+ assert package_names[2].match(/\Asecondary_packaged\z/)
70
+ assert_equal package_names[3], "noexist2"
71
+ end
72
+
73
+ def test_css_names_from_sources
74
+ package_names = RailsAssetPackager::AssetPackage.targets_from_sources("stylesheets", ["header", "screen", "noexist1", "foo", "noexist2"])
75
+ assert_equal 4, package_names.length
76
+ assert package_names[0].match(/\Abase_packaged\z/)
77
+ assert_equal package_names[1], "noexist1"
78
+ assert package_names[2].match(/\Asecondary_packaged\z/)
79
+ assert_equal package_names[3], "noexist2"
80
+ end
81
+
82
+ def test_should_return_merge_environments_when_set
83
+ RailsAssetPackager::AssetPackage.merge_environments = ["staging", "production"]
84
+ assert_equal ["staging", "production"], RailsAssetPackager::AssetPackage.merge_environments
85
+ end
86
+
87
+ def test_should_only_return_production_merge_environment_when_not_set
88
+ assert_equal ["production"], RailsAssetPackager::AssetPackage.merge_environments
89
+ end
90
+
91
+ end
@@ -0,0 +1,20 @@
1
+ javascripts:
2
+ - base:
3
+ - prototype
4
+ - effects
5
+ - controls
6
+ - dragdrop
7
+ - secondary:
8
+ - foo
9
+ - bar
10
+ - application
11
+ stylesheets:
12
+ - base:
13
+ - screen
14
+ - header
15
+ - secondary:
16
+ - foo
17
+ - bar
18
+ - subdir/styles:
19
+ - foo
20
+ - bar
@@ -0,0 +1,2 @@
1
+ // Place your application-specific JavaScript functions and classes here
2
+ // This file is automatically included by javascript_include_tag :defaults
@@ -0,0 +1,4 @@
1
+ bar bar bar
2
+ bar bar bar
3
+ bar bar bar
4
+