class Pod::Command::Strip

Public Instance Methods

analyze(source_files, images) click to toggle source

分析

# 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() click to toggle source

获取所有图片资源

# 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() click to toggle source

获取源文件

# 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() click to toggle source
# 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) click to toggle source

遍历目录找出所有图片

# 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!() click to toggle source
Calls superclass method
# 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