Class: Pindo::XcodeResHandler

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

Instance Method Summary collapse

Constructor Details

#initialize(proj_fullname: nil) ⇒ XcodeResHandler

Returns a new instance of XcodeResHandler.



10
11
12
13
# File 'lib/pindo/module/xcode/xcodereshandler.rb', line 10

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

Instance Method Details

#get_xcodeproj_icon_pathObject



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
41
42
43
44
45
46
47
# File 'lib/pindo/module/xcode/xcodereshandler.rb', line 15

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

    project_dir = @project_obj.project_dir
  
    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)
        icon_path_array = Dir.glob(File.join(project_dir, "**", "AppIcon.appiconset"))
        if icon_path_array.size > 1
            icon_path = icon_path_array.select{ |filename| File.directory?(filename) && filename.include?("Assets.xcassets/AppIcon.appiconset")}
        elsif icon_path_array.size == 1
            icon_path = icon_path_array.first
        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



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/pindo/module/xcode/xcodereshandler.rb', line 92

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
    project_dir = @project_obj.project_dir
    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)
            icon_path_array = Dir.glob(File.join(project_dir, "**", "iMessage App Icon.stickersiconset"))
            if icon_path_array.size > 1
                icon_path = icon_path_array.select{ |filename| File.directory?(filename) && filename.include?("Assets.xcassets/iMessage App Icon.stickersiconset")}
            elsif icon_path_array.size == 1
                icon_path = icon_path_array.first
            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



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/pindo/module/xcode/xcodereshandler.rb', line 169

def get_xcodeproj_launchimg_path

    launchimg_path = nil
    assets_path = nil
    select_target = @project_obj.targets.select { |target| target.product_type.include?(Xcodeproj::Constants::PRODUCT_TYPE_UTI[:application])  }.first
    project_dir = @project_obj.project_dir
    if !select_target.nil?
        assets_objs = select_target.resources_build_phase.files_references.select { |file| file.display_name.include?("Assets.xcassets") } || []
        if assets_objs.size > 0
            assets_path = assets_objs.first.real_path
            if File.exist?(assets_path) && File.exist?(File.join(assets_path, "LaunchImage.imageset"))
                launchimg_path = File.join(assets_path, "LaunchImage.imageset")
            elsif File.exist?(assets_path) && File.exist?(File.join(assets_path, "LaunchImage.launchimage"))
                launchimg_path = File.join(assets_path, "LaunchImage.launchimage")
            end
        end
    end

    if launchimg_path.nil? || launchimg_path.empty? || !File.exist?(launchimg_path)
        launchimg_path_array = Dir.glob(File.join(project_dir, "**", "LaunchImage.imageset"))
        if launchimg_path_array.size > 1
            launchimg_path = launchimg_path_array.select{ |filename| File.directory?(filename) && filename.include?("Assets.xcassets/LaunchImage.imageset")}
        elsif launchimg_path_array.size == 1
            launchimg_path = launchimg_path_array.first
        end
    end

    if launchimg_path.nil? || launchimg_path.empty? || !File.exist?(launchimg_path)
        launchimg_path_array = Dir.glob(File.join(project_dir, "**", "LaunchImage.launchimage"))
        if launchimg_path_array.size > 1
            launchimg_path = launchimg_path_array.select{ |filename| File.directory?(filename) && filename.include?("Assets.xcassets/LaunchImage.launchimage")}
        elsif launchimg_path_array.size == 1
            launchimg_path = launchimg_path_array.first
        end
    end

    return launchimg_path
end

#install_icon_res(new_icon_dir: nil) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/pindo/module/xcode/xcodereshandler.rb', line 49

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



124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/pindo/module/xcode/xcodereshandler.rb', line 124

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_pub_path: nil) ⇒ Object



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/pindo/module/xcode/xcodereshandler.rb', line 208

