class YJCocoa::UnusedImage

Usage

Attributes

class_content[RW]
dir[RW]

property

image_hash[RW]

Public Class Methods

new(argv) click to toggle source

初始化

Calls superclass method YJCocoa::Unused::new
# File lib/yjcocoa/unused/unused_image.rb, line 33
def initialize(argv)
    super
    self.dir = argv.option('dir')
    self.image_hash = Hash.new
    self.class_content = ""
end
options() click to toggle source
Calls superclass method YJCocoa::Command::options
# File lib/yjcocoa/unused/unused_image.rb, line 21
def self.options
    [['--dir', '项目文件夹'],
    ['--ignore', '忽略的字符串匹配, 多字符串用 "," 分隔'],
    ['--output', '日志存放路径,默认当前路径']] + super
end

Public Instance Methods

deal_with_file(filename) click to toggle source
# File lib/yjcocoa/unused/unused_image.rb, line 91
def deal_with_file(filename)
    self.class_content << File.read(filename) unless File.symlink?(filename)
end
deal_with_image(filename) click to toggle source
# File lib/yjcocoa/unused/unused_image.rb, line 79
def deal_with_image(filename)
    return if self.check_ignore(filename)
    basename = File.basename(filename)
    image_gsub = [".png", ".jpg", "@1x", "@2x", "@3x", "@1X", "@2X", "@3X"]
    image_gsub.each {|gsub| basename.gsub!(gsub, "")}
    if self.image_hash.include?(basename)
        self.image_hash[basename] << filename
    else
        self.image_hash[basename] = [filename]
    end
end
deal_with_path(path) click to toggle source
# File lib/yjcocoa/unused/unused_image.rb, line 72
def deal_with_path(path)
    Dir.chdir(path) {
        Dir["**/*.{png,jpg}"].each {|filename| self.deal_with_image(filename)}
        Dir["**/*.{h,m,mm,swift,xib,storyboard,plist}"].each {|filename| self.deal_with_file(filename)}
    }
end
run() click to toggle source
# File lib/yjcocoa/unused/unused_image.rb, line 53
def run
    unused_image = self.unreferenced_image
    puts "分析完毕,打开日志:#{self.output}".green
    File.write(self.output, unused_image.join("\n"))
    `open #{self.output}`
end
unreferenced_image() click to toggle source
# File lib/yjcocoa/unused/unused_image.rb, line 60
def unreferenced_image
    self.deal_with_path(self.dir)
    puts "分析图片资源".green
    result = []
    self.image_hash.each {|key, value|
        puts key
        result += value unless self.class_content.include?(key)
    }
    return result.sort
end
validate!() click to toggle source

businrss

Calls superclass method
# File lib/yjcocoa/unused/unused_image.rb, line 41
def validate!
    super
    unless self.dir && self.dir.length > 0
        puts "dir 文件地址为空".red
        self.banner!
    end
    unless Dir.exist?(self.dir)
        puts "dir 文件路径 #{self.dir} 不是文件夹".red
        self.banner!
    end
end