Skip to content
This repository has been archived by the owner on Oct 1, 2018. It is now read-only.

Commit

Permalink
Fix for #25
Browse files Browse the repository at this point in the history
  • Loading branch information
nikDemyankov committed Dec 17, 2015
1 parent 99ab30a commit 13b3c90
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 65 deletions.
85 changes: 23 additions & 62 deletions hooks/lib/ios/projectEntitlements.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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');
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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 {};
}

/**
Expand All @@ -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;
}
Expand All @@ -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;
}

/**
Expand All @@ -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
Expand Down
7 changes: 6 additions & 1 deletion hooks/lib/ios/xcodePreferences.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 13b3c90

Please sign in to comment.