Class: YJCocoa::UnusedImage

Inherits:
Unused show all
Defined in:
lib/yjcocoa/unused/unused_image.rb

Overview

Usage

Constant Summary

Constants inherited from Command

Command::DEFAULT_OPTIONS

Instance Attribute Summary collapse

Attributes inherited from Unused

#ignore, #match, #matcho, #output

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Unused

#check_ignore, #check_match

Methods inherited from Command

#askWithAnswers

Constructor Details

#initialize(argv) ⇒ UnusedImage

初始化



33
34
35
36
37
38
# 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

Instance Attribute Details

#class_contentObject

Returns the value of attribute class_content.



30
31
32
# File 'lib/yjcocoa/unused/unused_image.rb', line 30

def class_content
  @class_content
end

#dirObject

property



28
29
30
# File 'lib/yjcocoa/unused/unused_image.rb', line 28

def dir
  @dir
end

#image_hashObject

Returns the value of attribute image_hash.



29
30
31
# File 'lib/yjcocoa/unused/unused_image.rb', line 29

def image_hash
  @image_hash
end

Class Method Details

.optionsObject



21
22
23
24
25
# File 'lib/yjcocoa/unused/unused_image.rb', line 21

def self.options
    [['--dir', '项目文件夹'],
    ['--ignore', '忽略的字符串匹配, 多字符串用 "," 分隔'],
    ['--output', '日志存放路径,默认当前路径']] + super
end

Instance Method Details

#deal_with_file(filename) ⇒ Object



91
92
93
# 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) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
# 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) ⇒ Object



72
73
74
75
76
77
# 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

#runObject



53
54
55
56
57
58
# 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_imageObject



60
61
62
63
64
65
66
67
68
69
# 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!Object

businrss



41
42
43
44
45
46
47
48
49
50
51
# 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