Skip to content

Commit

Permalink
refactor: only pass preference for after prepare
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanneff committed Dec 16, 2016
1 parent 7d46741 commit 534d64c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 65 deletions.
16 changes: 14 additions & 2 deletions hooks/after_prepare.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
var iosPlist = require('./lib/ios/plist.js')
var iosPreferences = require('./lib/ios/preferences.js')
var iosAssociatedDomains = require('./lib/ios/associatedDomains.js')
var iosDevelopmentTeam = require('./lib/ios/developmentTeam.js')
// var androidManifest = require('.lib/android/androidManifest.js')
var IOS = 'ios'
var ANDROID = 'android'
Expand All @@ -29,9 +30,10 @@

}
if (platform === IOS) {
iosPlist.update(preferences)
iosPreferences.enableAssociatedDomains(context)
iosPlist.addBranchSettings(preferences)
iosPreferences.enableAssociatedDomains(preferences)
iosAssociatedDomains.addAssociatedDomains(preferences)
iosDevelopmentTeam.addDevelopmentTeam(context, preferences)
}
})
}
Expand All @@ -46,6 +48,15 @@

var branchXmlPerferences = branchXml[0]

var projectPlatform
try {
// pre-5.0 cordova structure
projectPlatform = context.requireCordovaModule('cordova-lib/src/plugman/platforms').ios
} catch (e) {
// post-5.0 cordova structure
projectPlatform = context.requireCordovaModule('cordova-lib/src/plugman/platforms/ios')
}

var bundleId = (configXml.widget['$'].hasOwnProperty('id')) ? configXml.widget['$']['id'] : null
var bundleName = (configXml.widget.hasOwnProperty('name')) ? configXml.widget.name[0] : null
var branchKey = (branchXmlPerferences.hasOwnProperty('branch-key')) ? branchXmlPerferences['branch-key'][0]['$']['value'] : null
Expand All @@ -56,6 +67,7 @@

return {
'projectRoot': context.opts.projectRoot,
'projectPlatform': projectPlatform,
'bundleId': bundleId,
'bundleName': bundleName,
'branchKey': branchKey,
Expand Down
79 changes: 16 additions & 63 deletions hooks/lib/ios/preferences.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,32 @@
'use strict'
var path = require('path')
var compare = require('node-version-compare')
var ConfigXmlHelper = require('../sdk/configXmlHelper.js')
var IOS_DEPLOYMENT_TARGET = '8.0'
var COMMENT_KEY = /_comment$/
var context

// entry
module.exports = {
enableAssociatedDomains: enableAssociatedDomains
}

function enableAssociatedDomains (cordovaContext) {
context = cordovaContext
function enableAssociatedDomains (preferences) {
var entitlementsFile = path.join(preferences.projectRoot, 'platforms', 'ios', preferences.bundleName, 'Resources', preferences.bundleName + '.entitlements')
var projectFile = preferences.projectPlatform.parseProjectFile(path.join(preferences.projectRoot, 'platforms', 'ios'))

var projectFile = loadProjectFile()

// adjust preferences
activateAssociativeDomains(projectFile.xcode)

// add entitlements file to pbxfilereference
addPbxReference(projectFile.xcode)

// save changes
activateAssociativeDomains(projectFile.xcode, entitlementsFile)
addPbxReference(projectFile.xcode, entitlementsFile)
projectFile.write()
}

function activateAssociativeDomains (xcodeProject) {
var configurations = nonComments(xcodeProject.pbxXCBuildConfigurationSection())
var entitlementsFilePath = pathToEntitlementsFile()
function activateAssociativeDomains (xcodeProject, entitlementsFile) {
var configurations = removeComments(xcodeProject.pbxXCBuildConfigurationSection())
var config
var buildSettings
var deploymentTargetIsUpdated

for (config in configurations) {
buildSettings = configurations[config].buildSettings
buildSettings.CODE_SIGN_ENTITLEMENTS = '"' + entitlementsFilePath + '"'
buildSettings.CODE_SIGN_ENTITLEMENTS = '"' + entitlementsFile + '"'

// if deployment target is less then the required one - increase it
if (buildSettings.IPHONEOS_DEPLOYMENT_TARGET) {
Expand All @@ -57,30 +48,29 @@
console.log('IOS project now has deployment target set as: ' + IOS_DEPLOYMENT_TARGET)
}

console.log('IOS project Code Sign Entitlements now set to: ' + entitlementsFilePath)
console.log('IOS project Code Sign Entitlements now set to: ' + entitlementsFile)
}

function addPbxReference (xcodeProject) {
var fileReferenceSection = nonComments(xcodeProject.pbxFileReferenceSection())
var entitlementsRelativeFilePath = pathToEntitlementsFile()
function addPbxReference (xcodeProject, entitlementsFile) {
var fileReferenceSection = removeComments(xcodeProject.pbxFileReferenceSection())

if (isPbxReferenceAlreadySet(fileReferenceSection, entitlementsRelativeFilePath)) {
if (isPbxReferenceAlreadySet(fileReferenceSection, entitlementsFile)) {
console.log('Entitlements file is in reference section.')
return
}

console.log('Entitlements file is not in references section, adding it')
createPbxFileReference(xcodeProject, entitlementsRelativeFilePath)
xcodeProject.addResourceFile(path.basename(entitlementsFile))
}

function isPbxReferenceAlreadySet (fileReferenceSection, entitlementsRelativeFilePath) {
function isPbxReferenceAlreadySet (fileReferenceSection, entitlementsFile) {
var isAlreadyInReferencesSection = false
var uuid
var fileRefEntry

for (uuid in fileReferenceSection) {
fileRefEntry = fileReferenceSection[uuid]
if (fileRefEntry.path && fileRefEntry.path.indexOf(entitlementsRelativeFilePath) > -1) {
if (fileRefEntry.path && fileRefEntry.path.indexOf(entitlementsFile) > -1) {
isAlreadyInReferencesSection = true
break
}
Expand All @@ -89,28 +79,7 @@
return isAlreadyInReferencesSection
}

function createPbxFileReference (xcodeProject, entitlementsRelativeFilePath) {
xcodeProject.addResourceFile(path.basename(entitlementsRelativeFilePath))
}

function loadProjectFile () {
var platform
var projectFile

try {
// try pre-5.0 cordova structure
platform = context.requireCordovaModule('cordova-lib/src/plugman/platforms').ios
projectFile = platform.parseProjectFile(iosPlatformPath())
} catch (e) {
// let's try cordova 5.0 structure
platform = context.requireCordovaModule('cordova-lib/src/plugman/platforms/ios')
projectFile = platform.parseProjectFile(iosPlatformPath())
}

return projectFile
}

function nonComments (obj) {
function removeComments (obj) {
var keys = Object.keys(obj)
var newObj = {}

Expand All @@ -122,20 +91,4 @@

return newObj
}

function iosPlatformPath () {
return path.join(projectRoot(), 'platforms', 'ios')
}

function projectRoot () {
return context.opts.projectRoot
}

function pathToEntitlementsFile () {
var configXmlHelper = new ConfigXmlHelper(context)
var projectName = configXmlHelper.getProjectName()
var fileName = projectName + '.entitlements'

return path.join(projectName, 'Resources', fileName)
}
})()

0 comments on commit 534d64c

Please sign in to comment.