Class: MakeGallery::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/make_gallery/command.rb

Constant Summary collapse

OUTLOG =
HighLine.new($stdin, $stdout)
ERRLOG =
HighLine.new($stdin, $stderr)
FATTRS =
%w[action source target size format quality force verbose debug dry_run mogrify_path]

Instance Method Summary collapse

Constructor Details

#initialize(action, options) ⇒ Command

Returns a new instance of Command.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/make_gallery/command.rb', line 19

def initialize(action, options)
  action(action)

  options.each do |k, v|
    public_send k, v
  end

  if debug
    fattrs.each do |attr|
      a = ERRLOG.color(attr, :cyan, :on_black)
      b = ERRLOG.color(public_send(attr).inspect, :white, :on_black)
      ERRLOG.say("DEBUG: #{a}: #{b}")
    end
  end

  fatal_say("#{target} exists. Use --force to overwrite") if File.exists?(target) && ! force
end

Instance Method Details

#build_commandObject



62
63
64
65
66
67
68
69
# File 'lib/make_gallery/command.rb', line 62

def build_command
  mogrify = locate_mogrify
  cmd_opts = set_mogrify_options
  source_images = select_source_images
  [mogrify, cmd_opts, source_images].flatten.tap do |t|
    debug_say("Command: #{t.inspect}")
  end
end

#debug_say(m, *colors) ⇒ Object



133
134
135
136
137
138
139
140
# File 'lib/make_gallery/command.rb', line 133

def debug_say(m, *colors)
  if colors.empty?
    colors = [:yellow, :on_black, :bold]
  end

  m = ERRLOG.color(m, *colors)
  ERRLOG.say("DEBUG: #{m}") if debug
end

#fatal_say(m, *colors) ⇒ Object



122
123
124
125
126
127
128
129
130
131
# File 'lib/make_gallery/command.rb', line 122

def fatal_say(m, *colors)
  if colors.empty?
    colors = [:red, :on_black, :bold]
  end

  m = ERRLOG.color(m, *colors)
  ERRLOG.say("FATAL: #{m}")

  raise MakeGallery::Exception.new(m)
end

#fattrsObject



14
15
16
# File 'lib/make_gallery/command.rb', line 14

def fattrs
  self.class.fattrs
end

#locate_mogrifyObject



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/make_gallery/command.rb', line 72

def locate_mogrify
  if mogrify_path
    fatal_say("Path given for mogrify command is wrong") unless File.exist?(mogrify_path)
    mogrify = mogrify_path
  else
    mogrify = %x(which mogrify).chomp
    fatal_say("Cannot find mogrify command. Use --mogrify_path option instead") unless File.exist?(mogrify)
  end
  debug_say("mogrify command: #{mogrify.inspect}")
  mogrify
end

#process_imagesObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/make_gallery/command.rb', line 41

def process_images
  say("Creating #{action} images in #{source} to #{target}\n", :green)
  
  FileUtils.mkdir_p(target) unless dry_run

  cmd = build_command.join(" ")
  say("\nRunning: #{cmd}", :green)

  # skip the rest if it's a dry run
  unless dry_run
    o, e, s = Open3.capture3(cmd)
    fatal e unless s.success?
    say("\nOutput:\n", :green)
    say(o, :cyan)
    say(e, :cyan)
    say("\n")
  end

  say("\nCompleted\n", :green)
end

#say(m, *colors) ⇒ Object



113
114
115
116
117
118
119
120
# File 'lib/make_gallery/command.rb', line 113

def say(m, *colors)
  if colors.empty?
    colors = [:red, :on_black, :bold]
  end

  m = OUTLOG.color(m, *colors)
  OUTLOG.say(m) if verbose
end

#select_source_imagesObject



104
105
106
107
108
109
110
111
# File 'lib/make_gallery/command.rb', line 104

def select_source_images
  debug_say "source: #{source}"
  debug_say "source files: #{Dir[File.join(source,'*')]}"
  source_images = Dir[File.join(source,'*')].grep(/jpe?g|png|gif/i)
  debug_say "source images: #{source_images.inspect}"
  fatal_say("No images found in #{source}!") if source_images.empty?
  source_images
end

#set_mogrify_optionsObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/make_gallery/command.rb', line 84

def set_mogrify_options
  cmd_opts = []
  cmd_opts << "-verbose" if verbose
  cmd_opts << "-format #{format}"
  cmd_opts << "-path #{target}"
  cmd_opts << "-quality #{quality}"

  if action == :thumbs
    geometry = [size, size].join("x")
    cmd_opts << "-thumbnail #{geometry}^"
    cmd_opts << "-gravity center"
    cmd_opts << "-extent #{geometry}"
  elsif action == :web
    cmd_opts << "-resize #{size}"
  end

  debug_say "command opts: #{cmd_opts.inspect}"
  cmd_opts
end

#to_hObject



37
38
39
# File 'lib/make_gallery/command.rb', line 37

def to_h
  fattrs.inject({}) {|hash, attr| hash.update attr => public_send(attr) }
end