prepare.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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. /* eslint no-useless-escape: 0 */
  18. var Q = require('q');
  19. var fs = require('fs');
  20. var path = require('path');
  21. var shell = require('shelljs');
  22. var events = require('cordova-common').events;
  23. var AndroidManifest = require('./AndroidManifest');
  24. var checkReqs = require('./check_reqs');
  25. var xmlHelpers = require('cordova-common').xmlHelpers;
  26. var CordovaError = require('cordova-common').CordovaError;
  27. var ConfigParser = require('cordova-common').ConfigParser;
  28. var FileUpdater = require('cordova-common').FileUpdater;
  29. var PlatformJson = require('cordova-common').PlatformJson;
  30. var PlatformMunger = require('cordova-common').ConfigChanges.PlatformMunger;
  31. var PluginInfoProvider = require('cordova-common').PluginInfoProvider;
  32. module.exports.prepare = function (cordovaProject, options) {
  33. var self = this;
  34. var platformJson = PlatformJson.load(this.locations.root, this.platform);
  35. var munger = new PlatformMunger(this.platform, this.locations.root, platformJson, new PluginInfoProvider());
  36. this._config = updateConfigFilesFrom(cordovaProject.projectConfig, munger, this.locations);
  37. // Update own www dir with project's www assets and plugins' assets and js-files
  38. return Q.when(updateWww(cordovaProject, this.locations)).then(function () {
  39. // update project according to config.xml changes.
  40. return updateProjectAccordingTo(self._config, self.locations);
  41. }).then(function () {
  42. updateIcons(cordovaProject, path.relative(cordovaProject.root, self.locations.res));
  43. updateSplashes(cordovaProject, path.relative(cordovaProject.root, self.locations.res));
  44. updateFileResources(cordovaProject, path.relative(cordovaProject.root, self.locations.root));
  45. }).then(function () {
  46. events.emit('verbose', 'Prepared android project successfully');
  47. });
  48. };
  49. module.exports.clean = function (options) {
  50. // A cordovaProject isn't passed into the clean() function, because it might have
  51. // been called from the platform shell script rather than the CLI. Check for the
  52. // noPrepare option passed in by the non-CLI clean script. If that's present, or if
  53. // there's no config.xml found at the project root, then don't clean prepared files.
  54. var projectRoot = path.resolve(this.root, '../..');
  55. if ((options && options.noPrepare) || !fs.existsSync(this.locations.configXml) ||
  56. !fs.existsSync(this.locations.configXml)) {
  57. return Q();
  58. }
  59. var projectConfig = new ConfigParser(this.locations.configXml);
  60. var self = this;
  61. return Q().then(function () {
  62. cleanWww(projectRoot, self.locations);
  63. cleanIcons(projectRoot, projectConfig, path.relative(projectRoot, self.locations.res));
  64. cleanSplashes(projectRoot, projectConfig, path.relative(projectRoot, self.locations.res));
  65. cleanFileResources(projectRoot, projectConfig, path.relative(projectRoot, self.locations.root));
  66. });
  67. };
  68. /**
  69. * Updates config files in project based on app's config.xml and config munge,
  70. * generated by plugins.
  71. *
  72. * @param {ConfigParser} sourceConfig A project's configuration that will
  73. * be merged into platform's config.xml
  74. * @param {ConfigChanges} configMunger An initialized ConfigChanges instance
  75. * for this platform.
  76. * @param {Object} locations A map of locations for this platform
  77. *
  78. * @return {ConfigParser} An instance of ConfigParser, that
  79. * represents current project's configuration. When returned, the
  80. * configuration is already dumped to appropriate config.xml file.
  81. */
  82. function updateConfigFilesFrom (sourceConfig, configMunger, locations) {
  83. events.emit('verbose', 'Generating platform-specific config.xml from defaults for android at ' + locations.configXml);
  84. // First cleanup current config and merge project's one into own
  85. // Overwrite platform config.xml with defaults.xml.
  86. shell.cp('-f', locations.defaultConfigXml, locations.configXml);
  87. // Then apply config changes from global munge to all config files
  88. // in project (including project's config)
  89. configMunger.reapply_global_munge().save_all();
  90. events.emit('verbose', 'Merging project\'s config.xml into platform-specific android config.xml');
  91. // Merge changes from app's config.xml into platform's one
  92. var config = new ConfigParser(locations.configXml);
  93. xmlHelpers.mergeXml(sourceConfig.doc.getroot(),
  94. config.doc.getroot(), 'android', /* clobber= */true);
  95. config.write();
  96. return config;
  97. }
  98. /**
  99. * Logs all file operations via the verbose event stream, indented.
  100. */
  101. function logFileOp (message) {
  102. events.emit('verbose', ' ' + message);
  103. }
  104. /**
  105. * Updates platform 'www' directory by replacing it with contents of
  106. * 'platform_www' and app www. Also copies project's overrides' folder into
  107. * the platform 'www' folder
  108. *
  109. * @param {Object} cordovaProject An object which describes cordova project.
  110. * @param {Object} destinations An object that contains destination
  111. * paths for www files.
  112. */
  113. function updateWww (cordovaProject, destinations) {
  114. var sourceDirs = [
  115. path.relative(cordovaProject.root, cordovaProject.locations.www),
  116. path.relative(cordovaProject.root, destinations.platformWww)
  117. ];
  118. // If project contains 'merges' for our platform, use them as another overrides
  119. var merges_path = path.join(cordovaProject.root, 'merges', 'android');
  120. if (fs.existsSync(merges_path)) {
  121. events.emit('verbose', 'Found "merges/android" folder. Copying its contents into the android project.');
  122. sourceDirs.push(path.join('merges', 'android'));
  123. }
  124. var targetDir = path.relative(cordovaProject.root, destinations.www);
  125. events.emit(
  126. 'verbose', 'Merging and updating files from [' + sourceDirs.join(', ') + '] to ' + targetDir);
  127. FileUpdater.mergeAndUpdateDir(
  128. sourceDirs, targetDir, { rootDir: cordovaProject.root }, logFileOp);
  129. }
  130. /**
  131. * Cleans all files from the platform 'www' directory.
  132. */
  133. function cleanWww (projectRoot, locations) {
  134. var targetDir = path.relative(projectRoot, locations.www);
  135. events.emit('verbose', 'Cleaning ' + targetDir);
  136. // No source paths are specified, so mergeAndUpdateDir() will clear the target directory.
  137. FileUpdater.mergeAndUpdateDir(
  138. [], targetDir, { rootDir: projectRoot, all: true }, logFileOp);
  139. }
  140. /**
  141. * Updates project structure and AndroidManifest according to project's configuration.
  142. *
  143. * @param {ConfigParser} platformConfig A project's configuration that will
  144. * be used to update project
  145. * @param {Object} locations A map of locations for this platform
  146. */
  147. function updateProjectAccordingTo (platformConfig, locations) {
  148. // Update app name by editing res/values/strings.xml
  149. var strings = xmlHelpers.parseElementtreeSync(locations.strings);
  150. var name = platformConfig.name();
  151. strings.find('string[@name="app_name"]').text = name.replace(/\'/g, '\\\'');
  152. var shortName = platformConfig.shortName && platformConfig.shortName();
  153. if (shortName && shortName !== name) {
  154. strings.find('string[@name="launcher_name"]').text = shortName.replace(/\'/g, '\\\'');
  155. }
  156. fs.writeFileSync(locations.strings, strings.write({indent: 4}), 'utf-8');
  157. events.emit('verbose', 'Wrote out android application name "' + name + '" to ' + locations.strings);
  158. // Java packages cannot support dashes
  159. var androidPkgName = (platformConfig.android_packageName() || platformConfig.packageName()).replace(/-/g, '_');
  160. var manifest = new AndroidManifest(locations.manifest);
  161. var manifestId = manifest.getPackageId();
  162. manifest.getActivity()
  163. .setOrientation(platformConfig.getPreference('orientation'))
  164. .setLaunchMode(findAndroidLaunchModePreference(platformConfig));
  165. manifest.setVersionName(platformConfig.version())
  166. .setVersionCode(platformConfig.android_versionCode() || default_versionCode(platformConfig.version()))
  167. .setPackageId(androidPkgName)
  168. .setMinSdkVersion(platformConfig.getPreference('android-minSdkVersion', 'android'))
  169. .setMaxSdkVersion(platformConfig.getPreference('android-maxSdkVersion', 'android'))
  170. .setTargetSdkVersion(platformConfig.getPreference('android-targetSdkVersion', 'android'))
  171. .write();
  172. // Java file paths shouldn't be hard coded
  173. var javaPattern = path.join(locations.javaSrc, manifestId.replace(/\./g, '/'), '*.java');
  174. var java_files = shell.ls(javaPattern).filter(function (f) {
  175. return shell.grep(/extends\s+CordovaActivity/g, f);
  176. });
  177. if (java_files.length === 0) {
  178. throw new CordovaError('No Java files found that extend CordovaActivity.');
  179. } else if (java_files.length > 1) {
  180. events.emit('log', 'Multiple candidate Java files that extend CordovaActivity found. Guessing at the first one, ' + java_files[0]);
  181. }
  182. var destFile = path.join(locations.root, 'app', 'src', 'main', 'java', androidPkgName.replace(/\./g, '/'), path.basename(java_files[0]));
  183. shell.mkdir('-p', path.dirname(destFile));
  184. shell.sed(/package [\w\.]*;/, 'package ' + androidPkgName + ';', java_files[0]).to(destFile);
  185. events.emit('verbose', 'Wrote out Android package name "' + androidPkgName + '" to ' + destFile);
  186. var removeOrigPkg = checkReqs.isWindows() || checkReqs.isDarwin() ?
  187. manifestId.toUpperCase() !== androidPkgName.toUpperCase() :
  188. manifestId !== androidPkgName;
  189. if (removeOrigPkg) {
  190. // If package was name changed we need to remove old java with main activity
  191. shell.rm('-Rf', java_files[0]);
  192. // remove any empty directories
  193. var currentDir = path.dirname(java_files[0]);
  194. var sourcesRoot = path.resolve(locations.root, 'src');
  195. while (currentDir !== sourcesRoot) {
  196. if (fs.existsSync(currentDir) && fs.readdirSync(currentDir).length === 0) {
  197. fs.rmdirSync(currentDir);
  198. currentDir = path.resolve(currentDir, '..');
  199. } else {
  200. break;
  201. }
  202. }
  203. }
  204. }
  205. // Consturct the default value for versionCode as
  206. // PATCH + MINOR * 100 + MAJOR * 10000
  207. // see http://developer.android.com/tools/publishing/versioning.html
  208. function default_versionCode (version) {
  209. var nums = version.split('-')[0].split('.');
  210. var versionCode = 0;
  211. if (+nums[0]) {
  212. versionCode += +nums[0] * 10000;
  213. }
  214. if (+nums[1]) {
  215. versionCode += +nums[1] * 100;
  216. }
  217. if (+nums[2]) {
  218. versionCode += +nums[2];
  219. }
  220. events.emit('verbose', 'android-versionCode not found in config.xml. Generating a code based on version in config.xml (' + version + '): ' + versionCode);
  221. return versionCode;
  222. }
  223. function getImageResourcePath (resourcesDir, type, density, name, sourceName) {
  224. if (/\.9\.png$/.test(sourceName)) {
  225. name = name.replace(/\.png$/, '.9.png');
  226. }
  227. var resourcePath = path.join(resourcesDir, (density ? type + '-' + density : type), name);
  228. return resourcePath;
  229. }
  230. function updateSplashes (cordovaProject, platformResourcesDir) {
  231. var resources = cordovaProject.projectConfig.getSplashScreens('android');
  232. // if there are "splash" elements in config.xml
  233. if (resources.length === 0) {
  234. events.emit('verbose', 'This app does not have splash screens defined');
  235. return;
  236. }
  237. var resourceMap = mapImageResources(cordovaProject.root, platformResourcesDir, 'drawable', 'screen.png');
  238. var hadMdpi = false;
  239. resources.forEach(function (resource) {
  240. if (!resource.density) {
  241. return;
  242. }
  243. if (resource.density === 'mdpi') {
  244. hadMdpi = true;
  245. }
  246. var targetPath = getImageResourcePath(
  247. platformResourcesDir, 'drawable', resource.density, 'screen.png', path.basename(resource.src));
  248. resourceMap[targetPath] = resource.src;
  249. });
  250. // There's no "default" drawable, so assume default == mdpi.
  251. if (!hadMdpi && resources.defaultResource) {
  252. var targetPath = getImageResourcePath(
  253. platformResourcesDir, 'drawable', 'mdpi', 'screen.png', path.basename(resources.defaultResource.src));
  254. resourceMap[targetPath] = resources.defaultResource.src;
  255. }
  256. events.emit('verbose', 'Updating splash screens at ' + platformResourcesDir);
  257. FileUpdater.updatePaths(
  258. resourceMap, { rootDir: cordovaProject.root }, logFileOp);
  259. }
  260. function cleanSplashes (projectRoot, projectConfig, platformResourcesDir) {
  261. var resources = projectConfig.getSplashScreens('android');
  262. if (resources.length > 0) {
  263. var resourceMap = mapImageResources(projectRoot, platformResourcesDir, 'drawable', 'screen.png');
  264. events.emit('verbose', 'Cleaning splash screens at ' + platformResourcesDir);
  265. // No source paths are specified in the map, so updatePaths() will delete the target files.
  266. FileUpdater.updatePaths(
  267. resourceMap, { rootDir: projectRoot, all: true }, logFileOp);
  268. }
  269. }
  270. function updateIcons (cordovaProject, platformResourcesDir) {
  271. var icons = cordovaProject.projectConfig.getIcons('android');
  272. // if there are icon elements in config.xml
  273. if (icons.length === 0) {
  274. events.emit('verbose', 'This app does not have launcher icons defined');
  275. return;
  276. }
  277. var resourceMap = mapImageResources(cordovaProject.root, platformResourcesDir, 'mipmap', 'icon.png');
  278. var android_icons = {};
  279. var default_icon;
  280. // http://developer.android.com/design/style/iconography.html
  281. var sizeToDensityMap = {
  282. 36: 'ldpi',
  283. 48: 'mdpi',
  284. 72: 'hdpi',
  285. 96: 'xhdpi',
  286. 144: 'xxhdpi',
  287. 192: 'xxxhdpi'
  288. };
  289. // find the best matching icon for a given density or size
  290. // @output android_icons
  291. var parseIcon = function (icon, icon_size) {
  292. // do I have a platform icon for that density already
  293. var density = icon.density || sizeToDensityMap[icon_size];
  294. if (!density) {
  295. // invalid icon defition ( or unsupported size)
  296. return;
  297. }
  298. var previous = android_icons[density];
  299. if (previous && previous.platform) {
  300. return;
  301. }
  302. android_icons[density] = icon;
  303. };
  304. // iterate over all icon elements to find the default icon and call parseIcon
  305. for (var i = 0; i < icons.length; i++) {
  306. var icon = icons[i];
  307. var size = icon.width;
  308. if (!size) {
  309. size = icon.height;
  310. }
  311. if (!size && !icon.density) {
  312. if (default_icon) {
  313. events.emit('verbose', 'Found extra default icon: ' + icon.src + ' (ignoring in favor of ' + default_icon.src + ')');
  314. } else {
  315. default_icon = icon;
  316. }
  317. } else {
  318. parseIcon(icon, size);
  319. }
  320. }
  321. // The source paths for icons and splashes are relative to
  322. // project's config.xml location, so we use it as base path.
  323. for (var density in android_icons) {
  324. var targetPath = getImageResourcePath(
  325. platformResourcesDir, 'mipmap', density, 'icon.png', path.basename(android_icons[density].src));
  326. resourceMap[targetPath] = android_icons[density].src;
  327. }
  328. // There's no "default" drawable, so assume default == mdpi.
  329. if (default_icon && !android_icons.mdpi) {
  330. var defaultTargetPath = getImageResourcePath(
  331. platformResourcesDir, 'mipmap', 'mdpi', 'icon.png', path.basename(default_icon.src));
  332. resourceMap[defaultTargetPath] = default_icon.src;
  333. }
  334. events.emit('verbose', 'Updating icons at ' + platformResourcesDir);
  335. FileUpdater.updatePaths(
  336. resourceMap, { rootDir: cordovaProject.root }, logFileOp);
  337. }
  338. function cleanIcons (projectRoot, projectConfig, platformResourcesDir) {
  339. var icons = projectConfig.getIcons('android');
  340. if (icons.length > 0) {
  341. var resourceMap = mapImageResources(projectRoot, platformResourcesDir, 'mipmap', 'icon.png');
  342. events.emit('verbose', 'Cleaning icons at ' + platformResourcesDir);
  343. // No source paths are specified in the map, so updatePaths() will delete the target files.
  344. FileUpdater.updatePaths(
  345. resourceMap, { rootDir: projectRoot, all: true }, logFileOp);
  346. }
  347. }
  348. /**
  349. * Gets a map containing resources of a specified name from all drawable folders in a directory.
  350. */
  351. function mapImageResources (rootDir, subDir, type, resourceName) {
  352. var pathMap = {};
  353. shell.ls(path.join(rootDir, subDir, type + '-*')).forEach(function (drawableFolder) {
  354. var imagePath = path.join(subDir, path.basename(drawableFolder), resourceName);
  355. pathMap[imagePath] = null;
  356. });
  357. return pathMap;
  358. }
  359. function updateFileResources (cordovaProject, platformDir) {
  360. var files = cordovaProject.projectConfig.getFileResources('android');
  361. // if there are resource-file elements in config.xml
  362. if (files.length === 0) {
  363. events.emit('verbose', 'This app does not have additional resource files defined');
  364. return;
  365. }
  366. var resourceMap = {};
  367. files.forEach(function (res) {
  368. var targetPath = path.join(platformDir, res.target);
  369. resourceMap[targetPath] = res.src;
  370. });
  371. events.emit('verbose', 'Updating resource files at ' + platformDir);
  372. FileUpdater.updatePaths(
  373. resourceMap, { rootDir: cordovaProject.root }, logFileOp);
  374. }
  375. function cleanFileResources (projectRoot, projectConfig, platformDir) {
  376. var files = projectConfig.getFileResources('android', true);
  377. if (files.length > 0) {
  378. events.emit('verbose', 'Cleaning resource files at ' + platformDir);
  379. var resourceMap = {};
  380. files.forEach(function (res) {
  381. var filePath = path.join(platformDir, res.target);
  382. resourceMap[filePath] = null;
  383. });
  384. FileUpdater.updatePaths(
  385. resourceMap, {
  386. rootDir: projectRoot, all: true}, logFileOp);
  387. }
  388. }
  389. /**
  390. * Gets and validates 'AndroidLaunchMode' prepference from config.xml. Returns
  391. * preference value and warns if it doesn't seems to be valid
  392. *
  393. * @param {ConfigParser} platformConfig A configParser instance for
  394. * platform.
  395. *
  396. * @return {String} Preference's value from config.xml or
  397. * default value, if there is no such preference. The default value is
  398. * 'singleTop'
  399. */
  400. function findAndroidLaunchModePreference (platformConfig) {
  401. var launchMode = platformConfig.getPreference('AndroidLaunchMode');
  402. if (!launchMode) {
  403. // Return a default value
  404. return 'singleTop';
  405. }
  406. var expectedValues = ['standard', 'singleTop', 'singleTask', 'singleInstance'];
  407. var valid = expectedValues.indexOf(launchMode) >= 0;
  408. if (!valid) {
  409. // Note: warn, but leave the launch mode as developer wanted, in case the list of options changes in the future
  410. events.emit('warn', 'Unrecognized value for AndroidLaunchMode preference: ' +
  411. launchMode + '. Expected values are: ' + expectedValues.join(', '));
  412. }
  413. return launchMode;
  414. }