Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

prepare.js 46KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151
  1. /**
  2. Licensed to the Apache Software Foundation (ASF) under one
  3. or more contributor license agreements. See the NOTICE file
  4. distributed with this work for additional information
  5. regarding copyright ownership. The ASF licenses this file
  6. to you under the Apache License, Version 2.0 (the
  7. "License"); you may not use this file except in compliance
  8. with the License. You may obtain a copy of the License at
  9. http://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing,
  11. software distributed under the License is distributed on an
  12. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  13. KIND, either express or implied. See the License for the
  14. specific language governing permissions and limitations
  15. under the License.
  16. */
  17. 'use strict';
  18. const fs = require('fs-extra');
  19. const path = require('path');
  20. const unorm = require('unorm');
  21. const plist = require('plist');
  22. const URL = require('url');
  23. const events = require('cordova-common').events;
  24. const xmlHelpers = require('cordova-common').xmlHelpers;
  25. const ConfigParser = require('cordova-common').ConfigParser;
  26. const CordovaError = require('cordova-common').CordovaError;
  27. const PlatformJson = require('cordova-common').PlatformJson;
  28. const PlatformMunger = require('cordova-common').ConfigChanges.PlatformMunger;
  29. const PluginInfoProvider = require('cordova-common').PluginInfoProvider;
  30. const FileUpdater = require('cordova-common').FileUpdater;
  31. const projectFile = require('./projectFile');
  32. // launch storyboard and related constants
  33. const IMAGESET_COMPACT_SIZE_CLASS = 'compact';
  34. const CDV_ANY_SIZE_CLASS = 'any';
  35. module.exports.prepare = function (cordovaProject, options) {
  36. const platformJson = PlatformJson.load(this.locations.root, 'ios');
  37. const munger = new PlatformMunger('ios', this.locations.root, platformJson, new PluginInfoProvider());
  38. this._config = updateConfigFile(cordovaProject.projectConfig, munger, this.locations);
  39. // Update own www dir with project's www assets and plugins' assets and js-files
  40. return updateWww(cordovaProject, this.locations)
  41. // update project according to config.xml changes.
  42. .then(() => updateProject(this._config, this.locations))
  43. .then(() => {
  44. updateIcons(cordovaProject, this.locations);
  45. updateLaunchStoryboardImages(cordovaProject, this.locations);
  46. updateBackgroundColor(cordovaProject, this.locations);
  47. updateFileResources(cordovaProject, this.locations);
  48. })
  49. .then(() => {
  50. alertDeprecatedPreference(this._config);
  51. })
  52. .then(() => {
  53. events.emit('verbose', 'Prepared iOS project successfully');
  54. });
  55. };
  56. module.exports.clean = function (options) {
  57. // A cordovaProject isn't passed into the clean() function, because it might have
  58. // been called from the platform shell script rather than the CLI. Check for the
  59. // noPrepare option passed in by the non-CLI clean script. If that's present, or if
  60. // there's no config.xml found at the project root, then don't clean prepared files.
  61. const projectRoot = path.resolve(this.root, '../..');
  62. const projectConfigFile = path.join(projectRoot, 'config.xml');
  63. if ((options && options.noPrepare) || !fs.existsSync(projectConfigFile) ||
  64. !fs.existsSync(this.locations.configXml)) {
  65. return Promise.resolve();
  66. }
  67. const projectConfig = new ConfigParser(this.locations.configXml);
  68. return Promise.resolve().then(() => {
  69. cleanWww(projectRoot, this.locations);
  70. cleanIcons(projectRoot, projectConfig, this.locations);
  71. cleanLaunchStoryboardImages(projectRoot, projectConfig, this.locations);
  72. cleanBackgroundColor(projectRoot, projectConfig, this.locations);
  73. cleanFileResources(projectRoot, projectConfig, this.locations);
  74. });
  75. };
  76. /**
  77. * Updates config files in project based on app's config.xml and config munge,
  78. * generated by plugins.
  79. *
  80. * @param {ConfigParser} sourceConfig A project's configuration that will
  81. * be merged into platform's config.xml
  82. * @param {ConfigChanges} configMunger An initialized ConfigChanges instance
  83. * for this platform.
  84. * @param {Object} locations A map of locations for this platform
  85. *
  86. * @return {ConfigParser} An instance of ConfigParser, that
  87. * represents current project's configuration. When returned, the
  88. * configuration is already dumped to appropriate config.xml file.
  89. */
  90. function updateConfigFile (sourceConfig, configMunger, locations) {
  91. events.emit('verbose', `Generating platform-specific config.xml from defaults for iOS at ${locations.configXml}`);
  92. // First cleanup current config and merge project's one into own
  93. // Overwrite platform config.xml with defaults.xml.
  94. fs.copySync(locations.defaultConfigXml, locations.configXml);
  95. // Then apply config changes from global munge to all config files
  96. // in project (including project's config)
  97. configMunger.reapply_global_munge().save_all();
  98. events.emit('verbose', 'Merging project\'s config.xml into platform-specific iOS config.xml');
  99. // Merge changes from app's config.xml into platform's one
  100. const config = new ConfigParser(locations.configXml);
  101. xmlHelpers.mergeXml(sourceConfig.doc.getroot(),
  102. config.doc.getroot(), 'ios', /* clobber= */true);
  103. config.write();
  104. return config;
  105. }
  106. /**
  107. * Logs all file operations via the verbose event stream, indented.
  108. */
  109. function logFileOp (message) {
  110. events.emit('verbose', ` ${message}`);
  111. }
  112. /**
  113. * Updates platform 'www' directory by replacing it with contents of
  114. * 'platform_www' and app www. Also copies project's overrides' folder into
  115. * the platform 'www' folder
  116. *
  117. * @param {Object} cordovaProject An object which describes cordova project.
  118. * @param {boolean} destinations An object that contains destinations
  119. * paths for www files.
  120. */
  121. function updateWww (cordovaProject, destinations) {
  122. const sourceDirs = [
  123. path.relative(cordovaProject.root, cordovaProject.locations.www),
  124. path.relative(cordovaProject.root, destinations.platformWww)
  125. ];
  126. // If project contains 'merges' for our platform, use them as another overrides
  127. const merges_path = path.join(cordovaProject.root, 'merges', 'ios');
  128. if (fs.existsSync(merges_path)) {
  129. events.emit('verbose', 'Found "merges/ios" folder. Copying its contents into the iOS project.');
  130. sourceDirs.push(path.join('merges', 'ios'));
  131. }
  132. const targetDir = path.relative(cordovaProject.root, destinations.www);
  133. events.emit(
  134. 'verbose', `Merging and updating files from [${sourceDirs.join(', ')}] to ${targetDir}`);
  135. FileUpdater.mergeAndUpdateDir(
  136. sourceDirs, targetDir, { rootDir: cordovaProject.root }, logFileOp);
  137. return Promise.resolve();
  138. }
  139. /**
  140. * Cleans all files from the platform 'www' directory.
  141. */
  142. function cleanWww (projectRoot, locations) {
  143. const targetDir = path.relative(projectRoot, locations.www);
  144. events.emit('verbose', `Cleaning ${targetDir}`);
  145. // No source paths are specified, so mergeAndUpdateDir() will clear the target directory.
  146. FileUpdater.mergeAndUpdateDir(
  147. [], targetDir, { rootDir: projectRoot, all: true }, logFileOp);
  148. }
  149. /**
  150. * Updates project structure and AndroidManifest according to project's configuration.
  151. *
  152. * @param {ConfigParser} platformConfig A project's configuration that will
  153. * be used to update project
  154. * @param {Object} locations A map of locations for this platform (In/Out)
  155. */
  156. function updateProject (platformConfig, locations) {
  157. // CB-6992 it is necessary to normalize characters
  158. // because node and shell scripts handles unicode symbols differently
  159. // We need to normalize the name to NFD form since iOS uses NFD unicode form
  160. const name = unorm.nfd(platformConfig.name());
  161. const version = platformConfig.version();
  162. const displayName = platformConfig.shortName && platformConfig.shortName();
  163. const originalName = path.basename(locations.xcodeCordovaProj);
  164. // Update package id (bundle id)
  165. const plistFile = path.join(locations.xcodeCordovaProj, `${originalName}-Info.plist`);
  166. const infoPlist = plist.parse(fs.readFileSync(plistFile, 'utf8'));
  167. // Update version (bundle version)
  168. infoPlist.CFBundleShortVersionString = version;
  169. const CFBundleVersion = platformConfig.getAttribute('ios-CFBundleVersion') || default_CFBundleVersion(version);
  170. infoPlist.CFBundleVersion = CFBundleVersion;
  171. if (platformConfig.getAttribute('defaultlocale')) {
  172. infoPlist.CFBundleDevelopmentRegion = platformConfig.getAttribute('defaultlocale');
  173. }
  174. if (displayName) {
  175. infoPlist.CFBundleDisplayName = displayName;
  176. }
  177. // replace Info.plist ATS entries according to <access> and <allow-navigation> config.xml entries
  178. const ats = writeATSEntries(platformConfig);
  179. if (Object.keys(ats).length > 0) {
  180. infoPlist.NSAppTransportSecurity = ats;
  181. } else {
  182. delete infoPlist.NSAppTransportSecurity;
  183. }
  184. handleOrientationSettings(platformConfig, infoPlist);
  185. /* eslint-disable no-tabs */
  186. // Write out the plist file with the same formatting as Xcode does
  187. let info_contents = plist.build(infoPlist, { indent: '\t', offset: -1 });
  188. /* eslint-enable no-tabs */
  189. info_contents = info_contents.replace(/<string>[\s\r\n]*<\/string>/g, '<string></string>');
  190. fs.writeFileSync(plistFile, info_contents, 'utf-8');
  191. events.emit('verbose', `Wrote out iOS Bundle Version "${version}" to ${plistFile}`);
  192. return handleBuildSettings(platformConfig, locations, infoPlist).then(() => {
  193. if (name === originalName) {
  194. events.emit('verbose', `iOS Product Name has not changed (still "${originalName}")`);
  195. return Promise.resolve();
  196. } else { // CB-11712 <name> was changed, we don't support it'
  197. const errorString =
  198. 'The product name change (<name> tag) in config.xml is not supported dynamically.\n' +
  199. 'To change your product name, you have to remove, then add your ios platform again.\n' +
  200. 'Make sure you save your plugins beforehand using `cordova plugin save`.\n' +
  201. '\tcordova plugin save\n' +
  202. '\tcordova platform rm ios\n' +
  203. '\tcordova platform add ios\n';
  204. return Promise.reject(new CordovaError(errorString));
  205. }
  206. });
  207. }
  208. function handleOrientationSettings (platformConfig, infoPlist) {
  209. switch (getOrientationValue(platformConfig)) {
  210. case 'portrait':
  211. infoPlist.UIInterfaceOrientation = ['UIInterfaceOrientationPortrait'];
  212. infoPlist.UISupportedInterfaceOrientations = ['UIInterfaceOrientationPortrait', 'UIInterfaceOrientationPortraitUpsideDown'];
  213. infoPlist['UISupportedInterfaceOrientations~ipad'] = ['UIInterfaceOrientationPortrait', 'UIInterfaceOrientationPortraitUpsideDown'];
  214. break;
  215. case 'landscape':
  216. infoPlist.UIInterfaceOrientation = ['UIInterfaceOrientationLandscapeLeft'];
  217. infoPlist.UISupportedInterfaceOrientations = ['UIInterfaceOrientationLandscapeLeft', 'UIInterfaceOrientationLandscapeRight'];
  218. infoPlist['UISupportedInterfaceOrientations~ipad'] = ['UIInterfaceOrientationLandscapeLeft', 'UIInterfaceOrientationLandscapeRight'];
  219. break;
  220. case 'all':
  221. infoPlist.UIInterfaceOrientation = ['UIInterfaceOrientationPortrait'];
  222. infoPlist.UISupportedInterfaceOrientations = ['UIInterfaceOrientationPortrait', 'UIInterfaceOrientationPortraitUpsideDown', 'UIInterfaceOrientationLandscapeLeft', 'UIInterfaceOrientationLandscapeRight'];
  223. infoPlist['UISupportedInterfaceOrientations~ipad'] = ['UIInterfaceOrientationPortrait', 'UIInterfaceOrientationPortraitUpsideDown', 'UIInterfaceOrientationLandscapeLeft', 'UIInterfaceOrientationLandscapeRight'];
  224. break;
  225. case 'default':
  226. infoPlist.UISupportedInterfaceOrientations = ['UIInterfaceOrientationPortrait', 'UIInterfaceOrientationLandscapeLeft', 'UIInterfaceOrientationLandscapeRight'];
  227. infoPlist['UISupportedInterfaceOrientations~ipad'] = ['UIInterfaceOrientationPortrait', 'UIInterfaceOrientationPortraitUpsideDown', 'UIInterfaceOrientationLandscapeLeft', 'UIInterfaceOrientationLandscapeRight'];
  228. delete infoPlist.UIInterfaceOrientation;
  229. }
  230. }
  231. function handleBuildSettings (platformConfig, locations, infoPlist) {
  232. const pkg = platformConfig.getAttribute('ios-CFBundleIdentifier') || platformConfig.packageName();
  233. const targetDevice = parseTargetDevicePreference(platformConfig.getPreference('target-device', 'ios'));
  234. const deploymentTarget = platformConfig.getPreference('deployment-target', 'ios');
  235. const swiftVersion = platformConfig.getPreference('SwiftVersion', 'ios');
  236. let project;
  237. try {
  238. project = projectFile.parse(locations);
  239. } catch (err) {
  240. return Promise.reject(new CordovaError(`Could not parse ${locations.pbxproj}: ${err}`));
  241. }
  242. const origPkg = project.xcode.getBuildProperty('PRODUCT_BUNDLE_IDENTIFIER', undefined, platformConfig.name());
  243. // no build settings provided and we don't need to update build settings for launch storyboards,
  244. // then we don't need to parse and update .pbxproj file
  245. if (origPkg === pkg && !targetDevice && !deploymentTarget && !swiftVersion) {
  246. return Promise.resolve();
  247. }
  248. if (origPkg !== pkg) {
  249. events.emit('verbose', `Set PRODUCT_BUNDLE_IDENTIFIER to ${pkg}.`);
  250. project.xcode.updateBuildProperty('PRODUCT_BUNDLE_IDENTIFIER', pkg, null, platformConfig.name());
  251. }
  252. if (targetDevice) {
  253. events.emit('verbose', `Set TARGETED_DEVICE_FAMILY to ${targetDevice}.`);
  254. project.xcode.updateBuildProperty('TARGETED_DEVICE_FAMILY', targetDevice);
  255. }
  256. if (deploymentTarget) {
  257. events.emit('verbose', `Set IPHONEOS_DEPLOYMENT_TARGET to "${deploymentTarget}".`);
  258. project.xcode.updateBuildProperty('IPHONEOS_DEPLOYMENT_TARGET', deploymentTarget);
  259. }
  260. if (swiftVersion) {
  261. events.emit('verbose', `Set SwiftVersion to "${swiftVersion}".`);
  262. project.xcode.updateBuildProperty('SWIFT_VERSION', swiftVersion);
  263. }
  264. project.write();
  265. return Promise.resolve();
  266. }
  267. function mapIconResources (icons, iconsDir) {
  268. // See https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/MobileHIG/IconMatrix.html
  269. // for launch images sizes reference.
  270. const platformIcons = [
  271. { dest: 'icon-20.png', width: 20, height: 20 },
  272. { dest: 'icon-20@2x.png', width: 40, height: 40 },
  273. { dest: 'icon-20@3x.png', width: 60, height: 60 },
  274. { dest: 'icon-40.png', width: 40, height: 40 },
  275. { dest: 'icon-40@2x.png', width: 80, height: 80 },
  276. { dest: 'icon-50.png', width: 50, height: 50 },
  277. { dest: 'icon-50@2x.png', width: 100, height: 100 },
  278. { dest: 'icon-60@2x.png', width: 120, height: 120 },
  279. { dest: 'icon-60@3x.png', width: 180, height: 180 },
  280. { dest: 'icon-72.png', width: 72, height: 72 },
  281. { dest: 'icon-72@2x.png', width: 144, height: 144 },
  282. { dest: 'icon-76.png', width: 76, height: 76 },
  283. { dest: 'icon-76@2x.png', width: 152, height: 152 },
  284. { dest: 'icon-83.5@2x.png', width: 167, height: 167 },
  285. { dest: 'icon-1024.png', width: 1024, height: 1024 },
  286. { dest: 'icon-29.png', width: 29, height: 29 },
  287. { dest: 'icon-29@2x.png', width: 58, height: 58 },
  288. { dest: 'icon-29@3x.png', width: 87, height: 87 },
  289. { dest: 'icon.png', width: 57, height: 57 },
  290. { dest: 'icon@2x.png', width: 114, height: 114 },
  291. { dest: 'icon-24@2x.png', width: 48, height: 48 },
  292. { dest: 'icon-27.5@2x.png', width: 55, height: 55 },
  293. { dest: 'icon-44@2x.png', width: 88, height: 88 },
  294. { dest: 'icon-86@2x.png', width: 172, height: 172 },
  295. { dest: 'icon-98@2x.png', width: 196, height: 196 }
  296. ];
  297. const pathMap = {};
  298. platformIcons.forEach(item => {
  299. const icon = icons.getBySize(item.width, item.height) || icons.getDefault();
  300. if (icon) {
  301. const target = path.join(iconsDir, item.dest);
  302. pathMap[target] = icon.src;
  303. }
  304. });
  305. return pathMap;
  306. }
  307. function getIconsDir (projectRoot, platformProjDir) {
  308. let iconsDir;
  309. const xcassetsExists = folderExists(path.join(projectRoot, platformProjDir, 'Images.xcassets/'));
  310. if (xcassetsExists) {
  311. iconsDir = path.join(platformProjDir, 'Images.xcassets/AppIcon.appiconset/');
  312. } else {
  313. iconsDir = path.join(platformProjDir, 'Resources/icons/');
  314. }
  315. return iconsDir;
  316. }
  317. function updateIcons (cordovaProject, locations) {
  318. const icons = cordovaProject.projectConfig.getIcons('ios');
  319. if (icons.length === 0) {
  320. events.emit('verbose', 'This app does not have icons defined');
  321. return;
  322. }
  323. const platformProjDir = path.relative(cordovaProject.root, locations.xcodeCordovaProj);
  324. const iconsDir = getIconsDir(cordovaProject.root, platformProjDir);
  325. const resourceMap = mapIconResources(icons, iconsDir);
  326. events.emit('verbose', `Updating icons at ${iconsDir}`);
  327. FileUpdater.updatePaths(
  328. resourceMap, { rootDir: cordovaProject.root }, logFileOp);
  329. }
  330. function cleanIcons (projectRoot, projectConfig, locations) {
  331. const icons = projectConfig.getIcons('ios');
  332. if (icons.length > 0) {
  333. const platformProjDir = path.relative(projectRoot, locations.xcodeCordovaProj);
  334. const iconsDir = getIconsDir(projectRoot, platformProjDir);
  335. const resourceMap = mapIconResources(icons, iconsDir);
  336. Object.keys(resourceMap).forEach(targetIconPath => {
  337. resourceMap[targetIconPath] = null;
  338. });
  339. events.emit('verbose', `Cleaning icons at ${iconsDir}`);
  340. // Source paths are removed from the map, so updatePaths() will delete the target files.
  341. FileUpdater.updatePaths(
  342. resourceMap, { rootDir: projectRoot, all: true }, logFileOp);
  343. }
  344. }
  345. /**
  346. * Returns the directory for the BackgroundColor.colorset asset, or null if no
  347. * xcassets exist.
  348. *
  349. * @param {string} projectRoot The project's root directory
  350. * @param {string} platformProjDir The platform's project directory
  351. */
  352. function getBackgroundColorDir (projectRoot, platformProjDir) {
  353. if (folderExists(path.join(projectRoot, platformProjDir, 'Images.xcassets/'))) {
  354. return path.join(platformProjDir, 'Images.xcassets', 'BackgroundColor.colorset');
  355. } else {
  356. return null;
  357. }
  358. }
  359. function colorPreferenceToComponents (pref) {
  360. if (!pref || !pref.match(/^(#[0-9A-F]{3}|(0x|#)([0-9A-F]{2})?[0-9A-F]{6})$/)) {
  361. return {
  362. platform: 'ios',
  363. reference: 'systemBackgroundColor'
  364. };
  365. }
  366. let red = 'FF';
  367. let green = 'FF';
  368. let blue = 'FF';
  369. let alpha = 1.0;
  370. if (pref[0] === '#' && pref.length === 4) {
  371. red = pref[1] + pref[1];
  372. green = pref[2] + pref[2];
  373. blue = pref[3] + pref[3];
  374. }
  375. if (pref.length >= 7 && (pref[0] === '#' || pref.substring(0, 2) === '0x')) {
  376. let offset = pref[0] === '#' ? 1 : 2;
  377. if (pref.substring(offset).length === 8) {
  378. alpha = parseInt(pref.substring(offset, offset + 2), 16) / 255.0;
  379. offset += 2;
  380. }
  381. red = pref.substring(offset, offset + 2);
  382. green = pref.substring(offset + 2, offset + 4);
  383. blue = pref.substring(offset + 4, offset + 6);
  384. }
  385. return {
  386. 'color-space': 'srgb',
  387. components: {
  388. red: '0x' + red,
  389. green: '0x' + green,
  390. blue: '0x' + blue,
  391. alpha: alpha.toFixed(3)
  392. }
  393. };
  394. }
  395. /**
  396. * Update the background color Contents.json in xcassets.
  397. *
  398. * @param {Object} cordovaProject The cordova project
  399. * @param {Object} locations A dictionary containing useful location paths
  400. */
  401. function updateBackgroundColor (cordovaProject, locations) {
  402. const pref = cordovaProject.projectConfig.getPreference('BackgroundColor', 'ios') || '';
  403. const platformProjDir = path.relative(cordovaProject.root, locations.xcodeCordovaProj);
  404. const backgroundColorDir = getBackgroundColorDir(cordovaProject.root, platformProjDir);
  405. if (backgroundColorDir) {
  406. const contentsJSON = {
  407. colors: [{
  408. idiom: 'universal',
  409. color: colorPreferenceToComponents(pref)
  410. }],
  411. info: {
  412. author: 'Xcode',
  413. version: 1
  414. }
  415. };
  416. events.emit('verbose', 'Updating Background Color color set Contents.json');
  417. fs.writeFileSync(path.join(cordovaProject.root, backgroundColorDir, 'Contents.json'),
  418. JSON.stringify(contentsJSON, null, 2));
  419. }
  420. }
  421. /**
  422. * Resets the background color Contents.json in xcassets to default.
  423. *
  424. * @param {string} projectRoot Path to the project root
  425. * @param {Object} projectConfig The project's config.xml
  426. * @param {Object} locations A dictionary containing useful location paths
  427. */
  428. function cleanBackgroundColor (projectRoot, projectConfig, locations) {
  429. const platformProjDir = path.relative(projectRoot, locations.xcodeCordovaProj);
  430. const backgroundColorDir = getBackgroundColorDir(projectRoot, platformProjDir);
  431. if (backgroundColorDir) {
  432. const contentsJSON = {
  433. colors: [{
  434. idiom: 'universal',
  435. color: colorPreferenceToComponents(null)
  436. }],
  437. info: {
  438. author: 'Xcode',
  439. version: 1
  440. }
  441. };
  442. events.emit('verbose', 'Cleaning Background Color color set Contents.json');
  443. fs.writeFileSync(path.join(projectRoot, backgroundColorDir, 'Contents.json'),
  444. JSON.stringify(contentsJSON, null, 2));
  445. }
  446. }
  447. function updateFileResources (cordovaProject, locations) {
  448. const platformDir = path.relative(cordovaProject.root, locations.root);
  449. const files = cordovaProject.projectConfig.getFileResources('ios');
  450. const project = projectFile.parse(locations);
  451. // if there are resource-file elements in config.xml
  452. if (files.length === 0) {
  453. events.emit('verbose', 'This app does not have additional resource files defined');
  454. return;
  455. }
  456. const resourceMap = {};
  457. files.forEach(res => {
  458. const src = res.src;
  459. let target = res.target;
  460. if (!target) {
  461. target = src;
  462. }
  463. let targetPath = path.join(project.resources_dir, target);
  464. targetPath = path.relative(cordovaProject.root, targetPath);
  465. if (!fs.existsSync(targetPath)) {
  466. project.xcode.addResourceFile(target);
  467. } else {
  468. events.emit('warn', `Overwriting existing resource file at ${targetPath}`);
  469. }
  470. resourceMap[targetPath] = src;
  471. });
  472. events.emit('verbose', `Updating resource files at ${platformDir}`);
  473. FileUpdater.updatePaths(
  474. resourceMap, { rootDir: cordovaProject.root }, logFileOp);
  475. project.write();
  476. }
  477. function alertDeprecatedPreference (configParser) {
  478. const deprecatedToNewPreferences = {
  479. MediaPlaybackRequiresUserAction: {
  480. newPreference: 'MediaTypesRequiringUserActionForPlayback',
  481. isDeprecated: true
  482. },
  483. MediaPlaybackAllowsAirPlay: {
  484. newPreference: 'AllowsAirPlayForMediaPlayback',
  485. isDeprecated: false
  486. }
  487. };
  488. Object.keys(deprecatedToNewPreferences).forEach(oldKey => {
  489. if (configParser.getPreference(oldKey)) {
  490. const isDeprecated = deprecatedToNewPreferences[oldKey].isDeprecated;
  491. const verb = isDeprecated ? 'has been' : 'is being';
  492. const newPreferenceKey = deprecatedToNewPreferences[oldKey].newPreference;
  493. // Create the Log Message
  494. const log = [`The preference name "${oldKey}" ${verb} deprecated.`];
  495. if (newPreferenceKey) {
  496. log.push(`It is recommended to replace this preference with "${newPreferenceKey}."`);
  497. } else {
  498. log.push('There is no replacement for this preference.');
  499. }
  500. /**
  501. * If the preference has been deprecated, the usage of the old preference is no longer used.
  502. * Therefore, the following line is not appended. It is added only if the old preference is still used.
  503. * We are only keeping the top lines for deprecated items only for an additional major release when
  504. * the pre-warning was not provided in a past major release due to a necessary quick deprecation.
  505. * Typically caused by implementation nature or third-party requirement changes.
  506. */
  507. if (!isDeprecated) {
  508. log.push('Please note that this preference will be removed in the near future.');
  509. }
  510. events.emit('warn', log.join(' '));
  511. }
  512. });
  513. }
  514. function cleanFileResources (projectRoot, projectConfig, locations) {
  515. const platformDir = path.relative(projectRoot, locations.root);
  516. const files = projectConfig.getFileResources('ios', true);
  517. if (files.length > 0) {
  518. events.emit('verbose', `Cleaning resource files at ${platformDir}`);
  519. const project = projectFile.parse(locations);
  520. const resourceMap = {};
  521. files.forEach(res => {
  522. const src = res.src;
  523. let target = res.target;
  524. if (!target) {
  525. target = src;
  526. }
  527. let targetPath = path.join(project.resources_dir, target);
  528. targetPath = path.relative(projectRoot, targetPath);
  529. const resfile = path.join('Resources', path.basename(targetPath));
  530. project.xcode.removeResourceFile(resfile);
  531. resourceMap[targetPath] = null;
  532. });
  533. FileUpdater.updatePaths(
  534. resourceMap, { rootDir: projectRoot, all: true }, logFileOp);
  535. project.write();
  536. }
  537. }
  538. /**
  539. * Returns an array of images for each possible idiom, scale, and size class. The images themselves are
  540. * located in the platform's splash images by their pattern (@scale~idiom~sizesize). All possible
  541. * combinations are returned, but not all will have a `filename` property. If the latter isn't present,
  542. * the device won't attempt to load an image matching the same traits. If the filename is present,
  543. * the device will try to load the image if it corresponds to the traits.
  544. *
  545. * The resulting return looks like this:
  546. *
  547. * [
  548. * {
  549. * idiom: 'universal|ipad|iphone',
  550. * scale: '1x|2x|3x',
  551. * width: 'any|com',
  552. * height: 'any|com',
  553. * filename: undefined|'Default@scale~idiom~widthheight.png',
  554. * src: undefined|'path/to/original/matched/image/from/splash/screens.png',
  555. * target: undefined|'path/to/asset/library/Default@scale~idiom~widthheight.png',
  556. * appearence: undefined|'dark'|'light'
  557. * }, ...
  558. * ]
  559. *
  560. * @param {Array<Object>} splashScreens splash screens as defined in config.xml for this platform
  561. * @param {string} launchStoryboardImagesDir project-root/Images.xcassets/LaunchStoryboard.imageset/
  562. * @return {Array<Object>}
  563. */
  564. function mapLaunchStoryboardContents (splashScreens, launchStoryboardImagesDir) {
  565. const platformLaunchStoryboardImages = [];
  566. const idioms = ['universal', 'ipad', 'iphone'];
  567. const scalesForIdiom = {
  568. universal: ['1x', '2x', '3x'],
  569. ipad: ['1x', '2x'],
  570. iphone: ['1x', '2x', '3x']
  571. };
  572. const sizes = ['com', 'any'];
  573. const appearences = ['', 'dark', 'light'];
  574. idioms.forEach(idiom => {
  575. scalesForIdiom[idiom].forEach(scale => {
  576. sizes.forEach(width => {
  577. sizes.forEach(height => {
  578. appearences.forEach(appearence => {
  579. const item = { idiom, scale, width, height };
  580. if (appearence !== '') {
  581. item.appearence = appearence;
  582. }
  583. /* examples of the search pattern:
  584. * scale ~ idiom ~ width height ~ appearence
  585. * @2x ~ universal ~ any any
  586. * @3x ~ iphone ~ com any ~ dark
  587. * @2x ~ ipad ~ com any ~ light
  588. */
  589. const searchPattern = '@' + scale + '~' + idiom + '~' + width + height + (appearence ? '~' + appearence : '');
  590. /* because old node versions don't have Array.find, the below is
  591. * functionally equivalent to this:
  592. * var launchStoryboardImage = splashScreens.find(function(item) {
  593. * return (item.src.indexOf(searchPattern) >= 0) ? (appearence !== '' ? true : ((item.src.indexOf(searchPattern + '~light') >= 0 || (item.src.indexOf(searchPattern + '~dark') >= 0)) ? false : true)) : false;
  594. * });
  595. */
  596. const launchStoryboardImage = splashScreens.reduce(
  597. (p, c) => (c.src.indexOf(searchPattern) >= 0) ? (appearence !== '' ? c : ((c.src.indexOf(searchPattern + '~light') >= 0 || (c.src.indexOf(searchPattern + '~dark') >= 0)) ? p : c)) : p,
  598. undefined
  599. );
  600. if (launchStoryboardImage) {
  601. item.filename = `Default${searchPattern}.png`;
  602. item.src = launchStoryboardImage.src;
  603. item.target = path.join(launchStoryboardImagesDir, item.filename);
  604. }
  605. platformLaunchStoryboardImages.push(item);
  606. });
  607. });
  608. });
  609. });
  610. });
  611. return platformLaunchStoryboardImages;
  612. }
  613. /**
  614. * Returns a dictionary representing the source and destination paths for the launch storyboard images
  615. * that need to be copied.
  616. *
  617. * The resulting return looks like this:
  618. *
  619. * {
  620. * 'target-path': 'source-path',
  621. * ...
  622. * }
  623. *
  624. * @param {Array<Object>} splashScreens splash screens as defined in config.xml for this platform
  625. * @param {string} launchStoryboardImagesDir project-root/Images.xcassets/LaunchStoryboard.imageset/
  626. * @return {Object}
  627. */
  628. function mapLaunchStoryboardResources (splashScreens, launchStoryboardImagesDir) {
  629. const platformLaunchStoryboardImages = mapLaunchStoryboardContents(splashScreens, launchStoryboardImagesDir);
  630. const pathMap = {};
  631. platformLaunchStoryboardImages.forEach(item => {
  632. if (item.target) {
  633. pathMap[item.target] = item.src;
  634. }
  635. });
  636. return pathMap;
  637. }
  638. /**
  639. * Builds the object that represents the contents.json file for the LaunchStoryboard image set.
  640. *
  641. * The resulting return looks like this:
  642. *
  643. * {
  644. * images: [
  645. * {
  646. * idiom: 'universal|ipad|iphone',
  647. * scale: '1x|2x|3x',
  648. * width-class: undefined|'compact',
  649. * height-class: undefined|'compact'
  650. * ...
  651. * }, ...
  652. * ],
  653. * info: {
  654. * author: 'Xcode',
  655. * version: 1
  656. * }
  657. * }
  658. *
  659. * A bit of minor logic is used to map from the array of images returned from mapLaunchStoryboardContents
  660. * to the format requried by Xcode.
  661. *
  662. * @param {Array<Object>} splashScreens splash screens as defined in config.xml for this platform
  663. * @param {string} launchStoryboardImagesDir project-root/Images.xcassets/LaunchStoryboard.imageset/
  664. * @return {Object}
  665. */
  666. function getLaunchStoryboardContentsJSON (splashScreens, launchStoryboardImagesDir) {
  667. const platformLaunchStoryboardImages = mapLaunchStoryboardContents(splashScreens, launchStoryboardImagesDir);
  668. const contentsJSON = {
  669. images: [],
  670. info: {
  671. author: 'Xcode',
  672. version: 1
  673. }
  674. };
  675. contentsJSON.images = platformLaunchStoryboardImages.map(item => {
  676. const newItem = {
  677. idiom: item.idiom,
  678. scale: item.scale
  679. };
  680. // Xcode doesn't want any size class property if the class is "any"
  681. // If our size class is "com", Xcode wants "compact".
  682. if (item.width !== CDV_ANY_SIZE_CLASS) {
  683. newItem['width-class'] = IMAGESET_COMPACT_SIZE_CLASS;
  684. }
  685. if (item.height !== CDV_ANY_SIZE_CLASS) {
  686. newItem['height-class'] = IMAGESET_COMPACT_SIZE_CLASS;
  687. }
  688. if (item.appearence) {
  689. newItem.appearances = [{ appearance: 'luminosity', value: item.appearence }];
  690. }
  691. // Xcode doesn't want a filename property if there's no image for these traits
  692. if (item.filename) {
  693. newItem.filename = item.filename;
  694. }
  695. return newItem;
  696. });
  697. return contentsJSON;
  698. }
  699. /**
  700. * Returns the directory for the Launch Storyboard image set, if image sets are being used. If they aren't
  701. * being used, returns null.
  702. *
  703. * @param {string} projectRoot The project's root directory
  704. * @param {string} platformProjDir The platform's project directory
  705. */
  706. function getLaunchStoryboardImagesDir (projectRoot, platformProjDir) {
  707. let launchStoryboardImagesDir;
  708. const xcassetsExists = folderExists(path.join(projectRoot, platformProjDir, 'Images.xcassets/'));
  709. if (xcassetsExists) {
  710. launchStoryboardImagesDir = path.join(platformProjDir, 'Images.xcassets/LaunchStoryboard.imageset/');
  711. } else {
  712. // if we don't have a asset library for images, we can't do the storyboard.
  713. launchStoryboardImagesDir = null;
  714. }
  715. return launchStoryboardImagesDir;
  716. }
  717. /**
  718. * Update the images for the Launch Storyboard and updates the image set's contents.json file appropriately.
  719. *
  720. * @param {Object} cordovaProject The cordova project
  721. * @param {Object} locations A dictionary containing useful location paths
  722. */
  723. function updateLaunchStoryboardImages (cordovaProject, locations) {
  724. const splashScreens = cordovaProject.projectConfig.getSplashScreens('ios');
  725. const platformProjDir = path.relative(cordovaProject.root, locations.xcodeCordovaProj);
  726. const launchStoryboardImagesDir = getLaunchStoryboardImagesDir(cordovaProject.root, platformProjDir);
  727. if (launchStoryboardImagesDir) {
  728. const resourceMap = mapLaunchStoryboardResources(splashScreens, launchStoryboardImagesDir);
  729. const contentsJSON = getLaunchStoryboardContentsJSON(splashScreens, launchStoryboardImagesDir);
  730. events.emit('verbose', `Updating launch storyboard images at ${launchStoryboardImagesDir}`);
  731. FileUpdater.updatePaths(
  732. resourceMap, { rootDir: cordovaProject.root }, logFileOp);
  733. events.emit('verbose', 'Updating Storyboard image set contents.json');
  734. fs.writeFileSync(path.join(cordovaProject.root, launchStoryboardImagesDir, 'Contents.json'),
  735. JSON.stringify(contentsJSON, null, 2));
  736. }
  737. }
  738. /**
  739. * Removes the images from the launch storyboard's image set and updates the image set's contents.json
  740. * file appropriately.
  741. *
  742. * @param {string} projectRoot Path to the project root
  743. * @param {Object} projectConfig The project's config.xml
  744. * @param {Object} locations A dictionary containing useful location paths
  745. */
  746. function cleanLaunchStoryboardImages (projectRoot, projectConfig, locations) {
  747. const splashScreens = projectConfig.getSplashScreens('ios');
  748. const platformProjDir = path.relative(projectRoot, locations.xcodeCordovaProj);
  749. const launchStoryboardImagesDir = getLaunchStoryboardImagesDir(projectRoot, platformProjDir);
  750. if (launchStoryboardImagesDir) {
  751. const resourceMap = mapLaunchStoryboardResources(splashScreens, launchStoryboardImagesDir);
  752. const contentsJSON = getLaunchStoryboardContentsJSON(splashScreens, launchStoryboardImagesDir);
  753. Object.keys(resourceMap).forEach(targetPath => {
  754. resourceMap[targetPath] = null;
  755. });
  756. events.emit('verbose', `Cleaning storyboard image set at ${launchStoryboardImagesDir}`);
  757. // Source paths are removed from the map, so updatePaths() will delete the target files.
  758. FileUpdater.updatePaths(
  759. resourceMap, { rootDir: projectRoot, all: true }, logFileOp);
  760. // delete filename from contents.json
  761. contentsJSON.images.forEach(image => {
  762. image.filename = undefined;
  763. });
  764. events.emit('verbose', 'Updating Storyboard image set contents.json');
  765. fs.writeFileSync(path.join(projectRoot, launchStoryboardImagesDir, 'Contents.json'),
  766. JSON.stringify(contentsJSON, null, 2));
  767. }
  768. }
  769. /**
  770. * Queries ConfigParser object for the orientation <preference> value. Warns if
  771. * global preference value is not supported by platform.
  772. *
  773. * @param {Object} platformConfig ConfigParser object
  774. *
  775. * @return {String} Global/platform-specific orientation in lower-case
  776. * (or empty string if both are undefined).
  777. */
  778. function getOrientationValue (platformConfig) {
  779. const ORIENTATION_DEFAULT = 'default';
  780. let orientation = platformConfig.getPreference('orientation');
  781. if (!orientation) {
  782. return '';
  783. }
  784. orientation = orientation.toLowerCase();
  785. // Check if the given global orientation is supported
  786. if (['default', 'portrait', 'landscape', 'all'].indexOf(orientation) >= 0) {
  787. return orientation;
  788. }
  789. events.emit('warn', `Unrecognized value for Orientation preference: ${orientation}. Defaulting to value: ${ORIENTATION_DEFAULT}.`);
  790. return ORIENTATION_DEFAULT;
  791. }
  792. /*
  793. Parses all <access> and <allow-navigation> entries and consolidates duplicates (for ATS).
  794. Returns an object with a Hostname as the key, and the value an object with properties:
  795. {
  796. Hostname, // String
  797. NSExceptionAllowsInsecureHTTPLoads, // boolean
  798. NSIncludesSubdomains, // boolean
  799. NSExceptionMinimumTLSVersion, // String
  800. NSExceptionRequiresForwardSecrecy, // boolean
  801. NSRequiresCertificateTransparency, // boolean
  802. // the three below _only_ show when the Hostname is '*'
  803. // if any of the three are set, it disables setting NSAllowsArbitraryLoads
  804. // (Apple already enforces this in ATS)
  805. NSAllowsArbitraryLoadsInWebContent, // boolean (default: false)
  806. NSAllowsLocalNetworking, // boolean (default: false)
  807. NSAllowsArbitraryLoadsForMedia, // boolean (default:false)
  808. }
  809. */
  810. function processAccessAndAllowNavigationEntries (config) {
  811. const accesses = config.getAccesses();
  812. const allow_navigations = config.getAllowNavigations();
  813. return allow_navigations
  814. // we concat allow_navigations and accesses, after processing accesses
  815. .concat(accesses.map(obj => {
  816. // map accesses to a common key interface using 'href', not origin
  817. obj.href = obj.origin;
  818. delete obj.origin;
  819. return obj;
  820. }))
  821. // we reduce the array to an object with all the entries processed (key is Hostname)
  822. .reduce((previousReturn, currentElement) => {
  823. const options = {
  824. minimum_tls_version: currentElement.minimum_tls_version,
  825. requires_forward_secrecy: currentElement.requires_forward_secrecy,
  826. requires_certificate_transparency: currentElement.requires_certificate_transparency,
  827. allows_arbitrary_loads_for_media: currentElement.allows_arbitrary_loads_in_media || currentElement.allows_arbitrary_loads_for_media,
  828. allows_arbitrary_loads_in_web_content: currentElement.allows_arbitrary_loads_in_web_content,
  829. allows_local_networking: currentElement.allows_local_networking
  830. };
  831. const obj = parseWhitelistUrlForATS(currentElement.href, options);
  832. if (obj) {
  833. // we 'union' duplicate entries
  834. let item = previousReturn[obj.Hostname];
  835. if (!item) {
  836. item = {};
  837. }
  838. for (const o in obj) {
  839. if (Object.prototype.hasOwnProperty.call(obj, o)) {
  840. item[o] = obj[o];
  841. }
  842. }
  843. previousReturn[obj.Hostname] = item;
  844. }
  845. return previousReturn;
  846. }, {});
  847. }
  848. /*
  849. Parses a URL and returns an object with these keys:
  850. {
  851. Hostname, // String
  852. NSExceptionAllowsInsecureHTTPLoads, // boolean (default: false)
  853. NSIncludesSubdomains, // boolean (default: false)
  854. NSExceptionMinimumTLSVersion, // String (default: 'TLSv1.2')
  855. NSExceptionRequiresForwardSecrecy, // boolean (default: true)
  856. NSRequiresCertificateTransparency, // boolean (default: false)
  857. // the three below _only_ apply when the Hostname is '*'
  858. // if any of the three are set, it disables setting NSAllowsArbitraryLoads
  859. // (Apple already enforces this in ATS)
  860. NSAllowsArbitraryLoadsInWebContent, // boolean (default: false)
  861. NSAllowsLocalNetworking, // boolean (default: false)
  862. NSAllowsArbitraryLoadsForMedia, // boolean (default:false)
  863. }
  864. null is returned if the URL cannot be parsed, or is to be skipped for ATS.
  865. */
  866. function parseWhitelistUrlForATS (url, options) {
  867. // @todo 'url.parse' was deprecated since v11.0.0. Use 'url.URL' constructor instead.
  868. const href = URL.parse(url); // eslint-disable-line
  869. const retObj = {};
  870. retObj.Hostname = href.hostname;
  871. // Guiding principle: we only set values in retObj if they are NOT the default
  872. if (url === '*') {
  873. retObj.Hostname = '*';
  874. let val;
  875. val = (options.allows_arbitrary_loads_in_web_content === 'true');
  876. if (options.allows_arbitrary_loads_in_web_content && val) { // default is false
  877. retObj.NSAllowsArbitraryLoadsInWebContent = true;
  878. }
  879. val = (options.allows_arbitrary_loads_for_media === 'true');
  880. if (options.allows_arbitrary_loads_for_media && val) { // default is false
  881. retObj.NSAllowsArbitraryLoadsForMedia = true;
  882. }
  883. val = (options.allows_local_networking === 'true');
  884. if (options.allows_local_networking && val) { // default is false
  885. retObj.NSAllowsLocalNetworking = true;
  886. }
  887. return retObj;
  888. }
  889. if (!retObj.Hostname) {
  890. // check origin, if it allows subdomains (wildcard in hostname), we set NSIncludesSubdomains to YES. Default is NO
  891. const subdomain1 = '/*.'; // wildcard in hostname
  892. const subdomain2 = '*://*.'; // wildcard in hostname and protocol
  893. const subdomain3 = '*://'; // wildcard in protocol only
  894. if (!href.pathname) {
  895. return null;
  896. } else if (href.pathname.indexOf(subdomain1) === 0) {
  897. retObj.NSIncludesSubdomains = true;
  898. retObj.Hostname = href.pathname.substring(subdomain1.length);
  899. } else if (href.pathname.indexOf(subdomain2) === 0) {
  900. retObj.NSIncludesSubdomains = true;
  901. retObj.Hostname = href.pathname.substring(subdomain2.length);
  902. } else if (href.pathname.indexOf(subdomain3) === 0) {
  903. retObj.Hostname = href.pathname.substring(subdomain3.length);
  904. } else {
  905. // Handling "scheme:*" case to avoid creating of a blank key in NSExceptionDomains.
  906. return null;
  907. }
  908. }
  909. if (options.minimum_tls_version && options.minimum_tls_version !== 'TLSv1.2') { // default is TLSv1.2
  910. retObj.NSExceptionMinimumTLSVersion = options.minimum_tls_version;
  911. }
  912. const rfs = (options.requires_forward_secrecy === 'true');
  913. if (options.requires_forward_secrecy && !rfs) { // default is true
  914. retObj.NSExceptionRequiresForwardSecrecy = false;
  915. }
  916. const rct = (options.requires_certificate_transparency === 'true');
  917. if (options.requires_certificate_transparency && rct) { // default is false
  918. retObj.NSRequiresCertificateTransparency = true;
  919. }
  920. // if the scheme is HTTP, we set NSExceptionAllowsInsecureHTTPLoads to YES. Default is NO
  921. if (href.protocol === 'http:') {
  922. retObj.NSExceptionAllowsInsecureHTTPLoads = true;
  923. } else if (!href.protocol && href.pathname.indexOf('*:/') === 0) { // wilcard in protocol
  924. retObj.NSExceptionAllowsInsecureHTTPLoads = true;
  925. }
  926. return retObj;
  927. }
  928. /*
  929. App Transport Security (ATS) writer from <access> and <allow-navigation> tags
  930. in config.xml
  931. */
  932. function writeATSEntries (config) {
  933. const pObj = processAccessAndAllowNavigationEntries(config);
  934. const ats = {};
  935. for (const hostname in pObj) {
  936. if (Object.prototype.hasOwnProperty.call(pObj, hostname)) {
  937. const entry = pObj[hostname];
  938. // Guiding principle: we only set values if they are available
  939. if (hostname === '*') {
  940. // always write this, for iOS 9, since in iOS 10 it will be overriden if
  941. // any of the other three keys are written
  942. ats.NSAllowsArbitraryLoads = true;
  943. // at least one of the overriding keys is present
  944. if (entry.NSAllowsArbitraryLoadsInWebContent) {
  945. ats.NSAllowsArbitraryLoadsInWebContent = true;
  946. }
  947. if (entry.NSAllowsArbitraryLoadsForMedia) {
  948. ats.NSAllowsArbitraryLoadsForMedia = true;
  949. }
  950. if (entry.NSAllowsLocalNetworking) {
  951. ats.NSAllowsLocalNetworking = true;
  952. }
  953. continue;
  954. }
  955. const exceptionDomain = {};
  956. for (const key in entry) {
  957. if (Object.prototype.hasOwnProperty.call(entry, key) && key !== 'Hostname') {
  958. exceptionDomain[key] = entry[key];
  959. }
  960. }
  961. if (!ats.NSExceptionDomains) {
  962. ats.NSExceptionDomains = {};
  963. }
  964. ats.NSExceptionDomains[hostname] = exceptionDomain;
  965. }
  966. }
  967. return ats;
  968. }
  969. function folderExists (folderPath) {
  970. try {
  971. const stat = fs.statSync(folderPath);
  972. return stat && stat.isDirectory();
  973. } catch (e) {
  974. return false;
  975. }
  976. }
  977. // Construct a default value for CFBundleVersion as the version with any
  978. // -rclabel stripped=.
  979. function default_CFBundleVersion (version) {
  980. return version.split('-')[0];
  981. }
  982. // Converts cordova specific representation of target device to XCode value
  983. function parseTargetDevicePreference (value) {
  984. if (!value) return null;
  985. const map = { universal: '"1,2"', handset: '"1"', tablet: '"2"' };
  986. if (map[value.toLowerCase()]) {
  987. return map[value.toLowerCase()];
  988. }
  989. events.emit('warn', `Unrecognized value for target-device preference: ${value}.`);
  990. return null;
  991. }