Class: Fastlane::ActHelper::IconPatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/fastlane/plugin/act/helper/icon_patcher.rb

Class Method Summary collapse

Class Method Details

.delete_icons(archive, plist_buddy, delete_old_iconset) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/fastlane/plugin/act/helper/icon_patcher.rb', line 60

def self.delete_icons(archive, plist_buddy, delete_old_iconset)
  existing_icon_set_keys = plist_buddy.parse_dict_keys(plist_buddy.exec("Print"))
                                      .map { |k| k.match(/^CFBundleIcons(~.+)?$/) }
                                      .select { |m| m }

  existing_icon_set_keys.each do |match|
    key = match[0]
    idiom_suffix = match[1]

    icon_list_key = ":#{key}:CFBundlePrimaryIcon:CFBundleIconFiles"

    begin
      icon_files_value = plist_buddy.exec "Print #{icon_list_key}"
    rescue
      next
    end

    existing_icons = plist_buddy.parse_scalar_array(icon_files_value)

    if existing_icons.size && delete_old_iconset
      icons_to_delete = existing_icons.map { |name| archive.app_path("#{name}#{idiom_suffix}*") }

      icons_to_delete.each do |icon_to_delete|
        archive.delete icon_to_delete
      end

    end

    plist_buddy.exec "Delete #{icon_list_key}"
  end
end

.get_icons_from_iconset(icon_set_path) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/fastlane/plugin/act/helper/icon_patcher.rb', line 36

def self.get_icons_from_iconset(icon_set_path)
  icon_set = File.basename(icon_set_path, ".*")

  icon_set_manifest_file = File.expand_path "#{icon_set_path}/Contents.json"
  raise ".iconset manifest #{icon_set_manifest_file} does not exist" unless File.exist? icon_set_manifest_file

  icon_set_manifest = JSON.parse(File.read(icon_set_manifest_file))

  valid_icon_images = icon_set_manifest["images"].select { |image| image['filename'] }

  return valid_icon_images.map do |entry|
    scale_suffix = entry['scale'] == '1x' ? '' : "@" + entry['scale']
    idiom_suffix = entry['idiom'] == "iphone" ? '' : "~" + entry['idiom']
    file_extension = File.extname(entry['filename'])

    {
      source: "#{icon_set_path}/#{entry['filename']}",
      name: "#{icon_set}#{entry['size']}",
      idiom: entry['idiom'],
      target: "#{icon_set}#{entry['size']}#{scale_suffix}#{idiom_suffix}#{file_extension}"
    }
  end
end

.patch(archive, iconset_path, delete_old_iconset) ⇒ Object



4
5
6
7
8
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
34
# File 'lib/fastlane/plugin/act/helper/icon_patcher.rb', line 4

def self.patch(archive, iconset_path, delete_old_iconset)
  plist_path = archive.app_path("Info.plist")
  archive.extract(plist_path)

  UI.message("Patching icons from: #{iconset_path}")

  plist_buddy = PlistBuddy.new archive.local_path(plist_path)

  self.delete_icons(archive, plist_buddy, delete_old_iconset)

  icon_list = get_icons_from_iconset(iconset_path)
  icon_list.group_by { |i| i[:idiom] }.each do |idiom, icons|
    idiom_suffix = idiom == "iphone" ? "" : "~#{idiom}"
    icons_plist_key = ":CFBundleIcons#{idiom_suffix}:CFBundlePrimaryIcon:CFBundleIconFiles"

    plist_buddy.exec("Add #{icons_plist_key} array")

    icons.each do |i|
      relative_path = archive.app_path( (i[:target]).to_s )
      local_path = archive.local_path(relative_path)
      `cp #{i[:source].shellescape} #{local_path.shellescape}`
      archive.replace(relative_path)
    end

    icons.map { |i| i[:name] }.uniq.each_with_index do |key, index|
      plist_buddy.exec("Add #{icons_plist_key}:#{index} string #{key}")
    end
  end

  archive.replace(plist_path)
end