Repository navigation
Expand file tree
/
Copy pathutils.js
More file actions
315 lines (271 loc) · 9.25 KB
/
Copy pathutils.js
File metadata and controls
315 lines (271 loc) · 9.25 KB
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
var config = require('./config').config;
const { Cc, Ci, Cr, Cu } = require('chrome');
Cu.import('resource://gre/modules/XPCOMUtils.jsm');
Cu.import('resource://gre/modules/FileUtils.jsm');
Cu.import('resource://gre/modules/Services.jsm');
function isSubjectToBranding(path) {
return /shared[\/\\][a-zA-Z]+[\/\\]branding$/.test(path) ||
/branding[\/\\]initlogo.png/.test(path);
}
/**
* Return a File object representing the directory of a given name/path.
*
* @param {String} dirName The name of the directory in GAIA_DIR or a full path.
* @param {Boolean} maybeGaiaSubDir
* If true, try to locate the dir within GAIA_DIR.
* @return {File} dir the File object.
*/
function getDir(dirName, maybeGaiaSubDir) {
let dir;
// Assume directory is a absolute path first:
try {
dir = new FileUtils.File(dirName);
} catch (e) {
//dump('-*- utils.js cannot find ' + dirName + '\n');
}
if (!maybeGaiaSubDir)
return dir;
if (!dir || !dir.exists()) {
// Assume directory is a subdirectory of GAIA_DIR
dir = new FileUtils.File(config.GAIA_DIR);
dirName.split('/').forEach(function(name) {
dir.append(name);
});
}
return dir;
}
/**
* Return an array of sub dir object with given dir object
*
* @param {File} dir The directory to search for sub directories.
* @return {Array} The list of all sub directories.
*/
function getSubDirs(dir) {
let subDirs = [];
let files = dir.directoryEntries;
while (files.hasMoreElements()) {
let file = files.getNext().QueryInterface(Ci.nsILocalFile);
if (file.isDirectory()) {
subDirs.push(file);
}
}
return subDirs;
}
/**
* Returns an array of nsIFile's for a given directory
*
* @param {nsIFile} dir directory to read.
* @param {boolean} recursive set to true in order to walk recursively.
* @param {RegExp} exclude optional filter to exclude file/directories.
*
* @return {Array} list of nsIFile's.
*/
function ls(dir, recursive, exclude) {
let results = [];
let files = dir.directoryEntries;
while (files.hasMoreElements()) {
let file = files.getNext().QueryInterface(Ci.nsILocalFile);
if (!exclude || !exclude.test(file.leafName)) {
results.push(file);
if (recursive && file.isDirectory()) {
results = results.concat(ls(file, true, exclude));
}
}
}
return results;
}
function getFileContent(file) {
try {
let fileStream = Cc['@mozilla.org/network/file-input-stream;1']
.createInstance(Ci.nsIFileInputStream);
fileStream.init(file, 1, 0, false);
let converterStream = Cc['@mozilla.org/intl/converter-input-stream;1']
.createInstance(Ci.nsIConverterInputStream);
converterStream.init(fileStream, 'utf-8', fileStream.available(),
Ci.nsIConverterInputStream.DEFAULT_REPLACEMENT_CHARACTER);
let out = {};
let count = fileStream.available();
converterStream.readString(count, out);
var content = out.value;
converterStream.close();
fileStream.close();
} catch (e) {
let msg = (file && file.path) ? '\nfile not found: ' + file.path : '';
throw new Error(' -*- build/utils.js: ' + e + msg + '\n');
}
return content;
}
function writeContent(file, content) {
var fileStream = Cc['@mozilla.org/network/file-output-stream;1']
.createInstance(Ci.nsIFileOutputStream);
fileStream.init(file, 0x02 | 0x08 | 0x20, 0666, 0);
let converterStream = Cc['@mozilla.org/intl/converter-output-stream;1']
.createInstance(Ci.nsIConverterOutputStream);
converterStream.init(fileStream, 'utf-8', 0, 0);
converterStream.writeString(content);
converterStream.close();
}
// Return an nsIFile by joining paths given as arguments
// First path has to be an absolute one
function getFile() {
try {
let file = new FileUtils.File(arguments[0]);
if (arguments.length > 1) {
for (let i = 1; i < arguments.length; i++) {
let dir = arguments[i];
dir.split('/').forEach(function(name) {
file.append(name);
});
}
}
return file;
} catch (e) {
throw new Error(' -*- build/utils.js: Invalid file path (' +
Array.slice(arguments).join(', ') + ')\n' + e + '\n');
}
}
function ensureFolderExists(file) {
if (!file.exists()) {
try {
file.create(Ci.nsIFile.DIRECTORY_TYPE, parseInt('0755', 8));
} catch (e if e.result == Cr.NS_ERROR_FILE_ALREADY_EXISTS) {
// Bug 808513: Ignore races between `if exists() then create()`.
return;
}
}
}
function getJSON(file) {
try {
let content = getFileContent(file);
return JSON.parse(content);
} catch (e) {
dump('Invalid JSON file : ' + file.path + '\n');
throw e;
}
}
function makeWebappsObject(appdirs) {
return {
forEach: function(fun) {
appdirs.forEach(function(app) {
let appDir = getFile(app);
if (!appDir.exists()) {
throw new Error(' -*- build/utils.js: file not found (' + app + ')\n');
}
let manifestFile = appDir.clone();
manifestFile.append('manifest.webapp');
let updateFile = appDir.clone();
updateFile.append('update.webapp');
// Ignore directories without manifest
if (!manifestFile.exists() && !updateFile.exists()) {
return;
}
let manifest = manifestFile.exists() ? manifestFile : updateFile;
// Use the folder name as the the domain name
let domain = appDir.leafName + '.' + config.GAIA_DOMAIN;
let webapp = {
manifest: getJSON(manifest),
manifestFile: manifest,
url: config.GAIA_SCHEME + domain + (config.GAIA_PORT ? config.GAIA_PORT : ''),
domain: domain,
sourceDirectoryFile: manifestFile.parent,
buildDirectoryFile: manifestFile.parent,
sourceDirectoryName: appDir.leafName,
sourceAppDirectoryName: appDir.parent.leafName
};
// External webapps have a `metadata.json` file
let metaData = webapp.sourceDirectoryFile.clone();
metaData.append('metadata.json');
if (metaData.exists()) {
webapp.metaData = getJSON(metaData);
}
// Some webapps control their own build
let buildMetaData = webapp.sourceDirectoryFile.clone();
buildMetaData.append('gaia_build.json');
if (buildMetaData.exists()) {
webapp.build = getJSON(buildMetaData);
if (webapp.build.dir) {
let buildDirectoryFile = webapp.sourceDirectoryFile.clone();
webapp.build.dir.split('/').forEach(function(segment) {
if (segment == "..")
buildDirectoryFile = buildDirectoryFile.parent;
else
buildDirectoryFile.append(segment);
});
webapp.buildDirectoryFile = buildDirectoryFile;
}
}
fun(webapp);
});
}
};
}
const Gaia = {
engine: config.GAIA_ENGINE,
sharedFolder: getFile(config.GAIA_DIR, 'shared'),
webapps: makeWebappsObject(config.GAIA_APPDIRS.split(' ')),
aggregatePrefix: 'gaia_build_',
distributionDir: config.GAIA_DISTRIBUTION_DIR
};
function registerProfileDirectory() {
let directoryProvider = {
getFile: function provider_getFile(prop, persistent) {
persistent.value = true;
if (prop != 'ProfD' && prop != 'ProfLDS') {
throw Cr.NS_ERROR_FAILURE;
}
return new FileUtils.File(config.PROFILE_DIR);
},
QueryInterface: XPCOMUtils.generateQI([Ci.nsIDirectoryServiceProvider,
Ci.nsISupports])
};
Cc['@mozilla.org/file/directory_service;1']
.getService(Ci.nsIProperties)
.QueryInterface(Ci.nsIDirectoryService)
.registerProvider(directoryProvider);
}
if (Gaia.engine === 'xpcshell') {
registerProfileDirectory();
}
function gaiaOriginURL(name) {
return config.GAIA_SCHEME + name + '.' + config.GAIA_DOMAIN + (config.GAIA_PORT ? config.GAIA_PORT : '');
}
function gaiaManifestURL(name) {
return gaiaOriginURL(name) + '/manifest.webapp';
}
function getDistributionFileContent(name, defaultContent) {
if (Gaia.distributionDir) {
let distributionFile = getFile(Gaia.distributionDir, name + '.json');
if (distributionFile.exists()) {
return getFileContent(distributionFile);
}
}
return JSON.stringify(defaultContent, null, ' ');
}
function getAbsoluteOrRelativePath(path) {
// First check relative path to gaia folder
let abs_path_chunks = [config.GAIA_DIR].concat(path.split(/\/|\\/));
let file = getFile.apply(null, abs_path_chunks);
if (!file.exists()) {
try {
// Then check absolute path
return getFile(path);
} catch(e) {}
}
return file;
}
exports.isSubjectToBranding = isSubjectToBranding;
exports.getDir = getDir;
exports.getSubDirs = getSubDirs;
exports.ls = ls;
exports.getFileContent = getFileContent;
exports.writeContent = writeContent;
exports.getFile = getFile;
exports.ensureFolderExists = ensureFolderExists;
exports.getJSON = getJSON;
exports.makeWebappsObject = makeWebappsObject;
exports.Gaia = Gaia;
exports.registerProfileDirectory = registerProfileDirectory;
exports.gaiaOriginURL = gaiaOriginURL;
exports.gaiaManifestURL = gaiaManifestURL;
exports.getDistributionFileContent = getDistributionFileContent;
exports.getAbsoluteOrRelativePath = getAbsoluteOrRelativePath