assets_booster 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,22 @@
1
+ module AssetsBooster
2
+ module Merger
3
+ class Simple
4
+ def self.name
5
+ "Simple Merger"
6
+ end
7
+
8
+ def self.merge(sources, target)
9
+ sources.inject("") do |code, source|
10
+ File.open(source, "r") do |file|
11
+ code << file.read.strip+"\n"
12
+ end
13
+ end.strip
14
+ end
15
+
16
+ def self.mtime(sources)
17
+ sources.map{ |source| File.mtime(source) }.max
18
+ end
19
+ end
20
+ end
21
+ end
22
+
@@ -0,0 +1,71 @@
1
+ module AssetsBooster
2
+ module Package
3
+ class Base
4
+ attr_accessor :name
5
+ attr_accessor :filename
6
+ attr_accessor :assets
7
+ attr_accessor :mtime
8
+
9
+ def initialize(name, assets)
10
+ self.name = name+"_packaged"
11
+ self.assets = assets
12
+ self.filename = self.class.asset_path(self.name)
13
+ end
14
+
15
+ def exists?
16
+ File.exists?(filename)
17
+ end
18
+
19
+ def mtime
20
+ @mtime ||= self.class.merger.mtime(sources)
21
+ end
22
+
23
+ def sources
24
+ assets.each.map{ |asset| self.class.asset_path(asset) }
25
+ end
26
+
27
+ def delete
28
+ File.delete(filename) if File.exists?(filename)
29
+ end
30
+
31
+ def merge
32
+ AssetsBooster.log("Merging assets using #{self.class.merger.name} to #{relative_filename}...")
33
+ save(self.class.merger.merge(sources, filename))
34
+ end
35
+
36
+ def compile
37
+ merged = merge
38
+ AssetsBooster.log("Compiling #{relative_filename} using #{self.class.compiler.name}...")
39
+ code = self.class.compiler.compile(merged)
40
+ AssetsBooster.log("Compilation finished: %5.2f%% saved." % [(1-code.size.to_f/merged.size)*100])
41
+ save(code)
42
+ end
43
+
44
+ def view_helper
45
+ sources = AssetsBooster::Configuration.boosted_environment? ? [name] : assets
46
+ [view_helper_method, sources]
47
+ end
48
+
49
+ private
50
+
51
+ def save(code)
52
+ File.open(filename, "w") do |file|
53
+ file.write(code)
54
+ end
55
+ File.utime(mtime, mtime, filename)
56
+ code
57
+ end
58
+
59
+ def read
60
+ File.open(filename, "r") do |file|
61
+ file.read
62
+ end
63
+ end
64
+
65
+ def relative_filename
66
+ filename.sub(Rails.root, ".")
67
+ end
68
+ end
69
+ end
70
+ end
71
+
@@ -0,0 +1,24 @@
1
+ module AssetsBooster
2
+ module Package
3
+ class Javascript < Base
4
+ def self.compiler
5
+ AssetsBooster::Configuration.compiler_for_type(:javascript)
6
+ end
7
+
8
+ def self.merger
9
+ require "assets_booster/merger/simple"
10
+ AssetsBooster::Merger::Simple
11
+ end
12
+
13
+ def self.asset_path(name)
14
+ path = AssetsBooster::Configuration.asset_path("javascripts")
15
+ path = File.join(path, name+".js") if name
16
+ end
17
+
18
+ def view_helper_method
19
+ :javascript_include_tag
20
+ end
21
+ end
22
+ end
23
+ end
24
+
@@ -0,0 +1,24 @@
1
+ module AssetsBooster
2
+ module Package
3
+ class Stylesheet < Base
4
+ def self.compiler
5
+ AssetsBooster::Configuration.compiler_for_type(:stylesheet)
6
+ end
7
+
8
+ def self.merger
9
+ require "assets_booster/merger/css"
10
+ AssetsBooster::Merger::CSS
11
+ end
12
+
13
+ def self.asset_path(name)
14
+ path = AssetsBooster::Configuration.asset_path("stylesheets")
15
+ path = File.join(path, name+".css") if name
16
+ end
17
+
18
+ def view_helper_method
19
+ :stylesheet_link_tag
20
+ end
21
+ end
22
+ end
23
+ end
24
+
@@ -0,0 +1,33 @@
1
+ module AssetsBooster
2
+ class Packager
3
+ cattr_accessor :packages
4
+
5
+ def self.init
6
+ require "assets_booster/configuration"
7
+ Configuration.load
8
+ end
9
+
10
+ def self.merge_all
11
+ each_package { |package| package.merge }
12
+ end
13
+
14
+ def self.compile_all
15
+ each_package { |package| package.compile }
16
+ end
17
+
18
+ def self.delete_all
19
+ each_package { |package| package.delete }
20
+ end
21
+
22
+ private
23
+
24
+ def self.each_package(&block)
25
+ packages.each_value do |packages|
26
+ packages.each_value do |package|
27
+ block.call(package)
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
33
+
@@ -0,0 +1,19 @@
1
+ require 'rails'
2
+
3
+ module AssetsBooster
4
+ class Railtie < Rails::Railtie
5
+ config.after_initialize do
6
+ require "assets_booster/packager"
7
+ AssetsBooster::Packager.init
8
+ ActiveSupport.on_load :action_view do
9
+ require 'assets_booster/view_helper'
10
+ ActionView::Base.send(:include, AssetsBooster::ViewHelper)
11
+ end
12
+ end
13
+
14
+ rake_tasks do
15
+ load "assets_booster/tasks/tasks.rake"
16
+ end
17
+ end
18
+ end
19
+
@@ -0,0 +1,29 @@
1
+ require "assets_booster/configuration"
2
+ require "assets_booster/packager"
3
+
4
+ namespace :assets_booster do
5
+ desc 'Merge assets'
6
+ task :merge do
7
+ AssetsBooster::Packager.init
8
+ AssetsBooster::Packager.merge_all
9
+ end
10
+
11
+ desc 'Merge and compile assets'
12
+ task :compile do
13
+ AssetsBooster::Packager.init
14
+ AssetsBooster::Packager.compile_all
15
+ end
16
+
17
+ desc 'Delete all asset builds'
18
+ task :delete do
19
+ AssetsBooster::Packager.init
20
+ AssetsBooster::Packager.delete_all
21
+ end
22
+
23
+ desc 'Generate assets_booster.yml from existing assets'
24
+ task :setup do
25
+ AssetsBooster::Packager.init
26
+ AssetsBooster::Configuration.create
27
+ end
28
+ end
29
+
@@ -0,0 +1,3 @@
1
+ module AssetsBooster
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,17 @@
1
+ module AssetsBooster
2
+ module ViewHelper
3
+ def should_merge?
4
+ AssetPackage.merge_environments.include?(Rails.env)
5
+ end
6
+
7
+ def assets_booster_tag(type, *names)
8
+ options = names.extract_options!
9
+ packages = AssetsBooster::Packager.packages[type]
10
+ html = names.map do |name|
11
+ methode, sources = packages[name].view_helper
12
+ send(methode, sources, options)
13
+ end*"\n"
14
+ html.html_safe
15
+ end
16
+ end
17
+ end
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: assets_booster
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Corin Langosch
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-03-20 00:00:00 +01:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: Instead of sending down a dozen JavaScript and CSS files full of formatting and comments, this gem makes it simple to merge and compress these into one or more files, increasing speed and saving bandwidth.
23
+ email:
24
+ - info@netskin.com
25
+ executables: []
26
+
27
+ extensions: []
28
+
29
+ extra_rdoc_files: []
30
+
31
+ files:
32
+ - .gitignore
33
+ - Gemfile
34
+ - README
35
+ - Rakefile
36
+ - asset_booster.gemspec
37
+ - lib/assets_booster.rb
38
+ - lib/assets_booster/compiler/closure.rb
39
+ - lib/assets_booster/compiler/dummy.rb
40
+ - lib/assets_booster/compiler/jsmin.rb
41
+ - lib/assets_booster/compiler/node-js/parse-js.js
42
+ - lib/assets_booster/compiler/node-js/process.js
43
+ - lib/assets_booster/compiler/node-js/squeeze-more.js
44
+ - lib/assets_booster/compiler/node-js/uglify.js
45
+ - lib/assets_booster/compiler/rainpress.rb
46
+ - lib/assets_booster/compiler/uglify.rb
47
+ - lib/assets_booster/configuration.rb
48
+ - lib/assets_booster/merger/css.rb
49
+ - lib/assets_booster/merger/simple.rb
50
+ - lib/assets_booster/package/base.rb
51
+ - lib/assets_booster/package/javascript.rb
52
+ - lib/assets_booster/package/stylesheet.rb
53
+ - lib/assets_booster/packager.rb
54
+ - lib/assets_booster/railtie.rb
55
+ - lib/assets_booster/tasks/tasks.rake
56
+ - lib/assets_booster/version.rb
57
+ - lib/assets_booster/view_helper.rb
58
+ has_rdoc: true
59
+ homepage: ""
60
+ licenses: []
61
+
62
+ post_install_message:
63
+ rdoc_options: []
64
+
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ hash: 3
73
+ segments:
74
+ - 0
75
+ version: "0"
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ none: false
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ hash: 3
82
+ segments:
83
+ - 0
84
+ version: "0"
85
+ requirements: []
86
+
87
+ rubyforge_project: assets_booster
88
+ rubygems_version: 1.5.3
89
+ signing_key:
90
+ specification_version: 3
91
+ summary: Assets (javascripts, css) compression for rails applications
92
+ test_files: []
93
+