# All files / src/platformTools/apple plistParser.js

21.01% Statements 25/119
0% Branches 0/46
0% Functions 0/12
19.82% Lines 22/111

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    1982x
    2x
    2x
    2x
    2x

    2x
    2x
    2x
    2x

    2x

    2x

    2x

    2x

    2x

    2x

    2x

    2x

    2x

    2x

    2x

    2x

    2x

     import path from 'path';
    import fs from 'fs';
    import chalk from 'chalk';
    import { isObject, isArray, isBool, isString } from '../../systemTools/objectUtils';
    import {
        logTask,
        logError,
        logWarning,
        getAppFolder,
        isPlatformActive,
        logDebug,
        getAppVersion,
        getAppTitle,
        getAppVersionCode,
        getEntryFile,
        writeCleanFile,
        getAppTemplateFolder,
        getAppId,
        getConfigProp,
        getIP,
        getBuildFilePath,
        logSuccess,
        getBuildsFolder
    } from '../../common';
    import { copyBuildsFolder } from '../../projectTools/projectParser';
    import { getMergedPlugin, parsePlugins } from '../../pluginTools';
    import { getAppFolderName } from './index';
    import { copyFolderContentsRecursiveSync, copyFileSync, mkdirSync, readObjectSync, mergeObjects } from '../../systemTools/fileutils';

    export const parseExportOptionsPlist = (c, platform) => new Promise((resolve, reject) => {
    // EXPORT OPTIONS
        const tId = getConfigProp(c, platform, 'teamID');
        const appFolder = getAppFolder(c, platform);
        const exportOptions = getConfigProp(c, platform, 'exportOptions') || {};
        const id = getConfigProp(c, platform, 'id');

        c.pluginConfigiOS.exportOptions = objToPlist(exportOptions);

        if (exportOptions.provisioningProfiles) {
            const expProvProfile = exportOptions.provisioningProfiles[id];
            if (!expProvProfile) {
                logError(`Your exportOptions.provisionProfiles object in ${c.paths.appConfig.config} does not include id ${id}!`);
            }
        }

        const bPath = getBuildFilePath(c, platform, 'exportOptions.plist');
        writeCleanFile(bPath, path.join(appFolder, 'exportOptions.plist'), [
            { pattern: '{{TEAM_ID}}', override: tId },
            { pattern: '{{PLUGIN_EXPORT_OPTIONS}}', override: c.pluginConfigiOS.exportOptions },
        ]);
        resolve();
    });

    export const parseEntitlementsPlist = (c, platform) => new Promise((resolve, reject) => {
        logTask(`parseEntitlementsPlistSync:${platform}`);

        const appFolder = getAppFolder(c, platform);
        const appFolderName = getAppFolderName(c, platform);
        const entitlementsPath = path.join(appFolder, `${appFolderName}/${appFolderName}.entitlements`);
        // PLUGIN ENTITLEMENTS
        let pluginsEntitlementsObj = getConfigProp(c, platform, 'entitlements');
        if (!pluginsEntitlementsObj) {
            pluginsEntitlementsObj = readObjectSync(path.join(c.paths.rnv.dir, 'src/platformTools/apple/supportFiles/entitlements.json'));
        }

        saveObjToPlistSync(entitlementsPath, pluginsEntitlementsObj);
        resolve();
    });

    export const parseInfoPlist = (c, platform) => new Promise((resolve, reject) => {
        logTask(`parseInfoPlist:${platform}`);

        const appFolder = getAppFolder(c, platform);
        const appFolderName = getAppFolderName(c, platform);
        const plat = c.buildConfig.platforms[platform];
        const { orientationSupport, urlScheme } = plat;
        const plistPath = path.join(appFolder, `${appFolderName}/Info.plist`);

        // PLIST
        let plistObj = readObjectSync(path.join(c.paths.rnv.dir, `src/platformTools/apple/supportFiles/info.plist.${platform}.json`));
        plistObj.CFBundleDisplayName = getAppTitle(c, platform);
        plistObj.CFBundleShortVersionString = getAppVersion(c, platform);
        plistObj.CFBundleVersion = getAppVersionCode(c, platform);
        // FONTS
        if (c.pluginConfigiOS.embeddedFonts.length) {
            plistObj.UIAppFonts = c.pluginConfigiOS.embeddedFonts;
        }
        // PERMISSIONS
        const pluginPermissions = '';
        const includedPermissions = getConfigProp(c, platform, 'includedPermissions') || getConfigProp(c, platform, 'permissions');
        if (includedPermissions) {
            const plat = c.buildConfig.permissions[platform] ? platform : 'ios';
            const pc = c.buildConfig.permissions[plat];
            if (includedPermissions.length && includedPermissions[0] === '*') {
                for (const v in pc) {
                    const key = pc[v].key || v;
                    plistObj[key] = pc[v].desc;
                }
            } else {
                includedPermissions.forEach((v) => {
                    if (pc[v]) {
                        const key = pc[v].key || v;
                        plistObj[key] = pc[v].desc;
                    }
                });
            }
        }
        // ORIENATATIONS
        if (orientationSupport) {
            if (orientationSupport.phone) {
                plistObj.UISupportedInterfaceOrientations = orientationSupport.phone;
            } else {
                plistObj.UISupportedInterfaceOrientations = ['UIInterfaceOrientationPortrait'];
            }
            if (orientationSupport.tab) {
                plistObj['UISupportedInterfaceOrientations~ipad'] = orientationSupport.tab;
            } else {
                plistObj['UISupportedInterfaceOrientations~ipad'] = ['UIInterfaceOrientationPortrait'];
            }
        }
        // URL_SCHEMES (LEGACY)
        if (urlScheme) {
            logWarning('urlScheme is DEPRECATED. use "plist:{ CFBundleURLTypes: []}" object instead');
            plistObj.CFBundleURLTypes.push({
                CFBundleTypeRole: 'Editor',
                CFBundleURLName: urlScheme,
                CFBundleURLSchemes: [urlScheme]
            });
        }

        // PLIST
        const plist = getConfigProp(c, platform, 'plist');
        if (plist) {
            plistObj = mergeObjects(c, plistObj, plist, true, true);
        }

        // PLUGINS
        parsePlugins(c, platform, (plugin, pluginPlat, key) => {
            if (pluginPlat.plist) {
                plistObj = mergeObjects(c, plistObj, pluginPlat.plist, true, true);
            }
        });
        saveObjToPlistSync(plistPath, plistObj);
        resolve();
    });

    const PLIST_START = `<?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
    <plist version="1.0">\n`;

    const PLIST_END = '</plist>\n';

    const objToPlist = (obj) => {
        let output = PLIST_START;
        output += _parseObject(obj, 0);
        output += PLIST_END;
        return output;
    };

    const _parseObject = (obj, level) => {
        let output = '';
        let space = '';
        for (let i = 0; i < level; i++) {
            space += '  ';
        }
        if (isArray(obj)) {
            output += `${space}<array>\n`;
            obj.forEach((v) => {
                output += _parseObject(v, level + 1);
            });
            output += `${space}</array>\n`;
        } else if (isBool(obj)) {
            output += `${space}<${obj} />\n`;
        } else if (isObject(obj)) {
            output += `${space}<dict>\n`;
            for (const key in obj) {
                output += `  ${space}<key>${key}</key>\n`;
                output += _parseObject(obj[key], level + 1);
            }
            output += `${space}</dict>\n`;
        } else if (isString(obj)) {
            output += `${space}<string>${obj}</string>\n`;
        }

        return output;
    };

    const saveObjToPlistSync = (filePath, obj) => {
        fs.writeFileSync(filePath, objToPlist(obj));
    };

    export {
        objToPlist,
        saveObjToPlistSync
    };