update_config.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. #!/usr/bin/env node
  2. module.exports = function(context) {
  3. var ConfigParser, XmlHelpers;
  4. try {
  5. // cordova-lib >= 5.3.4 doesn't contain ConfigParser and xml-helpers anymore
  6. ConfigParser = context.requireCordovaModule("cordova-common").ConfigParser;
  7. XmlHelpers = context.requireCordovaModule("cordova-common").xmlHelpers;
  8. } catch (e) {
  9. ConfigParser = context.requireCordovaModule("cordova-lib/src/configparser/ConfigParser");
  10. XmlHelpers = context.requireCordovaModule("cordova-lib/src/util/xml-helpers");
  11. }
  12. /** @external */
  13. var fs = context.requireCordovaModule('fs'),
  14. path = context.requireCordovaModule('path'),
  15. et = context.requireCordovaModule('elementtree');
  16. /** @defaults */
  17. var xwalkVariables = {},
  18. argumentsString = context.cmdLine,
  19. pluginConfigurationFile = path.join(context.opts.plugin.dir, 'plugin.xml'),
  20. androidPlatformDir = path.join(context.opts.projectRoot,
  21. 'platforms', 'android'),
  22. projectConfigurationFile = path.join(context.opts.projectRoot,
  23. 'config.xml'),
  24. platformConfigurationFile,
  25. projectManifestFile = path.join(androidPlatformDir,
  26. 'AndroidManifest.xml'),
  27. xwalk64bit = "xwalk64bit",
  28. xwalkLiteVersion = "",
  29. specificVersion = false;
  30. var oldConfigXMLLocation = path.join(androidPlatformDir, 'res', 'xml', 'config.xml');
  31. var newConfigXMLLocation = path.join(androidPlatformDir, 'app', 'src', 'main', 'res', 'xml', 'config.xml');
  32. if (fs.existsSync(newConfigXMLLocation)) {
  33. // cordova-android >= 7.0.0
  34. platformConfigurationFile = newConfigXMLLocation;
  35. } else {
  36. // cordova-android < 7.0.0
  37. platformConfigurationFile = oldConfigXMLLocation;
  38. }
  39. /** Init */
  40. var CordovaConfig = new ConfigParser(platformConfigurationFile);
  41. var addPermission = function() {
  42. var projectManifestXmlRoot = XmlHelpers.parseElementtreeSync(projectManifestFile);
  43. var child = et.XML('<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />');
  44. XmlHelpers.graftXML(projectManifestXmlRoot, [child], '/manifest');
  45. fs.writeFileSync(projectManifestFile, projectManifestXmlRoot.write({indent: 4}), 'utf-8');
  46. }
  47. var removePermission = function() {
  48. var projectManifestXmlRoot = XmlHelpers.parseElementtreeSync(projectManifestFile);
  49. var child = et.XML('<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />');
  50. XmlHelpers.pruneXML(projectManifestXmlRoot, [child], '/manifest');
  51. fs.writeFileSync(projectManifestFile, projectManifestXmlRoot.write({indent: 4}), 'utf-8');
  52. }
  53. var defaultPreferences = function() {
  54. var pluginPreferences = {};
  55. var pluginXmlRoot = XmlHelpers.parseElementtreeSync(pluginConfigurationFile),
  56. tagName = "preference",
  57. containerName = "config-file",
  58. targetPlatform = 'android',
  59. targetPlatformTag = pluginXmlRoot.find('./platform[@name="' + targetPlatform + '"]');
  60. var tagsInRoot = pluginXmlRoot.findall(tagName) || [],
  61. tagsInPlatform = targetPlatformTag ? targetPlatformTag.findall(tagName) : [],
  62. tagsInContainer = targetPlatformTag ? targetPlatformTag.findall(containerName) : [],
  63. tagsList = tagsInRoot.concat(tagsInContainer);
  64. // Parses <preference> tags within <config-file>-blocks
  65. tagsList.map(function(prefTag) {
  66. prefTag.getchildren().forEach(function(element) {
  67. if ((element.tag == 'preference') && (element.attrib['name']) && element.attrib['default']) {
  68. // Don't add xwalkLiteVersion in the app/config.xml
  69. if (element.attrib['name'] == "xwalkLiteVersion") {
  70. xwalkLiteVersion = element.attrib['default'];
  71. } else {
  72. pluginPreferences[element.attrib['name']] = element.attrib['default'];
  73. }
  74. }
  75. });
  76. });
  77. return pluginPreferences;
  78. }
  79. /** The style of name align with config.xml */
  80. var setConfigPreference = function(name, value) {
  81. var trimName = name.replace('_', '');
  82. for (var localName in xwalkVariables) {
  83. if (localName.toUpperCase() == trimName.toUpperCase()) {
  84. xwalkVariables[localName] = value;
  85. if (localName == 'xwalkVersion') {
  86. specificVersion = true;
  87. }
  88. }
  89. }
  90. }
  91. /** Pase the cli command to get the specific preference*/
  92. var parseCliPreference = function() {
  93. var commandlineVariablesList = argumentsString.split('--variable');
  94. if (commandlineVariablesList) {
  95. commandlineVariablesList.forEach(function(element) {
  96. element = element.trim();
  97. if(element && element.indexOf('XWALK') == 0) {
  98. var preference = element.split('=');
  99. if (preference && preference.length == 2) {
  100. setConfigPreference(preference[0], preference[1]);
  101. }
  102. }
  103. });
  104. }
  105. }
  106. /** Add preference */
  107. this.addPreferences = function() {
  108. // Pick the xwalk variables with the cli preferences
  109. // parseCliPreference();
  110. // Add the permission of writing external storage when using shared mode
  111. if (CordovaConfig.getGlobalPreference('xwalkMode') == 'shared') {
  112. addPermission();
  113. }
  114. // Configure the final value in the config.xml
  115. // var configXmlRoot = XmlHelpers.parseElementtreeSync(projectConfigurationFile);
  116. // var preferenceUpdated = false;
  117. // for (var name in xwalkVariables) {
  118. // var child = configXmlRoot.find('./preference[@name="' + name + '"]');
  119. // if(!child) {
  120. // preferenceUpdated = true;
  121. // child = et.XML('<preference name="' + name + '" value="' + xwalkVariables[name] + '" />');
  122. // XmlHelpers.graftXML(configXmlRoot, [child], '/*');
  123. // }
  124. // }
  125. // if(preferenceUpdated) {
  126. // fs.writeFileSync(projectConfigurationFile, configXmlRoot.write({indent: 4}), 'utf-8');
  127. // }
  128. }
  129. /** Remove preference*/
  130. this.removePreferences = function() {
  131. if (CordovaConfig.getGlobalPreference('xwalkMode') == 'shared') {
  132. // Add the permission of write_external_storage in shared mode
  133. removePermission();
  134. }
  135. // var configXmlRoot = XmlHelpers.parseElementtreeSync(projectConfigurationFile);
  136. // for (var name in xwalkVariables) {
  137. // var child = configXmlRoot.find('./preference[@name="' + name + '"]');
  138. // if (child) {
  139. // XmlHelpers.pruneXML(configXmlRoot, [child], '/*');
  140. // }
  141. // }
  142. // fs.writeFileSync(projectConfigurationFile, configXmlRoot.write({indent: 4}), 'utf-8');
  143. }
  144. var build64bit = function() {
  145. var build64bit = false;
  146. var commandlineVariablesList = argumentsString.split('--');
  147. if (commandlineVariablesList) {
  148. commandlineVariablesList.forEach(function(element) {
  149. element = element.trim();
  150. if(element && element.indexOf(xwalk64bit) == 0) {
  151. build64bit = true;
  152. }
  153. });
  154. }
  155. return build64bit;
  156. }
  157. this.beforeBuild64bit = function() {
  158. if(build64bit()) {
  159. var configXmlRoot = XmlHelpers.parseElementtreeSync(projectConfigurationFile);
  160. var child = configXmlRoot.find('./preference[@name="' + xwalk64bit + '"]');
  161. if(!child) {
  162. child = et.XML('<preference name="' + xwalk64bit + '" value="' + xwalk64bit + '" />');
  163. XmlHelpers.graftXML(configXmlRoot, [child], '/*');
  164. fs.writeFileSync(projectConfigurationFile, configXmlRoot.write({indent: 4}), 'utf-8');
  165. }
  166. }
  167. }
  168. this.afterBuild64bit = function() {
  169. if(build64bit()) {
  170. var configXmlRoot = XmlHelpers.parseElementtreeSync(projectConfigurationFile);
  171. var child = configXmlRoot.find('./preference[@name="' + xwalk64bit + '"]');
  172. if (child) {
  173. XmlHelpers.pruneXML(configXmlRoot, [child], '/*');
  174. fs.writeFileSync(projectConfigurationFile, configXmlRoot.write({indent: 4}), 'utf-8');
  175. }
  176. }
  177. console.log("Crosswalk info:");
  178. console.log(" After much discussion and analysis of the market,");
  179. console.log(" we have decided to discontinue support for Android 4.0 (ICS) in Crosswalk starting with version 20,");
  180. console.log(" so the minSdkVersion of Cordova project is configured to 16 by default. \n");
  181. }
  182. xwalkVariables = defaultPreferences();
  183. };