package_web_yfsdk.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. import file_utils
  2. import package_utils_yfsdk
  3. import os.path
  4. import sys
  5. import json
  6. import importlib
  7. def packageWeb():
  8. if len(sys.argv) < 2:
  9. print('argument is missing')
  10. return 1
  11. # 打包配置的路径
  12. packageConfig = sys.argv[1]
  13. jsonText = file_utils.readFile(packageConfig)
  14. config = json.loads(jsonText)
  15. print('*************config*****************')
  16. print(jsonText)
  17. print('************************************')
  18. package(config, config['sdk'])
  19. def package(config, sdk):
  20. print('use script yfsdk')
  21. jsonConfig = {}
  22. game = config['app']
  23. subChannel = None
  24. if 'subChannel' in config:
  25. subChannel = config['subChannel']
  26. jsonConfig['subChannel'] = config['subChannel']
  27. # 必须参数
  28. jsonConfig['packageName'] = config['packageName']
  29. jsonConfig['name'] = config['name']
  30. # 可选参数
  31. if 'outName' in config:
  32. jsonConfig['outName'] = config['outName']
  33. if 'outPath' in config:
  34. jsonConfig['outPath'] = config['outPath']
  35. if 'changeIcon' in config:
  36. jsonConfig['changeIcon'] = toBoolean(config['changeIcon'])
  37. if 'addLauncher' in config:
  38. jsonConfig['addLauncher'] = toBoolean(config['addLauncher'])
  39. if 'versionCode' in config:
  40. jsonConfig['versionCode'] = config['versionCode']
  41. if 'versionName' in config:
  42. jsonConfig['versionName'] = config['versionName']
  43. if 'targetSdkVersion' in config:
  44. jsonConfig['targetSdkVersion'] = config['targetSdkVersion']
  45. if 'v2disable' in config:
  46. jsonConfig['v2disable'] = toBoolean(config['v2disable'])
  47. if 'aapt2disable' in config:
  48. jsonConfig['aapt2disable'] = toBoolean(config['aapt2disable'])
  49. # 生成配置文件
  50. createOrUpdateConfigFile(game, sdk, jsonConfig)
  51. # 拷贝资源
  52. copyRes(game, sdk, subChannel, config)
  53. # 打包
  54. package_utils_yfsdk.packConsole(game, sdk, subChannel)
  55. def toBoolean(booleanStr):
  56. if type(booleanStr) == bool:
  57. return booleanStr
  58. if booleanStr == 'true':
  59. return True
  60. return False
  61. def createOrUpdateConfigFile(game, sdk, jsonConfig):
  62. '''
  63. 更新配置文件
  64. '''
  65. if 'subChannel' not in jsonConfig:
  66. return 0
  67. print('createOrUpdateConfigFile ...')
  68. channelPath = file_utils.getChannelPath(game, sdk)
  69. configPath = os.path.join(channelPath, 'config.json')
  70. # configPath = os.path.join(file_utils.getCurrentPath(), 'test', 'test.json')
  71. if os.path.exists(configPath):
  72. # 更新数据
  73. jsonText = file_utils.readFile(configPath)
  74. config = json.loads(jsonText)
  75. count = 0
  76. if type(config) == list:
  77. for item in config:
  78. if item['subChannel'] == jsonConfig['subChannel']:
  79. print('find same config ...')
  80. del config[count]
  81. break
  82. count += 1
  83. config.append(jsonConfig)
  84. createConfigFile(config, configPath)
  85. elif type(config) == dict:
  86. if config['subChannel'] == jsonConfig['subChannel']:
  87. print('find same config ...')
  88. createConfigFile(jsonConfig, configPath)
  89. else:
  90. print('add a new config ...')
  91. config = [config, jsonConfig]
  92. createConfigFile(config, configPath)
  93. else:
  94. print('create a new config ...')
  95. createConfigFile([jsonConfig], configPath)
  96. def createConfigFile(jsonConfig, configPath):
  97. '''
  98. 创建配置文件
  99. '''
  100. jsonStr = json.dumps(jsonConfig, ensure_ascii=False)
  101. print('*************out config*************')
  102. print(jsonStr)
  103. print('************************************')
  104. file_utils.createFile(configPath, jsonStr)
  105. def copyRes(game, sdk, subChannel, config):
  106. '''
  107. 拷贝资源
  108. '''
  109. if subChannel is None:
  110. return 0
  111. channelPath = file_utils.getChannelPath(game, sdk)
  112. subChannelPath = os.path.join(channelPath, subChannel)
  113. if 'icon' in config and os.path.exists(config['icon']):
  114. mipmapSupport = ['mipmap-xhdpi', 'mipmap-xxhdpi', 'mipmap-xxxhdpi']
  115. for mipmap in mipmapSupport:
  116. iconPath = os.path.join(subChannelPath, 'icon', mipmap, 'common_sdk_icon.png')
  117. file_utils.copyFile(config['icon'], iconPath)
  118. '''if 'switchIcon' in config and os.path.exists(config['switchIcon']):
  119. mipmapSupport = ['mipmap-xhdpi', 'mipmap-xxhdpi', 'mipmap-xxxhdpi']
  120. for mipmap in mipmapSupport:
  121. iconPath = os.path.join(subChannelPath, 'icon', mipmap, 'common_sdk_icon2.png')
  122. file_utils.copyFile(config['switchIcon'], iconPath)'''
  123. if 'splash' in config and os.path.exists(config['splash']):
  124. splashPath = os.path.join(subChannelPath, 'splash', 'drawable-hdpi', 'common_sdk_launcher_bg.jpg')
  125. file_utils.copyFile(config['splash'], splashPath)
  126. if 'copyList' in config:
  127. for item in config['copyList']:
  128. if item['toFile'] == '':
  129. continue
  130. if not os.path.exists(item['fromFile']):
  131. continue
  132. toFile = item['toFile']
  133. if toFile[:3] == 'res':
  134. toFile = 'image' + toFile[3:]
  135. resPath = getPackagePath(subChannelPath, toFile)
  136. file_utils.copyFile(item['fromFile'], resPath)
  137. if 'package' in config and os.path.exists(config['package']):
  138. newGameApk = config['package']
  139. gameApk = file_utils.getFullGameApk(game)
  140. file_utils.copyFile(newGameApk, gameApk)
  141. def getPackagePath(basePath, packageName):
  142. '''
  143. 包名对应的目录
  144. '''
  145. packageNameSplit = packageName.replace('\\', '/').split('/')
  146. newPath = basePath
  147. for item in packageNameSplit:
  148. newPath = os.path.join(newPath, item)
  149. return newPath
  150. #packageWeb()