Class: Icic::DrawableGenerator

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

Class Method Summary collapse

Class Method Details

.generate_android(options) ⇒ Object

GENERATE ANDROID ICONS



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/icic.rb', line 82

def self.generate_android(options)
  arr = %w(mdpi hdpi xhdpi xxhdpi xxxhdpi)
  arr.each do |res|
    img = MiniMagick::Image.open(options[:input])
    img.format 'png'

    case res
      when 'mdpi'
        img.resize '24x24'
      when 'hdpi'
        img.resize '36x36'
      when 'xhdpi'
        img.resize '48x48'
      when 'xxhdpi'
        img.resize '72x72'
      when 'xxxhdpi'
        img.resize '96x96'
      else
        break
    end

    # p "output: #{options[:output]}/drawable-#{res}/#{options[:name]}.png"
    FileUtils.makedirs "#{options[:output]}/res/drawable-#{res}/"
    img.write "#{options[:output]}/res/drawable-#{res}/#{options[:name]}.png"
  end

  puts "\n"
  puts "SUCCESS! Assets generated at #{options[:output]}/res"
end

.generate_ios(options) ⇒ Object

GENERATE IOS ICONS



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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/icic.rb', line 36

def self.generate_ios(options)
  contents_json = {
      images: [],
      info: {
          version: 1,
          author: 'xcode'
      }
  }
  dirname = "#{options[:output]}/#{options[:name]}.imageset"
  FileUtils.makedirs dirname
  arr = ['', '2x', '3x']
  arr.each do |res|
    img = MiniMagick::Image.open(options[:input])
    img.format 'png'

    case res
      when ''
        img.resize '29x29'
      when '2x'
        img.resize '58x58'
      when '3x'
        img.resize '87x87'
      else
        break
    end

    imageobj = {
        idiom: 'universal',
        filename: (res == '' ? "#{options[:name]}.png" : "#{options[:name]}_#{res}.png"),
        scale: res == '' ? '1x' : res
    }

    filepath = "#{dirname}/#{imageobj[:filename]}"
    img.write filepath

    contents_json[:images] << imageobj
  end
  File.open("#{dirname}/Contents.json", 'w') do |file|
    file.write(JSON.pretty_generate(contents_json))
  end

  puts "\n"
  puts "SUCCESS! Assets generated at #{options[:output]}/#{options[:name]}.imageset"
end

.runPromptObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/icic.rb', line 9

def self.runPrompt()
  prompt = TTY::Prompt.new

  options = {}
  options[:input] = prompt.ask('Original icon?', required: true)
  options[:platform] = prompt.select('Platform?', %w(ios android))

  original_name = File.basename(options[:input], ".*")
  original_dir = File.dirname options[:input]
  
  name = prompt.ask("Output filename? (press enter to use input's filename)") do |a|
    a.modify :trim
  end

  options[:name] = (name == nil || name.empty?) ? original_name : name

  output = prompt.ask("Output folder? (press enter to use input's parent folder)") do |a|
    a.modify :trim
  end

  options[:output] = (output == nil || output.empty?) ? original_dir : output

  # p options 
  (options[:platform] == 'ios') ? generate_ios(options) : generate_android(options)
end