squeezy 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +3 -0
- data/Gemfile.lock +15 -0
- data/README.rdoc +21 -0
- data/Rakefile +10 -0
- data/bin/squeezy +5 -0
- data/lib/jars/yuicompressor-2.3.6.jar +0 -0
- data/lib/squeezy.rb +13 -0
- data/lib/squeezy/compressor.rb +39 -0
- data/lib/squeezy/reporter.rb +28 -0
- data/lib/squeezy/service.rb +18 -0
- data/lib/squeezy/version.rb +3 -0
- data/spec/css_spec.rb +17 -0
- data/spec/fixtures/css-malformed.css +3 -0
- data/spec/fixtures/css-multiline.css +4 -0
- data/spec/fixtures/js-malformed.js +4 -0
- data/spec/fixtures/js-multiline.js +3 -0
- data/spec/js_spec.rb +24 -0
- data/spec/spec.opts +7 -0
- data/spec/spec_helper.rb +2 -0
- data/squeezy.gemspec +27 -0
- metadata +150 -0
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
data/README.rdoc
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
= Squeezy
|
2
|
+
|
3
|
+
CSS and JS go in, compressed version go out, that's what Squeezy is all about.
|
4
|
+
|
5
|
+
== Prerequisites
|
6
|
+
|
7
|
+
You need a recent version of jruby installed.
|
8
|
+
|
9
|
+
== Usage
|
10
|
+
|
11
|
+
* CLASSPATH=ext/jargs-1.0.jar:ext/yuicompressor-2.4.2.jar jruby -rubygems app.rb /path/to/rails/public/dir
|
12
|
+
* Redirect js/css requests from nginx (or whatever) to squeezy.
|
13
|
+
* Watch JS/CSS get compressed!
|
14
|
+
|
15
|
+
== Rationale
|
16
|
+
|
17
|
+
I hate pre-compiling things. JS/CSS compression should be a product of your environment, not something you do before hand.
|
18
|
+
|
19
|
+
== TODO
|
20
|
+
|
21
|
+
Squeezy should handle conditional get/head requests correctly. It should cache the compressed results.
|
data/Rakefile
ADDED
data/bin/squeezy
ADDED
Binary file
|
data/lib/squeezy.rb
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'digest/md5'
|
2
|
+
|
3
|
+
class Squeezy
|
4
|
+
|
5
|
+
include_class Java::com.yahoo.platform.yui.compressor.JavaScriptCompressor
|
6
|
+
include_class Java::com.yahoo.platform.yui.compressor.CssCompressor
|
7
|
+
|
8
|
+
def with_cached_file(file)
|
9
|
+
if File.exist?(file)
|
10
|
+
File.read(file)
|
11
|
+
else
|
12
|
+
contents = yield
|
13
|
+
File.open(file, 'w') {|f| f << contents}
|
14
|
+
contents
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def compress_css(contents)
|
19
|
+
with_cached_file "/tmp/#{Digest::MD5.hexdigest(contents)}.css" do
|
20
|
+
CssCompressor.new(java.io.StringReader.new(contents)).compress(result = java.io.StringWriter.new(), -1)
|
21
|
+
result.to_s
|
22
|
+
end
|
23
|
+
rescue Exception => e
|
24
|
+
$stderr.puts e
|
25
|
+
$stderr.puts "Compression of CSS failed!"
|
26
|
+
contents
|
27
|
+
end
|
28
|
+
|
29
|
+
def compress_js(contents)
|
30
|
+
with_cached_file "/tmp/#{Digest::MD5.hexdigest(contents)}.js" do
|
31
|
+
JavaScriptCompressor.new(java.io.StringReader.new(contents), Reporter.new).compress(result = java.io.StringWriter.new(), -1, true, false, false, false)
|
32
|
+
result.to_s
|
33
|
+
end
|
34
|
+
rescue Exception => e
|
35
|
+
$stderr.puts e
|
36
|
+
$stderr.puts "Compression of JS failed!"
|
37
|
+
contents
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
class Squeezy
|
2
|
+
class Reporter
|
3
|
+
|
4
|
+
include_class Java::org.mozilla.javascript.EvaluatorException
|
5
|
+
include_class Java::org.mozilla.javascript.ErrorReporter
|
6
|
+
|
7
|
+
def do_message(type, message, source_name, line, line_source, line_offset)
|
8
|
+
if (line < 0)
|
9
|
+
$stderr.puts("\n[#{type.to_s.upcase}] #{message} --> #{line_source}")
|
10
|
+
else
|
11
|
+
$stderr.puts("\n[#{type.to_s.upcase}] #{line} #{line_offset} : #{message} --> #{line_source}")
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def warning(message, source_name, live, line_source, line_offset)
|
16
|
+
do_message(:warning, message, source_name, live, line_source, line_offset)
|
17
|
+
end
|
18
|
+
|
19
|
+
def error(message, source_name, live, line_source, line_offset)
|
20
|
+
do_message(:error, message, source_name, live, line_source, line_offset)
|
21
|
+
end
|
22
|
+
|
23
|
+
def runtimeError(message, source_name, line, line_source, line_offset)
|
24
|
+
error(message, source_name, line, line_source, line_offset)
|
25
|
+
EvaluatorException.new(message)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'sinatra'
|
2
|
+
|
3
|
+
class Squeezy
|
4
|
+
class Service < Sinatra::Base
|
5
|
+
set :reload, false
|
6
|
+
set :environment, :production
|
7
|
+
|
8
|
+
squeezy = Squeezy.new
|
9
|
+
|
10
|
+
put('/js') do
|
11
|
+
squeezy.compress_js(request.env['rack.input'].read)
|
12
|
+
end
|
13
|
+
|
14
|
+
put('/css') do
|
15
|
+
squeezy.compress_css(request.env['rack.input'].read)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
data/spec/css_spec.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "A CSS Compressor" do
|
4
|
+
before(:each) do
|
5
|
+
@squeezy = Squeezy.new
|
6
|
+
`rm -rf /tmp/*.css`
|
7
|
+
`rm -rf /tmp/*.js`
|
8
|
+
@source = File.read("spec/fixtures/css-multiline.css")
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should return less than it started with" do
|
12
|
+
@source.size.should > @squeezy.compress_css(@source).size
|
13
|
+
end
|
14
|
+
it "should have no new lines" do
|
15
|
+
@squeezy.compress_css(@source).count("\n").should == 0
|
16
|
+
end
|
17
|
+
end
|
data/spec/js_spec.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "A JS Compressor" do
|
4
|
+
before(:each) do
|
5
|
+
@squeezy = Squeezy.new
|
6
|
+
`rm -rf /tmp/*.css`
|
7
|
+
`rm -rf /tmp/*.js`
|
8
|
+
end
|
9
|
+
|
10
|
+
it "should return less than it started with" do
|
11
|
+
js = IO.read("spec/fixtures/js-multiline.js")
|
12
|
+
js.size.should > @squeezy.compress_js(js).size
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should have no new lines" do
|
16
|
+
js = IO.read("spec/fixtures/js-multiline.js")
|
17
|
+
@squeezy.compress_js(js).count("\n").should == 0
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should return the original file when the js is malformed" do
|
21
|
+
js_malformed = IO.read("spec/fixtures/js-malformed.js")
|
22
|
+
@squeezy.compress_js(js_malformed).should == js_malformed
|
23
|
+
end
|
24
|
+
end
|
data/spec/spec.opts
ADDED
data/spec/spec_helper.rb
ADDED
data/squeezy.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "squeezy/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "squeezy"
|
7
|
+
s.version = Squeezy::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Joshua Hull"]
|
10
|
+
s.email = ["joshbuddy@gmail.com"]
|
11
|
+
s.homepage = "http://rubygems.org/gems/squeezy"
|
12
|
+
s.summary = %q{Asset compression service via YUI compressor}
|
13
|
+
s.description = %q{Asset compression service via YUI compressor}
|
14
|
+
|
15
|
+
s.rubyforge_project = "squeezy"
|
16
|
+
|
17
|
+
s.add_dependency "glassfish"
|
18
|
+
s.add_dependency "sinatra"
|
19
|
+
s.add_development_dependency "rspec"
|
20
|
+
s.add_development_dependency "rake"
|
21
|
+
s.add_development_dependency "bundler", ">= 1.0.0"
|
22
|
+
|
23
|
+
s.files = `git ls-files`.split("\n")
|
24
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
25
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
26
|
+
s.require_paths = ["lib"]
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,150 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: squeezy
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
version: 0.0.1
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Joshua Hull
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-10-22 00:00:00 -07:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: glassfish
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 0
|
29
|
+
version: "0"
|
30
|
+
type: :runtime
|
31
|
+
version_requirements: *id001
|
32
|
+
- !ruby/object:Gem::Dependency
|
33
|
+
name: sinatra
|
34
|
+
prerelease: false
|
35
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
segments:
|
40
|
+
- 0
|
41
|
+
version: "0"
|
42
|
+
type: :runtime
|
43
|
+
version_requirements: *id002
|
44
|
+
- !ruby/object:Gem::Dependency
|
45
|
+
name: rspec
|
46
|
+
prerelease: false
|
47
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
48
|
+
requirements:
|
49
|
+
- - ">="
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
segments:
|
52
|
+
- 0
|
53
|
+
version: "0"
|
54
|
+
type: :development
|
55
|
+
version_requirements: *id003
|
56
|
+
- !ruby/object:Gem::Dependency
|
57
|
+
name: rake
|
58
|
+
prerelease: false
|
59
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
60
|
+
requirements:
|
61
|
+
- - ">="
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
segments:
|
64
|
+
- 0
|
65
|
+
version: "0"
|
66
|
+
type: :development
|
67
|
+
version_requirements: *id004
|
68
|
+
- !ruby/object:Gem::Dependency
|
69
|
+
name: bundler
|
70
|
+
prerelease: false
|
71
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
segments:
|
76
|
+
- 1
|
77
|
+
- 0
|
78
|
+
- 0
|
79
|
+
version: 1.0.0
|
80
|
+
type: :development
|
81
|
+
version_requirements: *id005
|
82
|
+
description: Asset compression service via YUI compressor
|
83
|
+
email:
|
84
|
+
- joshbuddy@gmail.com
|
85
|
+
executables:
|
86
|
+
- squeezy
|
87
|
+
extensions: []
|
88
|
+
|
89
|
+
extra_rdoc_files: []
|
90
|
+
|
91
|
+
files:
|
92
|
+
- Gemfile
|
93
|
+
- Gemfile.lock
|
94
|
+
- README.rdoc
|
95
|
+
- Rakefile
|
96
|
+
- bin/squeezy
|
97
|
+
- lib/jars/yuicompressor-2.3.6.jar
|
98
|
+
- lib/squeezy.rb
|
99
|
+
- lib/squeezy/compressor.rb
|
100
|
+
- lib/squeezy/reporter.rb
|
101
|
+
- lib/squeezy/service.rb
|
102
|
+
- lib/squeezy/version.rb
|
103
|
+
- spec/css_spec.rb
|
104
|
+
- spec/fixtures/css-malformed.css
|
105
|
+
- spec/fixtures/css-multiline.css
|
106
|
+
- spec/fixtures/js-malformed.js
|
107
|
+
- spec/fixtures/js-multiline.js
|
108
|
+
- spec/js_spec.rb
|
109
|
+
- spec/spec.opts
|
110
|
+
- spec/spec_helper.rb
|
111
|
+
- squeezy.gemspec
|
112
|
+
has_rdoc: true
|
113
|
+
homepage: http://rubygems.org/gems/squeezy
|
114
|
+
licenses: []
|
115
|
+
|
116
|
+
post_install_message:
|
117
|
+
rdoc_options: []
|
118
|
+
|
119
|
+
require_paths:
|
120
|
+
- lib
|
121
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
122
|
+
requirements:
|
123
|
+
- - ">="
|
124
|
+
- !ruby/object:Gem::Version
|
125
|
+
segments:
|
126
|
+
- 0
|
127
|
+
version: "0"
|
128
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
129
|
+
requirements:
|
130
|
+
- - ">="
|
131
|
+
- !ruby/object:Gem::Version
|
132
|
+
segments:
|
133
|
+
- 0
|
134
|
+
version: "0"
|
135
|
+
requirements: []
|
136
|
+
|
137
|
+
rubyforge_project: squeezy
|
138
|
+
rubygems_version: 1.3.6
|
139
|
+
signing_key:
|
140
|
+
specification_version: 3
|
141
|
+
summary: Asset compression service via YUI compressor
|
142
|
+
test_files:
|
143
|
+
- spec/css_spec.rb
|
144
|
+
- spec/fixtures/css-malformed.css
|
145
|
+
- spec/fixtures/css-multiline.css
|
146
|
+
- spec/fixtures/js-malformed.js
|
147
|
+
- spec/fixtures/js-multiline.js
|
148
|
+
- spec/js_spec.rb
|
149
|
+
- spec/spec.opts
|
150
|
+
- spec/spec_helper.rb
|