Skip to content

Commit b96152b

Browse files
committed
Support for building .crx file (re-visited)
1 parent c16a6ae commit b96152b

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

make.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ var ROOT_DIR = __dirname + '/', // absolute path to project's root
77
BUILD_DIR = 'build/',
88
BUILD_TARGET = BUILD_DIR + 'pdf.js',
99
FIREFOX_BUILD_DIR = BUILD_DIR + '/firefox/',
10+
CHROME_BUILD_DIR = BUILD_DIR + '/chrome/',
1011
EXTENSION_SRC_DIR = 'extensions/',
1112
LOCALE_SRC_DIR = 'l10n/',
1213
GH_PAGES_DIR = BUILD_DIR + 'gh-pages/',
@@ -107,6 +108,8 @@ target.web = function() {
107108
cp('-R', GENERIC_DIR + '/*', GH_PAGES_DIR);
108109
cp(FIREFOX_BUILD_DIR + '/*.xpi', FIREFOX_BUILD_DIR + '/*.rdf',
109110
GH_PAGES_DIR + EXTENSION_SRC_DIR + 'firefox/');
111+
cp(CHROME_BUILD_DIR + '/*.crx', FIREFOX_BUILD_DIR + '/*.rdf',
112+
GH_PAGES_DIR + EXTENSION_SRC_DIR + 'chrome/');
110113
cp('web/index.html.template', GH_PAGES_DIR + '/index.html');
111114

112115
cd(GH_PAGES_DIR);
@@ -256,6 +259,7 @@ target.pagesrepo = function() {
256259
mkdir('-p', GH_PAGES_DIR + '/web/images');
257260
mkdir('-p', GH_PAGES_DIR + BUILD_DIR);
258261
mkdir('-p', GH_PAGES_DIR + EXTENSION_SRC_DIR + '/firefox');
262+
mkdir('-p', GH_PAGES_DIR + EXTENSION_SRC_DIR + '/chrome');
259263
};
260264

261265

@@ -556,6 +560,70 @@ target.chrome = function() {
556560
]
557561
};
558562
builder.build(setup);
563+
564+
// Bundle the files to a Chrome extension file .crx if path to key is set
565+
var pem = env['PDFJS_CHROME_KEY'];
566+
if (!pem) {
567+
return;
568+
}
569+
570+
echo();
571+
echo('### Bundling .crx extension into ' + CHROME_BUILD_DIR);
572+
573+
if (!test('-f', pem)) {
574+
echo('Incorrect PDFJS_CHROME_KEY path');
575+
exit(1);
576+
}
577+
578+
var browserManifest = env['PDF_BROWSERS'] ||
579+
'test/resources/browser_manifests/browser_manifest.json';
580+
581+
if (!test('-f', browserManifest)) {
582+
echo('Browser manifest file ' + browserManifest + ' does not exist.');
583+
echo('Try copying one of the examples in test/resources/browser_manifests');
584+
exit(1);
585+
}
586+
587+
try {
588+
var manifest = JSON.parse(cat(browserManifest));
589+
} catch (e) {
590+
echo('Malformed browser manifest file');
591+
echo(e.message);
592+
exit(1);
593+
}
594+
595+
var executable;
596+
manifest.forEach(function(browser) {
597+
if (browser.name === 'chrome') {
598+
executable = browser.path;
599+
}
600+
});
601+
602+
// If there was no chrome entry in the browser manifest, exit
603+
if(!executable) {
604+
echo('There was no \'chrome\' entry in the browser manifest');
605+
exit(1);
606+
}
607+
608+
// If we're on a Darwin (Mac) OS, then let's check for an .app path
609+
if (process.platform === 'darwin' && executable.indexOf('.app') !== -1) {
610+
executable = path.join(executable, 'Contents', 'MacOS', 'Google Chrome');
611+
}
612+
613+
// If the chrome executable doesn't exist
614+
if(!test('-f', executable)) {
615+
echo('Incorrect executable path to chrome');
616+
exit(1);
617+
}
618+
619+
// Let chrome pack the extension for us
620+
exec('"' + executable + '"' +
621+
' --no-message-box' +
622+
' "--pack-extension=' + ROOT_DIR + CHROME_BUILD_DIR + '"' +
623+
' "--pack-extension-key=' + pem + '"');
624+
625+
// Rename to pdf.js.crx
626+
mv(BUILD_DIR + 'chrome.crx', CHROME_BUILD_DIR + 'pdf.js.crx');
559627
};
560628

561629

0 commit comments

Comments
 (0)