DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Git (975a35c0f5)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
'use strict';

var utils = require('./utils');
var sh = new utils.Commander('sh');

const JOB_NAME = 'push-to-device';

sh.initPath(utils.getEnvPath());

/**
 * detect if b2g process needs to be restarted
 * @param  {string}  appName app name
 * @return {bool}            true if needed
 */
function needsB2gRestart(appName) {
  // b2g should be restarted if these app name is assigned by APP=appname
  var appList = ['system'];
  return (appList.indexOf(appName) !== -1);
}

function pushToDevice(profileFolder, remotePath, adb) {
  // MingGW on Windows takes '/remote/src' as 'c:\remote\src' which is
  // not right, so we use two slash before the remote path to prevent it.
  var webapps_path = '/' + remotePath + '/apps';
  return Promise.resolve()
    .then(function() {
      return sh.run(['-c', adb + ' shell rm -r ' + webapps_path]);
    })
    .then(function() {
      return sh.run(['-c', adb + ' shell rm //data/local/user.js']);
    })
    .then(function() {
      // adb push /gaia/profile/apps /system/b2g/apps
      return sh.run(['-c', adb + ' push "' + utils.joinPath(profileFolder,
                    'apps') + '" ' + webapps_path]);
    })
    .then(function() {
      // adb push /gaia/profile/user.js /data/local/user.js
      return sh.run(['-c', adb + ' push "' + utils.joinPath(profileFolder,
                    'user.js') + '" //data/local/user.js']);
    })
    .then(function() {
      var indexedDbFile = utils.getFile(profileFolder, 'indexedDB');
      if (indexedDbFile.exists() && indexedDbFile.isDirectory()) {
        // adb push /gaia/profile/indexedDB /data/local/indexedDB
        return sh.run(['-c', adb + ' push "' + indexedDbFile.path +
          '" //data/local/indexedDB']);
      }
    });
}

function installSvoperapps(profileFolder, adb) {
  var svoperappsUrl = '//data/local/svoperapps';

  return Promise.resolve()
    .then(function() {
      return sh.run(['-c', adb + ' shell rm -r ' + svoperappsUrl]);
    })
    .then(function() {
      return sh.run(['-c', adb + ' push "' + utils.joinPath(profileFolder,
        'svoperapps') + '" ' + svoperappsUrl]);
    });
}

function installOneApp(targetFolder, buildAppName,
                       remotePath, adb) {
  let devicePath = remotePath + '/apps/' + buildAppName + '/';
  // kill all of the previous app file and push new ones.
  return Promise.resolve()
    .then(function() {
      // "adb shell rm -r /system/b2g/apps/SOME_APP.gaiamobile.org/"
      utils.log(JOB_NAME, 'removing previous files of ' +
                          buildAppName + ' from ' + devicePath + '.');
      return sh.run(['-c',
        adb + ' shell "rm -r ' + devicePath + '"']);
    })
    .then(function() {
      // "adb push /gaia/profile/apps/SOME_APP.gaiamobile.org/application.zip
      //  /data/local/tmp/pushgaia/SOME_APP.gaiamobile.org/application.zip"
      utils.log(JOB_NAME, 'installing new files of ' +
                          buildAppName + ' to ' + devicePath + '.');
      return sh.run(['-c',
        adb + ' push "' + targetFolder + '" "' + devicePath + '"']);
    });
}

function getRemoteInstallPath(adb) {
  var tempDirName = 'pushGaia' + Math.random().toString(36).substr(2, 8);
  var tempDir = utils.getTempFolder(tempDirName);
  var tempFile = utils.getFile(tempDir.path, 'webapps.json');

  // Use |adb shell cat| instead of |adb pull| so we don't run into
  // error and exit when the file does not exist.
  sh.run(['-c', adb + ' shell cat /data/local/webapps/webapps.json > ' +
    tempFile.path]);

  // Read the file as JSON
  // If there were no webapps ever installed on the device (likely purged in
  // the previous step), default to /system/b2g
  var content;
  try {
    content = utils.getJSON(tempFile);
  } catch (e) {
    return '/system/b2g';
  }

  // Remove the entire temp directory
  utils.deleteFile(tempDir.path);

  // If any of the preload Gaia app was installed at /system/b2g,
  // we should overwrite them in /system/b2g
  for (var app in content) {
    if (content[app].basePath === '/system/b2g/apps') {
      return '/system/b2g';
    }
  }
  return '/data/local';
}

function execute(options) {
  const buildAppName = options.BUILD_APP_NAME;
  const gaiaDir = options.GAIA_DIR;
  const profileFolder = options.PROFILE_DIR;
  var remotePath = options.GAIA_INSTALL_PARENT;
  var targetFolder;
  var adb = options.ADB;
  var restartB2g = needsB2gRestart(buildAppName);

  if (restartB2g) {
    utils.log(JOB_NAME, 'b2g process will be restarted for ' +
      'installing ' + buildAppName + ' app, see bug 1000049 for ' +
      'more information.');
  }

  return Promise.resolve()
    .then(function() {
      var profile = utils.getFile(profileFolder);
      if (!profile.isDirectory()) {
        throw new Error(' -*- build/push-to-device.js: cannot locate' +
                        'profile folder in ' + options.PROFILE_DIR);
      }
    })
    .then(function() {
      return sh.run(['-c', adb + ' start-server']);
    })
    .then(function() {
      utils.log(JOB_NAME, 'Waiting for device ...');
      return sh.run(['-c', adb + ' wait-for-device']);
    })
    .then(function() {
      if (buildAppName === '*' || restartB2g) {
        return sh.run(['-c', adb + ' shell stop b2g']);
      }
    })
    .then(function() {
      return sh.run(['-c', adb + ' shell rm -r //cache/*']);
    })
    .then(function() {
      if (!remotePath) {
        utils.log(JOB_NAME, 'GAIA_INSTALL_PARENT unset; ' +
          'probing previous installation location...');
        remotePath = getRemoteInstallPath(adb);
      }
      utils.log(JOB_NAME, 'Install webapp(s) to ' + remotePath + '...');

      if (/^\/system\//.test(remotePath)) {
        return sh.run(['-c', adb + ' remount']);
      }
    })
    .then(function() {
      if (buildAppName === '*') {
        return pushToDevice(profileFolder, remotePath, adb);
      } else {
        targetFolder = utils.joinPath(
            profileFolder, 'apps', buildAppName);
        return installOneApp(targetFolder, buildAppName,
                             remotePath, adb);
      }
    })
    .then(function() {
      if (options.VARIANT_PATH) {
        return installSvoperapps(profileFolder, adb);
      }
    })
    .then(function() {
      if (buildAppName === '*') {
        return sh.run(['-c', adb + ' push ' +
          '"' + options.DEFAULT_GAIA_ICONS_FONT + '"' +
          ' //system/fonts/hidden/gaia-icons.ttf']);
      }
    })
    .then(function() {
      if (buildAppName === '*') {
        return sh.run(['-c', adb + ' push ' +
          '"' + options.DEFAULT_KEYBOAD_SYMBOLS_FONT + '"' +
          ' //system/fonts/hidden/Keyboard-Symbols.ttf']);
      }
    })
    .then(function() {
      if (buildAppName === '*' || restartB2g) {
        utils.log(JOB_NAME, 'Restarting B2G...');
        sh.run(['-c', adb + ' shell start b2g']);
      } else {
        return Promise.resolve()
          .then(function() {
            // Some app folder name is different with the process name,
            // ex. sms -> Messages
            var manifest = utils.readJSONFromPath(
                             utils.joinPath(targetFolder, 'manifest.webapp'));
            utils.log(JOB_NAME, 'Restarting ' + manifest.name + '...');
            utils.killAppByPid(manifest.name, gaiaDir);
          });
      }
    })
    .catch(function(err) {
      utils.log(JOB_NAME, err);
      utils.exit(1);
    });
}

exports.execute = execute;
exports.installSvoperapps = installSvoperapps;
exports.getRemoteInstallPath = getRemoteInstallPath;
exports.pushToDevice = pushToDevice;