Mac 自动化打包脚本

总目录:Mac + Apache + PHP+打包脚本 = Mac自动化打包

作为ios开发员,打包是家常便饭啦.....

所以,把复杂的流程简单化,有助于减轻自己的工作量,还能有效的防止问题发生...最重要的,没那么快秃顶!

打包共几个步骤

1.编译unity生成xcode工程

2.导入生成的文件到打包工程

3.更改打包工程配置,如版本号

4.编译

5.签名生成包

我们一步一步来

1.编译unity生成Xcode工程

unity里面提供的方法进行打包,只用在脚本里调用unity的类方法就能执行unity里写的打包类

 1 #compileunity工程,导出iOS工程!
 2 defexportIosProject(logfilePath):
 3 print "\n--------------------------------------------------------------- compile unity project....... --------------------------------------------------------------\n"
 4 command ="/Applications/Unity2017.2.3f1/Unity.app/Contents/MacOS/Unity -quit -batchmode -projectPath /Users/admin/projectName -executeMethod ProjectBuilderIOS.BuildForIosProjectIl2Cpp >>%s"%(logfilePath)
 5 printcommand
 6 unityCompile_statue = os.system(command)
 7 unityCompile_statue>>=8
 8 ifunityCompile_statue==0:
 9 print "\n***************************************************************** compile unity project succeed ********************************************************\n"
10 else:
11 print "\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! compile unity project fail !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"
12 exit(0)

2.导入生成的文件到打包工程
找到项目的路径,更改.pbxproj文件

####################################################################################################################################################
#新加的编辑xcodeproj工程文件的代码
pbxproj_dir = '%s/%s/project.pbxproj'%(pro_dir,fileName)
pro_dir_arr = pro_dir.split('/')
arr_len = len(pro_dir_arr)
pro_dir_arr[arr_len-1] = 'Classes/Native/'
Native_dir = '/'.join(pro_dir_arr)
Native_files = file_name(Native_dir)
for file in Native_files:
data = ''
uuid_f = ''.join(str(uuid.uuid4()).upper().split('-')[1:])
uuid_r = ''.join(str(uuid.uuid4()).upper().split('-')[1:])
if (isincludestr(pbxproj_dir, file)):
continue
while(isincludestr(pbxproj_dir, uuid_f)):
uuid_f =''.join(str(uuid.uuid4()).upper().split('-')[1:])
while(isincludestr(pbxproj_dir, uuid_r)):
uuid_r =''.join(str(uuid.uuid4()).upper().split('-')[1:])
buildfilesection_str = creat_buildfilesection(file, uuid_f, uuid_r)
refsection_str = creat_refsection(file, uuid_r)
PBXBuildFile_line = getstrlineinfile(pbxproj_dir, '/* Begin PBXBuildFile section */')
insertline(pbxproj_dir, PBXBuildFile_line ,buildfilesection_str)
PBXFileReference_line = getstrlineinfile(pbxproj_dir, '/* Begin PBXFileReference section */')
insertline(pbxproj_dir, PBXFileReference_line ,refsection_str)
folder_line = getstrlineinfile(pbxproj_dir, '/* Native */ = {')+2
insertline(pbxproj_dir, folder_line ,('%s /* %s */,' %(uuid_r,file)))
PBXSourcesBuildPhase_line = getstrlineinfile(pbxproj_dir, '/* Begin PBXSourcesBuildPhase section */')+4
insertline(pbxproj_dir, PBXSourcesBuildPhase_line ,('%s /* %s in Sources */,' %(uuid_f,file)))
###################################################################################################################################################


3.更改打包工程配置,如版本号
获取对应项目的info.plist文件进行更改版本号

#获取项目的info配置文件
try:
if os.path.exists(pro_dir+"/Info.plist"):
plist=readPlist(pro_dir+"/Info.plist");
else:
#这里需要注意工程目录下是否有该目录
plist=readPlist(pro_dir+"/"+target+"/Info.plist");
except InvalidPlistException,e:
print "not a plist or plist invalid:",e

#修改项目的info配置文件(修改了项目的游戏版本和compile版本号)
try:
plist['CFBundleVersion']=game_version
plist['CFBundleShortVersionString']=build_version
if os.path.exists(pro_dir+"/Info.plist"):
writePlist(plist,pro_dir+"/Info.plist")
else:
writePlist(plist,pro_dir+"/"+target+"/Info.plist")

except (InvalidPlistException, NotBinaryPlistException), e:
print "Something bad happened:", e

 

4.编译
在编译之前需要先清理下xcode工程

def clean(dir,pro_name,logfilePath):
print "\n--------------------------------------------------------------- ios begin clean....... --------------------------------------------------------------- \n"
command = "cd %s; xcodebuild -target %s clean >>%s"% (dir,pro_name,logfilePath)
print command
os.system(command)
iosBuild_status = os.system(command)
iosBuild_status>>=8
if iosBuild_status==0 :
print "\n***************************************************************** ios clean succeed ********************************************************\n"
else:
print "\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ios clean fail!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"

 


编译分为两种:
第一种build,这种打出来的包里面会有调试信息,包体会增大,建议测试包使用,

