Module: Pindo::XcodeResHelper

Defined in:
lib/pindo/module/xcode/xcodereshelper.rb

Class Method Summary collapse

Class Method Details

.create_icon(icon_name: nil, new_icon_dir: nil, xcode_icon_json: nil) ⇒ Object



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
35
36
37
38
39
40
# File 'lib/pindo/module/xcode/xcodereshelper.rb', line 9

def self.create_icon(icon_name:nil, new_icon_dir:nil, xcode_icon_json:nil)
    if !File.exist?(icon_name)
        raise  Informative, "文件不存在:#{icon_name}"
    end
    xcode_icon_json["images"].each do |image_data|
        width,height  = image_data["size"].split("x")
        image_name = image_data["filename"]
        iNum = image_name.split("@").last.chomp(".png").chomp("x").to_f
        width = width.to_f * iNum
        height = height.to_f * iNum

        width = width.to_i
        height = height.to_i

        command = [
            'sips',
            '--matchTo', '/System/Library/ColorSync/Profiles/sRGB Profile.icc',
            '-z', width.to_s, height.to_s,
            icon_name,
            '--out', File.join(new_icon_dir, image_name)
        ]
        Executable.capture_command('sips', command, capture: :out)

        # if !File.exist?(File.join(new_icon_dir, image_name))
        #     raise  Informative, "生成icon失败!"
        # end
        # system("sips --matchTo '/System/Library/ColorSync/Profiles/sRGB Profile.icc' -z #{width} #{height} #{icon_name} --out #{new_icon_dir}/#{image_name}")
    end
    File.open(File.join(new_icon_dir, "Contents.json"), "w") do |f|
        f.write(JSON.pretty_generate(xcode_icon_json))
    end
end

.create_icons(icon_name: nil, new_icon_dir: nil) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/pindo/module/xcode/xcodereshelper.rb', line 77

def self.create_icons(icon_name:nil, new_icon_dir:nil)
    begin
        FileUtils.mkdir_p(new_icon_dir) 
    rescue => e
        puts e
    end
    create_icon(icon_name: icon_name, new_icon_dir: new_icon_dir, xcode_icon_json: XcodoeResConst.xcode_ios_icon_json)
    icon_dir = File.dirname(icon_name)
    imessage_icon = File.join(icon_dir, "icon1024_768.png")
    new_imessage_icon_dir = File.join(new_icon_dir, "imessage")

    if File.exist?(imessage_icon)
        begin
            FileUtils.mkdir_p(new_imessage_icon_dir) 
        rescue => e
            puts e
        end
        create_imessage_icon(icon_name: icon_name, image_icon_name:imessage_icon, new_icon_dir: new_imessage_icon_dir, xcode_icon_json: XcodoeResConst.xcode_ios_imessage_icon_json)
    end
end

.create_imessage_icon(icon_name: nil, image_icon_name: nil, new_icon_dir: nil, xcode_icon_json: nil) ⇒ Object



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
# File 'lib/pindo/module/xcode/xcodereshelper.rb', line 43

def self.create_imessage_icon(icon_name:nil, image_icon_name:nil, new_icon_dir:nil, xcode_icon_json:nil)
    if !File.exist?(icon_name) || !File.exist?(image_icon_name) 
        raise  Informative, "文件不存在:#{image_icon_name} or #{icon_name}"
    end
    xcode_icon_json["images"].each do |image_data|

        height, width = image_data["size"].split("x")
        image_name = image_data["filename"]
        iNum = image_name.split("@").last.chomp(".png").chomp("x").to_f
        width = width.to_f * iNum
        height = height.to_f * iNum
        width = width.to_i
        height = height.to_i

        icon_ori_name = image_icon_name
        if width.to_s.eql?(height.to_s)
            icon_ori_name = icon_name
        end

        command = [
            'sips',
            '--matchTo', '/System/Library/ColorSync/Profiles/sRGB Profile.icc',
            '-z', width.to_s, height.to_s,
            icon_ori_name,
            '--out', File.join(new_icon_dir, image_name)
        ]
        Executable.capture_command('sips', command, capture: :out)
    end

    File.open(File.join(new_icon_dir, "Contents.json"), "w") do |f|
        f.write(JSON.pretty_generate(xcode_icon_json))
    end
end

.install_icon(proj_dir: nil, new_icon_dir: nil) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/pindo/module/xcode/xcodereshelper.rb', line 98

def self.install_icon(proj_dir:nil, new_icon_dir:nil)
    xcodeproj_file_name = Dir.glob(File.join(proj_dir, "/*.xcodeproj")).max_by {|f| File.mtime(f)}
    xcodereshandler = XcodeResHandler.new(proj_fullname: xcodeproj_file_name)
    xcodereshandler.install_icon_res(new_icon_dir: new_icon_dir)
    xcodereshandler.validate_icon_res

    new_imessage_icon_dir = File.join(new_icon_dir, "imessage")
    if File.exist?(new_imessage_icon_dir)
        xcodereshandler.install_imessage_icon_res(new_icon_dir: new_imessage_icon_dir)
        xcodereshandler.validate_imessage_icon_res
    end
end

.install_launchimg(proj_dir: nil, launchimg_filename: nil) ⇒ Object



111
112
113
114
115
# File 'lib/pindo/module/xcode/xcodereshelper.rb', line 111

def self.install_launchimg(proj_dir:nil, launchimg_filename:nil)
    xcodeproj_file_name = Dir.glob(File.join(proj_dir, "/*.xcodeproj")).max_by {|f| File.mtime(f)}
    xcodereshandler = XcodeResHandler.new(proj_fullname: xcodeproj_file_name)
    xcodereshandler.install_launchimg(launchimg_file:launchimg_filename)
end