From eefc2e13cbe479dd29fcc584952cda02cf12e779 Mon Sep 17 00:00:00 2001 From: "DerekChia jingqiang.jia@hand-china.com" Date: Tue, 6 Jun 2017 13:57:38 +0800 Subject: [PATCH 1/2] fix typo --- hooks/iosEnablePush.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hooks/iosEnablePush.js b/hooks/iosEnablePush.js index 193b38e..f62d184 100644 --- a/hooks/iosEnablePush.js +++ b/hooks/iosEnablePush.js @@ -154,7 +154,7 @@ try { // try to read ios developmentTeam from build.json console.log("Do not detected 'build.json' to get ios developent team. \n" + "Will not enable XCode Push Notification Capability. \n" + "Will only enable Push Notification for CI by add config to '" + basePath + xcodeprojName + "/Entitlements-Debug.plist' and '" + basePath + xcodeprojName + "/Entitlements-Release.plist' \n" + - "Please add 'build.json' to cordova project root folder to make after hook fullly functional. \n" + + "Please add 'build.json' to cordova project root folder to make after hook fully functional. \n" + "Reference [1]https://cordova.apache.org/docs/en/latest/reference/cordova-cli/#cordova-build-command \n" + "Reference [2]https://cordova.apache.org/docs/en/latest/guide/platforms/ios/#signing-an-app"); willEnablePushNotificationForXCode = false; From 4bc2fe3dc1285aa4c79ce1e666d6f0eff696fd27 Mon Sep 17 00:00:00 2001 From: "DerekChia jingqiang.jia@hand-china.com" <> Date: Fri, 23 Jun 2017 14:31:46 +0800 Subject: [PATCH 2/2] fix #242 --- hooks/common.js | 106 ++++++++++++++++++++++------------------- hooks/iosEnablePush.js | 50 +++++++++---------- 2 files changed, 83 insertions(+), 73 deletions(-) diff --git a/hooks/common.js b/hooks/common.js index c5852d3..b66db45 100644 --- a/hooks/common.js +++ b/hooks/common.js @@ -6,66 +6,74 @@ const fs = require('fs'); const path = require('path'); module.exports.addAPNSinEntitlements = (entitlementPath, isProduction) => { - fs.readFile(entitlementPath, "utf8", function(err, data) { - if (err) { - throw err; - } - - console.log("Reading entitlements file asynchronously"); - - let toInsert = 'aps-environment\n' + - '\t\tdevelopment'; - if(isProduction) { - toInsert = 'aps-environment\n' + - '\t\tproduction'; - } - - let re1 = new RegExp('aps-environment<\/key>(.|[\r\n])*.*<\/string>'); - let matched = data.match(re1); - let result; - if (matched === null) { - if(data.match(/<\/dict>/g)) { - result = data.replace(/<\/dict>/, '\t' + toInsert + '\n\t'); - } else if(data.match(//g)) { - result = data.replace(//, '\t\n\t\t' + toInsert + '\n\t'); - } - } else { - result = data.replace(re1, toInsert); - } - - // write result to entitlements file - fs.writeFile(entitlementPath, result, {"encoding": 'utf8'}, function(err) { + if( fs.existsSync(entitlementPath) ) { + fs.readFile(entitlementPath, "utf8", function(err, data) { if (err) { throw err; } - console.log(entitlementPath + " written successfully"); + + console.log("Reading entitlements file asynchronously"); + + let toInsert = 'aps-environment\n' + + '\t\tdevelopment'; + if(isProduction) { + toInsert = 'aps-environment\n' + + '\t\tproduction'; + } + + let re1 = new RegExp('aps-environment<\/key>(.|[\r\n])*.*<\/string>'); + let matched = data.match(re1); + let result; + if (matched === null) { + if(data.match(/<\/dict>/g)) { + result = data.replace(/<\/dict>/, '\t' + toInsert + '\n\t'); + } else if(data.match(//g)) { + result = data.replace(//, '\t\n\t\t' + toInsert + '\n\t'); + } + } else { + result = data.replace(re1, toInsert); + } + + // write result to entitlements file + fs.writeFile(entitlementPath, result, {"encoding": 'utf8'}, function(err) { + if (err) { + throw err; + } + console.log(entitlementPath + " written successfully"); + }); }); - }); + } else { + console.log("Entitlement File '" + entitlementPath + "' not found. Make sure your ios platform upper than 4.3.0"); + } } module.exports.removeAPNSinEntitlements = (entitlementPath) => { - fs.readFile(entitlementPath, "utf8", function(err, data) { - if (err) { - throw err; - } - - console.log("Reading entitlements file asynchronously"); - - let re1 = new RegExp('aps-environment<\/key>(.|[\r\n])*.*<\/string>'); - let matched = data.match(re1); - let result; - if (matched != null) { - result = data.replace(re1, ""); - } - - // write result to entitlements file - fs.writeFile(entitlementPath, result, {"encoding": 'utf8'}, function(err) { + if( fs.existsSync(entitlementPath) ) { + fs.readFile(entitlementPath, "utf8", function(err, data) { if (err) { throw err; } - console.log(entitlementPath + " written successfully"); + + console.log("Reading entitlements file asynchronously"); + + let re1 = new RegExp('aps-environment<\/key>(.|[\r\n])*.*<\/string>'); + let matched = data.match(re1); + let result; + if (matched != null) { + result = data.replace(re1, ""); + } + + // write result to entitlements file + fs.writeFile(entitlementPath, result, {"encoding": 'utf8'}, function(err) { + if (err) { + throw err; + } + console.log(entitlementPath + " written successfully"); + }); }); - }); + } else { + console.log("Entitlement File '" + entitlementPath + "' not found. Make sure your ios platform upper than 4.3.0"); + } } module.exports.getXcodeProjName = (searchPath) => { diff --git a/hooks/iosEnablePush.js b/hooks/iosEnablePush.js index f62d184..e944c24 100644 --- a/hooks/iosEnablePush.js +++ b/hooks/iosEnablePush.js @@ -139,29 +139,31 @@ let enablePushNotificationForXCode = (entitlementsPath, pbxprojPath, cordovaBuil }); } -let basePath = './platforms/ios/'; -let buildType = 'dev'; -let xcodeprojName = commonFuncs.getXcodeProjName(basePath); -let pbxprojPath = basePath + xcodeprojName + '.xcodeproj/project.pbxproj'; -let entitlementsPath = basePath + xcodeprojName + '/' + xcodeprojName + '.entitlements'; +module.exports = (context) => { + let basePath = './platforms/ios/'; + let buildType = 'dev'; + let xcodeprojName = commonFuncs.getXcodeProjName(basePath); + let pbxprojPath = basePath + xcodeprojName + '.xcodeproj/project.pbxproj'; + let entitlementsPath = basePath + xcodeprojName + '/' + xcodeprojName + '.entitlements'; -let cordovaBuildConfigPath = './build.json' -let cordovaBuildConfig = null; -let willEnablePushNotificationForXCode = true; -try { // try to read ios developmentTeam from build.json - cordovaBuildConfig = JSON.parse(fs.readFileSync(cordovaBuildConfigPath, "utf8")); -} catch(e) { - console.log("Do not detected 'build.json' to get ios developent team. \n" + - "Will not enable XCode Push Notification Capability. \n" + - "Will only enable Push Notification for CI by add config to '" + basePath + xcodeprojName + "/Entitlements-Debug.plist' and '" + basePath + xcodeprojName + "/Entitlements-Release.plist' \n" + - "Please add 'build.json' to cordova project root folder to make after hook fully functional. \n" + - "Reference [1]https://cordova.apache.org/docs/en/latest/reference/cordova-cli/#cordova-build-command \n" + - "Reference [2]https://cordova.apache.org/docs/en/latest/guide/platforms/ios/#signing-an-app"); - willEnablePushNotificationForXCode = false; -} - -enablePushNotificationForCI(basePath, xcodeprojName); - -if(willEnablePushNotificationForXCode) { - enablePushNotificationForXCode(entitlementsPath, pbxprojPath, cordovaBuildConfig); + let cordovaBuildConfigPath = './build.json' + let cordovaBuildConfig = null; + let willEnablePushNotificationForXCode = true; + try { // try to read ios developmentTeam from build.json + cordovaBuildConfig = JSON.parse(fs.readFileSync(cordovaBuildConfigPath, "utf8")); + } catch(e) { + console.log("Do not detected 'build.json' to get ios developent team. \n" + + "Will not enable XCode Push Notification Capability. \n" + + "Will only enable Push Notification for CI by add config to '" + basePath + xcodeprojName + "/Entitlements-Debug.plist' and '" + basePath + xcodeprojName + "/Entitlements-Release.plist' \n" + + "Please add 'build.json' to cordova project root folder to make after hook fully functional. \n" + + "Reference [1]https://cordova.apache.org/docs/en/latest/reference/cordova-cli/#cordova-build-command \n" + + "Reference [2]https://cordova.apache.org/docs/en/latest/guide/platforms/ios/#signing-an-app"); + willEnablePushNotificationForXCode = false; + } + + enablePushNotificationForCI(basePath, xcodeprojName); + + if(willEnablePushNotificationForXCode) { + enablePushNotificationForXCode(entitlementsPath, pbxprojPath, cordovaBuildConfig); + } }