betaflight-configurator/gulpfile.js

488 lines
14 KiB
JavaScript
Raw Normal View History

'use strict';
2018-01-10 14:36:12 +00:00
const pkg = require('./package.json');
const child_process = require('child_process');
const fs = require('fs');
const path = require('path');
const zip = require('gulp-zip');
const del = require('del');
const NwBuilder = require('nw-builder');
const makensis = require('makensis');
const deb = require('gulp-debian');
const gulp = require('gulp');
const concat = require('gulp-concat');
const install = require("gulp-install");
const rename = require('gulp-rename');
const os = require('os');
const DIST_DIR = './dist/';
const APPS_DIR = './apps/';
const DEBUG_DIR = './debug/';
const RELEASE_DIR = './release/';
2018-01-09 00:44:13 +00:00
var nwBuilderOptions = {
version: '0.27.4',
files: './dist/**/*',
2018-01-21 16:09:36 +00:00
macIcns: './src/images/bf_icon.icns',
2018-01-09 00:44:13 +00:00
macPlist: { 'CFBundleDisplayName': 'Betaflight Configurator'},
2018-01-21 16:09:36 +00:00
winIco: './src/images/bf_icon.ico'
2018-01-09 00:44:13 +00:00
};
2018-01-10 14:36:12 +00:00
//-----------------
//Pre tasks operations
//-----------------
const SELECTED_PLATFORMS = getInputPlatforms();
//-----------------
//Tasks
//-----------------
gulp.task('clean', gulp.parallel(clean_dist, clean_apps, clean_debug, clean_release));
gulp.task('clean-dist', clean_dist);
gulp.task('clean-apps', clean_apps);
gulp.task('clean-debug', clean_debug);
gulp.task('clean-release', clean_release);
gulp.task('clean-cache', clean_cache);
2018-01-24 23:37:07 +00:00
var distBuild = gulp.series(clean_dist, dist_src, dist_locale, dist_libraries, dist_resources);
2018-01-10 14:36:12 +00:00
gulp.task('dist', distBuild);
var appsBuild = gulp.series(gulp.parallel(clean_apps, distBuild), apps, gulp.parallel(listPostBuildTasks(APPS_DIR)));
gulp.task('apps', appsBuild);
var debugBuild = gulp.series(gulp.parallel(clean_debug, distBuild), debug, gulp.parallel(listPostBuildTasks(DEBUG_DIR)), start_debug)
gulp.task('debug', debugBuild);
var releaseBuild = gulp.series(gulp.parallel(clean_release, appsBuild), gulp.parallel(listReleaseTasks()));
gulp.task('release', releaseBuild);
gulp.task('default', debugBuild);
2018-01-09 00:44:13 +00:00
// -----------------
// Helper functions
// -----------------
// Get platform from commandline args
// #
2017-12-18 20:09:20 +00:00
// # gulp <task> [<platform>]+ Run only for platform(s) (with <platform> one of --linux64, --linux32, --osx64, --win32, --win64, or --chromeos)
// #
2018-01-10 14:36:12 +00:00
function getInputPlatforms() {
2018-01-08 10:37:32 +00:00
var supportedPlatforms = ['linux64', 'linux32', 'osx64', 'win32','win64', 'chromeos'];
var platforms = [];
var regEx = /--(\w+)/;
for (var i = 3; i < process.argv.length; i++) {
var arg = process.argv[i].match(regEx)[1];
if (supportedPlatforms.indexOf(arg) > -1) {
platforms.push(arg);
} else {
console.log('Unknown platform: ' + arg);
process.exit();
}
}
if (platforms.length === 0) {
var defaultPlatform = getDefaultPlatform();
if (supportedPlatforms.indexOf(defaultPlatform) > -1) {
platforms.push(defaultPlatform);
} else {
2018-01-08 10:37:32 +00:00
console.error(`Your current platform (${os.platform()}) is not a supported build platform. Please specify platform to build for on the command line.`);
process.exit();
}
}
2018-01-08 10:37:32 +00:00
if (platforms.length > 0) {
console.log('Building for platform(s): ' + platforms + '.');
} else {
console.error('No suitables platforms found.');
process.exit();
}
return platforms;
}
// Gets the default platform to be used
function getDefaultPlatform() {
var defaultPlatform;
2017-12-03 20:18:03 +00:00
switch (os.platform()) {
case 'darwin':
defaultPlatform = 'osx64';
2017-12-03 20:18:03 +00:00
break;
case 'linux':
defaultPlatform = 'linux64';
2017-12-03 20:18:03 +00:00
break;
case 'win32':
defaultPlatform = 'win32';
break;
default:
defaultPlatform = '';
break;
}
return defaultPlatform;
}
2018-01-10 14:36:12 +00:00
function getPlatforms() {
return SELECTED_PLATFORMS.slice();
}
2018-01-08 10:37:32 +00:00
function removeItem(platforms, item) {
var index = platforms.indexOf(item);
if (index >= 0) {
platforms.splice(index, 1);
}
}
function getRunDebugAppCommand(arch) {
switch (arch) {
case 'osx64':
2018-01-10 14:36:12 +00:00
return 'open ' + path.join(DEBUG_DIR, pkg.name, arch, pkg.name + '.app');
break;
case 'linux64':
case 'linux32':
2018-01-10 14:36:12 +00:00
return path.join(DEBUG_DIR, pkg.name, arch, pkg.name);
break;
2017-12-03 20:18:03 +00:00
case 'win32':
case 'win64':
2018-01-10 14:36:12 +00:00
return path.join(DEBUG_DIR, pkg.name, arch, pkg.name + '.exe');
2017-12-03 20:18:03 +00:00
break;
default:
2017-12-04 10:03:35 +00:00
return '';
2017-12-03 20:18:03 +00:00
break;
}
}
2018-01-10 14:36:12 +00:00
function getReleaseFilename(platform, ext) {
return 'betaflight-configurator_' + pkg.version + '_' + platform + '.' + ext;
}
2018-01-10 14:36:12 +00:00
function clean_dist() {
return del([DIST_DIR + '**'], { force: true });
};
2018-01-10 14:36:12 +00:00
function clean_apps() {
return del([APPS_DIR + '**'], { force: true });
};
2018-01-10 14:36:12 +00:00
function clean_debug() {
return del([DEBUG_DIR + '**'], { force: true });
};
2018-01-10 14:36:12 +00:00
function clean_release() {
return del([RELEASE_DIR + '**'], { force: true });
};
2018-01-10 14:36:12 +00:00
function clean_cache() {
return del(['./cache/**'], { force: true });
2018-01-10 14:36:12 +00:00
};
// Real work for dist task. Done in another task to call it via
// run-sequence.
2018-01-24 23:37:07 +00:00
function dist_src() {
var distSources = [
2018-01-29 14:40:04 +00:00
'./src/**/*',
'!./src/css/dropdown-lists/LICENSE',
'!./src/css/font-awesome/css/font-awesome.css',
'!./src/css/opensans_webfontkit/*.{txt,html}',
'!./src/support/**'
];
2018-01-24 23:37:07 +00:00
2018-01-21 16:09:36 +00:00
return gulp.src(distSources, { base: 'src' })
.pipe(gulp.src('manifest.json', { passthrougth: true }))
.pipe(gulp.src('package.json', { passthrougth: true }))
2018-01-24 23:37:07 +00:00
.pipe(gulp.src('changelog.html', { passthrougth: true }))
2018-01-25 23:52:10 +00:00
.pipe(gulp.dest(DIST_DIR))
.pipe(install({
npm: '--production --ignore-scripts'
}));
2018-01-10 14:36:12 +00:00
};
2018-01-24 23:37:07 +00:00
function dist_locale() {
return gulp.src('./locales/**/*', { base: 'locales'})
.pipe(gulp.dest(DIST_DIR + '_locales'));
}
function dist_libraries() {
return gulp.src('./libraries/**/*', { base: '.'})
.pipe(gulp.dest(DIST_DIR + 'js'));
}
function dist_resources() {
return gulp.src('./resources/**/*', { base: '.'})
.pipe(gulp.dest(DIST_DIR));
}
// Create runable app directories in ./apps
2018-01-10 14:36:12 +00:00
function apps(done) {
var platforms = getPlatforms();
2018-01-08 10:37:32 +00:00
removeItem(platforms, 'chromeos');
2018-01-10 14:36:12 +00:00
buildNWApps(platforms, 'normal', APPS_DIR, done);
};
2018-01-10 14:36:12 +00:00
function listPostBuildTasks(folder, done) {
2018-01-05 17:58:27 +00:00
var platforms = getPlatforms();
2018-01-10 14:36:12 +00:00
var postBuildTasks = [];
2018-01-05 17:58:27 +00:00
if (platforms.indexOf('linux32') != -1) {
2018-01-10 14:36:12 +00:00
postBuildTasks.push(function post_build_linux32(done){ return post_build('linux32', folder, done) });
2018-01-05 17:58:27 +00:00
}
if (platforms.indexOf('linux64') != -1) {
2018-01-10 14:36:12 +00:00
postBuildTasks.push(function post_build_linux64(done){ return post_build('linux64', folder, done) });
}
// We need to return at least one task, if not gulp will throw an error
if (postBuildTasks.length == 0) {
postBuildTasks.push(function post_build_none(done){ done() });
}
return postBuildTasks;
}
function post_build(arch, folder, done) {
if ((arch =='linux32') || (arch == 'linux64')) {
2018-01-05 17:58:27 +00:00
// Copy Ubuntu launcher scripts to destination dir
2018-01-10 14:36:12 +00:00
var launcherDir = path.join(folder, pkg.name, arch);
console.log('Copy Ubuntu launcher scripts to ' + launcherDir);
return gulp.src('assets/linux/**')
.pipe(gulp.dest(launcherDir));
2018-01-05 17:58:27 +00:00
}
2018-01-10 14:36:12 +00:00
return done();
}
// Create debug app directories in ./debug
2018-01-10 14:36:12 +00:00
function debug(done) {
var platforms = getPlatforms();
2018-01-08 10:37:32 +00:00
removeItem(platforms, 'chromeos');
2018-01-10 14:36:12 +00:00
buildNWApps(platforms, 'sdk', DEBUG_DIR, done);
}
function buildNWApps(platforms, flavor, dir, done) {
2018-01-08 10:37:32 +00:00
if (platforms.length > 0) {
2018-01-09 00:44:13 +00:00
var builder = new NwBuilder(Object.assign({
2018-01-10 14:36:12 +00:00
buildDir: dir,
2018-01-08 10:37:32 +00:00
platforms: platforms,
2018-01-10 14:36:12 +00:00
flavor: flavor
2018-01-09 00:44:13 +00:00
}, nwBuilderOptions));
2018-01-08 10:37:32 +00:00
builder.on('log', console.log);
builder.build(function (err) {
if (err) {
console.log('Error building NW apps: ' + err);
2018-01-10 14:36:12 +00:00
clean_debug();
process.exit(1);
2018-01-08 10:37:32 +00:00
}
done();
});
} else {
2018-01-10 14:36:12 +00:00
console.log('No platform suitable for NW Build')
done();
2018-01-08 10:37:32 +00:00
}
2018-01-10 14:36:12 +00:00
}
function start_debug(done) {
var platforms = getPlatforms();
var exec = require('child_process').exec;
if (platforms.length === 1) {
var run = getRunDebugAppCommand(platforms[0]);
console.log('Starting debug app (' + run + ')...');
exec(run);
} else {
console.log('More than one platform specified, not starting debug app');
}
done();
}
// Create installer package for windows platforms
2018-01-10 14:36:12 +00:00
function release_win(arch, done) {
2018-01-17 15:38:41 +00:00
// The makensis does not generate the folder correctly, manually
createDirIfNotExists(RELEASE_DIR);
2018-01-10 14:36:12 +00:00
// Parameters passed to the installer script
const options = {
verbose: 2,
define: {
'VERSION': pkg.version,
'PLATFORM': arch,
2018-01-10 14:36:12 +00:00
'DEST_FOLDER': RELEASE_DIR
}
}
2018-01-10 14:36:12 +00:00
var output = makensis.compileSync('./assets/windows/installer.nsi', options);
2018-01-10 14:36:12 +00:00
if (output.status !== 0) {
console.error('Installer for platform ' + arch + ' finished with error ' + output.status + ': ' + output.stderr);
}
2018-01-10 14:36:12 +00:00
done();
}
// Create distribution package (zip) for windows and linux platforms
2018-01-10 14:36:12 +00:00
function release_zip(arch) {
var src = path.join(APPS_DIR, pkg.name, arch, '**');
var output = getReleaseFilename(arch, 'zip');
var base = path.join(APPS_DIR, pkg.name, arch);
return compressFiles(src, base, output, 'Betaflight Configurator');
}
// Create distribution package for chromeos platform
function release_chromeos() {
var src = path.join(DIST_DIR, '**');
var output = getReleaseFilename('chromeos', 'zip');
var base = DIST_DIR;
return compressFiles(src, base, output, '.');
}
// Compress files from srcPath, using basePath, to outputFile in the RELEASE_DIR
function compressFiles(srcPath, basePath, outputFile, zipFolder) {
return gulp.src(srcPath, { base: basePath })
.pipe(rename(function(actualPath){ actualPath.dirname = path.join(zipFolder, actualPath.dirname) }))
.pipe(zip(outputFile))
.pipe(gulp.dest(RELEASE_DIR));
2018-01-05 17:58:27 +00:00
}
function release_deb(arch) {
var debArch;
2018-01-10 14:36:12 +00:00
2018-01-05 17:58:27 +00:00
switch (arch) {
case 'linux32':
debArch = 'i386';
break;
case 'linux64':
debArch = 'amd64';
break;
default:
console.error("Deb package error, arch: " + arch);
process.exit(1);
break;
}
2018-01-10 14:36:12 +00:00
return gulp.src([path.join(APPS_DIR, pkg.name, arch, '*')])
2018-01-05 17:58:27 +00:00
.pipe(deb({
package: pkg.name,
version: pkg.version,
section: 'base',
priority: 'optional',
architecture: debArch,
maintainer: pkg.author,
description: pkg.description,
2018-01-25 14:15:58 +00:00
postinst: ['xdg-desktop-menu install /opt/betaflight/betaflight-configurator/betaflight-configurator.desktop'],
prerm: ['xdg-desktop-menu uninstall betaflight-configurator.desktop'],
2018-01-05 17:58:27 +00:00
depends: 'libgconf-2-4',
changelog: [],
_target: 'opt/betaflight/betaflight-configurator',
2018-01-10 14:36:12 +00:00
_out: RELEASE_DIR,
2018-01-17 14:30:44 +00:00
_copyright: 'assets/linux/copyright',
2018-01-05 17:58:27 +00:00
_clean: true
}));
}
// Create distribution package for macOS platform
function release_osx64() {
var appdmg = require('gulp-appdmg');
2018-01-17 15:38:41 +00:00
// The appdmg does not generate the folder correctly, manually
createDirIfNotExists(RELEASE_DIR);
// The src pipe is not used
return gulp.src(['.'])
.pipe(appdmg({
2018-01-10 14:36:12 +00:00
target: path.join(RELEASE_DIR, getReleaseFilename('macOS', 'dmg')),
basepath: path.join(APPS_DIR, pkg.name, 'osx64'),
specification: {
title: 'Betaflight Configurator',
contents: [
{ 'x': 448, 'y': 342, 'type': 'link', 'path': '/Applications' },
{ 'x': 192, 'y': 344, 'type': 'file', 'path': pkg.name + '.app', 'name': 'Betaflight Configurator.app' }
],
2018-01-21 16:09:36 +00:00
background: path.join(__dirname, 'assets/osx/dmg-background.png'),
format: 'UDZO',
window: {
size: {
width: 638,
height: 479
}
}
2017-11-25 07:47:32 +00:00
},
})
);
}
2018-01-17 15:38:41 +00:00
// Create the dir directory, with write permissions
function createDirIfNotExists(dir) {
fs.mkdir(dir, '0775', function(err) {
if (err) {
if (err.code !== 'EEXIST') {
throw err;
}
}
});
}
2018-01-10 14:36:12 +00:00
// Create a list of the gulp tasks to execute for release
function listReleaseTasks(done) {
2018-01-08 10:37:32 +00:00
var platforms = getPlatforms();
2018-01-10 14:36:12 +00:00
var releaseTasks = [];
2017-12-04 10:03:35 +00:00
if (platforms.indexOf('chromeos') !== -1) {
2018-01-10 14:36:12 +00:00
releaseTasks.push(release_chromeos);
2017-12-04 10:03:35 +00:00
}
if (platforms.indexOf('linux64') !== -1) {
2018-01-10 14:36:12 +00:00
releaseTasks.push(function release_linux64_zip(){ return release_zip('linux64') });
releaseTasks.push(function release_linux64_deb(){ return release_deb('linux64') });
2017-11-25 03:38:26 +00:00
}
if (platforms.indexOf('linux32') !== -1) {
2018-01-10 14:36:12 +00:00
releaseTasks.push(function release_linux32_zip(){ return release_zip('linux32') });
releaseTasks.push(function release_linux32_deb(){ return release_deb('linux32') });
}
2018-01-10 14:36:12 +00:00
2017-11-25 03:38:26 +00:00
if (platforms.indexOf('osx64') !== -1) {
2018-01-10 14:36:12 +00:00
releaseTasks.push(release_osx64);
2017-11-25 03:38:26 +00:00
}
if (platforms.indexOf('win32') !== -1) {
2018-01-10 14:36:12 +00:00
releaseTasks.push(function release_win32(done){ return release_win('win32', done) });
}
if (platforms.indexOf('win64') !== -1) {
2018-01-10 14:36:12 +00:00
releaseTasks.push(function release_win64(done){ return release_win('win64', done) });
}
2018-01-10 14:36:12 +00:00
return releaseTasks;
}