signal-desktop/Gruntfile.js

445 lines
12 KiB
JavaScript
Raw Normal View History

2020-10-30 20:34:04 +00:00
// Copyright 2014-2020 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
const { join } = require('path');
const importOnce = require('node-sass-import-once');
const rimraf = require('rimraf');
const mkdirp = require('mkdirp');
const spectron = require('spectron');
const asar = require('asar');
const fs = require('fs');
const assert = require('assert');
const sass = require('node-sass');
2020-09-09 00:46:29 +00:00
const packageJson = require('./package.json');
/* eslint-disable more/no-then, no-console */
2014-11-23 03:36:52 +00:00
module.exports = grunt => {
const bower = grunt.file.readJSON('bower.json');
const components = [];
// eslint-disable-next-line guard-for-in, no-restricted-syntax
for (const i in bower.concat.app) {
components.push(bower.concat.app[i]);
}
const libtextsecurecomponents = [];
// eslint-disable-next-line guard-for-in, no-restricted-syntax
for (const i in bower.concat.libtextsecure) {
libtextsecurecomponents.push(bower.concat.libtextsecure[i]);
}
2018-04-27 21:25:04 +00:00
grunt.loadNpmTasks('grunt-sass');
grunt.initConfig({
2014-11-23 03:36:52 +00:00
pkg: grunt.file.readJSON('package.json'),
concat: {
components: {
src: components,
dest: 'js/components.js',
},
2015-01-14 22:22:26 +00:00
libtextsecurecomponents: {
src: libtextsecurecomponents,
dest: 'libtextsecure/components.js',
},
2014-11-10 05:44:56 +00:00
test: {
src: [
'node_modules/mocha/mocha.js',
'node_modules/chai/chai.js',
2018-04-27 21:25:04 +00:00
'test/_test.js',
2014-11-10 05:44:56 +00:00
],
dest: 'test/test.js',
},
// TODO: Move errors back down?
libtextsecure: {
options: {
2018-04-27 21:25:04 +00:00
banner: ';(function() {\n',
footer: '})();\n',
},
src: [
'libtextsecure/libsignal-protocol.js',
2016-04-22 20:43:09 +00:00
'libtextsecure/protocol_wrapper.js',
'libtextsecure/storage/user.js',
'libtextsecure/storage/unprocessed.js',
'libtextsecure/protobufs.js',
],
dest: 'js/libtextsecure.js',
},
libtextsecuretest: {
src: [
'node_modules/jquery/dist/jquery.js',
'components/mock-socket/dist/mock-socket.js',
'node_modules/mocha/mocha.js',
'node_modules/chai/chai.js',
2018-04-27 21:25:04 +00:00
'libtextsecure/test/_test.js',
],
dest: 'libtextsecure/test/test.js',
2018-04-27 21:25:04 +00:00
},
},
sass: {
options: {
implementation: sass,
sourceMap: true,
2018-04-27 21:25:04 +00:00
importer: importOnce,
},
dev: {
files: {
2018-04-27 21:25:04 +00:00
'stylesheets/manifest.css': 'stylesheets/manifest.scss',
2019-12-17 20:25:57 +00:00
'stylesheets/manifest_bridge.css': 'stylesheets/manifest_bridge.scss',
2018-04-27 21:25:04 +00:00
},
},
},
copy: {
deps: {
2018-04-27 21:25:04 +00:00
files: [
{
src: 'components/mp3lameencoder/lib/Mp3LameEncoder.js',
dest: 'js/Mp3LameEncoder.min.js',
},
{
src: 'components/webaudiorecorder/lib/WebAudioRecorderMp3.js',
dest: 'js/WebAudioRecorderMp3.js',
},
],
},
},
2014-11-23 03:36:52 +00:00
watch: {
2015-02-23 19:54:40 +00:00
libtextsecure: {
files: ['./libtextsecure/*.js', './libtextsecure/storage/*.js'],
2018-04-27 21:25:04 +00:00
tasks: ['concat:libtextsecure'],
2015-02-23 19:54:40 +00:00
},
protobuf: {
files: ['./protos/SignalService.proto'],
tasks: ['exec:build-protobuf'],
},
2018-05-07 18:10:49 +00:00
sass: {
files: ['./stylesheets/*.scss'],
tasks: ['sass'],
},
transpile: {
files: ['./ts/**/*.ts', './ts/**/*.tsx'],
2018-04-27 21:25:04 +00:00
tasks: ['exec:transpile'],
},
2014-11-23 03:36:52 +00:00
},
exec: {
2018-09-05 17:35:27 +00:00
'tx-pull-new': {
cmd: 'tx pull -a --minimum-perc=80',
},
'tx-pull': {
2018-04-27 21:25:04 +00:00
cmd: 'tx pull',
},
2018-04-27 21:25:04 +00:00
transpile: {
2018-05-07 18:11:12 +00:00
cmd: 'yarn transpile',
2018-04-27 21:25:04 +00:00
},
'build-protobuf': {
cmd: 'yarn build-protobuf',
},
},
'test-release': {
osx: {
2020-01-08 17:44:54 +00:00
archive: `mac/${packageJson.productName}.app/Contents/Resources/app.asar`,
exe: `mac/${packageJson.productName}.app/Contents/MacOS/${packageJson.productName}`,
},
mas: {
archive: 'mas/Signal.app/Contents/Resources/app.asar',
2020-01-08 17:44:54 +00:00
exe: `mas/${packageJson.productName}.app/Contents/MacOS/${packageJson.productName}`,
},
linux: {
archive: 'linux-unpacked/resources/app.asar',
exe: `linux-unpacked/${packageJson.name}`,
},
win: {
archive: 'win-unpacked/resources/app.asar',
exe: `win-unpacked/${packageJson.productName}.exe`,
2018-04-27 21:25:04 +00:00
},
},
2018-04-27 21:25:04 +00:00
gitinfo: {}, // to be populated by grunt gitinfo
2014-11-23 03:36:52 +00:00
});
Object.keys(grunt.config.get('pkg').devDependencies).forEach(key => {
2018-04-27 21:25:04 +00:00
if (/^grunt(?!(-cli)?$)/.test(key)) {
// ignore grunt and grunt-cli
2014-11-23 03:36:52 +00:00
grunt.loadNpmTasks(key);
}
});
// Transifex does not understand placeholders, so this task patches all non-en
// locales with missing placeholders
grunt.registerTask('locale-patch', () => {
const en = grunt.file.readJSON('_locales/en/messages.json');
grunt.file.recurse('_locales', (abspath, rootdir, subdir, filename) => {
2018-04-27 21:25:04 +00:00
if (subdir === 'en' || filename !== 'messages.json') {
return;
}
const messages = grunt.file.readJSON(abspath);
// eslint-disable-next-line no-restricted-syntax
for (const key in messages) {
2018-04-27 21:25:04 +00:00
if (en[key] !== undefined && messages[key] !== undefined) {
if (
en[key].placeholders !== undefined &&
messages[key].placeholders === undefined
) {
2016-03-11 20:00:55 +00:00
messages[key].placeholders = en[key].placeholders;
}
}
}
grunt.file.write(abspath, `${JSON.stringify(messages, null, 4)}\n`);
});
});
grunt.registerTask('getExpireTime', () => {
2018-04-27 21:25:04 +00:00
grunt.task.requires('gitinfo');
const gitinfo = grunt.config.get('gitinfo');
2018-12-13 21:41:42 +00:00
const committed = gitinfo.local.branch.current.lastCommitTime;
const time = Date.parse(committed) + 1000 * 60 * 60 * 24 * 90;
2018-04-27 21:25:04 +00:00
grunt.file.write(
'config/local-production.json',
`${JSON.stringify({ buildExpiration: time })}\n`
2018-04-27 21:25:04 +00:00
);
});
grunt.registerTask('clean-release', () => {
rimraf.sync('release');
mkdirp.sync('release');
});
function runTests(environment, cb) {
let failure;
const { Application } = spectron;
const electronBinary =
2018-04-27 21:25:04 +00:00
process.platform === 'win32' ? 'electron.cmd' : 'electron';
const path = join(__dirname, 'node_modules', '.bin', electronBinary);
const args = [join(__dirname, 'main.js')];
console.log('Starting path', path, 'with args', args);
const app = new Application({
path,
args,
env: {
2018-04-27 21:25:04 +00:00
NODE_ENV: environment,
},
requireName: 'unused',
});
function getMochaResults() {
// eslint-disable-next-line no-undef
return window.mochaResults;
}
2018-04-27 21:25:04 +00:00
app
.start()
.then(() => {
console.log('App started. Now waiting for test results...');
return app.client.waitUntil(
() =>
app.client
.execute(getMochaResults)
.then(data => Boolean(data.value)),
2018-12-13 21:41:42 +00:00
25000,
2018-04-27 21:25:04 +00:00
'Expected to find window.mochaResults set!'
);
})
.then(() => app.client.execute(getMochaResults))
.then(data => {
const results = data.value;
if (!results) {
failure = () => grunt.fail.fatal("Couldn't extract test results.");
return app.client.log('browser');
}
2018-04-27 21:25:04 +00:00
if (results.failures > 0) {
console.error(results.reports);
failure = () =>
grunt.fail.fatal(`Found ${results.failures} failing unit tests.`);
2018-04-27 21:25:04 +00:00
return app.client.log('browser');
}
grunt.log.ok(`${results.passes} tests passed.`);
return null;
2018-04-27 21:25:04 +00:00
})
.then(logs => {
2018-04-27 21:25:04 +00:00
if (logs) {
console.error();
console.error('Because tests failed, printing browser logs:');
console.error(logs);
}
})
.catch(error => {
failure = () =>
2018-04-27 21:25:04 +00:00
grunt.fail.fatal(
`Something went wrong: ${error.message} ${error.stack}`
2018-04-27 21:25:04 +00:00
);
})
.then(() => {
2018-04-27 21:25:04 +00:00
// We need to use the failure variable and this early stop to clean up before
// shutting down. Grunt's fail methods are the only way to set the return value,
// but they shut the process down immediately!
if (failure) {
console.log();
console.log('Main process logs:');
return app.client.getMainProcessLogs().then(logs => {
logs.forEach(log => {
console.log(log);
});
return app.stop();
});
}
return app.stop();
2018-04-27 21:25:04 +00:00
})
.then(() => {
2018-04-27 21:25:04 +00:00
if (failure) {
failure();
}
cb();
})
.catch(error => {
2018-04-27 21:25:04 +00:00
console.error('Second-level error:', error.message, error.stack);
if (failure) {
failure();
}
cb();
});
}
grunt.registerTask(
'unit-tests',
'Run unit tests w/Electron',
function thisNeeded() {
const environment = grunt.option('env') || 'test';
const done = this.async();
runTests(environment, done);
}
);
2018-04-27 21:25:04 +00:00
grunt.registerTask(
'lib-unit-tests',
'Run libtextsecure unit tests w/Electron',
function thisNeeded() {
const environment = grunt.option('env') || 'test-lib';
const done = this.async();
2018-04-27 21:25:04 +00:00
runTests(environment, done);
}
);
grunt.registerMultiTask(
'test-release',
'Test packaged releases',
function thisNeeded() {
const dir = grunt.option('dir') || 'release';
const environment = grunt.option('env') || 'production';
const config = this.data;
const archive = [dir, config.archive].join('/');
const files = [
'config/default.json',
`config/${environment}.json`,
`config/local-${environment}.json`,
];
console.log(this.target, archive);
const releaseFiles = files.concat(config.files || []);
releaseFiles.forEach(fileName => {
console.log(fileName);
try {
asar.statFile(archive, fileName);
return true;
} catch (e) {
console.log(e);
throw new Error(`Missing file ${fileName}`);
}
});
if (config.appUpdateYML) {
const appUpdateYML = [dir, config.appUpdateYML].join('/');
if (fs.existsSync(appUpdateYML)) {
console.log('auto update ok');
} else {
throw new Error(`Missing auto update config ${appUpdateYML}`);
}
}
const done = this.async();
// A simple test to verify a visible window is opened with a title
const { Application } = spectron;
const path = [dir, config.exe].join('/');
console.log('Starting path', path);
const app = new Application({
path,
});
const sleep = millis =>
new Promise(resolve => setTimeout(resolve, millis));
Promise.race([app.start(), sleep(15000)])
.then(() => {
if (!app.isRunning()) {
throw new Error('Application failed to start');
}
return app.client.getWindowCount();
})
.then(count => {
assert.equal(count, 1);
console.log('window opened');
})
.then(() =>
// Get the window's title
app.client.getTitle()
)
.then(title => {
// Verify the window's title
assert.equal(title, packageJson.productName);
console.log('title ok');
})
.then(() => {
assert(
app.chromeDriver.logLines.indexOf(`NODE_ENV ${environment}`) > -1
);
console.log('environment ok');
})
.then(
() =>
// Successfully completed test
app.stop(),
error =>
// Test failed!
app.stop().then(() => {
grunt.fail.fatal(`Test failed: ${error.message} ${error.stack}`);
})
)
.catch(error => {
console.log('Main process logs:');
app.client.getMainProcessLogs().then(logs => {
logs.forEach(log => {
console.log(log);
});
// Test failed!
grunt.fail.fatal(`Failure! ${error.message} ${error.stack}`);
});
})
.then(done);
}
);
2018-09-05 17:35:27 +00:00
grunt.registerTask('tx', [
'exec:tx-pull-new',
'exec:tx-pull',
'locale-patch',
]);
grunt.registerTask('dev', ['default', 'watch']);
grunt.registerTask('test', ['unit-tests', 'lib-unit-tests']);
grunt.registerTask('date', ['gitinfo', 'getExpireTime']);
2018-04-27 21:25:04 +00:00
grunt.registerTask('default', [
2018-05-07 23:05:49 +00:00
'exec:build-protobuf',
'exec:transpile',
2018-04-27 21:25:04 +00:00
'concat',
'copy:deps',
'sass',
'date',
]);
};