package_web_yfsdk.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. # sdk相关参数
  50. if 'properties' in config:
  51. jsonConfig['properties'] = config['properties']
  52. # 生成配置文件
  53. createOrUpdateConfigFile(game, sdk, jsonConfig)
  54. # 拷贝资源
  55. copyRes(game, sdk, subChannel, config)
  56. # 打包
  57. package_utils_yfsdk.packConsole(game, sdk, subChannel)
  58. def toBoolean(booleanStr):
  59. if type(booleanStr) == bool:
  60. return booleanStr
  61. if booleanStr == 'true':
  62. return True
  63. return False
  64. def createOrUpdateConfigFile(game, sdk, jsonConfig):
  65. '''
  66. 更新配置文件
  67. '''
  68. if 'subChannel' not in jsonConfig:
  69. return 0
  70. print('createOrUpdateConfigFile ...')
  71. channelPath = file_utils.getChannelPath(game, sdk)
  72. configPath = os.path.join(channelPath, 'config.json')
  73. # configPath = os.path.join(file_utils.getCurrentPath(), 'test', 'test.json')
  74. if os.path.exists(configPath):
  75. # 更新数据
  76. jsonText = file_utils.readFile(configPath)
  77. config = json.loads(jsonText)
  78. count = 0
  79. if type(config) == list:
  80. for item in config:
  81. if item['subChannel'] == jsonConfig['subChannel']:
  82. print('find same config ...')
  83. del config[count]
  84. break
  85. count += 1
  86. config.append(jsonConfig)
  87. createConfigFile(config, configPath)
  88. elif type(config) == dict:
  89. if config['subChannel'] == jsonConfig['subChannel']:
  90. print('find same config ...')
  91. createConfigFile(jsonConfig, configPath)
  92. else:
  93. print('add a new config ...')
  94. config = [config, jsonConfig]
  95. createConfigFile(config, configPath)
  96. else:
  97. print('create a new config ...')
  98. createConfigFile([jsonConfig], configPath)
  99. def createConfigFile(jsonConfig, configPath):
  100. '''
  101. 创建配置文件
  102. '''
  103. jsonStr = json.dumps(jsonConfig, ensure_ascii=False)
  104. print('*************out config*************')
  105. print(jsonStr)
  106. print('************************************')
  107. file_utils.createFile(configPath, jsonStr)
  108. def copyRes(game, sdk, subChannel, config):
  109. '''
  110. 拷贝资源
  111. '''
  112. if subChannel is None:
  113. return 0
  114. channelPath = file_utils.getChannelPath(game, sdk)
  115. subChannelPath = os.path.join(channelPath, subChannel)
  116. if 'icon' in config and os.path.exists(config['icon']):
  117. mipmapSupport = ['mipmap-xhdpi', 'mipmap-xxhdpi', 'mipmap-xxxhdpi']
  118. for mipmap in mipmapSupport:
  119. iconPath = os.path.join(subChannelPath, 'icon', mipmap, 'common_sdk_icon.png')
  120. print('yfsdk copyRes from--> ' + config['icon'])
  121. print('yfsdk copyRes to--> ' + iconPath)
  122. file_utils.copyFile(config['icon'], iconPath)
  123. '''if 'switchIcon' in config and os.path.exists(config['switchIcon']):
  124. mipmapSupport = ['mipmap-xhdpi', 'mipmap-xxhdpi', 'mipmap-xxxhdpi']
  125. for mipmap in mipmapSupport:
  126. iconPath = os.path.join(subChannelPath, 'icon', mipmap, 'common_sdk_icon2.png')
  127. file_utils.copyFile(config['switchIcon'], iconPath)'''
  128. if 'splash' in config and os.path.exists(config['splash']):
  129. splashPath = os.path.join(subChannelPath, 'splash', 'drawable-hdpi', 'common_sdk_launcher_bg.jpg')
  130. file_utils.copyFile(config['splash'], splashPath)
  131. if 'copyList' in config:
  132. for item in config['copyList']:
  133. if item['toFile'] == '':
  134. continue
  135. if not os.path.exists(item['fromFile']):
  136. continue
  137. toFile = item['toFile']
  138. if toFile[:3] == 'res':
  139. toFile = 'image' + toFile[3:]
  140. resPath = getPackagePath(subChannelPath, toFile)
  141. file_utils.copyFile(item['fromFile'], resPath)
  142. if 'package' in config and os.path.exists(config['package']):
  143. newGameApk = config['package']
  144. gameApk = file_utils.getFullGameApk(game)
  145. file_utils.copyFile(newGameApk, gameApk)
  146. def getPackagePath(basePath, packageName):
  147. '''
  148. 包名对应的目录
  149. '''
  150. packageNameSplit = packageName.replace('\\', '/').split('/')
  151. newPath = basePath
  152. for item in packageNameSplit:
  153. newPath = os.path.join(newPath, item)
  154. return newPath
  155. #packageWeb()