sprockets-image_compressor 0.2.4 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +8 -0
- data/README.md +2 -1
- data/lib/sprockets/image_compressor.rb +1 -0
- data/lib/sprockets/image_compressor/base.rb +3 -21
- data/lib/sprockets/image_compressor/binary_finder.rb +49 -0
- data/lib/sprockets/image_compressor/version.rb +1 -1
- data/spec/binary_finder_spec.rb +33 -0
- metadata +4 -4
- data/spec/compressors/jpg_compressor_spec.rb +0 -40
- data/spec/compressors/png_compressor_spec.rb +0 -47
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: aaed8e8733ed00024e60e873586d05562665938a
|
4
|
+
data.tar.gz: 14514d263b2cd8a2564e7549ddd623b74420786c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 71368ede2c27681e320663b1360c92cfce187697fbccaf508e0483d77b41e7d66dc1b6095f7e354448c5c67692daf8f4b58661f87403ac9c0f642fd8afa913c8
|
7
|
+
data.tar.gz: a8c5ab038f88e5ff1cf1a1166e0af62cad7d5ca46a2fec786ee2faef0992c43c2b29c8d2a905989de31e4d3826705e860d0e5a75c91befca17bbe6bf4a7843cb
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,11 @@
|
|
1
|
+
0.3.0 (09/17/2014)
|
2
|
+
-------------------------
|
3
|
+
* Feature: Windows support! Thanks, @JakeTheSnake3p0!
|
4
|
+
|
5
|
+
0.2.4 (05/18/2014)
|
6
|
+
-------------------------
|
7
|
+
* Bugfix: jpgs weren't being opened in binary mode. Thanks, @JakeTheSnake3p0!
|
8
|
+
|
1
9
|
0.2.3 (04/10/2014)
|
2
10
|
-------------------------
|
3
11
|
* Bugfix: Return original file and warn if compression fails. Thanks, @tomchentw!
|
data/README.md
CHANGED
@@ -37,9 +37,10 @@ See [#15](https://github.com/botandrose/sprockets-image_compressor/issues/15) fo
|
|
37
37
|
## Credits
|
38
38
|
|
39
39
|
* @nhogle for help with compiling and packaging the jpegoptim and pngcrush binaries
|
40
|
+
* @JakeTheSnake3p0 for Windows support
|
40
41
|
* @florentmorin for compatibility with sprockets-rails
|
41
42
|
* @dkubb for compatibility with Rails 4
|
42
43
|
|
43
44
|
## License
|
44
45
|
|
45
|
-
(MIT License) - Copyright (c)
|
46
|
+
(MIT License) - Copyright (c) 2014 Micah Geisel
|
@@ -2,32 +2,14 @@ require "tempfile"
|
|
2
2
|
|
3
3
|
module Sprockets
|
4
4
|
module ImageCompressor
|
5
|
+
|
5
6
|
GEM_ROOT = File.expand_path File.join(File.dirname(__FILE__), "../../../")
|
6
7
|
|
7
8
|
class Base
|
8
9
|
def binary_path
|
9
|
-
@binary_path ||=
|
10
|
-
try_system_binary or try_vendored_binaries or raise """
|
11
|
-
Can't find an installed version of #{@name}, and none of the vendored binaries seem to work.
|
12
|
-
Please install #{@name}, or open an issue on the project page at https://github.com/botandrose/sprockets-image_compressor
|
13
|
-
"""
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
private
|
18
|
-
|
19
|
-
def try_system_binary
|
20
|
-
system_binary = `which #{@name}`.chomp
|
21
|
-
system_binary if system_binary.length > 0
|
22
|
-
end
|
23
|
-
|
24
|
-
def try_vendored_binaries
|
25
|
-
# use the first vendored binary that doesn't shit the bed when we ask for its version
|
26
|
-
vendored_binaries = Dir["#{GEM_ROOT}/bin/#{@name}.*"].sort
|
27
|
-
vendored_binaries.find do |path|
|
28
|
-
system("#{path} -version 2> /dev/null > /dev/null")
|
29
|
-
end
|
10
|
+
@binary_path ||= BinaryFinder.new(@name).path
|
30
11
|
end
|
31
12
|
end
|
13
|
+
|
32
14
|
end
|
33
15
|
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
module Sprockets
|
2
|
+
module ImageCompressor
|
3
|
+
class BinaryFinder
|
4
|
+
def initialize binary_name
|
5
|
+
@binary_name = binary_name
|
6
|
+
end
|
7
|
+
|
8
|
+
attr_reader :binary_name
|
9
|
+
|
10
|
+
def path
|
11
|
+
try_system_binary or try_vendored_binaries or raise NotFound
|
12
|
+
end
|
13
|
+
|
14
|
+
class NotFound < StandardError
|
15
|
+
def message
|
16
|
+
"""
|
17
|
+
Can't find an installed version of #{binary_name}, and none of the vendored binaries seem to work.
|
18
|
+
Please install #{binary_name}, or open an issue on the project page at https://github.com/botandrose/sprockets-image_compressor
|
19
|
+
"""
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def try_system_binary
|
26
|
+
which(binary_name)
|
27
|
+
end
|
28
|
+
|
29
|
+
def try_vendored_binaries
|
30
|
+
# use the first vendored binary that doesn't shit the bed when we ask for its version
|
31
|
+
vendored_binaries = Dir["#{GEM_ROOT}/bin/#{ binary_name }.*"].sort
|
32
|
+
vendored_binaries.find do |path|
|
33
|
+
system("#{ path } -version 2> /dev/null > /dev/null")
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def which cmd
|
38
|
+
exts = ENV["PATHEXT"] ? ENV["PATHEXT"].split(";") : [""]
|
39
|
+
ENV["PATH"].split(File::PATH_SEPARATOR).each do |path|
|
40
|
+
exts.each do |ext|
|
41
|
+
exe = File.join(path, "#{cmd}#{ext}")
|
42
|
+
return exe if File.executable?(exe) && !File.directory?(exe)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
nil
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require "sprockets/image_compressor"
|
2
|
+
|
3
|
+
describe Sprockets::ImageCompressor::BinaryFinder do
|
4
|
+
subject { Sprockets::ImageCompressor::BinaryFinder.new(binary) }
|
5
|
+
let(:binary) { "example_binary_name" }
|
6
|
+
let(:path) { "/some/path/to/binary" }
|
7
|
+
|
8
|
+
describe "#path" do
|
9
|
+
context "when binary is installed locally " do
|
10
|
+
before { subject.stub try_system_binary: path }
|
11
|
+
|
12
|
+
it "prefers the system's binary" do
|
13
|
+
subject.path.should == path
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
context "when local binary is missing, but one of the vendored binaries works" do
|
18
|
+
before { subject.stub try_system_binary: nil, try_vendored_binaries: path }
|
19
|
+
|
20
|
+
it "falls back to the vendored binary" do
|
21
|
+
subject.path.should == path
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
context "when local binary is missing and none of the vendored binaries work" do
|
26
|
+
before { subject.stub try_system_binary: nil, try_vendored_binaries: nil }
|
27
|
+
|
28
|
+
it "raises an exception" do
|
29
|
+
expect { subject.path }.to raise_exception
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sprockets-image_compressor
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Micah Geisel
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-09-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: sprockets
|
@@ -104,13 +104,13 @@ files:
|
|
104
104
|
- lib/sprockets-image_compressor.rb
|
105
105
|
- lib/sprockets/image_compressor.rb
|
106
106
|
- lib/sprockets/image_compressor/base.rb
|
107
|
+
- lib/sprockets/image_compressor/binary_finder.rb
|
107
108
|
- lib/sprockets/image_compressor/integration.rb
|
108
109
|
- lib/sprockets/image_compressor/jpg_compressor.rb
|
109
110
|
- lib/sprockets/image_compressor/png_compressor.rb
|
110
111
|
- lib/sprockets/image_compressor/railtie.rb
|
111
112
|
- lib/sprockets/image_compressor/version.rb
|
112
|
-
- spec/
|
113
|
-
- spec/compressors/png_compressor_spec.rb
|
113
|
+
- spec/binary_finder_spec.rb
|
114
114
|
- spec/fixtures/largejpg.jpg
|
115
115
|
- spec/fixtures/largepng.png
|
116
116
|
- spec/fixtures/smalljpg.jpg
|
@@ -1,40 +0,0 @@
|
|
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
|
@@ -1,47 +0,0 @@
|
|
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
|
-
|
41
|
-
describe "#compress" do
|
42
|
-
it "returns uncompressable content as-is with a warning" do
|
43
|
-
compressor.should_receive(:warn)
|
44
|
-
compressor.compress("asdf").should == "asdf"
|
45
|
-
end
|
46
|
-
end
|
47
|
-
end
|