From 13b3c90f0069532597d7aee17011e746a86c6749 Mon Sep 17 00:00:00 2001 From: Nikolay Demyankov Date: Thu, 17 Dec 2015 15:08:11 +0100 Subject: [PATCH] Fix for https://github.com/nordnet/cordova-universal-links-plugin/issues/25 --- hooks/lib/ios/projectEntitlements.js | 85 ++++++++-------------------- hooks/lib/ios/xcodePreferences.js | 7 ++- package.json | 5 +- 3 files changed, 32 insertions(+), 65 deletions(-) diff --git a/hooks/lib/ios/projectEntitlements.js b/hooks/lib/ios/projectEntitlements.js index 50b4b3bc..dee70790 100644 --- a/hooks/lib/ios/projectEntitlements.js +++ b/hooks/lib/ios/projectEntitlements.js @@ -7,9 +7,10 @@ Script only generates content. File it self is included in the xcode project in */ (function() { - var xml2js = require('xml2js'), - path = require('path'), - xmlHelper = require('../xmlHelper.js'), + var path = require('path'), + fs = require('fs'), + plist = require('plist'), + mkpath = require('mkpath'), ConfigXmlHelper = require('../configXmlHelper.js'), ASSOCIATED_DOMAINS = 'com.apple.developer.associated-domains', context, @@ -48,14 +49,14 @@ Script only generates content. File it self is included in the xcode project in * @param {Object} content - data to save; JSON object that will be transformed into xml */ function saveContentToEntitlementsFile(content) { - var options = { - doctype: { - pubID: '-//Apple//DTD PLIST 1.0//EN', - sysID: 'http://www.apple.com/DTDs/PropertyList-1.0.dtd' - } - }; - - xmlHelper.writeJsonAsXml(content, pathToEntitlementsFile(), options); + var plistContent = plist.build(content), + filePath = pathToEntitlementsFile(); + + // ensure that file exists + mkpath.sync(path.dirname(filePath)); + + // save it's content + fs.writeFileSync(filePath, plistContent, 'utf8'); } /** @@ -64,13 +65,16 @@ Script only generates content. File it self is included in the xcode project in * @return {String} entitlements file content */ function getEntitlementsFileContent() { - var currentEntitlements = xmlHelper.readXmlAsJson(pathToEntitlementsFile()); - if (currentEntitlements != null) { - return currentEntitlements; + var pathToFile = pathToEntitlementsFile(), + content; + + try { + content = fs.readFileSync(pathToFile, 'utf8'); + } catch (err) { + return defaultEntitlementsFile(); } - // return default plist if it's not exist - return defaultEntitlementsFile(); + return plist.parse(content); } /** @@ -79,14 +83,7 @@ Script only generates content. File it self is included in the xcode project in * @return {String} default entitlements file content */ function defaultEntitlementsFile() { - return { - 'plist': { - '$': { - 'version': '1.0' - }, - 'dict': [] - } - }; + return {}; } /** @@ -98,16 +95,9 @@ Script only generates content. File it self is included in the xcode project in */ function injectPreferences(currentEntitlements, pluginPreferences) { var newEntitlements = currentEntitlements, - dictIndex = indexOfAssociatedDomainsDictionary(newEntitlements), content = generateAssociatedDomainsContent(pluginPreferences); - // if associated-domains entry was not found in entitlements file - add it; - // if was - replace it with the new version - if (dictIndex < 0) { - newEntitlements.plist.dict.push(content); - } else { - newEntitlements.plist.dict[dictIndex] = content; - } + newEntitlements[ASSOCIATED_DOMAINS] = content; return newEntitlements; } @@ -128,12 +118,7 @@ Script only generates content. File it self is included in the xcode project in domainsList.push(link); }); - return { - 'key': [ASSOCIATED_DOMAINS], - 'array': [{ - 'string': domainsList - }] - }; + return domainsList; } /** @@ -146,30 +131,6 @@ Script only generates content. File it self is included in the xcode project in return 'applinks:' + host.name; } - /** - * Find index of the associated-domains dictionary in the entitlements file. - * - * @param {Object} entitlements - entitlements file content - * @return {Integer} index of the associated-domains dictionary; -1 - if none was found - */ - function indexOfAssociatedDomainsDictionary(entitlements) { - if (entitlements['plist'] == null || entitlements.plist['dict'] == null) { - return -1; - } - - var index = -1; - entitlements.plist.dict.some(function(dictionary, dictIndex) { - if (dictionary.key == ASSOCIATED_DOMAINS) { - index = dictIndex; - return true; - } - - return false; - }); - - return index; - } - // endregion // region Path helper methods diff --git a/hooks/lib/ios/xcodePreferences.js b/hooks/lib/ios/xcodePreferences.js index 0534366c..e9a492dd 100644 --- a/hooks/lib/ios/xcodePreferences.js +++ b/hooks/lib/ios/xcodePreferences.js @@ -65,7 +65,12 @@ Which is: buildSettings['CODE_SIGN_ENTITLEMENTS'] = '"' + entitlementsFilePath + '"'; // if deployment target is less then the required one - increase it - if (compare(buildSettings['IPHONEOS_DEPLOYMENT_TARGET'], IOS_DEPLOYMENT_TARGET) == -1) { + if (buildSettings['IPHONEOS_DEPLOYMENT_TARGET']) { + if (compare(buildSettings['IPHONEOS_DEPLOYMENT_TARGET'], IOS_DEPLOYMENT_TARGET) == -1) { + buildSettings['IPHONEOS_DEPLOYMENT_TARGET'] = IOS_DEPLOYMENT_TARGET; + deploymentTargetIsUpdated = true; + } + } else { buildSettings['IPHONEOS_DEPLOYMENT_TARGET'] = IOS_DEPLOYMENT_TARGET; deploymentTargetIsUpdated = true; } diff --git a/package.json b/package.json index c6ad72b0..7e974dfc 100644 --- a/package.json +++ b/package.json @@ -36,10 +36,11 @@ } ], "dependencies": { - "mkpath": ">=0.1", + "mkpath": ">=1.0.0", "xml2js": ">=0.4", "rimraf": ">=2.4", - "node-version-compare": ">=1.0.1" + "node-version-compare": ">=1.0.1", + "plist": ">=1.2.0" }, "author": "Nikolay Demyankov for Nordnet Bank AB", "license": "MIT",