-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathcopyImages.rb
73 lines (62 loc) · 2.36 KB
/
copyImages.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/ruby
# -*- coding: UTF-8 -*-
# author zhangheng 2019-08-21
require 'fileutils'
#查找代码和xib中引用到的图片名
def findUsedImages
projectName = File.basename(Dir.getwd)
puts("使用当前脚本的路径")
projectDir = Dir.getwd + "/" + projectName
imageResults = Array.new()
Dir.glob(projectDir + "/**/**/**/**.{h,m}").each do |name|
next if Dir.exists? name
text = File.read(name)
pattern = /UIImage imageNamed:@"\S+\"/
results = text.scan(pattern)
for result in results
headerSuffix = result.gsub("UIImage imageNamed:@\"","")
finalImageName = headerSuffix.gsub("\"","");
imageResults.push(finalImageName)
end
end
# xibImageResults = Array.new()
Dir.glob(projectDir + "/**/**/**/**.{xib}").each do |xibname|
next if Dir.exists? xibname
text = File.read(xibname)
xibpattern = /image="\S+\"/
xibresults = text.scan(xibpattern)
for xibresult in xibresults
xibprefix = xibresult.gsub("image=\"","")
xibImageName = xibprefix.gsub("\"","");
imageResults.push(xibImageName)
end
end
uniqImages = imageResults.uniq()
copyImages(uniqImages)
end
#通过图片名遍历主工程iamgeAssets
def copyImages(needCopyImageNames)
mainProjectDir = "对应imageAssets目录"
Dir.glob(mainProjectDir + "/**/**.{png}").each do |assetName|
next if Dir.exists? assetName
dirArr = assetName.split("/")
dirCount = dirArr.count()
assetFolderName = dirArr[dirCount - 2]
# puts("asset名为" + assetFolderName)
needCopyResources = Array.new()
for imageName in needCopyImageNames
if assetFolderName.eql? (imageName + ".imageset")
currentFolderName = File.expand_path("..", assetName)
needCopyResources.push(currentFolderName)
end
end
#因为2倍图和3倍图都会被遍历到,所以需要做去重处理
finallyResouces = needCopyResources.uniq()
destFolderName = Dir.getwd + "/Images.xcassets/"
for resouceFolder in finallyResouces
puts("执行copy asset :" + resouceFolder + " => " + destFolderName)
FileUtils.cp_r(resouceFolder,destFolderName)
end
end
end
findUsedImages