You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

projectEntitlements.js 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. Script creates entitlements file with the list of hosts, specified in config.xml.
  3. File name is: ProjectName.entitlements
  4. Location: ProjectName/
  5. Script only generates content. File it self is included in the xcode project in another hook: xcodePreferences.js.
  6. */
  7. var path = require('path');
  8. var fs = require('fs');
  9. var plist = require('plist');
  10. var mkpath = require('mkpath');
  11. var ConfigXmlHelper = require('../configXmlHelper.js');
  12. var ASSOCIATED_DOMAINS = 'com.apple.developer.associated-domains';
  13. var context;
  14. var projectRoot;
  15. var projectName;
  16. var entitlementsFilePath;
  17. module.exports = {
  18. generateAssociatedDomainsEntitlements: generateEntitlements
  19. };
  20. // region Public API
  21. /**
  22. * Generate entitlements file content.
  23. *
  24. * @param {Object} cordovaContext - cordova context object
  25. * @param {Object} pluginPreferences - plugin preferences from config.xml; already parsed
  26. */
  27. function generateEntitlements(cordovaContext, pluginPreferences) {
  28. context = cordovaContext;
  29. var currentEntitlements = getEntitlementsFileContent();
  30. var newEntitlements = injectPreferences(currentEntitlements, pluginPreferences);
  31. saveContentToEntitlementsFile(newEntitlements);
  32. }
  33. // endregion
  34. // region Work with entitlements file
  35. /**
  36. * Save data to entitlements file.
  37. *
  38. * @param {Object} content - data to save; JSON object that will be transformed into xml
  39. */
  40. function saveContentToEntitlementsFile(content) {
  41. var plistContent = plist.build(content);
  42. var filePath = pathToEntitlementsFile();
  43. // ensure that file exists
  44. mkpath.sync(path.dirname(filePath));
  45. // save it's content
  46. fs.writeFileSync(filePath, plistContent, 'utf8');
  47. }
  48. /**
  49. * Read data from existing entitlements file. If none exist - default value is returned
  50. *
  51. * @return {String} entitlements file content
  52. */
  53. function getEntitlementsFileContent() {
  54. var pathToFile = pathToEntitlementsFile();
  55. var content;
  56. try {
  57. content = fs.readFileSync(pathToFile, 'utf8');
  58. } catch (err) {
  59. return defaultEntitlementsFile();
  60. }
  61. return plist.parse(content);
  62. }
  63. /**
  64. * Get content for an empty entitlements file.
  65. *
  66. * @return {String} default entitlements file content
  67. */
  68. function defaultEntitlementsFile() {
  69. return {};
  70. }
  71. /**
  72. * Inject list of hosts into entitlements file.
  73. *
  74. * @param {Object} currentEntitlements - entitlements where to inject preferences
  75. * @param {Object} pluginPreferences - list of hosts from config.xml
  76. * @return {Object} new entitlements content
  77. */
  78. function injectPreferences(currentEntitlements, pluginPreferences) {
  79. var newEntitlements = currentEntitlements;
  80. var content = generateAssociatedDomainsContent(pluginPreferences);
  81. newEntitlements[ASSOCIATED_DOMAINS] = content;
  82. return newEntitlements;
  83. }
  84. /**
  85. * Generate content for associated-domains dictionary in the entitlements file.
  86. *
  87. * @param {Object} pluginPreferences - list of hosts from conig.xml
  88. * @return {Object} associated-domains dictionary content
  89. */
  90. function generateAssociatedDomainsContent(pluginPreferences) {
  91. var domainsList = [];
  92. // generate list of host links
  93. pluginPreferences.hosts.forEach(function(host) {
  94. var link = domainsListEntryForHost(host);
  95. if (domainsList.indexOf(link) == -1) {
  96. domainsList.push(link);
  97. }
  98. });
  99. return domainsList;
  100. }
  101. /**
  102. * Generate domain record for the given host.
  103. *
  104. * @param {Object} host - host entry
  105. * @return {String} record
  106. */
  107. function domainsListEntryForHost(host) {
  108. return 'applinks:' + host.name;
  109. }
  110. // endregion
  111. // region Path helper methods
  112. /**
  113. * Path to entitlements file.
  114. *
  115. * @return {String} absolute path to entitlements file
  116. */
  117. function pathToEntitlementsFile() {
  118. if (entitlementsFilePath === undefined) {
  119. entitlementsFilePath = path.join(getProjectRoot(), 'platforms', 'ios', getProjectName(), 'Resources', getProjectName() + '.entitlements');
  120. }
  121. return entitlementsFilePath;
  122. }
  123. /**
  124. * Projects root folder path.
  125. *
  126. * @return {String} absolute path to the projects root
  127. */
  128. function getProjectRoot() {
  129. return context.opts.projectRoot;
  130. }
  131. /**
  132. * Name of the project from config.xml
  133. *
  134. * @return {String} project name
  135. */
  136. function getProjectName() {
  137. if (projectName === undefined) {
  138. var configXmlHelper = new ConfigXmlHelper(context);
  139. projectName = configXmlHelper.getProjectName();
  140. }
  141. return projectName;
  142. }
  143. // endregion