Class: Pindo::XcodeResHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/pindo/module/xcode/xcodereshandler.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(proj_fullname: nil) ⇒ XcodeResHandler

Returns a new instance of XcodeResHandler.



73
74
75
76
# File 'lib/pindo/module/xcode/xcodereshandler.rb', line 73

def initialize(proj_fullname:nil)
    @proj_fullname = proj_fullname
    @project_obj = Xcodeproj::Project.open(proj_fullname)
end

Class Method Details

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



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

def self.create_icon(icon_name:nil, new_icon_dir:nil, xcode_icon_json:nil)
    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_imessage_icon(icon_name: nil, image_icon_name: nil, new_icon_dir: nil, xcode_icon_json: nil) ⇒ Object



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

def self.create_imessage_icon(icon_name:nil, image_icon_name:nil, new_icon_dir:nil, xcode_icon_json:nil)
    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_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

Instance Method Details

#get_xcodeproj_icon_pathObject



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

def get_xcodeproj_icon_path

    icon_path = nil
    select_target = @project_obj.targets.select { |target| target.product_type.include?(Xcodeproj::Constants::PRODUCT_TYPE_UTI[:application])  }.first
    if !select_target.nil?
        file_refs = select_target.resources_build_phase.files_references.select { |file| file.display_name.include?("Assets.xcassets") } || []
        file_refs.each do |file_ref|
            icon_path = File.join(file_ref.real_path,"AppIcon.appiconset")
            if File.exist?(icon_path)
                break
            else
                next
            end
        end
    end

    if icon_path.nil? || icon_path.empty? || !File.exist?(icon_path)
        raise Informative, "没有找到Xcode icon 目录"
    end

    return icon_path
end

#get_xcodeproj_imessage_icon_pathObject



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/pindo/module/xcode/xcodereshandler.rb', line 125

def get_xcodeproj_imessage_icon_path
    icon_path = nil
    select_target = @project_obj.targets.select { |target| target.product_type.include?(Xcodeproj::Constants::PRODUCT_TYPE_UTI[:messages_extension])  }.first
    if !select_target.nil?
        file_refs = select_target.resources_build_phase.files_references.select { |file| file.display_name.include?("Assets.xcassets") } || []
        file_refs.each do |file_ref|
            icon_path = File.join(file_ref.real_path,"iMessage App Icon.stickersiconset")
            if File.exist?(icon_path)
                break
            else
                next
            end
        end

        if icon_path.nil? || icon_path.empty? || !File.exist?(icon_path)
            raise Informative, "没有找到Xcode iMessage icon 目录"
        end
    end

    return icon_path
end

#get_xcodeproj_launchimg_pathObject



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/pindo/module/xcode/xcodereshandler.rb', line 173

def get_xcodeproj_launchimg_path

    launchimg_path = nil
    select_target = @project_obj.targets.select { |target| target.product_type.include?(Xcodeproj::Constants::PRODUCT_TYPE_UTI[:application])  }.first
    file_ref = select_target.resources_build_phase.files_references.select { |file| file.display_name.include?("Assets.xcassets") }.first

    assets_path = file_ref.real_path
    if File.exist?(File.join(assets_path, "LaunchImage.imageset"))
        launchimg_path = File.join(assets_path, "LaunchImage.imageset")
    else
        launchimg_path = File.join(assets_path, "LaunchImage.launchimage")
    end

    return launchimg_path
end

#install_icon_res(new_icon_dir: nil) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/pindo/module/xcode/xcodereshandler.rb', line 101

def install_icon_res(new_icon_dir:nil)
    icon_path = get_xcodeproj_icon_path
    begin
        FileUtils.rm_rf(icon_path)
        FileUtils.mkdir_p(icon_path)
    rescue StandardError => e
    end
    files = Dir.glob("#{new_icon_dir}/*").select { |file| File.file?(file) }
    files.each do |file|
        FileUtils.cp(file, icon_path)
    end
end

#install_imessage_icon_res(new_icon_dir: nil) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/pindo/module/xcode/xcodereshandler.rb', line 147

def install_imessage_icon_res(new_icon_dir:nil)
    icon_path = get_xcodeproj_imessage_icon_path
    begin
        FileUtils.rm_rf(icon_path)
        FileUtils.mkdir_p(icon_path)
    rescue StandardError => e
    end

    files = Dir.glob("#{new_icon_dir}/*").select { |file| File.file?(file) }
    files.each do |file|
        FileUtils.cp(file, icon_path)
    end
end

#install_launchimg(launchimg_file: nil) ⇒ Object



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/pindo/module/xcode/xcodereshandler.rb', line 189

def install_launchimg(launchimg_file:nil)


    xcodeproj_launchimg_path = get_xcodeproj_launchimg_path
    if xcodeproj_launchimg_path.nil? || !File.exist?(xcodeproj_launchimg_path)
        return
    end

    project_origin_launchimg = Dir.glob(File.join(xcodeproj_launchimg_path, "/*.png")).max_by {|f| File.mtime(f)}

    if project_origin_launchimg.nil? || !File.exist?(project_origin_launchimg)
        return
    end

    if File.exist?(project_origin_launchimg) && !File.exist?(launchimg_file)
        raise  Informative, "缺少启动文件:#{launchimg_file}"
    end

    begin
        FileUtils.rm_rf(xcodeproj_launchimg_path)
        FileUtils.mkdir_p(xcodeproj_launchimg_path)
    rescue StandardError => e
    end

    launch_json = XcodoeResConst.xcode_ios_launchimg_json
    file_name = launch_json["images"].select { |image| image["scale"].include?("2x") }.first["filename"]
    xcode_launchimg_name = File.join(xcodeproj_launchimg_path, file_name)
    FileUtils.cp(launchimg_file, xcode_launchimg_name)

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

end

#validate_icon_resObject



114
115
116
117
118
119
120
121
122
123
# File 'lib/pindo/module/xcode/xcodereshandler.rb', line 114

def validate_icon_res()
    icon_path = get_xcodeproj_icon_path
    xcode_ios_icon_json = XcodoeResConst.xcode_ios_icon_json
    xcode_ios_icon_json["images"].each do |image_data|
        image_name = image_data["filename"]
        if !File.exist?(File.join(icon_path, image_name))
            raise  Informative, "Xcode生成icon 有缺失文件:#{File.join(icon_path, image_name)}"
        end
    end
end

#validate_imessage_icon_resObject



162
163
164
165
166
167
168
169
170
171
# File 'lib/pindo/module/xcode/xcodereshandler.rb', line 162

def validate_imessage_icon_res()
    icon_path = get_xcodeproj_imessage_icon_path
    xcode_ios_icon_json = XcodoeResConst.xcode_ios_imessage_icon_json
    xcode_ios_icon_json["images"].each do |image_data|
        image_name = image_data["filename"]
        if !File.exist?(File.join(icon_path, image_name))
            raise  Informative, "Xcode生成icon 有缺失文件:#{File.join(icon_path, image_name)}"
        end
    end
end