Skip to content
This repository has been archived by the owner on Nov 3, 2021. It is now read-only.

Commit

Permalink
Merge pull request #28691 from RickyChien/webapp-zip-to-nodejs
Browse files Browse the repository at this point in the history
Bug 955999 - Running webapp-zip.js on node.js r=gduan
  • Loading branch information
Ricky Chien committed Mar 10, 2015
2 parents ef6a7b7 + e6cb523 commit d705c4a
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 150 deletions.
9 changes: 7 additions & 2 deletions build/post-app.js
Expand Up @@ -25,7 +25,12 @@ exports.execute = function(options, webapp) {
require('./webapp-optimize').execute(options);

if (options.DEBUG === '0') {
// Generate $(PROFILE_FOLDER)/webapps/APP/application.zip
require('./webapp-zip').execute(options);
// Workaround for bug 955999, after multilocale, settings and system
// generate too long args exceed nsIProcess.runw() can handle.
// Thus, we clean webapp.asts values which generates from l10n in order to
// pass into nsIProcess.runw()
// It can remove by bug 1131516 once all post-app tasks are refactored.
options.webapp.asts = '';
nodeHelper.require('webapp-zip', options);
}
};
91 changes: 41 additions & 50 deletions build/test/unit/webapp_zip_test.js
Expand Up @@ -3,6 +3,7 @@
var assert = require('chai').assert;
var proxyquire = require('proxyquire');
var mockUtils = require('./mock_utils.js');
var fsPath = require('path');

