Class: Pindo::XcodeResHandler
- Inherits:
-
Object
- Object
- Pindo::XcodeResHandler
- Defined in:
- lib/pindo/module/xcode/res/xcode_res_handler.rb
Instance Method Summary collapse
-
#command_line_tool? ⇒ Boolean
工程是否为命令行工具(com.apple.product-type.tool).
-
#find_xcodeproj_icon_path ⇒ Object
查找工程的 AppIcon.appiconset 目录,找不到返回 nil(不抛异常).
- #get_xcodeproj_icon_path ⇒ Object
- #get_xcodeproj_imessage_icon_path ⇒ Object
- #get_xcodeproj_launchimg_path ⇒ Object
-
#has_app_icon_dir? ⇒ Boolean
工程是否包含 AppIcon 目录(macOS 命令行工具等无 icon 工程返回 false).
-
#initialize(proj_fullname: nil) ⇒ XcodeResHandler
constructor
A new instance of XcodeResHandler.
- #install_icon_res(new_icon_dir: nil) ⇒ Object
- #install_imessage_icon_res(new_icon_dir: nil) ⇒ Object
- #install_launchimg(launchimg_pub_path: nil) ⇒ Object
- #validate_icon_res ⇒ Object
- #validate_imessage_icon_res ⇒ Object
Constructor Details
#initialize(proj_fullname: nil) ⇒ XcodeResHandler
Returns a new instance of XcodeResHandler.
10 11 12 13 |
# File 'lib/pindo/module/xcode/res/xcode_res_handler.rb', line 10 def initialize(proj_fullname:nil) @proj_fullname = proj_fullname @project_obj = Xcodeproj::Project.open(proj_fullname) end |
Instance Method Details
#command_line_tool? ⇒ Boolean
工程是否为命令行工具(com.apple.product-type.tool)
55 56 57 58 59 60 |
# File 'lib/pindo/module/xcode/res/xcode_res_handler.rb', line 55 def command_line_tool?() tool_uti = Xcodeproj::Constants::PRODUCT_TYPE_UTI[:command_line_tool] @project_obj.targets.any? do |target| target.respond_to?(:product_type) && !target.product_type.nil? && target.product_type == tool_uti end end |
#find_xcodeproj_icon_path ⇒ Object
查找工程的 AppIcon.appiconset 目录,找不到返回 nil(不抛异常)
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/res/xcode_res_handler.rb', line 16 def find_xcodeproj_icon_path() icon_path = nil select_target = @project_obj.targets.select { |target| target.respond_to?(:product_type) && 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.find{ |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) return nil end return icon_path end |
#get_xcodeproj_icon_path ⇒ Object
62 63 64 65 66 67 68 |
# File 'lib/pindo/module/xcode/res/xcode_res_handler.rb', line 62 def get_xcodeproj_icon_path() icon_path = find_xcodeproj_icon_path() if icon_path.nil? raise Informative, "没有找到Xcode icon 目录" end return icon_path end |
#get_xcodeproj_imessage_icon_path ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/pindo/module/xcode/res/xcode_res_handler.rb', line 113 def icon_path = nil select_target = @project_obj.targets.select { |target| target.respond_to?(:product_type) && 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_path ⇒ Object
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 223 224 225 226 227 |
# File 'lib/pindo/module/xcode/res/xcode_res_handler.rb', line 190 def get_xcodeproj_launchimg_path launchimg_path = nil assets_path = nil select_target = @project_obj.targets.select { |target| target.respond_to?(:product_type) && 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 |
#has_app_icon_dir? ⇒ Boolean
工程是否包含 AppIcon 目录(macOS 命令行工具等无 icon 工程返回 false)
50 51 52 |
# File 'lib/pindo/module/xcode/res/xcode_res_handler.rb', line 50 def has_app_icon_dir?() !find_xcodeproj_icon_path().nil? end |
#install_icon_res(new_icon_dir: nil) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/pindo/module/xcode/res/xcode_res_handler.rb', line 70 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
145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/pindo/module/xcode/res/xcode_res_handler.rb', line 145 def (new_icon_dir:nil) 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
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 |
# File 'lib/pindo/module/xcode/res/xcode_res_handler.rb', line 229 def install_launchimg(launchimg_pub_path:nil) pub_launchimg_files = Dir.glob(File.join(launchimg_pub_path, "*.{png,jpg}")) # 如果配置仓库中没有启动图,直接跳过 if pub_launchimg_files.nil? || pub_launchimg_files.size == 0 return end # 配置仓库中有启动图,需要进行替换 xcodeproj_launchimg_path = get_xcodeproj_launchimg_path # 如果项目中没有启动图目录,报错中断 if xcodeproj_launchimg_path.nil? || !File.exist?(xcodeproj_launchimg_path) raise Informative, "配置仓库中有 #{pub_launchimg_files.size} 个启动图资源需要替换,但项目中未找到 LaunchImage.launchimage 目录,替换启动图失败!" 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_res ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/pindo/module/xcode/res/xcode_res_handler.rb', line 83 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.}" 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_res ⇒ Object
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/pindo/module/xcode/res/xcode_res_handler.rb', line 160 def () 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.}" 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 |