123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- /*
- Script creates entitlements file with the list of hosts, specified in config.xml.
- File name is: ProjectName.entitlements
- Location: ProjectName/
-
- Script only generates content. File it self is included in the xcode project in another hook: xcodePreferences.js.
- */
-
- var path = require('path');
- var fs = require('fs');
- var plist = require('plist');
- var mkpath = require('mkpath');
- var ConfigXmlHelper = require('../configXmlHelper.js');
- var ASSOCIATED_DOMAINS = 'com.apple.developer.associated-domains';
- var context;
- var projectRoot;
- var projectName;
- var entitlementsFilePath;
-
- module.exports = {
- generateAssociatedDomainsEntitlements: generateEntitlements
- };
-
- // region Public API
-
- /**
- * Generate entitlements file content.
- *
- * @param {Object} cordovaContext - cordova context object
- * @param {Object} pluginPreferences - plugin preferences from config.xml; already parsed
- */
- function generateEntitlements(cordovaContext, pluginPreferences) {
- context = cordovaContext;
-
- var currentEntitlements = getEntitlementsFileContent();
- var newEntitlements = injectPreferences(currentEntitlements, pluginPreferences);
-
- saveContentToEntitlementsFile(newEntitlements);
- }
-
- // endregion
-
- // region Work with entitlements file
-
- /**
- * Save data to entitlements file.
- *
- * @param {Object} content - data to save; JSON object that will be transformed into xml
- */
- function saveContentToEntitlementsFile(content) {
- var plistContent = plist.build(content);
- var filePath = pathToEntitlementsFile();
-
- // ensure that file exists
- mkpath.sync(path.dirname(filePath));
-
- // save it's content
- fs.writeFileSync(filePath, plistContent, 'utf8');
- }
-
- /**
- * Read data from existing entitlements file. If none exist - default value is returned
- *
- * @return {String} entitlements file content
- */
- function getEntitlementsFileContent() {
- var pathToFile = pathToEntitlementsFile();
- var content;
-
- try {
- content = fs.readFileSync(pathToFile, 'utf8');
- } catch (err) {
- return defaultEntitlementsFile();
- }
-
- return plist.parse(content);
- }
-
- /**
- * Get content for an empty entitlements file.
- *
- * @return {String} default entitlements file content
- */
- function defaultEntitlementsFile() {
- return {};
- }
-
- /**
- * Inject list of hosts into entitlements file.
- *
- * @param {Object} currentEntitlements - entitlements where to inject preferences
- * @param {Object} pluginPreferences - list of hosts from config.xml
- * @return {Object} new entitlements content
- */
- function injectPreferences(currentEntitlements, pluginPreferences) {
- var newEntitlements = currentEntitlements;
- var content = generateAssociatedDomainsContent(pluginPreferences);
-
- newEntitlements[ASSOCIATED_DOMAINS] = content;
-
- return newEntitlements;
- }
-
- /**
- * Generate content for associated-domains dictionary in the entitlements file.
- *
- * @param {Object} pluginPreferences - list of hosts from conig.xml
- * @return {Object} associated-domains dictionary content
- */
- function generateAssociatedDomainsContent(pluginPreferences) {
- var domainsList = [];
-
- // generate list of host links
- pluginPreferences.hosts.forEach(function(host) {
- var link = domainsListEntryForHost(host);
- if (domainsList.indexOf(link) == -1) {
- domainsList.push(link);
- }
- });
-
- return domainsList;
- }
-
- /**
- * Generate domain record for the given host.
- *
- * @param {Object} host - host entry
- * @return {String} record
- */
- function domainsListEntryForHost(host) {
- return 'applinks:' + host.name;
- }
-
- // endregion
-
- // region Path helper methods
-
- /**
- * Path to entitlements file.
- *
- * @return {String} absolute path to entitlements file
- */
- function pathToEntitlementsFile() {
- if (entitlementsFilePath === undefined) {
- entitlementsFilePath = path.join(getProjectRoot(), 'platforms', 'ios', getProjectName(), 'Resources', getProjectName() + '.entitlements');
- }
-
- return entitlementsFilePath;
- }
-
- /**
- * Projects root folder path.
- *
- * @return {String} absolute path to the projects root
- */
- function getProjectRoot() {
- return context.opts.projectRoot;
- }
-
- /**
- * Name of the project from config.xml
- *
- * @return {String} project name
- */
- function getProjectName() {
- if (projectName === undefined) {
- var configXmlHelper = new ConfigXmlHelper(context);
- projectName = configXmlHelper.getProjectName();
- }
-
- return projectName;
- }
-
- // endregion
|