suite('webapp-zip.js', function() {
var app;
Expand All @@ -17,18 +18,18 @@ suite('webapp-zip.js', function() {

setup(function() {
app = proxyquire.noCallThru().load(
'../../webapp-zip', {
'./utils': mockUtils
});
var GetFile = function(path) {
this.path = path;
'../../webapp-zip', {
'./utils': mockUtils
});
var GetFile = function(filePath) {
this.path = filePath;
this.append = function(subPath) {
this.path += ('/' + subPath);
};
this.clone = function() {
return new GetFile(this.path);
};
this.leafName = this.path;
this.leafName = fsPath.basename(this.path);
this.parent = this;
this.exists = function() {
return fileExists;
Expand All @@ -43,18 +44,15 @@ suite('webapp-zip.js', function() {
return isDirectory;
};
};
mockUtils.getFile = function(path) {
filePath = path;
return new GetFile(path);
mockUtils.getFile = function() {
var args = Array.prototype.slice.call(arguments);
filePath = fsPath.join.apply(fsPath, args);
return new GetFile(filePath);
};

mockUtils.createZip = function() {
return {
open: function(file, mode) {
zipFilePath = file.path;
zipMode = mode;
}
};
mockUtils.createZip = function(zipPath) {
zipFilePath = zipPath;
return {};
};

mockUtils.getCompression = function(type) {
Expand Down Expand Up @@ -96,24 +94,24 @@ suite('webapp-zip.js', function() {
test('setOptions', function() {
webappZip.setOptions(mockOptions);
assert.equal(zipFilePath, 'testTargetDir/testDomain/application.zip');
assert.equal(zipMode, 0x04 | 0x08 | 0x20);
});

test('getCompression', function() {
var pathInZip = 'pathInZip';
webappZip.webapp = {
metaData: {
external: false,
zip: {
webappZip.options = {
webapp: {
metaData: {
external: false,
zip: {}
}
}
};
webappZip.webapp.metaData.zip.mmap_files = [pathInZip];
webappZip.options.webapp.metaData.zip.mmap_files = [pathInZip];
assert.equal(webappZip.getCompression(pathInZip), 'none');

// we don't compress jpg file.
pathInZip = 'pathInZip.jpg';
delete webappZip.webapp.metaData;
delete webappZip.options.webapp.metaData;
assert.equal(webappZip.getCompression(pathInZip), 'none');

pathInZip = 'pathInZip.png';
Expand All @@ -124,23 +122,23 @@ suite('webapp-zip.js', function() {
var file = mockUtils.getFile('locales');
isFile = true;
fileExists = true;
webappZip.config = {
GAIA_CONCAT_LOCALES: '1'
};
webappZip.webapp = {
sourceDirectoryName: 'testSourceDirectory'
webappZip.options = {
GAIA_CONCAT_LOCALES: '1',
webapp: {
sourceDirectoryName: 'testSourceDirectory'
}
};
webappZip.buildDir = mockUtils.getFile('test');
webappZip.options.webapp.buildDirectoryFilePath = 'test';
assert.equal(webappZip.isExcludedFromZip(file), true,
'Ignore l10n files if they have been inlined or concatenated');

file = mockUtils.getFile('locales-obj');
webappZip.config.GAIA_CONCAT_LOCALES = '0';
webappZip.options.GAIA_CONCAT_LOCALES = '0';
assert.equal(webappZip.isExcludedFromZip(file), true,
'Ignore l10n files if they have been inlined or concatenated');

file = mockUtils.getFile('testapppath/test');
webappZip.buildDir = mockUtils.getFile('testapppath');
webappZip.options.webapp.buildDirectoryFilePath = 'testapppath';
assert.equal(webappZip.isExcludedFromZip(file), true,
'Ignore test file');
file = mockUtils.getFile('testapppath/.git');
Expand All @@ -166,42 +164,37 @@ suite('webapp-zip.js', function() {
var webappZip;
var isExcludedFromZip;
var testResult;

setup(function() {
fileExists = true;
isExcludedFromZip = false;
mockUtils.addEntryContentWithTime = function(zip, pathInZip, l10nFile,
time, needCompression) {
mockUtils.addFileToZip = function(zip, pathInZip, file, compression) {
testResult = {
zip: zip,
pathInZip: pathInZip,
l10nFile: l10nFile,
needCompression: needCompression
file: file,
compression: compression
};
};
webappZip = new app.WebappZip();
webappZip.buildDir = mockUtils.getFile('testBuildDir');
webappZip.getImagePathByResolution = function(file, pathInZip) {
return pathInZip + '_bestResolution';
webappZip.options = {
GAIA_DEFAULT_LOCALE: 'en-US-test',
webapp: {
buildDirectoryFilePath: 'testBuildDir'
}
};
webappZip.isExcludedFromZip = function() {
return isExcludedFromZip;
};
webappZip.getCompression = function() {
return '1';
};
webappZip.config = {
GAIA_DEFAULT_LOCALE: 'en-US-test'
};
webappZip.zipFile = {
hasEntry: function(pathInZip) {
return false;
},
};
});

teardown(function() {
testResult = null;
});

test('file is hiddenr or is set excluded from zip', function() {
var testFile = mockUtils.getFile('testfile');
isHidden = true;
Expand All @@ -216,14 +209,12 @@ suite('webapp-zip.js', function() {

test('add localize html file', function() {
var testFile = mockUtils.getFile('testBuildDir/testFile.html');
// testFile_bestResolution
isFile = true;
fileExists = true;
webappZip.addToZip(testFile);
assert.equal(testResult.pathInZip, 'testFile.html');
assert.equal(testResult.l10nFile.path,
'testBuildDir/testFile.html/testBuildDir/testFile.html.en-US-test');

assert.equal(testResult.file.path,
'testBuildDir/testFile.html.en-US-test');
});
});
});
11 changes: 9 additions & 2 deletions build/utils-node.js
Expand Up @@ -454,7 +454,7 @@ module.exports = {
}
},

addFileToZip: function(zip, zipPath, pathInZip, file, compression) {
addFileToZip: function(zip, pathInZip, file, compression) {
if (!file.exists()) {
return;
}
Expand All @@ -464,8 +464,15 @@ module.exports = {
});
},

hasFileInZip: function(zip, pathInZip) {
return zip.file(pathInZip);
},

closeZip: function(zip, zipPath) {
fs.writeFileSync(zipPath, zip.generate({ type: 'nodebuffer' }));
fs.writeFileSync(zipPath, zip.generate({
type: 'nodebuffer',
platform: process.platform
}));
},

getLocaleBasedir: function(src) {
Expand Down
44 changes: 23 additions & 21 deletions build/utils-xpc.js
Expand Up @@ -949,10 +949,9 @@ function Commander(cmd) {
process.init(_file);
process.runw(true, args, args.length);
callback && callback(process.exitValue);
} catch (e) {
} catch (err) {
callback && callback(1);
throw new Error('having trouble when execute ' + command +
' ' + args.join(' '));
throw err;
}

processEvents(function () {
Expand Down Expand Up @@ -1097,10 +1096,9 @@ function getDocument(content) {
* @param zip {nsIZipWriter} - the zip file
* @param pathInZip {string} - the relative path to the new file
* @param data {string} - the content of the file
* @param time {string} - the timestamp of the file
* @param compression {number} - the enum shows above
*/
function addEntryContentWithTime(zip, pathInZip, data, time, compression) {
function addFileToZip(zip, pathInZip, data, compression) {
if (!data) {
return;
}
Expand All @@ -1121,10 +1119,8 @@ function addEntryContentWithTime(zip, pathInZip, data, time, compression) {
input.init(data, -1, -1, 0);
}

zip.addEntryStream(
pathInZip, time || 0, compression, input, false);
zip.addEntryStream(pathInZip, Date.now() * 1000, compression, input, false);
input.close();

}

/**
Expand All @@ -1140,6 +1136,10 @@ function getCompression(type) {
}
}

function hasFileInZip(zip, pathInZip) {
return zip.hasEntry(pathInZip);
}

/**
* Generate UUID. It's just a wrapper of 'nsIUUIDGenerator'
* See the 'nsIUUIDGenerator' page on MDN.
Expand Down Expand Up @@ -1176,23 +1176,23 @@ function copyRec(source, target) {

/**
* Create an empty ZIP file.
* For users, the way to read/write a ZIP file is
*
* 1. create an nsIZipWriter
* 2. open it with the open method, which
* 3. puts an nsIFile as the first argument
*
* For example:
*
* createZip().open(getFile(<some file>, <mode>))
*
* @return {nsIZipWriter}
*/
function createZip() {
function createZip(zipPath) {
var zip = Cc['@mozilla.org/zipwriter;1'].createInstance(Ci.nsIZipWriter);
// PR_RDWR | PR_CREATE_FILE | PR_TRUNCATE
zip.open(getFile(zipPath), 0x04 | 0x08 | 0x20);
return zip;
}

function closeZip(zip) {
if (zip.alignStoredFiles) {
zip.alignStoredFiles(4096);
}
zip.close();
}

/**
* Remove all listed files in the directory.
*
Expand Down Expand Up @@ -1250,7 +1250,7 @@ var scriptLoader = {
* Run specific build task on Node.js if RUN_ON_NODE is on, otherwise we go back
* to XPCShell.
*/
function NodeHelper(path) {
function NodeHelper() {
if (getEnv('RUN_ON_NODE') === '1') {
var node = new Commander('node');
node.initPath(getEnvPath());
Expand All @@ -1260,7 +1260,7 @@ function NodeHelper(path) {
};
} else {
this.require = function(path, options) {
require(path).execute(options);
return require(path).execute(options);
};
}
}
Expand Down Expand Up @@ -1296,6 +1296,8 @@ exports.getOsType = getOsType;
exports.generateUUID = generateUUID;
exports.copyRec = copyRec;
exports.createZip = createZip;
exports.closeZip = closeZip;
exports.hasFileInZip = hasFileInZip;
exports.scriptParser = Reflect.parse;
exports.deleteFile = deleteFile;
exports.listFiles = listFiles;
Expand Down Expand Up @@ -1325,7 +1327,7 @@ exports.Services = Services;
exports.concatenatedScripts = concatenatedScripts;
exports.dirname = dirname;
exports.basename = basename;
exports.addEntryContentWithTime = addEntryContentWithTime;
exports.addFileToZip = addFileToZip;
exports.getCompression = getCompression;
exports.existsInAppDirs = existsInAppDirs;
exports.removeFiles = removeFiles;
Expand Down
4 changes: 3 additions & 1 deletion build/utils.js
Expand Up @@ -318,6 +318,8 @@ exports.generateUUID = utils.generateUUID;
exports.copyRec = utils.copyRec;
exports.getAppStatus = getAppStatus;
exports.createZip = utils.createZip;
exports.closeZip = utils.closeZip;
exports.hasFileInZip = utils.hasFileInZip;
exports.scriptParser = utils.scriptParser;
exports.scriptLoader = utils.scriptLoader;
exports.FILE_TYPE_FILE = FILE_TYPE_FILE;
Expand Down Expand Up @@ -353,7 +355,7 @@ exports.Services = utils.Services;
exports.concatenatedScripts = utils.concatenatedScripts;
exports.dirname = utils.dirname;
exports.basename = utils.basename;
exports.addEntryContentWithTime = utils.addEntryContentWithTime;
exports.addFileToZip = utils.addFileToZip;
exports.copyDirTo = utils.copyDirTo;
exports.existsInAppDirs = utils.existsInAppDirs;
exports.getCompression = utils.getCompression;
Expand Down

0 comments on commit d705c4a

Please sign in to comment.