Class: Mcicons::Command

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

Constant Summary collapse

SIZE_LIST =
['16', '32', '128', '256', '512', '1024']

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ Command

Returns a new instance of Command.



15
16
17
# File 'lib/mcicons/command.rb', line 15

def initialize(argv)
  @argv = argv
end

Class Method Details

.run(argv) ⇒ Object



11
12
13
# File 'lib/mcicons/command.rb', line 11

def self.run(argv)
  new(argv).execute
end

Instance Method Details

#executeObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/mcicons/command.rb', line 19

def execute
  option = {}

  OptionParser.new do |opt|
    opt.on('-i Image PATH', 'Image Path') { |v| option[:i] = v }
    opt.on('-o Outputs PATH', 'Outputs Icons Path') { |v| option[:o] = v }

    opt.parse!(ARGV)
  end


  image_path  = option[:i]
  dir_path    = File.dirname(image_path)
  output_path = option[:o].nil? ? dir_path : option[:o]
  tmp_path    = "#{output_path}/app.iconset"

  Dir.mkdir(tmp_path, 999) unless FileTest.exist?(tmp_path)

  @counter = 0

  puts 'conversion start'

  SIZE_LIST.each do |r|
    image = MiniMagick::Image.open(image_path)
    image.format 'png'
    image.resize "#{r}x#{r}!"
    image.write "#{tmp_path}/icon_#{r}x#{r}.png"
    retina_image = MiniMagick::Image.open(image_path)
    retina       = r.to_i * 2
    retina_image.format 'png'
    retina_image.resize " #{retina}x#{retina}!"
    retina_image.write "#{tmp_path}/icon_#{r}x#{r}@2x.png"

    @counter += 100 / SIZE_LIST.count
    puts "#{@counter}% complete."

  end

  puts '100% conversion complete'

  system("iconutil -c icns #{tmp_path}")
  system("rm -rf #{tmp_path}")

  puts 'created icons file'

end