squeezy 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source :rubygems
2
+ gem "sinatra"
3
+ gem "glassfish"
@@ -0,0 +1,15 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ glassfish (1.0.2-java)
5
+ rack (>= 0.4.0)
6
+ rack (1.2.1)
7
+ sinatra (1.0)
8
+ rack (>= 1.0)
9
+
10
+ PLATFORMS
11
+ java
12
+
13
+ DEPENDENCIES
14
+ glassfish
15
+ sinatra
@@ -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.
@@ -0,0 +1,10 @@
1
+ require 'spec/rake/spectask'
2
+
3
+ require 'bundler'
4
+ Bundler::GemHelper.install_tasks
5
+
6
+ Spec::Rake::SpecTask.new do |t|
7
+ t.spec_opts ||= []
8
+ t.spec_opts << "--options" << "spec/spec.opts"
9
+ t.spec_files = FileList['spec/**/*_spec.rb']
10
+ end
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'squeezy'
4
+
5
+ Squeezy.run_service
@@ -0,0 +1,13 @@
1
+ require 'jars/yuicompressor-2.3.6.jar'
2
+ require 'squeezy/reporter'
3
+ require 'squeezy/compressor'
4
+ require 'squeezy/service'
5
+ require 'squeezy/version'
6
+
7
+ class Squeezy
8
+ include Java
9
+
10
+ def self.run_service
11
+ Squeezy::Service.run!
12
+ end
13
+ end
@@ -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
@@ -0,0 +1,3 @@
1
+ class Squeezy
2
+ VERSION = '0.0.1'
3
+ end
@@ -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
@@ -0,0 +1,3 @@
1
+ .test {
2
+ asd: asd: 'test';
3
+ }
@@ -0,0 +1,4 @@
1
+ BODY {
2
+ font-color: red;
3
+ size: 14pt;
4
+ }
@@ -0,0 +1,4 @@
1
+ function blah(opt1, opt2) {
2
+ opt1 == {asd: 'asd' qwe: 'qwe',}
3
+ return "i hates trailin' commas"
4
+ }
@@ -0,0 +1,3 @@
1
+ function blah(opt1, opt2) {
2
+ return opt1 == "asd\nasdasd";
3
+ }
@@ -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
@@ -0,0 +1,7 @@
1
+ --colour
2
+ --format
3
+ specdoc
4
+ --loadby
5
+ mtime
6
+ --reverse
7
+ --backtrace
@@ -0,0 +1,2 @@
1
+ $LOAD_PATH << File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ require 'squeezy'
@@ -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