import package_utils
import file_utils
import xml_utils
import config_utils
import os
import os.path
import json

def packTest():
    '''
    测试代码
    '''
    game = 'sample'
    sdk = 'float'
    subChannel = 'test'

    channelPath = file_utils.getChannelPath(game, sdk)
    configPath = os.path.join(channelPath, 'config.json')
    jsonText = file_utils.readFile(configPath)
    config = json.loads(jsonText)
    config_utils.replaceArgs(config)

    config = getItemConfig(config, subChannel)
    if config is None:
        return 1

    package_utils.decomplie(game, sdk, subChannel, config)
    package_utils.removeNoSupportAttr(game, sdk, subChannel, config)
    package_utils.mergeDrawableRes(game, sdk, subChannel, config)
    package_utils.removeSameRes(game, sdk, subChannel, config)
    package_utils.copyRes(game, sdk, subChannel, config)
    package_utils.mergeManifestRes(game, sdk, subChannel, config)
    package_utils.changePlaceholders(game, sdk, subChannel, config)
    package_utils.addMetaData(game, sdk, subChannel, config)
    package_utils.copyAppRes(game, sdk, subChannel, config)
    package_utils.changePackageName(game, sdk, subChannel, config)
    package_utils.changeAppName(game, sdk, subChannel, config)
    package_utils.changeAppIcon(game, sdk, subChannel, config)
    package_utils.addLauncher(game, sdk, subChannel, config)
    package_utils.packJar(game, sdk, subChannel, config)
    package_utils.doSDKPostScript(game, sdk, config)
    package_utils.doGamePostScript(game, sdk, config)
    package_utils.generateNewRFile(game, sdk, subChannel, config)
    package_utils.splitDex(game, sdk, subChannel, config)
    package_utils.recomplie(game, sdk, subChannel, config)
    package_utils.alignApk(game, sdk, subChannel, config)
    package_utils.apksignerApk(game, sdk, subChannel, config)
    package_utils.clearTemp(game, sdk, subChannel, config)

def getCmd(cmd):
    p = os.popen(cmd)
    content = p.read()
    #print(content)
    return content

def getItemConfig(config, subChannel):
    if type(config) == dict:
        if subChannel is None or config['subChannel'] == subChannel:
            return config
    elif type(config) == list:
        for itemConfig in config:
            if subChannel is None or itemConfig['subChannel'] == subChannel:
                return subChannel
    return None

packTest()