GenericBuilder.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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-self-assign: 0 */
  18. /* eslint no-unused-vars: 0 */
  19. var Q = require('q');
  20. var fs = require('fs');
  21. var path = require('path');
  22. var shell = require('shelljs');
  23. var events = require('cordova-common').events;
  24. var CordovaError = require('cordova-common').CordovaError;
  25. function GenericBuilder (projectDir) {
  26. this.root = projectDir || path.resolve(__dirname, '../../..');
  27. this.binDirs = {
  28. ant: path.join(this.root, hasCustomRules(this.root) ? 'ant-build' : 'bin'),
  29. gradle: path.join(this.root, 'build', 'outputs', 'apk')
  30. };
  31. }
  32. function hasCustomRules (projectRoot) {
  33. return fs.existsSync(path.join(projectRoot, 'custom_rules.xml'));
  34. }
  35. GenericBuilder.prototype.prepEnv = function () {
  36. return Q();
  37. };
  38. GenericBuilder.prototype.build = function () {
  39. events.emit('log', 'Skipping build...');
  40. return Q(null);
  41. };
  42. GenericBuilder.prototype.clean = function () {
  43. return Q();
  44. };
  45. GenericBuilder.prototype.findOutputApks = function (build_type, arch) {
  46. var self = this;
  47. return Object.keys(this.binDirs).reduce(function (result, builderName) {
  48. var binDir = self.binDirs[builderName];
  49. return result.concat(findOutputApksHelper(binDir, build_type, builderName === 'ant' ? null : arch));
  50. }, []).sort(apkSorter);
  51. };
  52. GenericBuilder.prototype.readProjectProperties = function () {
  53. function findAllUniq (data, r) {
  54. var s = {};
  55. var m;
  56. while ((m = r.exec(data))) {
  57. s[m[1]] = 1;
  58. }
  59. return Object.keys(s);
  60. }
  61. var data = fs.readFileSync(path.join(this.root, 'project.properties'), 'utf8');
  62. return {
  63. libs: findAllUniq(data, /^\s*android\.library\.reference\.\d+=(.*)(?:\s|$)/mg),
  64. gradleIncludes: findAllUniq(data, /^\s*cordova\.gradle\.include\.\d+=(.*)(?:\s|$)/mg),
  65. systemLibs: findAllUniq(data, /^\s*cordova\.system\.library\.\d+=(.*)(?:\s|$)/mg)
  66. };
  67. };
  68. GenericBuilder.prototype.extractRealProjectNameFromManifest = function () {
  69. var manifestPath = path.join(this.root, 'AndroidManifest.xml');
  70. var manifestData = fs.readFileSync(manifestPath, 'utf8');
  71. var m = /<manifest[\s\S]*?package\s*=\s*"(.*?)"/i.exec(manifestData);
  72. if (!m) {
  73. throw new CordovaError('Could not find package name in ' + manifestPath);
  74. }
  75. var packageName = m[1];
  76. var lastDotIndex = packageName.lastIndexOf('.');
  77. return packageName.substring(lastDotIndex + 1);
  78. };
  79. module.exports = GenericBuilder;
  80. function apkSorter (fileA, fileB) {
  81. // De-prioritize unsigned builds
  82. var unsignedRE = /-unsigned/;
  83. if (unsignedRE.exec(fileA)) {
  84. return 1;
  85. } else if (unsignedRE.exec(fileB)) {
  86. return -1;
  87. }
  88. var timeDiff = fs.statSync(fileA).mtime - fs.statSync(fileB).mtime;
  89. return timeDiff === 0 ? fileA.length - fileB.length : timeDiff;
  90. }
  91. function findOutputApksHelper (dir, build_type, arch) {
  92. var shellSilent = shell.config.silent;
  93. shell.config.silent = true;
  94. var ret = shell.ls(path.join(dir, '*.apk')).filter(function (candidate) {
  95. var apkName = path.basename(candidate);
  96. // Need to choose between release and debug .apk.
  97. if (build_type === 'debug') {
  98. return /-debug/.exec(apkName) && !/-unaligned|-unsigned/.exec(apkName);
  99. }
  100. if (build_type === 'release') {
  101. return /-release/.exec(apkName) && !/-unaligned/.exec(apkName);
  102. }
  103. return true;
  104. }).sort(apkSorter);
  105. shellSilent = shellSilent;
  106. if (ret.length === 0) {
  107. return ret;
  108. }
  109. // Assume arch-specific build if newest apk has -x86 or -arm.
  110. var archSpecific = !!/-x86|-arm/.exec(path.basename(ret[0]));
  111. // And show only arch-specific ones (or non-arch-specific)
  112. ret = ret.filter(function (p) {
  113. /* jshint -W018 */
  114. return !!/-x86|-arm/.exec(path.basename(p)) === archSpecific;
  115. /* jshint +W018 */
  116. });
  117. if (archSpecific && ret.length > 1 && arch) {
  118. ret = ret.filter(function (p) {
  119. return path.basename(p).indexOf('-' + arch) !== -1;
  120. });
  121. }
  122. return ret;
  123. }