auto_pack.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #!/usr/bin/python3
  2. # -*- coding:utf-8 -*-
  3. # __author__ = '__spring__'
  4. '''
  5. 归因上报抖音数据的脚本
  6. '''
  7. import json
  8. import os
  9. class Handle:
  10. def __init__(self):
  11. pass
  12. """
  13. 将替换的字符串写到一个新的文件中,然后将原文件删除,新文件改为原来文件的名字
  14. :param file: 文件路径
  15. :param old_str: 需要替换的字符串
  16. :param new_str: 替换的字符串
  17. :return: None
  18. """
  19. def updateFile(self,file,old_str,new_str):
  20. with open(file, "r", encoding="utf-8") as f1,open("%s.bak" % file, "w", encoding="utf-8") as f2:
  21. for line in f1:
  22. if old_str in line:
  23. line = line.replace(old_str, new_str)
  24. f2.write(line)
  25. os.remove(file)
  26. os.rename("%s.bak" % file, file)
  27. def main(self):
  28. # 读取需要打包的app 列表
  29. with open('./app_list.json','r') as f :
  30. data = f.read()
  31. data = json.loads(data)
  32. app_list = data["app_list"]
  33. tar_ver = data["taroConfig"]
  34. #读取版本号
  35. with open('./package.json','r') as f :
  36. package = f.read()
  37. package = json.loads(package)
  38. #更新版本号
  39. with open('./package.json','w') as f :
  40. package["taroConfig"] = tar_ver
  41. json.dump(package,f,indent=4,ensure_ascii=False)
  42. #遍历需要打包的应用
  43. for app_info in app_list:
  44. path_name = app_info["path_name"]
  45. #读取配置文件
  46. with open(path_name,'r') as f:
  47. pro_data = f.read()
  48. pro_data = json.loads(pro_data)
  49. #修改配置文件
  50. with open(path_name,'w') as f:
  51. pro_data["appid"] = app_info["appid"]
  52. pro_data["projectname"] = app_info["projectname"]
  53. pro_data["description"] = app_info["description"]
  54. json.dump(pro_data,f,indent=4,ensure_ascii=False)
  55. #修改抖音自定义导航栏的样式
  56. # self.updateFile("./src/pages/albums_play/index.config.js","custom","default")
  57. if app_info["appid"].find("wx") > -1:
  58. #修改打包插件的appid
  59. self.updateFile("./config/index.js","__APPID__",app_info["appid"])
  60. #修改抖音自定义导航栏的样式
  61. # self.updateFile("./src/pages/albums_play/index.config.js","default","custom")
  62. #自己添加打包插件
  63. self.updateFile("./config/index.js","plugins:[]",'plugins:[["@tarojs/plugin-mini-ci", CIPluginOpt]]')
  64. #开始运行打包命令
  65. cmd = "npm run build:weapp_upload"
  66. if path_name.find("tt") > 0:
  67. cmd = "npm run build:tt_upload"
  68. os.system(cmd) #执行命令
  69. if app_info["appid"].find("wx") > -1:
  70. #修改打包插件的appid
  71. self.updateFile("./config/index.js",app_info["appid"],"__APPID__")
  72. self.updateFile("./config/index.js",'plugins:[["@tarojs/plugin-mini-ci", CIPluginOpt]]',"plugins:[]")
  73. pass
  74. if __name__=='__main__':
  75. Handle().main()