Skip to content

Commit

Permalink
Remove make.js and the target fetching in gulpfile.js
Browse files Browse the repository at this point in the history
Note that we have to use `fs.writeFileSync` since `.to()` is not
available anymore. Moreover, introduce `safeSpawnSync` to make sure that
we check the return codes of the spawned processes properly.
  • Loading branch information
timvandermeij committed May 1, 2017
1 parent 0f1bd80 commit f5611d3
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 331 deletions.
104 changes: 40 additions & 64 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,16 @@ var DEFINES = {
PDFJS_NEXT: false,
};

function safeSpawnSync(command, parameters, options) {
var result = spawnSync(command, parameters, options);
if (result.status) {
console.log('Error: command "' + command + '" with parameters "' +
parameters + '" exited with code ' + result.status);
process.exit(result.status);
}
return result;
}

function createStringSource(filename, content) {
var source = stream.Readable({ objectMode: true });
source._read = function () {
Expand Down Expand Up @@ -679,12 +689,13 @@ gulp.task('minified-post', ['minified-pre'], function () {
// V8 chokes on very long sequences. Works around that.
var optsForHugeFile = {compress: {sequences: false}};

UglifyJS.minify(viewerFiles).code
.to(MINIFIED_DIR + '/web/pdf.viewer.js');
UglifyJS.minify(MINIFIED_DIR + '/build/pdf.js').code
.to(MINIFIED_DIR + '/build/pdf.min.js');
UglifyJS.minify(MINIFIED_DIR + '/build/pdf.worker.js', optsForHugeFile).code
.to(MINIFIED_DIR + '/build/pdf.worker.min.js');
fs.writeFileSync(MINIFIED_DIR + '/web/pdf.viewer.js',
UglifyJS.minify(viewerFiles).code);
fs.writeFileSync(MINIFIED_DIR + '/build/pdf.min.js',
UglifyJS.minify(MINIFIED_DIR + '/build/pdf.js').code);
fs.writeFileSync(MINIFIED_DIR + '/build/pdf.worker.min.js',
UglifyJS.minify(MINIFIED_DIR + '/build/pdf.worker.js',
optsForHugeFile).code);

console.log();
console.log('### Cleaning js files');
Expand Down Expand Up @@ -1220,14 +1231,14 @@ gulp.task('gh-pages-git', ['gh-pages-prepare', 'wintersmith'], function () {
var VERSION = getVersionJSON().version;
var reason = process.env['PDFJS_UPDATE_REASON'];

spawnSync('git', ['init'], {cwd: GH_PAGES_DIR});
spawnSync('git', ['remote', 'add', 'origin', REPO], {cwd: GH_PAGES_DIR});
spawnSync('git', ['add', '-A'], {cwd: GH_PAGES_DIR});
spawnSync('git', [
safeSpawnSync('git', ['init'], {cwd: GH_PAGES_DIR});
safeSpawnSync('git', ['remote', 'add', 'origin', REPO], {cwd: GH_PAGES_DIR});
safeSpawnSync('git', ['add', '-A'], {cwd: GH_PAGES_DIR});
safeSpawnSync('git', [
'commit', '-am', 'gh-pages site created via gulpfile.js script',
'-m', 'PDF.js version ' + VERSION + (reason ? ' - ' + reason : '')
], {cwd: GH_PAGES_DIR});
spawnSync('git', ['branch', '-m', 'gh-pages'], {cwd: GH_PAGES_DIR});
safeSpawnSync('git', ['branch', '-m', 'gh-pages'], {cwd: GH_PAGES_DIR});

console.log();
console.log('Website built in ' + GH_PAGES_DIR);
Expand All @@ -1243,7 +1254,7 @@ gulp.task('dist-repo-prepare', ['dist-pre'], function () {

rimraf.sync(DIST_DIR);
mkdirp.sync(DIST_DIR);
spawnSync('git', ['clone', '--depth', '1', DIST_REPO_URL, DIST_DIR]);
safeSpawnSync('git', ['clone', '--depth', '1', DIST_REPO_URL, DIST_DIR]);

console.log();
console.log('### Overwriting all files');
Expand Down Expand Up @@ -1331,10 +1342,10 @@ gulp.task('dist-repo-git', ['dist-repo-prepare'], function () {

var reason = process.env['PDFJS_UPDATE_REASON'];
var message = 'PDF.js version ' + VERSION + (reason ? ' - ' + reason : '');
spawnSync('git', ['add', '*'], {cwd: DIST_DIR});
spawnSync('git', ['commit', '-am', message], {cwd: DIST_DIR});
spawnSync('git', ['tag', '-a', 'v' + VERSION, '-m', message],
{cwd: DIST_DIR});
safeSpawnSync('git', ['add', '*'], {cwd: DIST_DIR});
safeSpawnSync('git', ['commit', '-am', message], {cwd: DIST_DIR});
safeSpawnSync('git', ['tag', '-a', 'v' + VERSION, '-m', message],
{cwd: DIST_DIR});

console.log();
console.log('Done. Push with');
Expand All @@ -1349,15 +1360,15 @@ gulp.task('mozcentralbaseline', function (done) {
console.log();
console.log('### Creating mozcentral baseline environment');

spawnSync('gulp', ['baseline'], {env: process.env});
safeSpawnSync('gulp', ['baseline'], {env: process.env, stdio: 'inherit'});

// Create a mozcentral build. We have to pass the working directory to the
// Gulp process using the `--cwd` option to override the behavior of Gulp
// changing to the root directory automatically.
// Create a mozcentral build.
if (checkDir(BASELINE_DIR + BUILD_DIR)) {
rimraf.sync(BASELINE_DIR + BUILD_DIR);
}
spawnSync('gulp', ['mozcentral', '--cwd', BASELINE_DIR], {env: process.env});
var workingDirectory = path.resolve(process.cwd(), BASELINE_DIR);
safeSpawnSync('gulp', ['mozcentral'],
{env: process.env, cwd: workingDirectory, stdio: 'inherit'});

// Copy the mozcentral build to the mozcentral baseline directory.
if (checkDir(MOZCENTRAL_BASELINE_DIR)) {
Expand All @@ -1369,10 +1380,10 @@ gulp.task('mozcentralbaseline', function (done) {
.pipe(gulp.dest(MOZCENTRAL_BASELINE_DIR))
.on('end', function () {
// Commit the mozcentral baseline.
spawnSync('git', ['init'], {cwd: MOZCENTRAL_BASELINE_DIR});
spawnSync('git', ['add', '.'], {cwd: MOZCENTRAL_BASELINE_DIR});
spawnSync('git', ['commit', '-m', 'mozcentral baseline'],
{cwd: MOZCENTRAL_BASELINE_DIR});
safeSpawnSync('git', ['init'], {cwd: MOZCENTRAL_BASELINE_DIR});
safeSpawnSync('git', ['add', '.'], {cwd: MOZCENTRAL_BASELINE_DIR});
safeSpawnSync('git', ['commit', '-m', '"mozcentral baseline"'],
{cwd: MOZCENTRAL_BASELINE_DIR});
done();
});
});
Expand All @@ -1381,7 +1392,8 @@ gulp.task('mozcentraldiff', ['mozcentral'], function (done) {
console.log();
console.log('### Creating mozcentral diff');

spawnSync('gulp', ['mozcentralbaseline'], {env: process.env});
safeSpawnSync('gulp', ['mozcentralbaseline'],
{env: process.env, stdio: 'inherit'});

// Create the diff between the current mozcentral build and the
// baseline mozcentral build, which both exist at this point.
Expand All @@ -1393,8 +1405,8 @@ gulp.task('mozcentraldiff', ['mozcentral'], function (done) {
gulp.src([BUILD_DIR + 'mozcentral/**/*'])
.pipe(gulp.dest(MOZCENTRAL_BASELINE_DIR))
.on('end', function () {
spawnSync('git', ['add', '-A'], {cwd: MOZCENTRAL_BASELINE_DIR});
var diff = spawnSync('git',
safeSpawnSync('git', ['add', '-A'], {cwd: MOZCENTRAL_BASELINE_DIR});
var diff = safeSpawnSync('git',
['diff', '--binary', '--cached', '--unified=8'],
{cwd: MOZCENTRAL_BASELINE_DIR}).stdout;

Expand All @@ -1407,39 +1419,3 @@ gulp.task('mozcentraldiff', ['mozcentral'], function (done) {
});
});
});

// Getting all shelljs registered tasks and register them with gulp
require('./make.js');

var gulpContext = false;
for (var taskName in global.target) {
if (taskName in gulp.tasks) {
continue;
}

var task = (function (shellJsTask) {
return function () {
gulpContext = true;
try {
shellJsTask.call(global.target);
} finally {
gulpContext = false;
}
};
})(global.target[taskName]);
gulp.task(taskName, task);
}

Object.keys(gulp.tasks).forEach(function (taskName) {
var oldTask = global.target[taskName] || function () {
gulp.run(taskName);
};

global.target[taskName] = function (args) {
// The require('shelljs/make') import in make.js will try to execute tasks
// listed in arguments, guarding with gulpContext
if (gulpContext) {
oldTask.call(global.target, args);
}
};
});
Loading

0 comments on commit f5611d3

Please sign in to comment.