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 #28252 from cctuan/1131526
Browse files Browse the repository at this point in the history
Bug 1131526 - Running webapp-shared.js on node.js, r=ricky
  • Loading branch information
cctuan committed Feb 25, 2015
2 parents 1cb43e4 + c428ef3 commit 9ef3a27
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 71 deletions.
2 changes: 1 addition & 1 deletion apps/email/build/build.js
Expand Up @@ -212,7 +212,7 @@ exports.execute = function(options) {
removeLoader(options);
removeFiles(options);
}, function (err) {
console.error(err);
utils.log(err);
throw err;
});
};
2 changes: 1 addition & 1 deletion build/pre-app.js
Expand Up @@ -25,7 +25,7 @@ function execute(options) {
}

// Copy shared files to stage folders
require('./webapp-shared').execute(options);
nodeHelper.require('webapp-shared', options);

// Copy common files such as webapps.json
require('./copy-common-files').execute(options);
Expand Down
47 changes: 32 additions & 15 deletions build/test/unit/webapp_shared_test.js
Expand Up @@ -12,6 +12,7 @@ suite('webapp-shared.js', function() {
var isDirectory;
var isHidden;
var isFile;
var mRelativePath;
setup(function() {
app = proxyquire.noCallThru().load(
'../../webapp-shared', {
Expand All @@ -23,6 +24,7 @@ suite('webapp-shared.js', function() {
isFile = true;
var getFile = function() {
var filePath = path.join.apply(this, arguments);
var leafName = filePath.split('/').pop();
return {
isHidden: function() {
return isHidden;
Expand All @@ -43,18 +45,27 @@ suite('webapp-shared.js', function() {
return fileExists;
},
path: filePath,
leafName: filePath,
leafName: leafName,
isDirectory: function() {
return isDirectory;
},
isFile: function() {
return isFile;
},
getRelativeDescriptor: function() {
return filePath;
}
};
};
mockUtils.relativePath = function(path, subPath) {
return mRelativePath || subPath;
};

mockUtils.joinPath = function() {
var path = '';
for (var i in arguments) {
path += (arguments[i] + '/');
}
return path.slice(0, -1);
};

mockUtils.getFile = getFile;
mockUtils.getFileContent = function(file) {
return file;
Expand Down Expand Up @@ -346,7 +357,8 @@ suite('webapp-shared.js', function() {
];
webappShared.pushLocale(localePath);
assert.equal(result[0].path, 'shared/locales/' + localePath);
assert.equal(result[0].file.path, sharedFilePath);
assert.equal(result[0].file.path, sharedFilePath + '/locales/' +
localePath);
assert.equal(result[1].path, lsContentFilePath);
});

Expand All @@ -359,10 +371,11 @@ suite('webapp-shared.js', function() {

result.length = 0;
var brandingPath = 'brandingPath';
lsFiles = [{leafName: brandingPath + '@2x.png'}];
webappShared.gaia = {
sharedFolder: mockUtils.getFile(brandingPath + '.png')
};
lsFiles = [{
leafName: brandingPath + '@2x.png',
path: 'shared/resources/' + brandingPath +
'@2x.png'
}];
webappShared.pushResource(brandingPath + '.png');
assert.equal(result[0].path, 'shared/resources/' + brandingPath +
'@2x.png');
Expand Down Expand Up @@ -409,19 +422,21 @@ suite('webapp-shared.js', function() {
elementFile = 'gaia_component/script.js';
var testFiles = [
'elements/gaia_component/style.css',
'elements/gaia_component/images/myimg.png',
'elements/gaia_component/js/myfile.js'
'elements/gaia_component/css',
'elements/gaia_component/js',
'elements/gaia_component/images'
];

testFiles.forEach(function(testFile) {
result.length = 0;
webappShared.gaia = {
sharedFolder: mockUtils.getFile(testFile)
sharedFolder: mockUtils.getFile('')
};
webappShared.pushElements(elementFile);
assert.equal(result[0].path, 'shared/elements/' + elementFile);
assert.equal(result[1].path, 'shared/' + testFile);
});
assert.equal(result[1].path, 'shared/' + testFiles[0]);
assert.equal(result[2].path, 'shared/' + testFiles[1]);
assert.equal(result[3].path, 'shared/' + testFiles[2]);
assert.equal(result[4].path, 'shared/' + testFiles[3]);
});

test('copyBuildingBlock', function() {
Expand All @@ -443,9 +458,11 @@ suite('webapp-shared.js', function() {
}
}
];
mRelativePath = blockName + '2.css';
webappShared.copyBuildingBlock(blockName, dirName);
assert.equal(result[1].path, 'shared/' + dirName + '/' +
blockName + '2.css');
mRelativePath = null;
});

test('copySharedPage', function() {
Expand Down
5 changes: 4 additions & 1 deletion build/utils-node.js
Expand Up @@ -230,14 +230,17 @@ module.exports = {

ls: function(dir, recursive, pattern, include) {
var files = [];
if (!dir || !dir.exists()) {
return [];
}
dive(dir.path, { recursive: recursive }, function(err, filePath) {
if (err) {
// Skip error
return;
}
var file = this.getFile(filePath);
if (!pattern || !(include ^ pattern.test(file.leafName))) {
files.push(this.getFile(file));
files.push(file);
}
}.bind(this));
return files;
Expand Down
7 changes: 7 additions & 0 deletions build/utils-xpc.js
Expand Up @@ -1259,6 +1259,12 @@ function NodeHelper(path) {
}
}

function relativePath(from, to) {
var fromFile = utils.getFile(from);
var toFile = utils.getFile(to);
return toFile.getRelativeDescriptor(fromFile);
}

exports.Q = Promise;
exports.ls = ls;
exports.getFileContent = getFileContent;
Expand Down Expand Up @@ -1318,3 +1324,4 @@ exports.existsInAppDirs = existsInAppDirs;
exports.removeFiles = removeFiles;
exports.scriptLoader = scriptLoader;
exports.NodeHelper = NodeHelper;
exports.relativePath = relativePath;
1 change: 1 addition & 0 deletions build/utils.js
Expand Up @@ -362,3 +362,4 @@ exports.serializeDocument = serializeDocument;
exports.cloneJSON = cloneJSON;
exports.jsComparator = jsComparator;
exports.NodeHelper = utils.NodeHelper;
exports.relativePath = utils.relativePath;
1 change: 0 additions & 1 deletion build/webapp-manifests.js
@@ -1,5 +1,4 @@
'use strict';
/* global require, exports */
// Generate webapps_stage.json.

var utils = require('./utils');
Expand Down
88 changes: 36 additions & 52 deletions build/webapp-shared.js
@@ -1,4 +1,3 @@
/*global require, exports*/
'use strict';
var utils = require('./utils');

Expand Down Expand Up @@ -73,25 +72,21 @@ WebappShared.prototype.copyBuildingBlock =
var dirPath = 'shared/' + paths.join('/');

// Compute the nsIFile for this shared style
var styleFolder = this.gaia.sharedFolder.clone();
paths.forEach(function(path) {
styleFolder.append(path);
});
var cssFile = styleFolder.clone();
var styleFolder = utils.getFile(this.gaia.sharedFolder.path,
paths.join('/'));
if (!styleFolder.exists()) {
throw new Error('Using inexistent shared style: ' + blockName);
}

cssFile.append(blockName + '.css');
var cssFile = utils.getFile(styleFolder.path, blockName + '.css');
var pathInStage = dirPath + '/' + blockName + '.css';
this.moveToBuildDir(cssFile, pathInStage);

// Copy everything but index.html and any other HTML page into the
// style/<block> folder.
var subFolder = styleFolder.clone();
subFolder.append(blockName);
var subFolder = utils.getFile(styleFolder.path, blockName);
utils.ls(subFolder, true).forEach(function(file) {
var relativePath = file.getRelativeDescriptor(styleFolder);
var relativePath = utils.relativePath(styleFolder.path, file.path);
// Ignore HTML files at style root folder
if (relativePath.match(/^[^\/]+\.html$/)) {
return;
Expand All @@ -105,11 +100,7 @@ WebappShared.prototype.copyBuildingBlock =
};

WebappShared.prototype.pushJS = function(path) {
var file = this.gaia.sharedFolder.clone();
file.append('js');
path.split('/').forEach(function(segment) {
file.append(segment);
});
var file = utils.getFile(this.gaia.sharedFolder.path, 'js', path);
if (!file.exists()) {
throw new Error('Using inexistent shared JS file: ' + path + ' from: ' +
this.webapp.domain);
Expand All @@ -119,11 +110,7 @@ WebappShared.prototype.pushJS = function(path) {
};

WebappShared.prototype.copyPage = function(path) {
var file = this.gaia.sharedFolder.clone();
file.append('pages');
path.split('/').forEach(function(segment) {
file.append(segment);
});
var file = utils.getFile(this.gaia.sharedFolder.path, 'pages', path);

if (!file.exists()) {
throw new Error('Using inexistent shared page file: ' + path +
Expand All @@ -142,19 +129,18 @@ WebappShared.prototype.copyPage = function(path) {
};

WebappShared.prototype.pushResource = function(path) {
let file = this.gaia.sharedFolder.clone();
file.append('resources');

var paths = utils.joinPath(this.gaia.sharedFolder.path, 'resources');
path.split('/').forEach(function(segment) {
file.append(segment);
if (utils.isSubjectToBranding(file.path)) {
file.append((this.config.OFFICIAL === '1') ? 'official' : 'unofficial');
paths = utils.joinPath(paths, segment);
if (utils.isSubjectToBranding(paths)) {
paths = utils.joinPath(paths,
(this.config.OFFICIAL === '1' ? 'official' : 'unofficial'));
}
if (utils.isSubjectToDeviceType(file.path)) {
file.append(this.config.GAIA_DEVICE_TYPE);
if (utils.isSubjectToDeviceType(paths)) {
paths = utils.joinPath(paths, this.config.GAIA_DEVICE_TYPE);
}
}.bind(this));

var file = utils.getFile(paths);
if (!file.exists()) {
throw new Error('Using inexistent shared resource: ' + path +
' from: ' + this.webapp.domain + '\n');
Expand All @@ -170,14 +156,15 @@ WebappShared.prototype.pushResource = function(path) {
let fileNameRegexp = new RegExp(
'^' + file.leafName.replace(/(\.[a-z]+$)/, '((@.*x)?(\\$1))') + '$');

utils.ls(file.parent, false).forEach(function(listFile) {
var matches = fileNameRegexp.exec(listFile.leafName);
if (matches) {
var pathInStage = 'shared/resources/' +
path.replace(matches[3], matches[1]);
this.moveToBuildDir(listFile, pathInStage);
}
}.bind(this));
utils.ls(utils.getFile(utils.dirname(file.path)), false).forEach(
function(listFile) {
var matches = fileNameRegexp.exec(listFile.leafName);
if (matches) {
var pathInStage = 'shared/resources/' +
path.replace(matches[3], matches[1]);
this.moveToBuildDir(listFile, pathInStage);
}
}.bind(this));

if (file.isDirectory()) {
utils.ls(file, true).forEach(function(fileInResources) {
Expand All @@ -195,9 +182,8 @@ WebappShared.prototype.pushResource = function(path) {
};

WebappShared.prototype.pushLocale = function(name) {
var localeFolder = this.gaia.sharedFolder.clone();
localeFolder.append('locales');
localeFolder.append(name);
var localeFolder = utils.getFile(this.gaia.sharedFolder.path, 'locales',
name);
if (!localeFolder.exists()) {
throw new Error('Using inexistent shared locale: ' + name + ' from: ' +
this.webapp.domain);
Expand All @@ -214,18 +200,19 @@ WebappShared.prototype.pushLocale = function(name) {
};

WebappShared.prototype.pushElements = function(path) {
var file = this.gaia.sharedFolder.clone();
file.append('elements');
var filePath = this.gaia.sharedFolder.path;
filePath = utils.joinPath(filePath, 'elements');

var elems = path.split('/');

for (var i = 0; i < elems.length; i++) {
file.append(elems[i]);
filePath = utils.joinPath(filePath, elems[i]);
if (elems[i] === 'locales') {
break;
}
}

var file = utils.getFile(filePath);
if (!file.exists()) {
throw new Error('Using inexistent shared elements file: ' + path +
' from: ' + this.webapp.domain);
Expand All @@ -249,14 +236,11 @@ WebappShared.prototype.pushElements = function(path) {
// Copy possible resources from components.
var resources = ['style.css', 'css', 'js', 'images', 'locales', 'fonts'];
resources.forEach(function(resource) {
var eachFile = this.gaia.sharedFolder.clone();
eachFile.append('elements');
eachFile.append(elementName);
eachFile.append(resource);

var eachFile = utils.getFile(this.gaia.sharedFolder.path, 'elements',
elementName, resource);
if (eachFile.exists()) {
var stagePath = 'shared/' + eachFile.getRelativeDescriptor(
this.gaia.sharedFolder);
var stagePath = 'shared/' +
utils.relativePath(this.gaia.sharedFolder.path, eachFile.path);
this.moveToBuildDir(eachFile, stagePath);
}
}, this);
Expand Down Expand Up @@ -388,8 +372,8 @@ WebappShared.prototype.copyShared = function() {

WebappShared.prototype.customizeShared = function() {
var self = this;
var sharedDataFile = this.webapp.buildDirectoryFile.clone();
sharedDataFile.append('gaia_shared.json');
var sharedDataFile = utils.getFile(this.webapp.buildDirectoryFile.path,
'gaia_shared.json');
if (sharedDataFile.exists()) {
var sharedData = JSON.parse(utils.getFileContent(sharedDataFile));
Object.keys(sharedData).forEach(function(kind) {
Expand Down

0 comments on commit 9ef3a27

Please sign in to comment.