隨著項目版本不斷的迭代開發(fā),項目中的圖片資源文件也不斷的替換拖刃,如果沒有養(yǎng)成一個好的使用習(xí)慣叛甫,時間久了饭望,就會容易產(chǎn)生很多無用圖片資源讨彼,如果手動去查找刪除那么效率會非常低冰寻,于是寫了一個python腳本來自動查找刪除這些沒用的圖片資源撰糠。文本搜索使用的是the silver searcher(ag)
验毡,因為使用grep
搜索代碼用戶體驗實在太差了升熊。在日常操作中虚青,我們也可以用ag
取代grep
菱涤。
使用方式很簡單,下面來介紹 the silver searcher
的安裝方法和python腳本的使用方法如迟。腳本目前只能清除.xcassets
文件中未被使用的圖片
一攻走、安裝 the silver searcher
這里只介紹MacOS安裝方式:
brew install the_silver_searcher
安裝成功之后,系統(tǒng)會新增一個ag
指令來使用the silver searcher
玲销,使用方式:ag [FILE-TYPE] [OPTIONS] PATTERN [PATH]
摘符,例如:
mac:Desktop mac$ ag "record_empty" /Users/mac/Desktop/Demo
/Users/mac/Desktop/Demo/Demo/ViewController.swift
15: let image = UIImage(named: "record_empty")
mac:Desktop mac$
二策吠、查找 .xcassets 文件夾腳本
find_xcassets()
函數(shù)作用是查找路徑root
下的所有.xcassets
文件夾猴抹,代碼如下:
import os
import re
root_path = "/Users/mac/Desktop/Code/beijingbus_ios/BJBus"
xcasset_paths = []
def find_xcassets(root):
items = os.listdir(root)
for item in items:
path = os.path.join(root, item)
if is_xcassets_dir(item):
xcasset_paths.append(path)
print '[+] %s' % path
elif os.path.isdir(path):
find_xcassets(path)
return xcasset_paths
def is_xcassets_dir(dir):
return re.search(r'.xcassets', dir)
if __name__ == '__main__':
find_xcassets(root_path)
運行腳本會找出出項目中所有的.xcassets
文件草讶,例如
mac:Desktop mac$ python find_xcassets.py
[+] /Users/mac/Desktop/Code/beijingbus_ios/BJBus/BJBus/Business/Login/resources/Image.xcassets
[+] /Users/mac/Desktop/Code/beijingbus_ios/BJBus/BJBus/Business/BusLines/resources/Image.xcassets
[+] /Users/mac/Desktop/Code/beijingbus_ios/BJBus/BJBus/Business/Trip/resources/Image.xcassets
[+] /Users/mac/Desktop/Code/beijingbus_ios/BJBus/BJBus/Business/Order/resources/Image.xcassets
[+] /Users/mac/Desktop/Code/beijingbus_ios/BJBus/BJBus/Business/Me/resources/Image.xcassets
[+] /Users/mac/Desktop/Code/beijingbus_ios/BJBus/BJBus/Business/Message/resources/Image.xcassets
[+] /Users/mac/Desktop/Code/beijingbus_ios/BJBus/BJBus/Business/ETABus/resources/Image.xcassets
[+] /Users/mac/Desktop/Code/beijingbus_ios/BJBus/BJBus/Business/QRCode/resources/Image.xcassets
mac:Desktop mac$
三堕战、查找 .xcassets 中未使用的圖片資源腳本
remove_unused_images.py
文件中引用了find_xcassets.py
文件中的find_xcassets()
函數(shù),代碼如下:
import glob
import os
import re
from find_xcassets import *
# 搜索目標(biāo)路徑
path = '/Users/mac/Desktop/Code/beijingbus_ios/BJBus'
# 忽略的圖片名稱正則數(shù)組
ignores = {r'guide_\d+'}
def find_unused_images_at_path(root):
xcassets = find_xcassets(root)
unused_images = []
for xcasset in xcassets:
images = glob.glob(xcasset + '/*/*.imageset')
unused_images += find_unused_images(images)
text_path = 'unused_images.txt'
text = '\n'.join(sorted(unused_images))
os.system('echo "%s" > %s' % (text, text_path))
print 'unused images:%d' % (len(unused_images))
print 'Done!'
def find_unused_images(images):
image_names = [os.path.basename(image)[:-len(".imageset")] for image in images]
unused_images = []
for i in range(0, len(images)):
image_name = image_names[i]
if is_ignore(image_name):
continue
command = 'ag "%s" %s' % (image_name, path)
result = os.popen(command).read()
if result == '':
unused_images.append(images[i])
print 'remove %s' % (images[i])
os.system('rm -rf %s' % (images[i]))
return unused_images
def is_ignore(image_name):
for ignore in ignores:
if re.match(ignore, image_name):
return True
return False
if __name__ == '__main__':
find_unused_images_at_path(path)
四、腳本使用方式:
1汁政、修改remove_unused_images.py
文件中的搜索目標(biāo)路徑path
為你的工程路徑缀旁;
2、在文件remove_unused_images.py
中設(shè)置要忽略的圖片目木,修改數(shù)組ignores
懊渡,元素可以是正則表達(dá)式字符串;
3誓禁、在終端運行腳本:
mac:Desktop mac$ python remove_unused_images.py
remove /Users/mac/Desktop/Code/beijingbus_ios/BJBus/BJBus/Business/BusLines/resources/Image.xcassets/BusLines/buslines_search.imageset
remove /Users/mac/Desktop/Code/beijingbus_ios/BJBus/BJBus/Business/BusLines/resources/Image.xcassets/BusLines/bus_lines_direction.imageset
remove /Users/mac/Desktop/Code/beijingbus_ios/BJBus/BJBus/Business/BusLines/resources/Image.xcassets/BusLines/bus_lines_share.imageset
remove /Users/mac/Desktop/Code/beijingbus_ios/BJBus/BJBus/Business/Order/resources/Image.xcassets/BusOrder/bus_order_price.imageset
remove /Users/mac/Desktop/Code/beijingbus_ios/BJBus/BJBus/Business/Order/resources/Image.xcassets/BusOrder/bus_order_score.imageset
remove /Users/mac/Desktop/Code/beijingbus_ios/BJBus/BJBus/Business/Order/resources/Image.xcassets/SubwayOrder/subway_order_time.imageset
unused images:6
Done!
mac:Desktop mac$
4摹恰、最后會在當(dāng)前路徑下生成一個日志文件unused_images.txt
怒见,內(nèi)容為移除的圖片路徑。
代碼地址:https://github.com/wlFlyingFish/scripts/tree/main/fund_unused_images