sprockets-image_compressor 0.1.2 → 0.2.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/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ 0.2.0 (01/28/2012)
2
+ -------------------------
3
+
4
+ * Feature: Fallback to vendored binaries if pngcrush and/or jpegoptim aren't installed on the system. Works on Heroku, now!
5
+
1
6
  0.1.2 (01/26/2012)
2
7
  -------------------------
3
8
 
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Image compressor processor for Sprockets
2
2
 
3
- Sprockets preprocessor to losslessly compress .png and .jpg images. Requires [pngcrush](http://pmt.sourceforge.net/pngcrush/) and [jpegoptim](http://www.kokkonen.net/tjko/projects.html) to be installed and in your PATH.
3
+ Sprockets preprocessor to losslessly compress .png and .jpg images using [pngcrush](http://pmt.sourceforge.net/pngcrush/) and [jpegoptim](http://www.kokkonen.net/tjko/projects.html).
4
4
 
5
5
  ## Integration with Rails 3.1+
6
6
 
@@ -12,12 +12,19 @@ gem 'sprockets-image_compressor'
12
12
 
13
13
  The gem ships with a Railtie which will automatically register the compressor preprocessors.
14
14
 
15
+ ## Now with vendored binary fallbacks / Heroku support!
16
+
17
+ If the environment doesn't have pngcrush and/or jpegoptim installed, the gem will fall back on binaries packaged with the gem. Currently, only 32bit and 64bit linux binaries are included. Pull requests welcome for other architectures!
18
+
15
19
  ## TODO
16
20
 
17
- * Detect missing pngcrush or jpegoptim installations (vendor?)
18
21
  * Provide configuration hooks
19
22
  * Test Railtie
20
23
 
24
+ ## Credits
25
+
26
+ * Thanks to @nhogle for help with compiling and packaging the jpegoptim and pngcrush binaries
27
+
21
28
  ## License
22
29
 
23
- (MIT License) - Copyright (c) 2011 Micah Geisel
30
+ (MIT License) - Copyright (c) 2012 Micah Geisel
Binary file
Binary file
Binary file
Binary file
@@ -0,0 +1,31 @@
1
+ module Sprockets
2
+ module ImageCompressor
3
+ GEM_ROOT = File.expand_path File.join(File.dirname(__FILE__), "../../../")
4
+
5
+ class Base
6
+ def binary_path
7
+ @binary_path ||= begin
8
+ try_system_binary or try_vendored_binaries or raise """
9
+ Can't find an installed version of #{@name}, and none of the vendored binaries seem to work.
10
+ Please install #{@name}, or open an issue on the project page at https://github.com/botandrose/sprockets-image_compressor
11
+ """
12
+ end
13
+ end
14
+
15
+ private
16
+
17
+ def try_system_binary
18
+ system_binary = `which #{@name}`.chomp
19
+ system_binary if system_binary.length > 0
20
+ end
21
+
22
+ def try_vendored_binaries
23
+ # use the first vendored binary that doesn't shit the bed when we ask for its version
24
+ vendored_binaries = Dir["#{GEM_ROOT}/bin/#{@name}.*"].sort
25
+ vendored_binaries.find do |path|
26
+ system("#{path} -version 2> /dev/null > /dev/null")
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -1,6 +1,12 @@
1
+ require "sprockets/image_compressor/base"
2
+
1
3
  module Sprockets
2
4
  module ImageCompressor
3
- class JpgCompressor
5
+ class JpgCompressor < Base
6
+ def initialize
7
+ @name = "jpegoptim"
8
+ end
9
+
4
10
  def compress(content)
5
11
  compressed_jpg_data = ""
6
12
  Tempfile.open ["file", ".jpg"] do |file|
@@ -8,7 +14,7 @@ module Sprockets
8
14
  file.write content
9
15
  file.close
10
16
 
11
- out = `jpegoptim --strip-all #{file.path} 2>&1`
17
+ out = `#{binary_path} --strip-all #{file.path} 2>&1`
12
18
  file.open
13
19
  compressed_jpg_data = file.read
14
20
  end
@@ -1,6 +1,12 @@
1
+ require "sprockets/image_compressor/base"
2
+
1
3
  module Sprockets
2
4
  module ImageCompressor
3
- class PngCompressor
5
+ class PngCompressor < Base
6
+ def initialize
7
+ @name = "pngcrush"
8
+ end
9
+
4
10
  def compress(content)
5
11
  compressed_png_data = ""
6
12
  Tempfile.open ["in_file", ".png"] do |in_file|
@@ -8,7 +14,7 @@ module Sprockets
8
14
  out_file_path = in_file.path + ".optimized.png"
9
15
  in_file.write content
10
16
  in_file.close
11
- out = `pngcrush #{in_file.path} #{out_file_path} 2>&1`
17
+ out = `#{binary_path} #{in_file.path} #{out_file_path} 2>&1`
12
18
  in_file.delete
13
19
 
14
20
  File.open out_file_path, "rb" do |out_file|
@@ -1,5 +1,5 @@
1
1
  module Sprockets
2
2
  module Imagecompressor
3
- VERSION = "0.1.2"
3
+ VERSION = "0.2.0"
4
4
  end
5
5
  end
@@ -0,0 +1,40 @@
1
+ require "sprockets/image_compressor"
2
+
3
+ describe Sprockets::ImageCompressor::JpgCompressor do
4
+ let(:compressor) { Sprockets::ImageCompressor::JpgCompressor.new }
5
+ let(:gem_root) { Sprockets::ImageCompressor::GEM_ROOT }
6
+
7
+ describe "#binary_path" do
8
+ context "when jpegoptim is installed" do
9
+ before do
10
+ compressor.should_receive(:`).with("which jpegoptim").and_return("/path/to/jpegoptim\n")
11
+ end
12
+
13
+ it "prefers the system's binary" do
14
+ compressor.binary_path.should == "/path/to/jpegoptim"
15
+ end
16
+ end
17
+
18
+ context "when jpegoptim is missing and we're on a supported system" do
19
+ before do
20
+ compressor.should_receive(:`).with("which jpegoptim").and_return("")
21
+ compressor.should_receive(:system).and_return(true)
22
+ end
23
+
24
+ it "falls back to a vendored binary" do
25
+ compressor.binary_path.should include("#{gem_root}/bin/jpegoptim")
26
+ end
27
+ end
28
+
29
+ context "when jpegoptim is missing and we're on an unsupported system" do
30
+ before do
31
+ compressor.should_receive(:`).with("which jpegoptim").and_return("")
32
+ compressor.should_receive(:system).twice.and_return(false)
33
+ end
34
+
35
+ it "raises hell" do
36
+ lambda { compressor.binary_path }.should raise_exception
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,40 @@
1
+ require "sprockets/image_compressor"
2
+
3
+ describe Sprockets::ImageCompressor::PngCompressor do
4
+ let(:compressor) { Sprockets::ImageCompressor::PngCompressor.new }
5
+ let(:gem_root) { Sprockets::ImageCompressor::GEM_ROOT }
6
+
7
+ describe "#binary_path" do
8
+ context "when pngcrush is installed" do
9
+ before do
10
+ compressor.should_receive(:`).with("which pngcrush").and_return("/path/to/pngcrush\n")
11
+ end
12
+
13
+ it "prefers the system's binary" do
14
+ compressor.binary_path.should == "/path/to/pngcrush"
15
+ end
16
+ end
17
+
18
+ context "when pngcrush is missing and we're on a supported system" do
19
+ before do
20
+ compressor.should_receive(:`).with("which pngcrush").and_return("")
21
+ compressor.should_receive(:system).and_return(true)
22
+ end
23
+
24
+ it "falls back to a vendored binary" do
25
+ compressor.binary_path.should include("#{gem_root}/bin/pngcrush")
26
+ end
27
+ end
28
+
29
+ context "when pngcrush is missing and we're an unsupported system" do
30
+ before do
31
+ compressor.should_receive(:`).with("which pngcrush").and_return("")
32
+ compressor.should_receive(:system).twice.and_return(false)
33
+ end
34
+
35
+ it "raises hell" do
36
+ lambda { compressor.binary_path }.should raise_exception
37
+ end
38
+ end
39
+ end
40
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sprockets-image_compressor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-01-27 00:00:00.000000000Z
12
+ date: 2012-01-28 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: sprockets
16
- requirement: &80292680 !ruby/object:Gem::Requirement
16
+ requirement: &73839430 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *80292680
24
+ version_requirements: *73839430
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: ruby-debug
27
- requirement: &80292420 !ruby/object:Gem::Requirement
27
+ requirement: &73839200 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *80292420
35
+ version_requirements: *73839200
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rspec
38
- requirement: &80292210 !ruby/object:Gem::Requirement
38
+ requirement: &73838940 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *80292210
46
+ version_requirements: *73838940
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: rack-test
49
- requirement: &80291950 !ruby/object:Gem::Requirement
49
+ requirement: &73838700 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,11 +54,15 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *80291950
57
+ version_requirements: *73838700
58
58
  description: Losslessly compress images in the Rails asset pipeline
59
59
  email:
60
60
  - micah@botandrose.com
61
- executables: []
61
+ executables:
62
+ - jpegoptim.i386.linux
63
+ - jpegoptim.x86_64.linux
64
+ - pngcrush.i386.linux
65
+ - pngcrush.x86_64.linux
62
66
  extensions: []
63
67
  extra_rdoc_files: []
64
68
  files:
@@ -67,13 +71,20 @@ files:
67
71
  - Gemfile
68
72
  - README.md
69
73
  - Rakefile
74
+ - bin/jpegoptim.i386.linux
75
+ - bin/jpegoptim.x86_64.linux
76
+ - bin/pngcrush.i386.linux
77
+ - bin/pngcrush.x86_64.linux
70
78
  - lib/sprockets-image_compressor.rb
71
79
  - lib/sprockets/image_compressor.rb
80
+ - lib/sprockets/image_compressor/base.rb
72
81
  - lib/sprockets/image_compressor/integration.rb
73
82
  - lib/sprockets/image_compressor/jpg_compressor.rb
74
83
  - lib/sprockets/image_compressor/png_compressor.rb
75
84
  - lib/sprockets/image_compressor/railtie.rb
76
85
  - lib/sprockets/image_compressor/version.rb
86
+ - spec/compressors/jpg_compressor_spec.rb
87
+ - spec/compressors/png_compressor_spec.rb
77
88
  - spec/fixtures/largejpg.jpg
78
89
  - spec/fixtures/largepng.png
79
90
  - spec/fixtures/smalljpg.jpg