def install_launchimg(launchimg_pub_path:nil)

    xcodeproj_launchimg_path = get_xcodeproj_launchimg_path
    pub_launchimg_files = Dir.glob(File.join(launchimg_pub_path, "*.{png,jpg}"))

    if xcodeproj_launchimg_path.nil? || !File.exist?(xcodeproj_launchimg_path)
        if !pub_launchimg_files.nil? && pub_launchimg_files.size > 0
            raise  Informative, "有需要替换的资源,但是未找到工程启动图目录,替换启动图失败!"
        end
        return
    end

    # if pub_launchimg_files.nil? || pub_launchimg_files.size == 0
    #     raise  Informative, "有需要替换的资源,但是配置中未放置替换的启动图!"
    # end

    xcodeproj_launchimg_json_file = File.join(xcodeproj_launchimg_path, "Contents.json")
    if !File.exist?(xcodeproj_launchimg_json_file)
        raise  Informative, "工程启动图目录缺少Contents.json,替换启动图失败!"
    end

    xcodeproj_launchimg_json = JSON.parse(File.read(xcodeproj_launchimg_json_file)) 
    image_array = xcodeproj_launchimg_json["images"].select { |image| !image["filename"].nil? && !image["filename"].empty? }
    
    if pub_launchimg_files.size == image_array.size
        if image_array.size == 1
            launchimg_filename = image_array.first["filename"]
            launchimg_full_filename = File.join(xcodeproj_launchimg_path, launchimg_filename)
            FileUtils.cp(pub_launchimg_files.first, launchimg_full_filename)
        else
            raise  Informative, "启动图有多张,替换异常,遇到这种情况再处理!"
        end
    else
        raise  Informative, "工程中启动图数目和配置仓库的图片数目不一致"
    end

end

#validate_icon_resObject



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/pindo/module/xcode/xcodereshandler.rb', line 62

def validate_icon_res()
    icon_path = get_xcodeproj_icon_path()
    contents_json_path = File.join(icon_path, "Contents.json")

    # 检查 Contents.json 是否存在
    if !File.exist?(contents_json_path)
        raise Informative, "Contents.json 文件不存在: #{contents_json_path}"
    end

    # 读取实际的 Contents.json 文件
    begin
        actual_json = JSON.parse(File.read(contents_json_path))
    rescue JSON::ParserError => e
        raise Informative, "Contents.json 文件格式错误: #{e.message}"
    end

    # 基于实际的 Contents.json 验证对应的图标文件
    if actual_json["images"] && actual_json["images"].is_a?(Array)
        actual_json["images"].each do |image_data|
            # 只验证有 filename 字段的条目
            if image_data["filename"] && !image_data["filename"].empty?
                image_file_path = File.join(icon_path, image_data["filename"])
                if !File.exist?(image_file_path)
                    raise Informative, "Xcode icon 缺失文件: #{image_file_path}"
                end
            end
        end
    end
end

#validate_imessage_icon_resObject



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/pindo/module/xcode/xcodereshandler.rb', line 139

def validate_imessage_icon_res()
    icon_path = get_xcodeproj_imessage_icon_path
    contents_json_path = File.join(icon_path, "Contents.json")

    # 检查 Contents.json 是否存在
    if !File.exist?(contents_json_path)
        raise Informative, "iMessage Contents.json 文件不存在: #{contents_json_path}"
    end

    # 读取实际的 Contents.json 文件
    begin
        actual_json = JSON.parse(File.read(contents_json_path))
    rescue JSON::ParserError => e
        raise Informative, "iMessage Contents.json 文件格式错误: #{e.message}"
    end

    # 基于实际的 Contents.json 验证对应的图标文件
    if actual_json["images"] && actual_json["images"].is_a?(Array)
        actual_json["images"].each do |image_data|
            # 只验证有 filename 字段的条目
            if image_data["filename"] && !image_data["filename"].empty?
                image_file_path = File.join(icon_path, image_data["filename"])
                if !File.exist?(image_file_path)
                    raise Informative, "iMessage icon 缺失文件: #{image_file_path}"
                end
            end
        end
    end
end