Class: Pod::Command::Strip
- Inherits:
-
Pod::Command
- Object
- Pod::Command
- Pod::Command::Strip
- Defined in:
- lib/cocoapods-dongjia/command/strip.rb
Instance Method Summary collapse
-
#analyze(source_files, images) ⇒ Object
分析.
-
#get_images ⇒ Object
获取所有图片资源.
-
#get_source_files ⇒ Object
获取源文件.
- #run ⇒ Object
-
#traverse(path, except_dirs, bk = nil) ⇒ Object
遍历目录找出所有图片.
- #validate! ⇒ Object
Instance Method Details
#analyze(source_files, images) ⇒ Object
分析
130 131 132 133 134 135 136 137 138 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/cocoapods-dongjia/command/strip.rb', line 130 def analyze(source_files, images) source_file_paths = source_files.map { |x| x.file_ref.real_path } .select { |x| File.exist?(x) } count = source_file_paths.count source_file_paths.each_index do | index | percent = index.to_f / count.to_f * 100 print "\r" if index != 0 print "#{format("%.2f", percent)}%" path = source_file_paths[index] File.open(path, 'r') do |f| image_using = [] f.each_line do |line| next unless line.include?('"') ['"', '/'].each do |prefix| images.each do |img| if line.include?(prefix + img.name) image_using << img else # 根据下划线分割剔除最后一项,再尝试匹配 if !img.fuzzy_name.empty? ['%', '"'].each do |suffix| image_using << img if line.include?(prefix + img.fuzzy_name + suffix) end end end end end end images = images - image_using end end print "\r" images end |
#get_images ⇒ Object
获取所有图片资源
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/cocoapods-dongjia/command/strip.rb', line 104 def get_images images = Set[] except_dirs = [ 'QYResource.bundle', 'DJMate', 'IQKeyboardManager', 'MJRefresh.bundle', 'AlipaySDK.bundle', 'AppIcon.appiconset', 'KPCameraImages.xcassets', 'UMSocialSDKResources.bundle', 'LaunchResource' ] except_files = [ 'image_placeholder', 'register_add_avatar' ] bk = Proc.new do |dir, name, ext| next if except_files.include?(name) images << Image.new(dir, name) end traverse(Dir.pwd, except_dirs, bk) images end |
#get_source_files ⇒ Object
获取源文件
88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/cocoapods-dongjia/command/strip.rb', line 88 def get_source_files source_files = [] @proj_paths.each do |path| proj = Xcodeproj::Project.open(path) proj.targets.each do |target| target.build_phases.each do |phase| if phase.is_a?(Xcodeproj::Project::PBXSourcesBuildPhase) source_files |= phase.files end end end end source_files end |
#run ⇒ Object
169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/cocoapods-dongjia/command/strip.rb', line 169 def run images = analyze(get_source_files, get_images) if images.empty? puts "未找到无效资源" else puts "无效资源文件:" images.each do |img| path = Pathname.new(img.fullpath).relative_path_from(Dir.pwd).to_s puts " #{path}" end end end |
#traverse(path, except_dirs, bk = nil) ⇒ Object
遍历目录找出所有图片
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/cocoapods-dongjia/command/strip.rb', line 63 def traverse(path, except_dirs, bk = nil) dir = File.dirname(path) ext = File.extname(path) name = File.basename(path, ext) basename = File.basename(path) if except_dirs.include?(basename) return end if File.directory?(path) if ext == '.imageset' # imageset 直接返回 bk.call(dir, name, ext) else Dir.foreach(path) do |name| unless name.start_with?('.') || name.end_with?('.launchimage') traverse(File.join(path, name), except_dirs, bk) end end end else bk.call(dir, name, ext) if @img_exts.include?(ext) end end |
#validate! ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/cocoapods-dongjia/command/strip.rb', line 50 def validate! super @img_exts = ['.png', '.jpg', '.jpeg', '.webp', '.gif'] @proj_paths = [] @proj_paths |= Pathname.glob('*.xcodeproj') @proj_paths |= Pathname.glob('Pods/*.xcodeproj') help! '未发现任何工程' unless @proj_paths.count > 0 end |