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

Adb.js 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. var Q = require('q');
  18. var os = require('os');
  19. var events = require('cordova-common').events;
  20. var spawn = require('cordova-common').superspawn.spawn;
  21. var CordovaError = require('cordova-common').CordovaError;
  22. var Adb = {};
  23. function isDevice (line) {
  24. return line.match(/\w+\tdevice/) && !line.match(/emulator/);
  25. }
  26. function isEmulator (line) {
  27. return line.match(/device/) && line.match(/emulator/);
  28. }
  29. /**
  30. * Lists available/connected devices and emulators
  31. *
  32. * @param {Object} opts Various options
  33. * @param {Boolean} opts.emulators Specifies whether this method returns
  34. * emulators only
  35. *
  36. * @return {Promise<String[]>} list of available/connected
  37. * devices/emulators
  38. */
  39. Adb.devices = function (opts) {
  40. return spawn('adb', ['devices'], { cwd: os.tmpdir() }).then(function (output) {
  41. return output.split('\n').filter(function (line) {
  42. // Filter out either real devices or emulators, depending on options
  43. return (line && opts && opts.emulators) ? isEmulator(line) : isDevice(line);
  44. }).map(function (line) {
  45. return line.replace(/\tdevice/, '').replace('\r', '');
  46. });
  47. });
  48. };
  49. Adb.install = function (target, packagePath, opts) {
  50. events.emit('verbose', 'Installing apk ' + packagePath + ' on target ' + target + '...');
  51. var args = ['-s', target, 'install'];
  52. if (opts && opts.replace) args.push('-r');
  53. return spawn('adb', args.concat(packagePath), { cwd: os.tmpdir() }).then(function (output) {
  54. // 'adb install' seems to always returns no error, even if installation fails
  55. // so we catching output to detect installation failure
  56. if (output.match(/Failure/)) {
  57. if (output.match(/INSTALL_PARSE_FAILED_NO_CERTIFICATES/)) {
  58. output += '\n\n' + 'Sign the build using \'-- --keystore\' or \'--buildConfig\'' +
  59. ' or sign and deploy the unsigned apk manually using Android tools.';
  60. } else if (output.match(/INSTALL_FAILED_VERSION_DOWNGRADE/)) {
  61. output += '\n\n' + 'You\'re trying to install apk with a lower versionCode that is already installed.' +
  62. '\nEither uninstall an app or increment the versionCode.';
  63. }
  64. return Q.reject(new CordovaError('Failed to install apk to device: ' + output));
  65. }
  66. });
  67. };
  68. Adb.uninstall = function (target, packageId) {
  69. events.emit('verbose', 'Uninstalling package ' + packageId + ' from target ' + target + '...');
  70. return spawn('adb', ['-s', target, 'uninstall', packageId], { cwd: os.tmpdir() });
  71. };
  72. Adb.shell = function (target, shellCommand) {
  73. events.emit('verbose', 'Running adb shell command "' + shellCommand + '" on target ' + target + '...');
  74. var args = ['-s', target, 'shell'];
  75. shellCommand = shellCommand.split(/\s+/);
  76. return spawn('adb', args.concat(shellCommand), { cwd: os.tmpdir() }).catch(function (output) {
  77. return Q.reject(new CordovaError('Failed to execute shell command "' +
  78. shellCommand + '"" on device: ' + output));
  79. });
  80. };
  81. Adb.start = function (target, activityName) {
  82. events.emit('verbose', 'Starting application "' + activityName + '" on target ' + target + '...');
  83. return Adb.shell(target, 'am start -W -a android.intent.action.MAIN -n' + activityName).catch(function (output) {
  84. return Q.reject(new CordovaError('Failed to start application "' +
  85. activityName + '"" on device: ' + output));
  86. });
  87. };
  88. module.exports = Adb;