smart_asset 0.1.1

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/LICENSE ADDED
@@ -0,0 +1,18 @@
1
+ Copyright (c) 2010 Winton Welsh
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
4
+ this software and associated documentation files (the "Software"), to deal in
5
+ the Software without restriction, including without limitation the rights to
6
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
7
+ the Software, and to permit persons to whom the Software is furnished to do so,
8
+ subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all
11
+ copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
15
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
16
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
17
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,11 @@
1
+ SmartAsset
2
+ ===========
3
+
4
+ Smart asset packaging for Rails and Sinatra.
5
+
6
+ Requirements
7
+ ------------
8
+
9
+ <pre>
10
+ gem install smart_asset
11
+ </pre>
Binary file
data/bin/smart_asset ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require File.expand_path(File.dirname(__FILE__) + "/../lib/smart_asset")
4
+
5
+ SmartAsset.binary(ARGV[0] || Dir.pwd, ARGV[1])
Binary file
@@ -0,0 +1,5 @@
1
+ SmartAsset.env = Rails.env
2
+ SmartAsset.load_config(Rails.root)
3
+
4
+ ActionController::Base.send(:include, SmartAsset::Helper)
5
+ ActionController::Base.helper(SmartAsset::Helper)
@@ -0,0 +1,5 @@
1
+ SmartAsset.env = Rails.env
2
+ SmartAsset.load_config(Rails.root)
3
+
4
+ ActionController::Base.send(:include, SmartAsset::Helper)
5
+ ActionController::Base.helper(SmartAsset::Helper)
@@ -0,0 +1,15 @@
1
+ class SmartAsset
2
+ module Adapters
3
+ module Sinatra
4
+
5
+ def self.included(klass)
6
+ if klass.environment && klass.root
7
+ SmartAsset.env = klass.environment
8
+ SmartAsset.load_config klass.root
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
14
+
15
+ Sinatra::Base.send(:include, SmartAsset::Helper)
@@ -0,0 +1,45 @@
1
+ unless defined?(SmartAsset::Gems)
2
+
3
+ require 'rubygems'
4
+
5
+ class SmartAsset
6
+ class Gems
7
+
8
+ VERSIONS = {
9
+ :framework_fixture => '0.1.2',
10
+ :'rack-test' => '0.5.6',
11
+ :rake => '=0.8.7',
12
+ :rspec => '=1.3.1'
13
+ }
14
+
15
+ TYPES = {
16
+ :gemspec => [],
17
+ :gemspec_dev => [ :rspec ],
18
+ :lib => [],
19
+ :rake => [ :rake, :rspec ],
20
+ :spec_first => [ :framework_fixture ],
21
+ :spec => [ :'rack-test', :rspec ]
22
+ }
23
+
24
+ class <<self
25
+
26
+ def lockfile
27
+ file = File.expand_path('../../../gems', __FILE__)
28
+ unless File.exists?(file)
29
+ File.open(file, 'w') do |f|
30
+ Gem.loaded_specs.each do |key, value|
31
+ f.puts "#{key} #{value.version.version}"
32
+ end
33
+ end
34
+ end
35
+ end
36
+
37
+ def require(type=nil)
38
+ (TYPES[type] || TYPES.values.flatten.compact).each do |name|
39
+ gem name.to_s, VERSIONS[name]
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,26 @@
1
+ class SmartAsset
2
+ module Helper
3
+
4
+ def javascript_include_merged(*javascripts)
5
+ output = javascript_paths(*javascripts).collect { |js|
6
+ "<script src=\"#{js}\" type=\"text/javascript\"></script>"
7
+ }.join("\n")
8
+ defined?(Rails) && Rails.version[0..0] == '3' ? output.html_safe : output
9
+ end
10
+
11
+ def stylesheet_link_merged(*stylesheets)
12
+ output = stylesheet_paths(*stylesheets).collect { |css|
13
+ "<link href=\"#{css}\" media=\"screen\" rel=\"Stylesheet\" type=\"text/css\" />"
14
+ }.join("\n")
15
+ defined?(Rails) && Rails.version[0..0] == '3' ? output.html_safe : output
16
+ end
17
+
18
+ def javascript_paths(*javascripts)
19
+ javascripts.collect { |js| SmartAsset.paths('javascripts', js) }.flatten.uniq
20
+ end
21
+
22
+ def stylesheet_paths(*stylesheets)
23
+ stylesheets.collect { |css| SmartAsset.paths('stylesheets', css) }.flatten.uniq
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,3 @@
1
+ class SmartAsset
2
+ VERSION = "0.1.1" unless defined?(::SmartAsset::VERSION)
3
+ end
@@ -0,0 +1,156 @@
1
+ require File.dirname(__FILE__) + '/smart_asset/gems'
2
+
3
+ SmartAsset::Gems.require(:lib)
4
+
5
+ require 'fileutils'
6
+ require 'time'
7
+ require 'yaml'
8
+
9
+ $:.unshift File.dirname(__FILE__) + '/smart_asset'
10
+
11
+ require 'helper'
12
+ require 'version'
13
+
14
+ class SmartAsset
15
+ class <<self
16
+
17
+ attr_accessor :asset_host, :cache, :config, :dest, :env, :pub, :root, :sources
18
+
19
+ BIN = File.expand_path(File.dirname(__FILE__) + '/../bin')
20
+ CLOSURE_COMPILER = BIN + '/closure_compiler.jar'
21
+ YUI_COMPRESSOR = BIN + '/yui_compressor.jar'
22
+
23
+ def binary(root, relative_config=nil)
24
+ load_config root, relative_config
25
+ compress 'javascripts'
26
+ compress 'stylesheets'
27
+ end
28
+
29
+ def compress(type)
30
+ dir = "#{@pub}/#{@sources[type]}"
31
+ ext = ext_from_type type
32
+ FileUtils.mkdir_p @dest
33
+ (@config[type] || {}).each do |package, files|
34
+ create_package = false
35
+ compressed = {}
36
+ if files
37
+ files.each do |file|
38
+ if File.exists?(source = "#{dir}/#{file}.#{ext}")
39
+ modified = `cd #{@root} && git log --pretty=format:%cd -n 1 --date=iso #{@config['public']}/#{@sources[type]}/#{file}.#{ext}`
40
+ if modified.strip.empty?
41
+ modified = Time.now.utc.strftime("%Y%m%d%H%M%S")
42
+ else
43
+ modified = Time.parse(modified).utc.strftime("%Y%m%d%H%M%S")
44
+ end
45
+ file = file.to_s.gsub('/', '_')
46
+ unless File.exists?(destination = "#{@dest}/#{modified}_#{file}.#{ext}")
47
+ create_package = true
48
+ Dir["#{@dest}/*_#{file}.#{ext}"].each do |old|
49
+ FileUtils.rm old
50
+ end
51
+ puts "\nCompressing #{source}..."
52
+ if ext == 'js'
53
+ `java -jar #{CLOSURE_COMPILER} --js #{source} --js_output_file #{destination} --warning_level QUIET`
54
+ elsif ext == 'css'
55
+ `java -jar #{YUI_COMPRESSOR} #{source} -o #{destination}`
56
+ end
57
+ end
58
+ compressed[destination] = modified
59
+ end
60
+ end
61
+ if modified = compressed.values.compact.sort.last
62
+ old_packages = "#{@dest}/*_#{package}.#{ext}"
63
+ package = "#{@dest}/#{modified}_#{package}.#{ext}"
64
+ if create_package || !File.exists?(package)
65
+ Dir[old_packages].each do |old|
66
+ FileUtils.rm old
67
+ end
68
+ data = compressed.keys.collect do |file|
69
+ File.read file
70
+ end
71
+ File.open(package, 'w') { |f| f.write(data.join) }
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end
77
+
78
+ def load_config(root, relative_config=nil)
79
+ relative_config ||= 'config/assets.yml'
80
+ @root = File.expand_path(root)
81
+ @config = YAML::load(File.read("#{@root}/#{relative_config}"))
82
+
83
+ @config['asset_host'] ||= ActionController::Base.asset_host rescue nil
84
+ @config['public'] ||= 'public'
85
+ @config['destination'] ||= 'packaged'
86
+ @config['sources'] ||= {}
87
+ @config['sources']['javascripts'] ||= "javascripts"
88
+ @config['sources']['stylesheets'] ||= "stylesheets"
89
+
90
+ @asset_host = @config['asset_host']
91
+ @sources = @config['sources']
92
+
93
+ @pub = File.expand_path("#{@root}/#{@config['public']}")
94
+ @dest = "#{@pub}/#{@config['destination']}"
95
+ end
96
+
97
+ def paths(type, match)
98
+ match = match.to_s
99
+
100
+ @cache ||= {}
101
+ @cache[type] ||= {}
102
+
103
+ if @cache[type][match]
104
+ return @cache[type][match]
105
+ end
106
+
107
+ host =
108
+ @asset_host.respond_to?(:keys) ?
109
+ @asset_host[@env.to_s] :
110
+ @asset_host
111
+
112
+ ext = ext_from_type type
113
+
114
+ if @env.intern == :production
115
+ match = match.gsub('/', '_')
116
+ @cache[type][match] =
117
+ if result = Dir["#{@dest}/*_#{match}.#{ext}"].sort.last
118
+ [ "#{host}#{result.gsub(@pub, '')}" ]
119
+ else
120
+ []
121
+ end
122
+ elsif @config && @config[type]
123
+ result = @config[type].collect do |package, files|
124
+ if package.to_s == match
125
+ files.collect do |file|
126
+ file = "/#{@sources[type]}/#{file}.#{ext}"
127
+ "#{host}#{file}" if File.exists?("#{@pub}/#{file}")
128
+ end
129
+ elsif files
130
+ files.collect do |file|
131
+ if file.to_s == match
132
+ file = "/#{@sources[type]}/#{file}.#{ext}"
133
+ "#{host}#{file}" if File.exists?("#{@pub}/#{file}")
134
+ end
135
+ end
136
+ end
137
+ end
138
+ result.flatten.compact.uniq
139
+ end
140
+ end
141
+
142
+ private
143
+
144
+ def ext_from_type(type)
145
+ case type
146
+ when 'javascripts' then
147
+ 'js'
148
+ when 'stylesheets' then
149
+ 'css'
150
+ end
151
+ end
152
+ end
153
+ end
154
+
155
+ require "adapters/rails#{Rails.version[0..0]}" if defined?(Rails)
156
+ require "adapters/sinatra" if defined?(Sinatra)
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: smart_asset
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 1
9
+ version: 0.1.1
10
+ platform: ruby
11
+ authors:
12
+ - Winton Welsh
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-12-06 00:00:00 -08:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: rspec
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - "="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 1
30
+ - 3
31
+ - 1
32
+ version: 1.3.1
33
+ type: :development
34
+ version_requirements: *id001
35
+ description: ""
36
+ email:
37
+ - mail@wintoni.us
38
+ executables:
39
+ - closure_compiler.jar
40
+ - smart_asset
41
+ - yui_compressor.jar
42
+ extensions: []
43
+
44
+ extra_rdoc_files: []
45
+
46
+ files:
47
+ - bin/closure_compiler.jar
48
+ - bin/smart_asset
49
+ - bin/yui_compressor.jar
50
+ - lib/smart_asset/adapters/rails2.rb
51
+ - lib/smart_asset/adapters/rails3.rb
52
+ - lib/smart_asset/adapters/sinatra.rb
53
+ - lib/smart_asset/gems.rb
54
+ - lib/smart_asset/helper.rb
55
+ - lib/smart_asset/version.rb
56
+ - lib/smart_asset.rb
57
+ - LICENSE
58
+ - README.md
59
+ has_rdoc: true
60
+ homepage: http://github.com/winton/smart_asset
61
+ licenses: []
62
+
63
+ post_install_message:
64
+ rdoc_options: []
65
+
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
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
+ segments:
82
+ - 0
83
+ version: "0"
84
+ requirements: []
85
+
86
+ rubyforge_project:
87
+ rubygems_version: 1.3.7
88
+ signing_key:
89
+ specification_version: 3
90
+ summary: ""
91
+ test_files: []
92
+