diff --git a/.gitignore b/.gitignore index 4b97b5db..887d52c7 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ testbed/platforms testbed/plugins testbed/hooks +testbed/build.json node_modules .installed .gradle/ diff --git a/hooks/afterPrepare.js b/hooks/afterPrepare.js index a911986c..8f8e155a 100644 --- a/hooks/afterPrepare.js +++ b/hooks/afterPrepare.js @@ -26,7 +26,7 @@ iosPlist.addBranchSettings(preferences) iosCapabilities.enableAssociatedDomains(preferences) iosAssociatedDomains.addAssociatedDomains(preferences) - iosDevelopmentTeam.addDevelopmentTeam(context, preferences) // does not work + iosDevelopmentTeam.addDevelopmentTeam(preferences) // does not work, still needs to run through Xcode } }) } diff --git a/hooks/lib/ios/developmentTeam.js b/hooks/lib/ios/developmentTeam.js index 5bf283db..d352b136 100644 --- a/hooks/lib/ios/developmentTeam.js +++ b/hooks/lib/ios/developmentTeam.js @@ -1,11 +1,12 @@ -// TODO: does not work +// TODO: does not work. Automatic Signing of Provisioning Profile is not updating (https://pewpewthespells.com/blog/migrating_code_signing.html#automatic-signing-xcode-7) +// TODO: users will have to bypass by always opening up Xcode (function () { // properties 'use strict' var fs = require('fs') var path = require('path') - var encoding = 'utf-8' - var filepath = 'platforms/ios/cordova/build.xcconfig' + + var fileName = 'build.json' // entry module.exports = { @@ -13,32 +14,84 @@ } // updates the development team for Universal Links - function addDevelopmentTeam (context, preferences) { - console.log('BRANCH SDK: Updating Development Team') + function addDevelopmentTeam (preferences) { + console.log('BRANCH SDK: Updating iOS development team') + + var file = path.join(preferences.projectRoot, fileName) + var content = getBuildJson(file) + content = convertStringToJson(content) - if (context.opts.cordova.platforms.indexOf('ios') === -1) return - if (!context.opts.options) return - if (!context.opts.options.buildConfig) return + createDefaultBuildJson(content) + updateDevelopmentTeam(content, preferences) - var buildType = context.opts.options.release ? 'release' : 'debug' + content = convertJsonToString(content) + setBuildJson(file, content) + } - var buildConfigPath = context.opts.options.buildConfig - if (!path.isAbsolute(buildConfigPath)) { - buildConfigPath = path.join(context.opts.projectRoot, context.opts.options.buildConfig) + // json helper functions + function convertJsonToString (content) { + try { + // pretty-json + return JSON.stringify(content, null, 2) + } catch (err) { + throw new Error('Branch SDK cannot write build.json within your root directory.') } - var config = require(buildConfigPath) + } - if (!config.ios) return - if (!config.ios[buildType]) return - if (!config.ios[buildType].developmentTeam) return + function convertStringToJson (content) { + // handle blank file + content = !content ? '{}' : content + try { + return JSON.parse(content) + } catch (err) { + throw new Error('Branch SDK cannot read build.json within your root directory.') + } + } - var xcconfig = fs.readFileSync(filepath, encoding) + // read build.json + function getBuildJson (file) { + try { + return fs.readFileSync(file, 'utf8') + } catch (err) { + // handle no file + return '{}' + } + } - if (xcconfig.indexOf('DEVELOPMENT_TEAM') === -1) { - var content = '\nDEVELOPMENT_TEAM = ' + config.ios[buildType].developmentTeam + // write build.json + function setBuildJson (file, content) { + fs.writeFileSync(file, content, 'utf8') + } - xcconfig += content - fs.writeFileSync(filepath, xcconfig, encoding) + // creates basic build.json if key-value pairs are missing + // { + // "ios": { + // "debug": { + // "developmentTeam": "FG35JLLMXX4A" + // }, + // "release": { + // "developmentTeam": "FG35JLLMXX4A" + // } + // } + // } + function createDefaultBuildJson (content) { + if (!content.ios) { + content.ios = {} + } + if (!content.ios.debug) { + content.ios.debug = {} + } + if (!content.ios.release) { + content.ios.release = {} } } + + // update build.json with developmentTeam from config.xml + function updateDevelopmentTeam (content, preferences) { + var release = preferences.iosTeamProd + var debug = (preferences.iosTeamDev) ? preferences.iosTeamDev : preferences.iosTeamProd + + content.ios.release.developmentTeam = release + content.ios.debug.developmentTeam = debug + } })() diff --git a/hooks/lib/sdk/configXml.js b/hooks/lib/sdk/configXml.js index 73590926..2ea4668f 100644 --- a/hooks/lib/sdk/configXml.js +++ b/hooks/lib/sdk/configXml.js @@ -51,9 +51,10 @@ var bundleId = (configXml.widget['$'].hasOwnProperty('id')) ? configXml.widget['$']['id'] : null var bundleName = (configXml.widget.hasOwnProperty('name')) ? configXml.widget.name[0] : null var branchKey = (branchXml.hasOwnProperty('branch-key')) ? branchXml['branch-key'][0]['$']['value'] : null - var uriScheme = (branchXml.hasOwnProperty('uri-scheme')) ? branchXml['uri-scheme'][0]['$']['value'] : null var linkDomain = (branchXml.hasOwnProperty('link-domain')) ? branchXml['link-domain'][0]['$']['value'] : null - var iosTeamId = (branchXml.hasOwnProperty('ios-team-id')) ? branchXml['ios-team-id'][0]['$']['value'] : null + var uriScheme = (branchXml.hasOwnProperty('uri-scheme')) ? branchXml['uri-scheme'][0]['$']['value'] : null + var iosTeamProd = (branchXml.hasOwnProperty('ios-team-prod')) ? branchXml['ios-team-prod'][0]['$']['value'] : null + var iosTeamDev = (branchXml.hasOwnProperty('ios-team-dev')) ? branchXml['ios-team-dev'][0]['$']['value'] : null var androidPrefix = (branchXml.hasOwnProperty('android-prefix')) ? branchXml['android-prefix'][0]['$']['value'] : null return { @@ -64,8 +65,9 @@ 'branchKey': branchKey, 'uriScheme': uriScheme, 'linkDomain': linkDomain, - 'iosTeamId': iosTeamId, - 'androidPrefix': androidPrefix + 'iosTeamProd': iosTeamProd, + 'iosTeamDev': iosTeamDev, // optional + 'androidPrefix': androidPrefix // optional } } @@ -101,8 +103,8 @@ if (preferences.linkDomain === null) { throw new Error('Branch SDK plugin is missing "uri-scheme" in in your config.xml') } - if (preferences.iosTeamId === null) { - throw new Error('Branch SDK plugin is missing "ios-team-id" in in your config.xml') + if (preferences.iosTeamProd === null) { + throw new Error('Branch SDK plugin is missing "ios-team-prod" in in your config.xml') } } })() diff --git a/testbed/config.xml b/testbed/config.xml index a07f0d7d..26eca360 100644 --- a/testbed/config.xml +++ b/testbed/config.xml @@ -4,9 +4,9 @@ - + Branch Testing