stapler 0.1.0

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,20 @@
1
+ Copyright (c) 2010 Michael Rykov
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,47 @@
1
+ = stapler
2
+
3
+ == DESCRIPTION:
4
+
5
+ Stapler uses YUI Compressor to automatically compress cached assets in Rails
6
+
7
+ == FEATURES/PROBLEMS:
8
+
9
+ * Support both CSS and Javascript
10
+
11
+ == REQUIREMENTS:
12
+
13
+ * yuicompressor gem
14
+
15
+ == INSTALL:
16
+
17
+ * gem install stapler
18
+
19
+ == REFERENCES:
20
+
21
+ * YUI Compressor
22
+ http://developer.yahoo.com/yui/compressor/
23
+
24
+ == LICENSE:
25
+
26
+ (The MIT License)
27
+
28
+ Copyright (c) 2009 Michael Rykov
29
+
30
+ Permission is hereby granted, free of charge, to any person obtaining
31
+ a copy of this software and associated documentation files (the
32
+ 'Software'), to deal in the Software without restriction, including
33
+ without limitation the rights to use, copy, modify, merge, publish,
34
+ distribute, sublicense, and/or sell copies of the Software, and to
35
+ permit persons to whom the Software is furnished to do so, subject to
36
+ the following conditions:
37
+
38
+ The above copyright notice and this permission notice shall be
39
+ included in all copies or substantial portions of the Software.
40
+
41
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
42
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
43
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
44
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
45
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
46
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
47
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ task :default => :spec
2
+ task :test => :spec
3
+
4
+ desc "Run specs"
5
+ task :spec do
6
+ exec "spec spec/stapler_spec.rb"
7
+ end
data/lib/stapler.rb ADDED
@@ -0,0 +1,16 @@
1
+ #--
2
+ # Copyright (c) 2009 Michael Rykov
3
+ #++
4
+
5
+ $:.unshift File.dirname(__FILE__)
6
+
7
+ require 'yuicompressor'
8
+ require 'stapler/config'
9
+ require 'stapler/compressor'
10
+ require 'stapler/stapler'
11
+ require 'stapler/helper'
12
+ require 'stapler/middleware'
13
+
14
+ if defined?(::ActionView::Helpers)
15
+ ::ActionView::Helpers.send(:include, Stapler::Helper)
16
+ end
@@ -0,0 +1,40 @@
1
+ #--
2
+ # Copyright (c) 2009 Michael Rykov
3
+ #++
4
+
5
+ module Stapler
6
+ class Compressor
7
+ def initialize(from, to)
8
+ @from_path = from
9
+ @to_path = to
10
+ end
11
+
12
+ def compress!
13
+ compress(@from_path, @to_path)
14
+ logger.info("[Stapler] Compressed #{@from_path}")
15
+ rescue => e
16
+ logger.warn("[Stapler] Compression failure for #{@from_path}: #{e}")
17
+ FileUtil.cp(@from_path, @to_path)
18
+ end
19
+
20
+ protected
21
+ def compress(from, to)
22
+ out = File.open(from) do |from_file|
23
+ if from =~ /\.css$/
24
+ YUICompressor.compress_css(from_file)
25
+ elsif from =~ /\.js$/
26
+ YUICompressor.compress_js(from_file, :munge => true)
27
+ else
28
+ raise StandardError.new("Unrecognized file type")
29
+ end
30
+ end
31
+
32
+ File.open(to, 'w') { |f| f.write(out) }
33
+ end
34
+
35
+ def logger
36
+ Rails.logger
37
+ end
38
+ end
39
+ end
40
+
@@ -0,0 +1,15 @@
1
+ #--
2
+ # Copyright (c) 2009 Michael Rykov
3
+ #
4
+ # TODO: Make path selectors configurable
5
+ #++
6
+
7
+ module Stapler
8
+ module Config
9
+ WHITELIST = [%r{^/javascripts/.*\.js$}, %r{^/stylesheets/.*\.css$}]
10
+
11
+ def self.stapleable?(source)
12
+ ActionController::Base.perform_caching && WHITELIST.any? { |re| source =~ re }
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,35 @@
1
+ #--
2
+ # Copyright (c) 2009 Michael Rykov
3
+ #++
4
+
5
+ module Stapler
6
+ module Helper
7
+ StaplerRoot = "/stapler"
8
+
9
+ def self.included(base)
10
+ base.send :alias_method_chain, :rewrite_asset_path, :stapler
11
+ #base.send :alias_method_chain, :compute_asset_host, :stapler
12
+ end
13
+
14
+ private
15
+ def rewrite_asset_path_with_stapler(source)
16
+ if Config.stapleable?(source)
17
+ @@stapler ||= Stapler.new
18
+ stapled_source = "#{StaplerRoot}#{source}"
19
+ stapled_path = asset_file_path(stapled_source)
20
+
21
+ if @@stapler.ready?(stapled_path)
22
+ source = stapled_source
23
+ else
24
+ @@stapler.process(asset_file_path(source), stapled_path)
25
+ end
26
+ end
27
+
28
+ rewrite_asset_path_without_stapler(source)
29
+ end
30
+
31
+ #def compute_asset_host_with_stapler(source)
32
+ # compute_asset_host_without_stapler(source)
33
+ #end
34
+ end
35
+ end
@@ -0,0 +1,35 @@
1
+ module Stapler
2
+ class Middleware
3
+ ASSETS_DIR = defined?(Rails.public_path) ? Rails.public_path : "public"
4
+
5
+ def initialize(app)
6
+ @app = app
7
+ end
8
+
9
+ def call(env)
10
+ path = env['PATH_INFO']
11
+ puts "LOOKING AT PATH #{path}"
12
+
13
+ if path =~ %r(^/stapler/(.*)$)
14
+ stapler_call(env, $1)
15
+ else
16
+ @app.call(env)
17
+ end
18
+ end
19
+
20
+ private
21
+ def stapler_call(env, path)
22
+ file_path = asset_file_path(path)
23
+
24
+ if File.file?(file_path)
25
+ [200, {"Content-Type" => "text/plain"}, ['NOT DONE!']]
26
+ else
27
+ [404, {"Content-Type" => "text/html"}, ["Not Found!"]]
28
+ end
29
+ end
30
+
31
+ def asset_file_path(path)
32
+ File.join(ASSETS_DIR, path.split('?').first)
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,33 @@
1
+ #--
2
+ # Copyright (c) 2009 Michael Rykov
3
+ #++
4
+
5
+ module Stapler
6
+ class Stapler
7
+
8
+ def initialize
9
+ @in_progress = {}
10
+ @in_progress_guard = Mutex.new
11
+ end
12
+
13
+ def ready?(path)
14
+ !@in_progress[path] && File.exists?(path)
15
+ end
16
+
17
+ def process(from, to)
18
+ @in_progress_guard.synchronize do
19
+ return if @in_progress[to]
20
+ @in_progress[to] = true
21
+ end
22
+
23
+ Thread.new { self.compress(from, to) }
24
+ end
25
+
26
+ protected
27
+ def compress(from, to)
28
+ FileUtils.mkdir_p(File.dirname(to))
29
+ Compressor.new(from, to).compress!
30
+ @in_progress.delete(to)
31
+ end
32
+ end
33
+ end
@@ -0,0 +1 @@
1
+ require File.join(File.dirname(__FILE__), 'tasks')
@@ -0,0 +1,24 @@
1
+ # Re-definitions are appended to existing tasks
2
+ task :environment
3
+ task :merb_env
4
+
5
+ namespace :stapler do
6
+ PUBLIC_DIR = "#{RAILS_ROOT}/public"
7
+ STAPLE_DIR = "#{PUBLIC_DIR}/stapler"
8
+
9
+ desc "Staple all files"
10
+ task :run => [:merb_env, :environment, :clean] do
11
+ file_list = FileList["#{PUBLIC_DIR}/javascripts/**/*.js", "#{PUBLIC_DIR}/stylesheets/**/*.css"]
12
+ file_list.each do |from|
13
+ to = from.gsub(/#{PUBLIC_DIR}/, STAPLE_DIR)
14
+ FileUtils.mkdir_p(File.dirname(to))
15
+ puts "[Stapler] Stapling #{from} to #{to}"
16
+ Stapler::Compressor.new(from, to).compress!
17
+ end
18
+ end
19
+
20
+ task :clean => [:merb_env, :environment] do |t, args|
21
+ puts "[Stapler] Cleaning #{STAPLE_DIR}"
22
+ FileUtils.rm_rf(STAPLE_DIR)
23
+ end
24
+ end
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: stapler
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Michael Rykov
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-08-27 00:00:00 -07:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: yuicompressor
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ hash: 31
30
+ segments:
31
+ - 1
32
+ - 2
33
+ - 0
34
+ version: 1.2.0
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ description: |
38
+ Dynamic asset compression for Rails using YUI Compressor
39
+
40
+ email: mrykov@gmail
41
+ executables: []
42
+
43
+ extensions: []
44
+
45
+ extra_rdoc_files: []
46
+
47
+ files:
48
+ - README.md
49
+ - Rakefile
50
+ - LICENSE
51
+ - lib/stapler/compressor.rb
52
+ - lib/stapler/config.rb
53
+ - lib/stapler/helper.rb
54
+ - lib/stapler/middleware.rb
55
+ - lib/stapler/stapler.rb
56
+ - lib/stapler.rb
57
+ - lib/tasks/stapler.rake
58
+ - lib/tasks/tasks.rb
59
+ has_rdoc: false
60
+ homepage: http://github.com/rykov/stapler
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
+ hash: 3
74
+ segments:
75
+ - 0
76
+ version: "0"
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ none: false
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ hash: 3
83
+ segments:
84
+ - 0
85
+ version: "0"
86
+ requirements: []
87
+
88
+ rubyforge_project:
89
+ rubygems_version: 1.3.7
90
+ signing_key:
91
+ specification_version: 3
92
+ summary: Dynamic asset compression for Rails
93
+ test_files: []
94
+