Skip to content

Commit

Permalink
Removed unix-whereis dependency since it doesn't find jpegtran on OS …
Browse files Browse the repository at this point in the history
…X when installed with Homebrew. `which` a much better, simpler and more portable alternative.
  • Loading branch information
cmer committed Mar 6, 2012
1 parent 103b3a0 commit 655a66c
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 46 deletions.
4 changes: 0 additions & 4 deletions Gemfile.lock
Expand Up @@ -4,7 +4,6 @@ PATH
jpegtran (0.1.3)
command-builder (>= 0.2.0)
hash-utils (>= 1.0.0)
unix-whereis (>= 0.1.0)

GEM
remote: http://rubygems.org/
Expand All @@ -16,9 +15,6 @@ GEM
ruby-version
pipe-run (0.3.0)
ruby-version (0.3.1)
unix-whereis (0.1.1)
command-builder (>= 0.2.0)
hash-utils (>= 0.18.0)

PLATFORMS
ruby
Expand Down
1 change: 0 additions & 1 deletion jpegtran.gemspec
Expand Up @@ -24,7 +24,6 @@ Gem::Specification.new do |s|

s.add_runtime_dependency 'hash-utils', '>= 1.0.0'
s.add_runtime_dependency 'command-builder', '>= 0.2.0'
s.add_runtime_dependency 'unix-whereis', '>= 0.1.0'
s.add_development_dependency 'bundler'
end

82 changes: 41 additions & 41 deletions lib/jpegtran.rb
Expand Up @@ -3,7 +3,6 @@

require "hash-utils/object" # >= 0.18.0
require "command-builder" # >= 0.2.0
require "unix/whereis"
require "set"

##
Expand All @@ -16,122 +15,123 @@ module Jpegtran
##
# Holds +jpegoptim+ command.
#

COMMAND = :jpegtran

##
# Indicates turn on/off style arguments.
#

BOOLEAN_ARGS = Set::new [
:optimize, :progressive, :grayscale, :perfect, :transpose,
:optimize, :progressive, :grayscale, :perfect, :transpose,
:transverse, :trim, :arithmetic
]

##
# Holds copy values.
#

COPY_OPTIONS = Set::new [
:none, :comments, :all
]

##
# Holds flip values.
#

FLIP_OPTIONS = Set::new [
:horizontal, :vertical
]

##
# Result structure.
#
# Return value contains only +:errors+ member. +:errors+ contains
# Return value contains only +:errors+ member. +:errors+ contains
# simply array of error messages.
#
#

Result = Struct::new(:errors)

##
# Holds output matchers.
#

ERROR = /jpegtran:\s*(.*)\s*/

##
# Checks if +jpegtran+ is available.
# @return [Boolean] +true+ if it is, +false+ in otherwise
#

def self.available?
return Whereis.available? self::COMMAND
`which #{self::COMMAND}`
$? == 0
end

##
# Performs optimizations above file. For list of arguments, see
# Performs optimizations above file. For list of arguments, see
# reference of +jpegtran+.
#
# If block is given, runs +jpegoptim+ asynchronously. In that case,
# If block is given, runs +jpegoptim+ asynchronously. In that case,
# +em-pipe-run+ file must be already required.
#
# @param [String, Array] paths file path or array of paths for optimizing
# @param [Hash] options options
# @param [Hash] options options
# @param [Proc] block block for giving back the results
# @return [Struct] see {Result}
#

def self.optimize(path, options = { }, &block)

# Command
cmd = CommandBuilder::new(self::COMMAND)
cmd.separators = ["-", " ", "-", " "]

# Turn on/off arguments
options.each_pair do |k, v|
if v.true? and k.in? self::BOOLEAN_ARGS
cmd << k
end
end

# Rotate
if options[:rotate].kind_of? Integer
cmd.arg(:rotate, options[:rotate].to_i)
elsif :rotate.in? options
raise Exception::new("Invalid value for :rotate option. Integer expected.")
end

# Rotate
if options[:restart].kind_of? Integer
cmd.arg(:restart, options[:restart].to_i)
elsif :restart.in? options
raise Exception::new("Invalid value for :restart option. Integer expected.")
end

# Crop
if options[:crop].string?
cmd.arg(:crop, options[:crop].to_s)
elsif :crop.in? options
raise Exception::new("Invalid value for :crop option. Structured string expected. See 'jpegtran' reference.")
end

# Scans
if options[:scans].string?
cmd.arg(:scans, options[:scans].to_s)
elsif :scans.in? options
raise Exception::new("Invalid value for :scans option. String expected.")
end

# Copy
if :copy.in? options
if :copy.in? options
value = options[:copy].to_sym
if vlaue.in? self::COPY_OPTIONS
cmd.arg(:copy, value)
else
raise Exception::new("Invalid value for :copy. Expected " << self::COPY_OPTIONS.to_s)
end
end
end

# Flip
if :flip.in? options
value = options[:flip].to_sym
Expand All @@ -141,7 +141,7 @@ def self.optimize(path, options = { }, &block)
raise Exception::new("Invalid value for :flip. Expected " << self::FLIP_OPTIONS.to_s)
end
end

# Outfile
if :outfile.in? options
if options[:outfile].string?
Expand All @@ -152,16 +152,16 @@ def self.optimize(path, options = { }, &block)
else
value = path.to_s
end

cmd.arg(:outfile, value)

# Runs the command
cmd << path.to_s

if options[:debug] == true
STDERR.write cmd.to_s + "\n"
end

# Blocking
if block.nil?
cmd.execute!
Expand All @@ -172,10 +172,10 @@ def self.optimize(path, options = { }, &block)
block.call()
end
end

end


# private
#
##
Expand All @@ -189,7 +189,7 @@ def self.optimize(path, options = { }, &block)
# errors << m[1]
# end
# end
#
#
# return errors
# end
end

0 comments on commit 655a66c

Please sign in to comment.