def build(dir,pro_name,build_config,code_sign,logfilePath):
print "\n--------------------------------------------------------------- ios compile begin --------------------------------------------------------------- \n"
command = "cd %s;xcodebuild -target %s -sdk iphoneos -configuration %s CODE_SIGN_IDENTITY='%s' >> %s" % (dir,pro_name,build_config,code_sign,logfilePath)
print command
iosBuild_status = os.system(command)
iosBuild_status>>=8
if iosBuild_status==0 :
print "\n***************************************************************** ios compile succeed ********************************************************\n"
else:
print "\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ios compile fail !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"
exit(0)

 


第二种是Archive,这种是比较好的打包方式,没有调试信息,发布包建议用这种方式

def buildForArchive(dir,pro_name,logfilePath):
print "\n--------------------------------------------------------------- ios compile begin --------------------------------------------------------------- \n"
ipa_out_put_archive = "%s/build/%s.xcarchive" % (dir,pro_name)
command = "cd %s;xcodebuild archive -project %s.xcodeproj -scheme %s -archivePath %s >> %s" % (dir,pro_name,pro_name,ipa_out_put_archive,logfilePath)
print command
iosBuild_status = os.system(command)
iosBuild_status>>=8
if iosBuild_status==0 :
print "\n***************************************************************** ios compile succeed ********************************************************\n"
else:
print "\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ios compile fail !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"
exit(0)

 


5.签名生成包
和编译对应的,生成包的方式也有两种,
第一种build,对build出来的app签名生成ipa

def export(current_time,current_section,dir,build_config,pro_name,version,code_sign,code_profile,save_dir,logfilePath):
#设置ipa包的包名和存储位置
ipa_out_put_name = "%s-%s-%s-%s-%s"%(current_time,version,pro_name,current_section,build_config)
ipa_out_put = os.path.join(sys.path[0],"pack/%s.ipa"%(ipa_out_put_name))
if not os.path.exists(os.path.join(sys.path[0],"pack")):
os.makedirs(os.path.join(sys.path[0],"pack"))
print "pack 文件夹不存在 新建一个"
_appPath = "%s/build/%s" % (dir,build_config)
_appName = getFileFromDir(_appPath,"app")
print "\n--------------------------------------------------------------- ios pack begin --------------------------------------------------------------- \n"
command = "cd %s;xcrun -sdk iphoneos PackageApplication -v %s/build/%s/%s.app -o '%s' —sign '%s' —embed %s >> %s" % (dir,dir,build_config,_appName,ipa_out_put,code_sign,code_profile,logfilePath)
print command
os.system(command)
iosBuild_status = os.system(command)
iosBuild_status>>=8
if iosBuild_status==0 :
print "\n***************************************************************** ios pack succeed ********************************************************\n"
else:
print "\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ios pack fail !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"
exit(0)
```
第二种Archive,对Archive出来的.xcarchive文件签名生成ipa
```
def exportForArchive(current_time,current_section,dir,build_config,pro_name,version,save_dir,logfilePath):
#设置ipa包的包名和存储位置
ipa_out_put_name = "%s-%s-%s-%s-%s"%(current_time,version,pro_name,current_section,build_config)
ipa_out_put = os.path.join(sys.path[0],"pack/%s"%(ipa_out_put_name))
if not os.path.exists(os.path.join(sys.path[0],"pack")):
os.makedirs(os.path.join(sys.path[0],"pack"))
print "pack 文件夹不存在 新建一个"

#获取项目的打包配置文件
pakegePlistPath = "%s/release.plist" % (dir)
#开始打包
print "\n--------------------------------------------------------------- ios pack begin--------------------------------------------------------------- \n"
command = "cd %s;xcodebuild -exportArchive -archivePath %s/build/%s.xcarchive -exportPath %s -exportOptionsPlist %s >> %s" % (dir,dir,pro_name,ipa_out_put,pakegePlistPath,logfilePath)
print command
iosBuild_status = os.system(command)
iosBuild_status>>=8
if iosBuild_status==0 :
print "\n***************************************************************** ios pack succeed ********************************************************\n"
else:
print "\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ios pack fail !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n"
exit(0)
#将生成的IPA重命名
copyfile(ipa_out_put+"/"+pro_name+".ipa",ipa_out_put+".ipa")
deletetree(ipa_out_put);

 以上最主要的几步都已列出,按照自己的需要使用

下面贴上我使用的脚本,其中有些数据如"os.environ["BASKETBALL_PRO_DIR"]"这种,是在系统设置的环境变量,是路径字符串
顺带贴上设置环境变量的方法

IOS : 环境变量 BASKETBALL_PRO_DIR (项目根目录)如 : /444/111/www
配置指引:在终端 输入命令 vim .bash_profile, vim打开 bash_profile 
按键盘 ‘i’键进入编辑模式
在编辑模式下,配置项目跟目录 export BASKETBALL_PRO_DIR=“pro_dir” 如:export BASKETBALL_PRO_DIR= /444/111/www
确认路径正确之后,按esc 进入vim 的命令模式输入 ":wq" 保存文件并退出vim
输入命令 source .bash_profile 立即生效环境变量
验证环境变量:在终端 输入 echo $BASKETBALL_PRO_DIR 环境变量设置正确的情况下 终端将会输出您所设置的篮球项目根目录
在终端 输入 echo $BASKETBALL_GAMERENDER_DIR 环境变量设置正确的情况下 终端将会输出您所设置的gamerender 的根目录路径

 

 

 

点个赞再走呗。。。

如有疑问,联系作者

博客园:这个我不知道诶


 
 
posted @ 2020-03-10 10:46  这个我知道  阅读(927)  评论(0编辑  收藏  举报