Created
July 10, 2018 05:23
-
-
Save originme/43ea59a9f7dd3d5fc21461e840068f69 to your computer and use it in GitHub Desktop.
把主工程的 Image.xcassets分到不同的子工程bundle里面,并替换图片初始化方法
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import re | |
import shutil | |
class Bundle: | |
def __init__(self, name, assetsTarget, bundlePath): | |
self.name = name | |
self.bundlePath = bundlePath | |
self.assetsTarget = assetsTarget | |
assetsPath = '/Users/liaochaolong054/SMT-iOS/FreeCitizen/Resource/Assets.xcassets' | |
assetsDict = dict() #存着所有的图片路径 .assets结尾的文件夹,key为资源名 | |
#递归遍历所有文件的方法 | |
for dirpath, dirnames, filenames, in os.walk(assetsPath): | |
for dirName in dirnames: | |
if dirName.endswith('imageset'): | |
filepath = dirpath + '/' + dirName | |
name = dirName[:-9] | |
assetsDict[name] = filepath | |
# 定义bundle | |
projectPath = '/Users/liaochaolong054/SMT-iOS/' | |
modules = ['Common', 'Home', 'Live', 'Main', 'Mine', 'Affrairs', 'Account'] | |
bundles = [] | |
for m in modules: | |
path = 'SMT' + m | |
bundle = Bundle('PA'+m+'Bundle',projectPath+'Modules/'+path+'/CommonBundle/Resource/Image.xcassets', projectPath+'Modules/'+path) | |
bundles.append(bundle) | |
# 搜索Bundle中的.m文件 使用imageNamed GetImage方法设置的图、xib文件设置的图片名 | |
def addBundleImport(s, bundle): | |
matchStr =r'#import "%s.h"' % bundle.name | |
try: | |
s.index(matchStr) | |
except ValueError as e: | |
# 找不到,则插入 | |
im = '\n#import "' | |
s.index(im) | |
s = s.replace(im, matchStr+im, 1) | |
return s | |
def handleMFile(path, bundle): | |
# 处理 .m 文件,把这些 'imageNamed:', 'GetImage'方法的全部替换掉 | |
p1 = re.compile(r'\[UIImage imageNamed:@".+"\]') | |
p2 = re.compile('GetImage\(@".+"\)') | |
with open(path, 'r') as f: | |
s = f.read() | |
f.close() | |
list1 = p1.findall(s) | |
for matchStr in list1: | |
name = matchStr[22:-2] #获取imageNamed里面的内容 | |
moveAssets(name, bundle) #移动资源 | |
s = s.replace(matchStr, '[%s imageNamed:@"%s"]' % (bundle.name, name)) | |
s = addBundleImport(s, bundle) | |
list2 = p2.findall(s) | |
for matchStr in list2: | |
name = matchStr[11:-2] #获取GetImage里面的内容 | |
print('处理资源%s bundle:%s' % (name, bundle.name)) | |
moveAssets(name, bundle) #移动资源 | |
s = s.replace(matchStr, '[%s imageNamed:@"%s"]' % (bundle.name, name)) | |
s = addBundleImport(s, bundle) | |
with open(path, 'w') as f2: | |
f2.write(s) | |
f2.close() | |
def handleXibFile(path, bundle): | |
p = re.compile(r'image=".+"') | |
with open(path, 'r') as f: | |
s = f.read() | |
list1 = p.findall(s) | |
for matchStr in list1: | |
name = matchStr[10:-2] | |
moveAssets(name, bundle) | |
f.close() | |
# 移动资源文件 | |
def moveAssets(assetName, bundle): | |
try: | |
assetPath = assetsDict[assetName] | |
relativepath = assetPath.replace(assetsPath, '') | |
shutil.copytree(assetPath, bundle.assetsTarget + '/' + relativepath) | |
print('移动资源 %s' % assetName) | |
except Exception as e: | |
print(e) | |
def handleBundle(bundle): | |
for dirpath, dirnames, filenames in os.walk(bundle.bundlePath): | |
for name in filenames: | |
if name.endswith('.m'): | |
mFilePath = dirpath + '/' + name | |
handleMFile(mFilePath, bundle) | |
elif name.endswith('.xib'): | |
xibFilePath = dirpath + '/' + name | |
handleXibFile(xibFilePath, bundle) | |
else: | |
pass | |
for b in bundles: | |
print('---------------------------') | |
print('开始处理bundle: %s' % bundle.name) | |
handleBundle(b) | |
print('处理bundle: %s 完毕' % bundle.name) | |
print('---------------------------') | |
print('结束') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment