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
'use strict';

/**
 * Generate $(PROFILE_FOLDER)/apps/APP/content from build_stage to profile.
 * Additionally, it will also filter images by resolution and some excluded
 * conditions, which should move to other task, bug 1010095.
 */

var utils = require('./utils');

var WebappZip = function() {
  this.options = null;
};

WebappZip.prototype.setOptions = function(options) {
  this.options = options;

  this.targetAppFolder = utils.getFile(options.webapp.profileDirectoryFilePath);
  utils.ensureFolderExists(this.targetAppFolder);
};

WebappZip.prototype.getCompression = function(pathInZip) {
  var webapp = this.options.webapp;
  if (webapp.metaData && webapp.metaData.external === false &&
    webapp.metaData.zip && webapp.metaData.zip.mmap_files &&
    webapp.metaData.zip.mmap_files.indexOf(pathInZip) !== -1) {
    return 'STORE';
  } else {
    // Don't store some files compressed since that's not giving us any
    // benefit but costs cpu when reading from the zip.
    var ext = pathInZip.split('.').reverse()[0].toLowerCase();
    return (['jpg', 'jpeg'].indexOf(ext) !== -1) ? 'STORE' : 'DEFLATE';
  }
};

WebappZip.prototype.isExcludedFromZip = function(file) {
  try {
    if (!(file && file.exists() && file.isFile())) {
      return true;
    }
  } catch (err) {
    utils.log('webapp-zip', 'isExcludedFromZip error with ' + file.path + '\n');
    throw(err);
  }
  var options = this.options;
  var excludedFuncs = [
    function fileExist(file) {
      return !file.exists();
    },
    function isLocales(file) {
      return options.GAIA_CONCAT_LOCALES === '1' &&
        /locales[^-]/.test(file.path);
    },
    function isSpecificProperties(file) {
      if(utils.getExtension(file.path) === 'properties' &&
        file.path.indexOf('locales') !== -1 &&
        options.GAIA_CONCAT_LOCALES === '0' &&
        options.LOCALE_BASEDIR && options.LOCALES_FILE) {
        let localesFile = utils.resolve(options.LOCALES_FILE, options.GAIA_DIR);
        if (!localesFile.exists()) {
          throw new Error('file not found: ' + localesFile.path);
        }
        let locales = Object.keys(utils.getJSON(localesFile));

        return !locales.some(function(locale) {
          return file.path.indexOf(locale + '.properties') !== -1;
        });
      }
      return false;
    },
    function isBuild(file) {
      var appDirPath = options.webapp.sourceDirectoryName;
      return new RegExp(utils.joinPath(appDirPath, 'build')
        .replace(/\\/g, '\\\\') + '|build.txt')
        .test(file.path);
    },
    function isMakefile(file) {
      return /Makefile/.test(file.path);
    },
    function isReadme(file) {
      return /README/.test(file.path);
    },
    function isPackageJSON(file) {
      return /package\.json/.test(file.path);
    },
    function fileHidden(file) {
      return file.isHidden();
    },
    function isTest(file) {
      var appPath = options.webapp.buildDirectoryFilePath;
      var pathElements = file.path.substr(appPath.length + 1).split(/[\\/]/);
      return pathElements.some(function(path) { return path === 'test'; });
    },
    function isGit(file) {
      var appPath = options.webapp.buildDirectoryFilePath;
      var path = file.path.substr(appPath.length + 1).split(/[\\/]/)[0];
      return path === '.git';
    },
    function isL10n(file) {
      return (options.GAIA_CONCAT_LOCALES === '1' &&
        (file.leafName === 'locales' ||
          utils.getFile(file.path, '..').leafName === 'locales'));
    },
    function isConcatenatedL10n(file) {
      return ((file.leafName === 'locales-obj' ||
        utils.getFile(file.path, '..').leafName === 'locales-obj') &&
        options.GAIA_CONCAT_LOCALES !== '1');
    }
  ];
  for (var index in excludedFuncs) {
    if (!excludedFuncs[index](file)) {
      continue;
    } else {
      return true;
    }
  }
  return false;
};

WebappZip.prototype.addToZip = function(file) {
  if (this.isExcludedFromZip(file)) {
    return;
  }

  var pathInZip = file.path.substr(
    this.options.webapp.buildDirectoryFilePath.length + 1);
  // var compression = this.getCompression(pathInZip);
  pathInZip = pathInZip.replace(/\\/g, '/');

  if (!pathInZip) {
    return;
  }

  // nsIZipWriter should not receive any path starting with `/`,
  // it would put files in a folder with empty name...
  pathInZip = pathInZip.replace(/^\/+/, '');

  // Regular file
  if (file.isFile()) {
    if (/\.html$/.test(file.leafName)) {
      // This file might have been pre-translated for the default locale
      var l10nFile = utils.getFile(file.path + '.' +
        this.options.GAIA_DEFAULT_LOCALE);
      if (l10nFile.exists()) {
	file.copyTo(this.targetAppFolder, pathInZip);
        return;
      }
    }

    var re = new RegExp('\\.html\\.' + this.options.GAIA_DEFAULT_LOCALE);
    if (!this.targetAppFolder.isFile(pathInZip) && !re.test(file.leafName)) {
      file.copyTo(this.targetAppFolder, pathInZip);
    }
  }
};

WebappZip.prototype.execute = function(options) {
  // If BUILD_APP_NAME isn't `*`, we only accept one webapp
  if (options.BUILD_APP_NAME != '*' &&
    options.webapp.sourceDirectoryName != options.BUILD_APP_NAME) {
    return;
  }

  // Zip generation is not needed for external apps, aaplication data
  // is copied to profile webapps folder in webapp-manifests.js
  if (utils.isExternalApp(options.webapp)) {
    return;
  }

  this.setOptions(options);

  var buildDir = utils.getFile(options.webapp.buildDirectoryFilePath);
  utils.ls(buildDir, true).forEach(this.addToZip.bind(this));
};

function execute(options) {
  var profileDir = utils.getFile(options.PROFILE_DIR);
  utils.ensureFolderExists(profileDir);
  var appsDir = utils.getFile(options.COREWEBAPPS_DIR, 'apps');
  utils.ensureFolderExists(appsDir);

  (new WebappZip()).execute(options);
}

exports.execute = execute;
exports.WebappZip = WebappZip;