Class: XcodeAssetsGen::IconGen

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#assets_pathObject

Returns the value of attribute assets_path.



10
11
12
# File 'lib/xcode_assets_gen/icon_gen.rb', line 10

def assets_path
  @assets_path
end

#icon_set_listObject

Returns the value of attribute icon_set_list.



10
11
12
# File 'lib/xcode_assets_gen/icon_gen.rb', line 10

def icon_set_list
  @icon_set_list
end

#o_icon_pathObject

Returns the value of attribute o_icon_path.



10
11
12
# File 'lib/xcode_assets_gen/icon_gen.rb', line 10

def o_icon_path
  @o_icon_path
end

Instance Method Details

#gen_icon_set(icon_set, icon_save_path, icon_path) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/xcode_assets_gen/icon_gen.rb', line 57

def gen_icon_set icon_set, icon_save_path, icon_path
  images = []
  icon_set["icons"].each { |icon|
    image = MiniMagick::Image.open(icon_path)
    size = get_icon_size icon
    image.resize "#{size}x#{size}"
    image.format "png"
    icon_filename = "icon#{size}x#{size}.png"
    image.write File.join(icon_save_path, icon_filename)
    puts Rainbow("Generate #{icon_filename}").green
    icon['idiom'] = icon_set["idiom"]
    icon['filename'] = "icon#{size}x#{size}.png"
    images.push(icon)
  }
  # return content file json
  return images

end

#gen_icons(icon_set_list, assets_path, icon_path) ⇒ Object

icon_set_list is a list of icon set to generate, example: [‘ipad-ios7+’, ‘iphone-ios7+’]



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
# File 'lib/xcode_assets_gen/icon_gen.rb', line 22

def gen_icons icon_set_list, assets_path, icon_path
  config = load_config
  icon_save_path = File.join(assets_path, 'AppIcon.appiconset')
  FileUtils::mkdir_p icon_save_path
  puts Rainbow("Start generate icons").bright

  images = []
  icon_set_list.each { |icon_set|
    puts Rainbow("Icon set #{icon_set}").cyan
    sets = get_config_by_name(config, icon_set)
    if sets != nil
      images += gen_icon_set(sets, icon_save_path, icon_path)
    else
      puts Rainbow("Error: No #{icon_set}").red
    end
  }
  content = {
    "images": images,
    "info": {
      "version": 1,
      "author": "xcode"
    }
  }
  File.open(File.join(icon_save_path, "Contents.json"), "w") do |f|
    f.write(content.to_json)
  end
  puts Rainbow("Finish generate icons").green
end

#get_config_by_name(config, name) ⇒ Object



16
17
18
# File 'lib/xcode_assets_gen/icon_gen.rb', line 16

def get_config_by_name config, name
  config.detect { |config| config["name"] == name }
end

#get_icon_size(icon) ⇒ Object



51
52
53
54
55
# File 'lib/xcode_assets_gen/icon_gen.rb', line 51

def get_icon_size icon
  size = icon["size"].split('x').first.to_f
  scale = icon["scale"].split('x').first.to_f
  (size * scale).to_i
end

#load_configObject



12
13
14
# File 'lib/xcode_assets_gen/icon_gen.rb', line 12

def load_config
  return YAML.load_file(File.expand_path('../icons_size.yml', __FILE__))
end