Class: FindImage

Inherits:
Object
  • Object
show all
Defined in:
lib/happy_day_plugin.rb

Instance Method Summary collapse

Constructor Details

#initialize(dir) ⇒ FindImage

Returns a new instance of FindImage.



31
32
33
34
35
# File 'lib/happy_day_plugin.rb', line 31

def initialize(dir)
  @imgs = Array.new
  @mFiles = Array.new
  @dir = dir
end

Instance Method Details

#checkImageUsingObject

检查图片是否有被使用



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/happy_day_plugin.rb', line 77

def checkImageUsing
  @imgs.each do |img|
    _isUsed = false
    @mFiles.each do |file|
      # 在file中检查 img
      content = File.read(file)
      # puts content
      is2 = content =~ /#{File.basename(img, '@2x.png')}(.*)/
      is3 = content =~ /#{File.basename(img, '@3x.png')}(.*)/
      if (is2 || is3)
        _isUsed = true
      end
    end

    unless _isUsed
      outFile = File.new("unused.txt", "a+")
      if outFile
        outFile.syswrite(img)
        outFile.syswrite("\n")
      end
      puts img
    end
  end
end

#findObject



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/happy_day_plugin.rb', line 102

def find
  puts "|--开始"

  findImageWithPathDir(@dir)
  findMwithPathDir(@dir)
  puts "|--没有使用\n"
  checkImageUsing

  # @imgs.each do |img|
  #  puts img
  # end

  # @mFiles.each do |file|
  #  puts file
  # end

  puts "|--结束"
end

#findImageWithPathDir(pathDir) ⇒ Object

获得文件夹内的所有图片



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/happy_day_plugin.rb', line 38

def findImageWithPathDir(pathDir)

  Dir.foreach(pathDir) do |file|

    if file != '.' && file != '..' && !file.eql?('Pods')
      if File.directory? ("#{pathDir}/#{file}")

        findImageWithPathDir("#{pathDir}/#{file}")
      else 
        isPng = file.end_with? ".png"
        isJpg = file.end_with? ".jpg"
        if isPng || isJpg
          @imgs.push("#{pathDir}/#{file}")
        end
      end
    end
  end
end

#findMwithPathDir(pathDir) ⇒ Object

获得文件夹内所有.m文件



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/happy_day_plugin.rb', line 58

def findMwithPathDir(pathDir)
  Dir.foreach(pathDir) do |file|

    if file != '.' && file != '..'
      if File.directory? ("#{pathDir}/#{file}")

        findMwithPathDir("#{pathDir}/#{file}")
      else 
        isM = file.end_with? ".m"
        isJson = file.end_with? ".json"
        if isM || isJson
          @mFiles.push("#{pathDir}/#{file}")
        end
      end
    end
  end
end