2017-10-20 00:36:33 +00:00
'use strict' ;
2018-01-10 14:36:12 +00:00
const child _process = require ( 'child_process' ) ;
const fs = require ( 'fs' ) ;
2018-05-13 19:00:56 +00:00
const fse = require ( 'fs-extra' ) ;
const https = require ( 'follow-redirects' ) . https ;
2018-01-10 14:36:12 +00:00
const path = require ( 'path' ) ;
const zip = require ( 'gulp-zip' ) ;
const del = require ( 'del' ) ;
const NwBuilder = require ( 'nw-builder' ) ;
2020-06-30 13:10:22 +00:00
const innoSetup = require ( '@quanle94/innosetup' ) ;
2018-01-10 14:36:12 +00:00
const deb = require ( 'gulp-debian' ) ;
2018-01-29 15:14:24 +00:00
const buildRpm = require ( 'rpm-builder' ) ;
const commandExistsSync = require ( 'command-exists' ) . sync ;
2018-05-13 19:00:56 +00:00
const targz = require ( 'targz' ) ;
2018-01-10 14:36:12 +00:00
const gulp = require ( 'gulp' ) ;
2020-10-10 11:31:07 +00:00
const rollup = require ( 'rollup' ) ;
2019-05-08 13:39:21 +00:00
const yarn = require ( "gulp-yarn" ) ;
2018-01-10 14:36:12 +00:00
const rename = require ( 'gulp-rename' ) ;
2020-07-03 14:18:55 +00:00
const replace = require ( 'gulp-replace' ) ;
const jeditor = require ( "gulp-json-editor" ) ;
const xmlTransformer = require ( "gulp-xml-transformer" ) ;
2018-01-10 14:36:12 +00:00
const os = require ( 'os' ) ;
2021-08-22 06:54:34 +00:00
const git = require ( 'simple-git' ) ( ) ;
2019-07-04 23:17:54 +00:00
const source = require ( 'vinyl-source-stream' ) ;
const stream = require ( 'stream' ) ;
2021-08-22 06:54:34 +00:00
const prompt = require ( 'gulp-prompt' ) ;
2022-05-20 17:42:25 +00:00
const less = require ( 'gulp-less' ) ;
const sourcemaps = require ( 'gulp-sourcemaps' ) ;
2018-01-10 14:36:12 +00:00
2020-07-03 14:18:55 +00:00
const cordova = require ( "cordova-lib" ) . cordova ;
2021-08-22 06:54:34 +00:00
2018-01-10 14:36:12 +00:00
const DIST _DIR = './dist/' ;
const APPS _DIR = './apps/' ;
const DEBUG _DIR = './debug/' ;
const RELEASE _DIR = './release/' ;
2020-07-03 14:18:55 +00:00
const CORDOVA _DIR = './cordova/' ;
2020-07-06 07:23:54 +00:00
const CORDOVA _DIST _DIR = './dist_cordova/' ;
2017-10-20 00:36:33 +00:00
2019-03-23 22:52:59 +00:00
const LINUX _INSTALL _DIR = '/opt/betaflight' ;
2020-10-10 11:31:07 +00:00
const NODE _ENV = process . env . NODE _ENV || 'production' ;
2021-08-22 06:54:34 +00:00
const NAME _REGEX = /-/g ;
2020-10-15 14:28:32 +00:00
const nwBuilderOptions = {
2023-02-10 11:56:40 +00:00
version : '0.72.0' ,
2020-11-27 12:15:42 +00:00
files : ` ${ DIST _DIR } **/* ` ,
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-06-17 12:10:26 +00:00
winIco : './src/images/bf_icon.ico' ,
2020-10-15 14:28:32 +00:00
zip : false ,
2018-01-09 00:44:13 +00:00
} ;
2022-06-02 22:40:55 +00:00
const nwArmVersion = 'nw60-arm64_2022-01-08' ;
2018-05-13 19:00:56 +00:00
2021-08-22 06:54:34 +00:00
let metadata = { } ;
2020-07-03 14:18:55 +00:00
let cordovaDependencies = true ;
2021-08-22 06:54:34 +00:00
2018-01-10 14:36:12 +00:00
//-----------------
//Pre tasks operations
//-----------------
2021-08-22 06:54:34 +00:00
2018-01-10 14:36:12 +00:00
const SELECTED _PLATFORMS = getInputPlatforms ( ) ;
2021-08-22 06:54:34 +00:00
2018-01-10 14:36:12 +00:00
//-----------------
//Tasks
//-----------------
2020-07-06 07:23:54 +00:00
gulp . task ( 'clean' , gulp . parallel ( clean _dist , clean _apps , clean _debug , clean _release , clean _cordova ) ) ;
2018-01-10 14:36:12 +00:00
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 ) ;
2020-07-06 07:23:54 +00:00
gulp . task ( 'clean-cordova' , clean _cordova ) ;
2020-07-03 14:18:55 +00:00
2021-08-22 06:54:34 +00:00
2019-07-04 23:17:54 +00:00
// Function definitions are processed before function calls.
2021-08-22 06:54:34 +00:00
function process _package _release ( done ) {
getGitRevision ( done , processPackage , true ) ;
}
function process _package _debug ( done ) {
getGitRevision ( done , processPackage , false ) ;
}
2019-07-27 03:33:02 +00:00
// dist_yarn MUST be done after dist_src
2021-08-22 06:54:34 +00:00
2022-05-20 17:42:25 +00:00
const distCommon = gulp . series ( dist _src , dist _less , dist _changelog , dist _yarn , dist _locale , dist _libraries , dist _resources , dist _rollup , gulp . series ( cordova _dist ( ) ) ) ;
const distBuild = gulp . series ( process _package _release , distCommon ) ;
2021-08-22 06:54:34 +00:00
2022-05-20 17:42:25 +00:00
const debugDistBuild = gulp . series ( process _package _debug , distCommon ) ;
2021-08-22 06:54:34 +00:00
2020-03-19 10:40:29 +00:00
const distRebuild = gulp . series ( clean _dist , distBuild ) ;
2018-08-01 09:22:12 +00:00
gulp . task ( 'dist' , distRebuild ) ;
2018-01-10 14:36:12 +00:00
2021-08-22 06:54:34 +00:00
const appsBuild = gulp . series ( gulp . parallel ( clean _apps , distRebuild ) , apps , gulp . series ( cordova _apps ( true ) ) , gulp . parallel ( listPostBuildTasks ( APPS _DIR ) ) ) ;
2018-01-10 14:36:12 +00:00
gulp . task ( 'apps' , appsBuild ) ;
2021-08-22 06:54:34 +00:00
const debugAppsBuild = gulp . series ( gulp . parallel ( clean _debug , gulp . series ( clean _dist , debugDistBuild ) ) , debug , gulp . series ( cordova _apps ( false ) ) , gulp . parallel ( listPostBuildTasks ( DEBUG _DIR ) ) ) ;
2020-03-19 10:40:29 +00:00
2021-09-16 09:34:16 +00:00
const debugBuildNoStart = gulp . series ( debugDistBuild , debug , gulp . parallel ( listPostBuildTasks ( DEBUG _DIR ) ) ) ;
const debugBuild = gulp . series ( debugBuildNoStart , start _debug ) ;
2018-01-10 14:36:12 +00:00
gulp . task ( 'debug' , debugBuild ) ;
2021-09-16 09:34:16 +00:00
gulp . task ( 'debug-no-start' , debugBuildNoStart ) ;
2018-01-10 14:36:12 +00:00
2021-08-22 06:54:34 +00:00
const releaseBuild = gulp . series ( gulp . parallel ( clean _release , appsBuild ) , gulp . parallel ( listReleaseTasks ( true , APPS _DIR ) ) ) ;
2018-01-10 14:36:12 +00:00
gulp . task ( 'release' , releaseBuild ) ;
2021-08-22 06:54:34 +00:00
const debugReleaseBuild = gulp . series ( gulp . parallel ( clean _release , debugAppsBuild ) , gulp . parallel ( listReleaseTasks ( false , DEBUG _DIR ) ) ) ;
2020-03-19 10:40:29 +00:00
gulp . task ( 'debug-release' , debugReleaseBuild ) ;
2018-01-10 14:36:12 +00:00
gulp . task ( 'default' , debugBuild ) ;
2018-01-09 00:44:13 +00:00
2021-08-22 06:54:34 +00:00
2017-11-22 20:19:34 +00:00
// -----------------
// Helper functions
// -----------------
// Get platform from commandline args
// #
2022-06-02 22:40:55 +00:00
// # gulp <task> [<platform>]+ Run only for platform(s) (with <platform> one of --linux64, --linux32, --armv8, --osx64, --win32, --win64, or --android)
2018-05-13 19:00:56 +00:00
// #
2018-01-10 14:36:12 +00:00
function getInputPlatforms ( ) {
2022-06-02 22:40:55 +00:00
const supportedPlatforms = [ 'linux64' , 'linux32' , 'armv8' , 'osx64' , 'win32' , 'win64' , 'android' ] ;
2020-10-15 14:28:32 +00:00
const platforms = [ ] ;
const regEx = /--(\w+)/ ;
2020-11-22 19:00:41 +00:00
2020-10-15 14:28:32 +00:00
for ( let i = 3 ; i < process . argv . length ; i ++ ) {
const arg = process . argv [ i ] . match ( regEx ) [ 1 ] ;
2017-11-24 03:40:02 +00:00
if ( supportedPlatforms . indexOf ( arg ) > - 1 ) {
2019-08-13 21:45:22 +00:00
platforms . push ( arg ) ;
2020-07-03 14:18:55 +00:00
} else if ( arg === 'nowinicon' ) {
console . log ( 'ignoring winIco' ) ;
2019-08-13 21:45:22 +00:00
delete nwBuilderOptions [ 'winIco' ] ;
2020-07-03 14:18:55 +00:00
} else if ( arg === 'skipdep' ) {
console . log ( 'ignoring cordova dependencies' ) ;
cordovaDependencies = false ;
2017-11-24 03:40:02 +00:00
} else {
2020-10-15 14:28:32 +00:00
console . log ( ` Unknown platform: ${ arg } ` ) ;
2019-08-13 21:45:22 +00:00
process . exit ( ) ;
2017-11-22 20:19:34 +00:00
}
2018-05-13 19:00:56 +00:00
}
2017-11-24 03:40:02 +00:00
if ( platforms . length === 0 ) {
2020-10-15 14:28:32 +00:00
const defaultPlatform = getDefaultPlatform ( ) ;
2017-12-07 15:00:13 +00:00
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. ` ) ;
2017-12-07 15:00:13 +00:00
process . exit ( ) ;
2017-11-24 03:40:02 +00:00
}
}
2018-01-08 10:37:32 +00:00
if ( platforms . length > 0 ) {
2020-10-15 14:28:32 +00:00
console . log ( ` Building for platform(s): ${ platforms } . ` ) ;
2018-01-08 10:37:32 +00:00
} else {
console . error ( 'No suitables platforms found.' ) ;
process . exit ( ) ;
}
2017-11-24 03:40:02 +00:00
return platforms ;
2017-11-22 20:19:34 +00:00
}
2017-12-07 15:00:13 +00:00
// Gets the default platform to be used
function getDefaultPlatform ( ) {
2020-10-15 14:28:32 +00:00
let defaultPlatform ;
2017-12-03 20:18:03 +00:00
switch ( os . platform ( ) ) {
case 'darwin' :
2017-12-07 15:00:13 +00:00
defaultPlatform = 'osx64' ;
2017-12-03 20:18:03 +00:00
break ;
2017-12-07 15:00:13 +00:00
case 'linux' :
defaultPlatform = 'linux64' ;
2017-12-03 20:18:03 +00:00
break ;
2017-12-07 15:00:13 +00:00
case 'win32' :
2021-03-28 07:16:21 +00:00
defaultPlatform = 'win64' ;
2017-12-07 15:00:13 +00:00
break ;
2018-05-13 19:00:56 +00:00
2017-12-07 15:00:13 +00:00
default :
defaultPlatform = '' ;
2018-05-13 19:00:56 +00:00
2017-12-07 15:00:13 +00:00
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 ) {
2020-10-15 14:28:32 +00:00
const index = platforms . indexOf ( item ) ;
2018-01-08 10:37:32 +00:00
if ( index >= 0 ) {
platforms . splice ( index , 1 ) ;
}
}
2017-12-07 15:00:13 +00:00
function getRunDebugAppCommand ( arch ) {
2020-10-01 07:39:17 +00:00
let command ;
2017-12-07 15:00:13 +00:00
switch ( arch ) {
case 'osx64' :
2021-08-22 06:54:34 +00:00
const packageName = ` ${ metadata . name } .app ` ;
command = ` open ${ path . join ( DEBUG _DIR , metadata . name , arch , packageName ) } ` ;
2017-12-07 15:00:13 +00:00
break ;
case 'linux64' :
2017-12-10 20:25:16 +00:00
case 'linux32' :
2022-06-02 22:40:55 +00:00
case 'armv8' :
2021-08-22 06:54:34 +00:00
command = path . join ( DEBUG _DIR , metadata . name , arch , metadata . name ) ;
2017-12-10 20:25:16 +00:00
break ;
2017-12-03 20:18:03 +00:00
case 'win32' :
2017-12-07 15:00:13 +00:00
case 'win64' :
2021-08-22 06:54:34 +00:00
command = path . join ( DEBUG _DIR , metadata . name , arch , ` ${ metadata . name } .exe ` ) ;
2017-12-03 20:18:03 +00:00
break ;
default :
2020-10-01 07:39:17 +00:00
command = '' ;
2017-12-04 10:03:35 +00:00
2017-12-03 20:18:03 +00:00
break ;
}
2020-10-01 07:39:17 +00:00
return command ;
2017-12-03 20:18:03 +00:00
}
2022-01-04 13:52:22 +00:00
function getReleaseFilename ( platform , ext , portable = false ) {
return ` ${ metadata . name } _ ${ metadata . version } _ ${ platform } ${ portable ? "-portable" : "" } . ${ ext } ` ;
2017-10-20 00:36:33 +00:00
}
2018-05-13 19:00:56 +00:00
function clean _dist ( ) {
2020-10-15 14:28:32 +00:00
return del ( [ ` ${ DIST _DIR } ** ` ] , { force : true } ) ;
2019-07-27 03:33:02 +00:00
}
2017-11-22 20:19:34 +00:00
2018-05-13 19:00:56 +00:00
function clean _apps ( ) {
2020-10-15 14:28:32 +00:00
return del ( [ ` ${ APPS _DIR } ** ` ] , { force : true } ) ;
2019-07-27 03:33:02 +00:00
}
2017-11-22 20:19:34 +00:00
2018-05-13 19:00:56 +00:00
function clean _debug ( ) {
2020-10-15 14:28:32 +00:00
return del ( [ ` ${ DEBUG _DIR } ** ` ] , { force : true } ) ;
2019-07-27 03:33:02 +00:00
}
2017-11-22 20:19:34 +00:00
2018-05-13 19:00:56 +00:00
function clean _release ( ) {
2020-10-15 14:28:32 +00:00
return del ( [ ` ${ RELEASE _DIR } ** ` ] , { force : true } ) ;
2019-07-27 03:33:02 +00:00
}
2017-10-20 00:36:33 +00:00
2018-05-13 19:00:56 +00:00
function clean _cache ( ) {
return del ( [ './cache/**' ] , { force : true } ) ;
2019-07-27 03:33:02 +00:00
}
2017-12-08 12:48:55 +00:00
2017-10-20 00:36:33 +00:00
// Real work for dist task. Done in another task to call it via
// run-sequence.
2021-08-22 06:54:34 +00:00
function processPackage ( done , gitRevision , isReleaseBuild ) {
const metadataKeys = [ 'name' , 'productName' , 'description' , 'author' , 'license' , 'version' ] ;
const pkg = require ( './package.json' ) ;
// remove gulp-appdmg from the package.json we're going to write
delete pkg . optionalDependencies [ 'gulp-appdmg' ] ;
pkg . gitRevision = gitRevision ;
if ( ! isReleaseBuild ) {
pkg . productName = ` ${ pkg . productName } (Debug Build) ` ;
pkg . description = ` ${ pkg . description } (Debug Build) ` ;
pkg . version = ` ${ pkg . version } -debug- ${ gitRevision } ` ;
metadata . packageId = ` ${ pkg . name } -debug ` ;
} else {
metadata . packageId = pkg . name ;
}
2021-08-22 06:54:34 +00:00
function version _prompt ( ) {
return gulp . src ( '.' )
. pipe ( prompt . prompt ( [ {
type : 'input' ,
name : 'version' ,
message : ` Package version (default: ${ pkg . version } ): ` ,
} , {
type : 'input' ,
name : 'storeVersion' ,
message : 'Google Play store version (<x.y.z>, default: package version):' ,
} ] , function ( res ) {
if ( res . version ) {
pkg . version = res . version ;
}
if ( res . storeVersion ) {
metadata . storeVersion = res . storeVersion ;
}
} ) ) ;
}
2021-08-22 06:54:34 +00:00
2021-08-22 06:54:34 +00:00
function write _package _file ( ) {
Object . keys ( pkg )
. filter ( key => metadataKeys . includes ( key ) )
. forEach ( ( key ) => {
metadata [ key ] = pkg [ key ] ;
} ) ;
2021-08-22 06:54:34 +00:00
2021-08-22 06:54:34 +00:00
const packageJson = new stream . Readable ;
packageJson . push ( JSON . stringify ( pkg , undefined , 2 ) ) ;
packageJson . push ( null ) ;
2021-08-22 06:54:34 +00:00
2021-08-22 06:54:34 +00:00
return packageJson
. pipe ( source ( 'package.json' ) )
. pipe ( gulp . dest ( DIST _DIR ) ) ;
}
const platforms = getPlatforms ( ) ;
if ( platforms . indexOf ( 'android' ) !== - 1 && isReleaseBuild ) {
gulp . series ( version _prompt , write _package _file ) ( done ) ;
} else {
gulp . series ( write _package _file ) ( done ) ;
}
2021-08-22 06:54:34 +00:00
}
2018-01-24 23:37:07 +00:00
function dist _src ( ) {
2020-10-15 14:28:32 +00:00
const distSources = [
2018-01-29 14:40:04 +00:00
'./src/**/*' ,
'!./src/css/dropdown-lists/LICENSE' ,
2020-10-15 14:28:32 +00:00
'!./src/support/**' ,
2022-05-20 17:42:25 +00:00
'!./src/**/*.less' ,
2017-10-20 00:36:33 +00:00
] ;
2018-01-24 23:37:07 +00:00
2021-08-22 06:54:34 +00:00
return gulp . src ( distSources , { base : 'src' } )
2020-11-27 12:15:42 +00:00
. pipe ( gulp . src ( 'yarn.lock' ) )
2019-07-27 03:33:02 +00:00
. pipe ( gulp . dest ( DIST _DIR ) ) ;
}
2022-05-20 17:42:25 +00:00
function dist _less ( ) {
return gulp . src ( './src/**/*.less' )
. pipe ( sourcemaps . init ( ) )
. pipe ( less ( ) )
. pipe ( sourcemaps . write ( '.' ) )
. pipe ( gulp . dest ( ` ${ DIST _DIR } ` ) ) ;
}
2019-08-02 06:48:50 +00:00
function dist _changelog ( ) {
return gulp . src ( 'changelog.html' )
2020-10-15 14:28:32 +00:00
. pipe ( gulp . dest ( ` ${ DIST _DIR } tabs/ ` ) ) ;
2019-08-02 06:48:50 +00:00
}
2019-07-27 03:33:02 +00:00
// This function relies on files from the dist_src function
function dist _yarn ( ) {
2020-11-27 12:15:42 +00:00
return gulp . src ( [ ` ${ DIST _DIR } package.json ` , ` ${ DIST _DIR } yarn.lock ` ] )
. pipe ( gulp . dest ( DIST _DIR ) )
2019-05-08 13:39:21 +00:00
. pipe ( yarn ( {
2020-10-15 14:28:32 +00:00
production : true ,
2018-01-25 23:52:10 +00:00
} ) ) ;
2019-07-27 03:33:02 +00:00
}
2017-10-20 00:36:33 +00:00
2018-01-24 23:37:07 +00:00
function dist _locale ( ) {
return gulp . src ( './locales/**/*' , { base : 'locales' } )
2020-10-23 07:01:46 +00:00
. pipe ( gulp . dest ( ` ${ DIST _DIR } locales ` ) ) ;
2018-01-24 23:37:07 +00:00
}
function dist _libraries ( ) {
return gulp . src ( './libraries/**/*' , { base : '.' } )
2020-10-15 14:28:32 +00:00
. pipe ( gulp . dest ( ` ${ DIST _DIR } js ` ) ) ;
2018-01-24 23:37:07 +00:00
}
function dist _resources ( ) {
2019-07-01 10:44:36 +00:00
return gulp . src ( [ './resources/**/*' , '!./resources/osd/**/*.png' ] , { base : '.' } )
2018-01-24 23:37:07 +00:00
. pipe ( gulp . dest ( DIST _DIR ) ) ;
}
2020-10-10 11:31:07 +00:00
function dist _rollup ( ) {
const commonjs = require ( '@rollup/plugin-commonjs' ) ;
const resolve = require ( '@rollup/plugin-node-resolve' ) . default ;
const alias = require ( '@rollup/plugin-alias' ) ;
const vue = require ( 'rollup-plugin-vue' ) ;
const rollupReplace = require ( '@rollup/plugin-replace' ) ;
return rollup
. rollup ( {
2020-10-24 06:36:36 +00:00
input : {
// For any new file migrated to modules add the output path
// in dist on the left, on the right it's input file path.
// If all the things used by other files are importing
// it with `import/export` file doesn't have to be here.
2020-11-04 11:29:31 +00:00
// I will be picked up by rollup and bundled accordingly.
2020-10-24 06:36:36 +00:00
'js/main_cordova' : 'src/js/main_cordova.js' ,
2021-01-01 18:26:30 +00:00
'js/utils/common' : 'src/js/utils/common.js' ,
2021-03-01 18:03:37 +00:00
'js/main' : 'src/js/main.js' ,
2020-10-24 06:36:36 +00:00
} ,
2020-10-10 11:31:07 +00:00
plugins : [
alias ( {
entries : {
vue : require . resolve ( 'vue/dist/vue.esm.js' ) ,
} ,
} ) ,
rollupReplace ( {
'process.env.NODE_ENV' : JSON . stringify ( NODE _ENV ) ,
} ) ,
resolve ( ) ,
commonjs ( ) ,
vue ( ) ,
] ,
} )
. then ( bundle =>
bundle . write ( {
format : 'esm' ,
2020-10-24 06:36:36 +00:00
// rollup is smart about how `name` is treated.
// so `input` you create file like `components/init`
// `[name]` will be replaced with it creating directories
// accordingly inside of `dist`
entryFileNames : '[name].js' ,
2022-12-29 13:42:36 +00:00
// anything what's not an entry will have the same name
// no hashing since we are not web app and don't care
// about cache busting
chunkFileNames : '[name].js' ,
// we want to see code in the same way as it
// is in the source files while debugging
sourcemap : true ,
// put any 3rd party module in vendor.js
manualChunks ( id ) {
if ( id . includes ( 'node_modules' ) ) {
return 'vendor' ;
}
} ,
2020-10-24 06:36:36 +00:00
dir : DIST _DIR ,
2021-12-18 07:21:05 +00:00
} ) ,
2020-10-10 11:31:07 +00:00
) ;
}
2017-11-22 20:19:34 +00:00
// Create runable app directories in ./apps
2018-01-10 14:36:12 +00:00
function apps ( done ) {
2020-10-15 14:28:32 +00:00
const platforms = getPlatforms ( ) ;
2020-07-03 14:18:55 +00:00
removeItem ( platforms , 'android' ) ;
2018-01-08 10:37:32 +00:00
2018-05-13 19:00:56 +00:00
buildNWAppsWrapper ( platforms , 'normal' , APPS _DIR , done ) ;
}
2017-11-11 14:00:18 +00:00
2020-10-15 14:28:32 +00:00
function listPostBuildTasks ( folder ) {
2018-01-05 17:58:27 +00:00
2020-10-15 14:28:32 +00:00
const platforms = getPlatforms ( ) ;
2018-01-05 17:58:27 +00:00
2020-10-15 14:28:32 +00:00
const postBuildTasks = [ ] ;
2018-01-05 17:58:27 +00:00
2020-10-15 14:28:32 +00:00
if ( platforms . indexOf ( 'linux32' ) !== - 1 ) {
2018-05-20 01:11:36 +00:00
postBuildTasks . push ( function post _build _linux32 ( done ) {
return post _build ( 'linux32' , folder , done ) ;
} ) ;
2018-01-05 17:58:27 +00:00
}
2020-10-15 14:28:32 +00:00
if ( platforms . indexOf ( 'linux64' ) !== - 1 ) {
2018-05-20 01:11:36 +00:00
postBuildTasks . push ( function post _build _linux64 ( done ) {
return post _build ( 'linux64' , folder , done ) ;
} ) ;
2018-01-10 14:36:12 +00:00
}
2022-06-02 22:40:55 +00:00
if ( platforms . indexOf ( 'armv8' ) !== - 1 ) {
postBuildTasks . push ( function post _build _armv8 ( done ) {
return post _build ( 'armv8' , folder , done ) ;
2018-05-20 01:11:36 +00:00
} ) ;
2018-05-13 19:00:56 +00:00
}
2018-01-10 14:36:12 +00:00
// We need to return at least one task, if not gulp will throw an error
2020-10-15 14:28:32 +00:00
if ( postBuildTasks . length === 0 ) {
2018-05-20 01:11:36 +00:00
postBuildTasks . push ( function post _build _none ( done ) {
done ( ) ;
} ) ;
2018-01-10 14:36:12 +00:00
}
return postBuildTasks ;
}
function post _build ( arch , folder , done ) {
2018-05-13 19:00:56 +00:00
if ( ( arch === 'linux32' ) || ( arch === 'linux64' ) ) {
2018-01-05 17:58:27 +00:00
// Copy Ubuntu launcher scripts to destination dir
2021-08-22 06:54:34 +00:00
const launcherDir = path . join ( folder , metadata . name , arch ) ;
2020-10-15 14:28:32 +00:00
console . log ( ` Copy Ubuntu launcher scripts to ${ launcherDir } ` ) ;
2018-01-10 14:36:12 +00:00
return gulp . src ( 'assets/linux/**' )
. pipe ( gulp . dest ( launcherDir ) ) ;
2018-01-05 17:58:27 +00:00
}
2022-06-02 22:40:55 +00:00
if ( arch === 'armv8' ) {
console . log ( 'Moving armv8 build from "linux32" to "armv8" directory...' ) ;
fse . moveSync ( path . join ( folder , metadata . name , 'linux32' ) , path . join ( folder , metadata . name , 'armv8' ) ) ;
2018-05-13 19:00:56 +00:00
}
2018-01-10 14:36:12 +00:00
return done ( ) ;
}
2017-11-11 14:00:18 +00:00
// Create debug app directories in ./debug
2018-01-10 14:36:12 +00:00
function debug ( done ) {
2020-10-15 14:28:32 +00:00
const platforms = getPlatforms ( ) ;
2020-07-03 14:18:55 +00:00
removeItem ( platforms , 'android' ) ;
2018-01-10 14:36:12 +00:00
2018-05-13 19:00:56 +00:00
buildNWAppsWrapper ( platforms , 'sdk' , DEBUG _DIR , done ) ;
2018-01-10 14:36:12 +00:00
}
2021-08-22 06:54:34 +00:00
function injectARMCache ( flavor , done ) {
2020-10-15 14:28:32 +00:00
const flavorPostfix = ` - ${ flavor } ` ;
const flavorDownloadPostfix = flavor !== 'normal' ? ` - ${ flavor } ` : '' ;
2018-05-13 19:00:56 +00:00
clean _cache ( ) . then ( function ( ) {
2018-05-20 00:16:50 +00:00
if ( ! fs . existsSync ( './cache' ) ) {
fs . mkdirSync ( './cache' ) ;
}
2022-06-02 22:40:55 +00:00
fs . closeSync ( fs . openSync ( './cache/_ARMv8_IS_CACHED' , 'w' ) ) ;
2020-10-15 14:28:32 +00:00
const versionFolder = ` ./cache/ ${ nwBuilderOptions . version } ${ flavorPostfix } ` ;
2018-05-20 00:16:50 +00:00
if ( ! fs . existsSync ( versionFolder ) ) {
fs . mkdirSync ( versionFolder ) ;
}
2020-10-15 14:28:32 +00:00
const linux32Folder = ` ${ versionFolder } /linux32 ` ;
if ( ! fs . existsSync ( linux32Folder ) ) {
fs . mkdirSync ( linux32Folder ) ;
2018-05-20 00:16:50 +00:00
}
2020-10-15 14:28:32 +00:00
const downloadedArchivePath = ` ${ versionFolder } /nwjs ${ flavorPostfix } -v ${ nwArmVersion } -linux-arm.tar.gz ` ;
2022-06-02 22:40:55 +00:00
const downloadUrl = ` https://github.com/LeonardLaszlo/nw.js-armv7-binaries/releases/download/ ${ nwArmVersion } / ${ nwArmVersion } .tar.gz ` ;
2018-05-13 19:00:56 +00:00
if ( fs . existsSync ( downloadedArchivePath ) ) {
2022-06-02 22:40:55 +00:00
console . log ( 'Prebuilt ARMv8 binaries found in /tmp' ) ;
2018-05-13 19:00:56 +00:00
downloadDone ( flavorDownloadPostfix , downloadedArchivePath , versionFolder ) ;
} else {
2022-06-02 22:40:55 +00:00
console . log ( ` Downloading prebuilt ARMv8 binaries from " ${ downloadUrl } "... ` ) ;
2018-05-13 19:00:56 +00:00
process . stdout . write ( '> Starting download...\r' ) ;
2020-10-15 14:28:32 +00:00
const armBuildBinary = fs . createWriteStream ( downloadedArchivePath ) ;
https . get ( downloadUrl , function ( res ) {
const totalBytes = res . headers [ 'content-length' ] ;
let downloadedBytes = 0 ;
2018-05-13 19:00:56 +00:00
res . pipe ( armBuildBinary ) ;
res . on ( 'data' , function ( chunk ) {
downloadedBytes += chunk . length ;
process . stdout . write ( ` > ${ parseInt ( ( downloadedBytes * 100 ) / totalBytes ) } % done \r ` ) ;
} ) ;
armBuildBinary . on ( 'finish' , function ( ) {
process . stdout . write ( '> 100% done \n' ) ;
armBuildBinary . close ( function ( ) {
downloadDone ( flavorDownloadPostfix , downloadedArchivePath , versionFolder ) ;
} ) ;
} ) ;
} ) ;
}
} ) ;
2020-10-15 14:28:32 +00:00
function downloadDone ( flavorDownload , downloadedArchivePath , versionFolder ) {
2022-06-02 22:40:55 +00:00
console . log ( 'Injecting prebuilt ARMv8 binaries into Linux32 cache...' ) ;
2018-05-13 19:00:56 +00:00
targz . decompress ( {
src : downloadedArchivePath ,
dest : versionFolder ,
2018-05-20 00:16:50 +00:00
} , function ( err ) {
2018-05-13 19:00:56 +00:00
if ( err ) {
console . log ( err ) ;
clean _debug ( ) ;
process . exit ( 1 ) ;
} else {
fs . rename (
2020-10-15 14:28:32 +00:00
` ${ versionFolder } /nwjs ${ flavorDownload } -v ${ nwArmVersion } -linux-arm ` ,
2018-05-13 19:00:56 +00:00
` ${ versionFolder } /linux32 ` ,
2020-10-15 14:28:32 +00:00
( renameErr ) => {
if ( renameErr ) {
console . log ( renameErr ) ;
2018-05-13 19:00:56 +00:00
clean _debug ( ) ;
process . exit ( 1 ) ;
}
2021-08-22 06:54:34 +00:00
done ( ) ;
2021-12-18 07:21:05 +00:00
} ,
2018-05-13 19:00:56 +00:00
) ;
}
} ) ;
}
}
2017-11-22 20:19:34 +00:00
2018-05-13 19:00:56 +00:00
function buildNWAppsWrapper ( platforms , flavor , dir , done ) {
function buildNWAppsCallback ( ) {
buildNWApps ( platforms , flavor , dir , done ) ;
}
2022-06-02 22:40:55 +00:00
if ( platforms . indexOf ( 'armv8' ) !== - 1 ) {
2018-05-13 19:00:56 +00:00
if ( platforms . indexOf ( 'linux32' ) !== - 1 ) {
2022-06-02 22:40:55 +00:00
console . log ( 'Cannot build ARMv8 and Linux32 versions at the same time!' ) ;
2018-05-13 19:00:56 +00:00
clean _debug ( ) ;
process . exit ( 1 ) ;
}
2022-06-02 22:40:55 +00:00
removeItem ( platforms , 'armv8' ) ;
2018-05-13 19:00:56 +00:00
platforms . push ( 'linux32' ) ;
2022-06-02 22:40:55 +00:00
if ( ! fs . existsSync ( './cache/_ARMv8_IS_CACHED' , 'w' ) ) {
2018-05-13 19:00:56 +00:00
console . log ( 'Purging cache because it needs to be overwritten...' ) ;
clean _cache ( ) . then ( ( ) => {
injectARMCache ( flavor , buildNWAppsCallback ) ;
2020-10-15 14:28:32 +00:00
} ) ;
2018-05-13 19:00:56 +00:00
} else {
buildNWAppsCallback ( ) ;
}
} else {
2022-06-02 22:40:55 +00:00
if ( platforms . indexOf ( 'linux32' ) !== - 1 && fs . existsSync ( './cache/_ARMv8_IS_CACHED' ) ) {
2018-05-13 19:00:56 +00:00
console . log ( 'Purging cache because it was previously overwritten...' ) ;
clean _cache ( ) . then ( buildNWAppsCallback ) ;
} else {
buildNWAppsCallback ( ) ;
}
}
}
function buildNWApps ( platforms , flavor , dir , done ) {
2018-01-08 10:37:32 +00:00
if ( platforms . length > 0 ) {
2020-10-15 14:28:32 +00:00
const builder = new NwBuilder ( Object . assign ( {
2018-01-10 14:36:12 +00:00
buildDir : dir ,
2020-10-15 14:28:32 +00:00
platforms ,
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 ) {
2020-10-15 14:28:32 +00:00
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 {
2020-10-15 14:28:32 +00:00
console . log ( 'No platform suitable for NW Build' ) ;
2017-10-20 00:36:33 +00:00
done ( ) ;
2018-01-08 10:37:32 +00:00
}
2018-01-10 14:36:12 +00:00
}
2021-08-22 06:54:34 +00:00
function getGitRevision ( done , callback , isReleaseBuild ) {
let gitRevision = 'norevision' ;
2021-08-22 06:54:34 +00:00
git . diff ( [ '--shortstat' ] , function ( err1 , diff ) {
if ( ! err1 && ! diff ) {
git . log ( [ '-1' , '--pretty=format:%h' ] , function ( err2 , rev ) {
if ( ! err2 ) {
2021-08-22 06:54:34 +00:00
gitRevision = rev . latest . hash ;
}
callback ( done , gitRevision , isReleaseBuild ) ;
} ) ;
2018-08-22 11:28:53 +00:00
} else {
2021-08-22 06:54:34 +00:00
callback ( done , gitRevision , isReleaseBuild ) ;
2018-08-22 11:28:53 +00:00
}
2018-08-12 08:19:02 +00:00
} ) ;
}
2018-01-10 14:36:12 +00:00
function start _debug ( done ) {
2020-10-15 14:28:32 +00:00
const platforms = getPlatforms ( ) ;
2018-01-10 14:36:12 +00:00
if ( platforms . length === 1 ) {
2020-07-03 14:18:55 +00:00
if ( platforms [ 0 ] === 'android' ) {
cordova _debug ( ) ;
} else {
2020-10-15 14:28:32 +00:00
const run = getRunDebugAppCommand ( platforms [ 0 ] ) ;
console . log ( ` Starting debug app ( ${ run } )... ` ) ;
2020-11-28 11:14:40 +00:00
child _process . exec ( run ) ;
2020-07-03 14:18:55 +00:00
}
2018-01-10 14:36:12 +00:00
} else {
console . log ( 'More than one platform specified, not starting debug app' ) ;
}
done ( ) ;
}
2017-10-20 00:36:33 +00:00
2017-12-29 13:57:45 +00:00
// Create installer package for windows platforms
2020-03-19 10:40:29 +00:00
function release _win ( arch , appDirectory , done ) {
2017-12-29 13:57:45 +00:00
// Parameters passed to the installer script
2020-06-30 13:10:22 +00:00
const parameters = [ ] ;
// Extra parameters to replace inside the iss file
2021-08-22 06:54:34 +00:00
parameters . push ( ` /Dversion= ${ metadata . version } ` ) ;
2020-06-30 13:10:22 +00:00
parameters . push ( ` /DarchName= ${ arch } ` ) ;
parameters . push ( ` /DarchAllowed= ${ ( arch === 'win32' ) ? 'x86 x64' : 'x64' } ` ) ;
parameters . push ( ` /DarchInstallIn64bit= ${ ( arch === 'win32' ) ? '' : 'x64' } ` ) ;
parameters . push ( ` /DsourceFolder= ${ appDirectory } ` ) ;
parameters . push ( ` /DtargetFolder= ${ RELEASE _DIR } ` ) ;
// Show only errors in console
parameters . push ( ` /Q ` ) ;
// Script file to execute
parameters . push ( "assets/windows/installer.iss" ) ;
innoSetup ( parameters , { } ,
function ( error ) {
if ( error != null ) {
console . error ( ` Installer for platform ${ arch } finished with error ${ error } ` ) ;
} else {
console . log ( ` Installer for platform ${ arch } finished ` ) ;
2017-12-29 13:57:45 +00:00
}
2020-06-30 13:10:22 +00:00
done ( ) ;
} ) ;
2017-12-29 13:57:45 +00:00
}
// Create distribution package (zip) for windows and linux platforms
2020-03-19 10:40:29 +00:00
function release _zip ( arch , appDirectory ) {
2021-08-22 06:54:34 +00:00
const src = path . join ( appDirectory , metadata . name , arch , '**' ) ;
2022-01-04 13:52:22 +00:00
const output = getReleaseFilename ( arch , 'zip' , true ) ;
2021-08-22 06:54:34 +00:00
const base = path . join ( appDirectory , metadata . name , arch ) ;
2018-01-10 14:36:12 +00:00
return compressFiles ( src , base , output , 'Betaflight Configurator' ) ;
}
// Compress files from srcPath, using basePath, to outputFile in the RELEASE_DIR
function compressFiles ( srcPath , basePath , outputFile , zipFolder ) {
return gulp . src ( srcPath , { base : basePath } )
2018-05-20 01:11:36 +00:00
. pipe ( rename ( function ( actualPath ) {
actualPath . dirname = path . join ( zipFolder , actualPath . dirname ) ;
} ) )
2018-01-10 14:36:12 +00:00
. pipe ( zip ( outputFile ) )
. pipe ( gulp . dest ( RELEASE _DIR ) ) ;
2018-01-05 17:58:27 +00:00
}
2020-03-19 10:40:29 +00:00
function release _deb ( arch , appDirectory , done ) {
2018-01-10 14:36:12 +00:00
2018-01-29 15:14:24 +00:00
// Check if dpkg-deb exists
if ( ! commandExistsSync ( 'dpkg-deb' ) ) {
2020-10-15 14:28:32 +00:00
console . warn ( ` dpkg-deb command not found, not generating deb package for ${ arch } ` ) ;
2020-03-19 10:40:29 +00:00
done ( ) ;
2022-05-23 18:47:50 +00:00
return null ;
2018-01-05 17:58:27 +00:00
}
2021-08-22 06:54:34 +00:00
return gulp . src ( [ path . join ( appDirectory , metadata . name , arch , '*' ) ] )
2018-01-05 17:58:27 +00:00
. pipe ( deb ( {
2021-08-22 06:54:34 +00:00
package : metadata . name ,
version : metadata . version ,
2020-10-15 14:28:32 +00:00
section : 'base' ,
priority : 'optional' ,
architecture : getLinuxPackageArch ( 'deb' , arch ) ,
2021-08-22 06:54:34 +00:00
maintainer : metadata . author ,
description : metadata . description ,
preinst : [ ` rm -rf ${ LINUX _INSTALL _DIR } / ${ metadata . name } ` ] ,
2020-10-15 14:28:32 +00:00
postinst : [
` chown root:root ${ LINUX _INSTALL _DIR } ` ,
2021-08-22 06:54:34 +00:00
` chown -R root:root ${ LINUX _INSTALL _DIR } / ${ metadata . name } ` ,
` xdg-desktop-menu install ${ LINUX _INSTALL _DIR } / ${ metadata . name } / ${ metadata . name } .desktop ` ,
2022-09-28 16:51:35 +00:00
` chmod +xr ${ LINUX _INSTALL _DIR } / ${ metadata . name } /chrome_crashpad_handler ` ,
` chmod +xr ${ LINUX _INSTALL _DIR } / ${ metadata . name } / ${ metadata . name } ` ,
` chmod -R +Xr ${ LINUX _INSTALL _DIR } / ${ metadata . name } / ` ,
2020-10-15 14:28:32 +00:00
] ,
2021-08-22 06:54:34 +00:00
prerm : [ ` xdg-desktop-menu uninstall ${ metadata . name } .desktop ` ] ,
2022-12-25 01:31:17 +00:00
conffiles : './test/configs/opt/etc/dummy.cfg' ,
2022-05-23 18:47:50 +00:00
depends : [ 'libgconf-2-4' , 'libatomic1' ] ,
2020-10-15 14:28:32 +00:00
changelog : [ ] ,
2021-08-22 06:54:34 +00:00
_target : ` ${ LINUX _INSTALL _DIR } / ${ metadata . name } ` ,
2020-10-15 14:28:32 +00:00
_out : RELEASE _DIR ,
_copyright : 'assets/linux/copyright' ,
_clean : true ,
2018-01-05 17:58:27 +00:00
} ) ) ;
2017-12-10 20:25:16 +00:00
}
2020-03-19 10:40:29 +00:00
function release _rpm ( arch , appDirectory , done ) {
2018-01-29 15:14:24 +00:00
2022-05-23 18:47:50 +00:00
// Check if rpmbuild exists
2018-01-29 15:14:24 +00:00
if ( ! commandExistsSync ( 'rpmbuild' ) ) {
2020-10-15 14:28:32 +00:00
console . warn ( ` rpmbuild command not found, not generating rpm package for ${ arch } ` ) ;
2020-03-19 10:40:29 +00:00
done ( ) ;
2022-05-23 18:47:50 +00:00
return ;
2018-01-29 15:14:24 +00:00
}
2018-02-02 19:23:09 +00:00
// The buildRpm does not generate the folder correctly, manually
2018-01-29 15:14:24 +00:00
createDirIfNotExists ( RELEASE _DIR ) ;
2020-10-15 14:28:32 +00:00
const options = {
2021-08-22 06:54:34 +00:00
name : metadata . name ,
version : metadata . version . replace ( NAME _REGEX , '_' ) , // RPM does not like release candidate versions
2020-10-15 14:28:32 +00:00
buildArch : getLinuxPackageArch ( 'rpm' , arch ) ,
2021-08-22 06:54:34 +00:00
vendor : metadata . author ,
summary : metadata . description ,
2020-10-15 14:28:32 +00:00
license : 'GNU General Public License v3.0' ,
2022-06-28 02:49:52 +00:00
requires : [ 'GConf2' , 'libatomic' ] ,
2020-10-15 14:28:32 +00:00
prefix : '/opt' ,
files : [ {
2021-08-22 06:54:34 +00:00
cwd : path . join ( appDirectory , metadata . name , arch ) ,
2020-10-15 14:28:32 +00:00
src : '*' ,
2021-08-22 06:54:34 +00:00
dest : ` ${ LINUX _INSTALL _DIR } / ${ metadata . name } ` ,
2020-10-15 14:28:32 +00:00
} ] ,
2022-12-12 12:22:28 +00:00
postInstallScript : [
` chown root:root ${ LINUX _INSTALL _DIR } ` ,
` chown -R root:root ${ LINUX _INSTALL _DIR } / ${ metadata . name } ` ,
` xdg-desktop-menu install ${ LINUX _INSTALL _DIR } / ${ metadata . name } / ${ metadata . name } .desktop ` ,
` chmod +xr ${ LINUX _INSTALL _DIR } / ${ metadata . name } /chrome_crashpad_handler ` ,
` chmod +xr ${ LINUX _INSTALL _DIR } / ${ metadata . name } / ${ metadata . name } ` ,
` chmod -R +Xr ${ LINUX _INSTALL _DIR } / ${ metadata . name } / ` ,
] ,
2021-08-22 06:54:34 +00:00
preUninstallScript : [ ` xdg-desktop-menu uninstall ${ metadata . name } .desktop ` ] ,
2020-10-15 14:28:32 +00:00
tempDir : path . join ( RELEASE _DIR , ` tmp-rpm-build- ${ arch } ` ) ,
keepTemp : false ,
verbose : false ,
rpmDest : RELEASE _DIR ,
execOpts : { maxBuffer : 1024 * 1024 * 16 } ,
2018-01-29 15:14:24 +00:00
} ;
2020-10-15 14:28:32 +00:00
buildRpm ( options , function ( err ) {
2018-01-29 15:14:24 +00:00
if ( err ) {
2020-10-15 14:28:32 +00:00
console . error ( ` Error generating rpm package: ${ err } ` ) ;
2018-01-29 15:14:24 +00:00
}
done ( ) ;
} ) ;
}
function getLinuxPackageArch ( type , arch ) {
2020-10-15 14:28:32 +00:00
let packArch ;
2018-01-29 15:14:24 +00:00
switch ( arch ) {
case 'linux32' :
packArch = 'i386' ;
break ;
case 'linux64' :
2020-10-15 14:28:32 +00:00
if ( type === 'rpm' ) {
2018-01-29 15:14:24 +00:00
packArch = 'x86_64' ;
} else {
packArch = 'amd64' ;
}
break ;
default :
2020-10-15 14:28:32 +00:00
console . error ( ` Package error, arch: ${ arch } ` ) ;
2018-01-29 15:14:24 +00:00
process . exit ( 1 ) ;
break ;
}
return packArch ;
}
2017-11-22 20:19:34 +00:00
// Create distribution package for macOS platform
2020-03-19 10:40:29 +00:00
function release _osx64 ( appDirectory ) {
2021-12-17 17:38:53 +00:00
const appdmg = require ( './gulp-appdmg' ) ;
2017-10-20 00:36:33 +00:00
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 ( [ '.' ] )
2017-11-22 20:19:34 +00:00
. pipe ( appdmg ( {
2018-01-10 14:36:12 +00:00
target : path . join ( RELEASE _DIR , getReleaseFilename ( 'macOS' , 'dmg' ) ) ,
2021-08-22 06:54:34 +00:00
basepath : path . join ( appDirectory , metadata . name , 'osx64' ) ,
2017-11-22 20:19:34 +00:00
specification : {
2017-11-25 12:53:28 +00:00
title : 'Betaflight Configurator' ,
contents : [
2017-11-22 20:19:34 +00:00
{ 'x' : 448 , 'y' : 342 , 'type' : 'link' , 'path' : '/Applications' } ,
2021-08-22 06:54:34 +00:00
{ 'x' : 192 , 'y' : 344 , 'type' : 'file' , 'path' : ` ${ metadata . name } .app ` , 'name' : 'Betaflight Configurator.app' } ,
2017-11-25 12:53:28 +00:00
] ,
2018-01-21 16:09:36 +00:00
background : path . join ( _ _dirname , 'assets/osx/dmg-background.png' ) ,
2017-11-25 12:53:28 +00:00
format : 'UDZO' ,
window : {
size : {
width : 638 ,
2020-10-15 14:28:32 +00:00
height : 479 ,
} ,
} ,
2017-11-25 07:47:32 +00:00
} ,
2021-12-18 07:21:05 +00:00
} ) ,
2017-11-22 20:19:34 +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 ) {
2020-10-15 14:28:32 +00:00
if ( err && err . code !== 'EEXIST' ) {
throw err ;
2018-01-17 15:38:41 +00:00
}
} ) ;
}
2018-01-10 14:36:12 +00:00
// Create a list of the gulp tasks to execute for release
2021-08-22 06:54:34 +00:00
function listReleaseTasks ( isReleaseBuild , appDirectory ) {
2017-11-21 10:40:39 +00:00
2020-10-15 14:28:32 +00:00
const platforms = getPlatforms ( ) ;
2017-11-24 03:40:02 +00:00
2020-10-15 14:28:32 +00:00
const releaseTasks = [ ] ;
2018-01-10 14:36:12 +00:00
2017-11-24 03:40:02 +00:00
if ( platforms . indexOf ( 'linux64' ) !== - 1 ) {
2018-05-20 01:11:36 +00:00
releaseTasks . push ( function release _linux64 _zip ( ) {
2020-03-19 10:40:29 +00:00
return release _zip ( 'linux64' , appDirectory ) ;
2018-05-20 01:11:36 +00:00
} ) ;
releaseTasks . push ( function release _linux64 _deb ( done ) {
2020-03-19 10:40:29 +00:00
return release _deb ( 'linux64' , appDirectory , done ) ;
2018-05-20 01:11:36 +00:00
} ) ;
releaseTasks . push ( function release _linux64 _rpm ( done ) {
2020-03-19 10:40:29 +00:00
return release _rpm ( 'linux64' , appDirectory , done ) ;
2018-05-20 01:11:36 +00:00
} ) ;
2017-11-25 03:38:26 +00:00
}
2017-12-10 20:25:16 +00:00
if ( platforms . indexOf ( 'linux32' ) !== - 1 ) {
2018-05-20 01:11:36 +00:00
releaseTasks . push ( function release _linux32 _zip ( ) {
2020-03-19 10:40:29 +00:00
return release _zip ( 'linux32' , appDirectory ) ;
2018-05-20 01:11:36 +00:00
} ) ;
releaseTasks . push ( function release _linux32 _deb ( done ) {
2020-03-19 10:40:29 +00:00
return release _deb ( 'linux32' , appDirectory , done ) ;
2018-05-20 01:11:36 +00:00
} ) ;
releaseTasks . push ( function release _linux32 _rpm ( done ) {
2020-03-19 10:40:29 +00:00
return release _rpm ( 'linux32' , appDirectory , done ) ;
2018-05-20 01:11:36 +00:00
} ) ;
2017-12-10 20:25:16 +00:00
}
2018-01-10 14:36:12 +00:00
2022-06-02 22:40:55 +00:00
if ( platforms . indexOf ( 'armv8' ) !== - 1 ) {
releaseTasks . push ( function release _armv8 _zip ( ) {
return release _zip ( 'armv8' , appDirectory ) ;
2018-05-20 01:11:36 +00:00
} ) ;
2018-05-13 19:00:56 +00:00
}
2017-11-25 03:38:26 +00:00
if ( platforms . indexOf ( 'osx64' ) !== - 1 ) {
2020-03-19 10:40:29 +00:00
releaseTasks . push ( function ( ) {
return release _osx64 ( appDirectory ) ;
} ) ;
2017-11-25 03:38:26 +00:00
}
if ( platforms . indexOf ( 'win32' ) !== - 1 ) {
2022-01-04 13:52:22 +00:00
releaseTasks . push ( function release _win32 _zip ( ) {
return release _zip ( 'win32' , appDirectory ) ;
} ) ;
2018-05-20 01:11:36 +00:00
releaseTasks . push ( function release _win32 ( done ) {
2020-03-19 10:40:29 +00:00
return release _win ( 'win32' , appDirectory , done ) ;
2018-05-20 01:11:36 +00:00
} ) ;
2017-12-07 15:00:13 +00:00
}
2018-01-29 15:14:24 +00:00
2017-12-07 15:00:13 +00:00
if ( platforms . indexOf ( 'win64' ) !== - 1 ) {
2022-01-04 13:52:22 +00:00
releaseTasks . push ( function release _win64 _zip ( ) {
return release _zip ( 'win64' , appDirectory ) ;
} ) ;
2018-05-20 01:11:36 +00:00
releaseTasks . push ( function release _win64 ( done ) {
2020-03-19 10:40:29 +00:00
return release _win ( 'win64' , appDirectory , done ) ;
2018-05-20 01:11:36 +00:00
} ) ;
2017-11-22 20:19:34 +00:00
}
2017-10-20 00:36:33 +00:00
2020-07-03 14:18:55 +00:00
if ( platforms . indexOf ( 'android' ) !== - 1 ) {
releaseTasks . push ( function release _android ( ) {
2021-08-22 06:54:34 +00:00
if ( isReleaseBuild ) {
return cordova _release ( ) ;
} else {
return cordova _debug _release ( ) ;
}
2020-07-03 14:18:55 +00:00
} ) ;
}
2018-01-10 14:36:12 +00:00
return releaseTasks ;
}
2020-07-03 14:18:55 +00:00
// Cordova
function cordova _dist ( ) {
const distTasks = [ ] ;
const platforms = getPlatforms ( ) ;
if ( platforms . indexOf ( 'android' ) !== - 1 ) {
2020-07-06 07:23:54 +00:00
distTasks . push ( clean _cordova ) ;
2020-07-03 14:18:55 +00:00
distTasks . push ( cordova _copy _www ) ;
distTasks . push ( cordova _resources ) ;
distTasks . push ( cordova _include _www ) ;
2020-07-06 07:23:54 +00:00
distTasks . push ( cordova _copy _src ) ;
distTasks . push ( cordova _rename _src _config ) ;
distTasks . push ( cordova _rename _src _package ) ;
2020-07-03 14:18:55 +00:00
distTasks . push ( cordova _packagejson ) ;
2021-08-22 06:54:34 +00:00
distTasks . push ( cordova _manifestjson ) ;
2020-07-03 14:18:55 +00:00
distTasks . push ( cordova _configxml ) ;
2021-08-22 06:54:34 +00:00
distTasks . push ( cordova _rename _build _json ) ;
2020-07-03 14:18:55 +00:00
distTasks . push ( cordova _depedencies ) ;
if ( cordovaDependencies ) {
distTasks . push ( cordova _platforms ) ;
}
} else {
distTasks . push ( function cordova _dist _none ( done ) {
done ( ) ;
} ) ;
}
return distTasks ;
}
2021-08-22 06:54:34 +00:00
function cordova _apps ( isReleaseBuild ) {
2020-07-03 14:18:55 +00:00
const appsTasks = [ ] ;
const platforms = getPlatforms ( ) ;
if ( platforms . indexOf ( 'android' ) !== - 1 ) {
2021-08-22 06:54:34 +00:00
if ( isReleaseBuild ) {
appsTasks . push ( cordova _build ) ;
} else {
appsTasks . push ( cordova _debug _build ) ;
}
2020-07-03 14:18:55 +00:00
} else {
appsTasks . push ( function cordova _dist _none ( done ) {
done ( ) ;
} ) ;
}
return appsTasks ;
}
2020-07-06 07:23:54 +00:00
function clean _cordova ( ) {
const patterns = [ ] ;
2020-07-03 14:18:55 +00:00
if ( cordovaDependencies ) {
2020-07-06 07:23:54 +00:00
patterns . push ( ` ${ CORDOVA _DIST _DIR } ** ` ) ;
} else {
patterns . push ( ` ${ CORDOVA _DIST _DIR } www/** ` ) ;
patterns . push ( ` ${ CORDOVA _DIST _DIR } resources/** ` ) ;
2020-07-03 14:18:55 +00:00
}
return del ( patterns , { force : true } ) ;
}
2021-08-22 06:54:34 +00:00
2020-07-03 14:18:55 +00:00
function cordova _copy _www ( ) {
return gulp . src ( ` ${ DIST _DIR } ** ` , { base : DIST _DIR } )
2020-07-06 07:23:54 +00:00
. pipe ( gulp . dest ( ` ${ CORDOVA _DIST _DIR } www/ ` ) ) ;
2020-07-03 14:18:55 +00:00
}
2021-08-22 06:54:34 +00:00
2020-07-03 14:18:55 +00:00
function cordova _resources ( ) {
return gulp . src ( 'assets/android/**' )
2020-07-06 07:23:54 +00:00
. pipe ( gulp . dest ( ` ${ CORDOVA _DIST _DIR } resources/android/ ` ) ) ;
2020-07-03 14:18:55 +00:00
}
2021-08-22 06:54:34 +00:00
2020-07-03 14:18:55 +00:00
function cordova _include _www ( ) {
2020-07-06 07:23:54 +00:00
return gulp . src ( ` ${ CORDOVA _DIST _DIR } www/main.html ` )
2020-07-03 14:18:55 +00:00
. pipe ( replace ( '<!-- CORDOVA_INCLUDE js/cordova_chromeapi.js -->' , '<script type="text/javascript" src="./js/cordova_chromeapi.js"></script>' ) )
. pipe ( replace ( '<!-- CORDOVA_INCLUDE js/cordova_startup.js -->' , '<script type="text/javascript" src="./js/cordova_startup.js"></script>' ) )
. pipe ( replace ( '<!-- CORDOVA_INCLUDE cordova.js -->' , '<script type="text/javascript" src="cordova.js"></script>' ) )
2020-07-06 07:23:54 +00:00
. pipe ( gulp . dest ( ` ${ CORDOVA _DIST _DIR } www ` ) ) ;
}
2021-08-22 06:54:34 +00:00
2020-07-06 07:23:54 +00:00
function cordova _copy _src ( ) {
2021-08-22 06:54:34 +00:00
return gulp . src ( [ ` ${ CORDOVA _DIR } ** ` , ` ! ${ CORDOVA _DIR } config_template.xml ` , ` ! ${ CORDOVA _DIR } package_template.json ` , ` ! ${ CORDOVA _DIR } build_template.json ` ] )
2020-07-06 07:23:54 +00:00
. pipe ( gulp . dest ( ` ${ CORDOVA _DIST _DIR } ` ) ) ;
}
2021-08-22 06:54:34 +00:00
2020-07-06 07:23:54 +00:00
function cordova _rename _src _config ( ) {
return gulp . src ( ` ${ CORDOVA _DIR } config_template.xml ` )
. pipe ( rename ( 'config.xml' ) )
. pipe ( gulp . dest ( ` ${ CORDOVA _DIST _DIR } ` ) ) ;
}
2021-08-22 06:54:34 +00:00
2020-07-06 07:23:54 +00:00
function cordova _rename _src _package ( ) {
return gulp . src ( ` ${ CORDOVA _DIR } package_template.json ` )
. pipe ( rename ( 'package.json' ) )
. pipe ( gulp . dest ( ` ${ CORDOVA _DIST _DIR } ` ) ) ;
2020-07-03 14:18:55 +00:00
}
2021-08-22 06:54:34 +00:00
2020-07-03 14:18:55 +00:00
function cordova _packagejson ( ) {
2020-07-06 07:23:54 +00:00
return gulp . src ( ` ${ CORDOVA _DIST _DIR } package.json ` )
2020-07-03 14:18:55 +00:00
. pipe ( jeditor ( {
2021-08-22 06:54:34 +00:00
'name' : metadata . name ,
'description' : metadata . description ,
'version' : metadata . version ,
'author' : metadata . author ,
'license' : metadata . license ,
2020-07-03 14:18:55 +00:00
} ) )
2021-08-22 06:54:34 +00:00
. pipe ( gulp . dest ( CORDOVA _DIST _DIR ) )
. pipe ( rename ( 'manifest.json' ) )
2020-07-06 07:23:54 +00:00
. pipe ( gulp . dest ( CORDOVA _DIST _DIR ) ) ;
2020-07-03 14:18:55 +00:00
}
2021-08-22 06:54:34 +00:00
// Required to make getManifest() work in cordova
function cordova _manifestjson ( ) {
return gulp . src ( ` ${ DIST _DIR } package.json ` )
. pipe ( rename ( 'manifest.json' ) )
. pipe ( gulp . dest ( CORDOVA _DIST _DIR ) ) ;
}
2020-07-03 14:18:55 +00:00
function cordova _configxml ( ) {
2021-08-22 06:54:34 +00:00
const androidName = metadata . packageId . replace ( NAME _REGEX , '_' ) ;
2021-08-22 06:54:34 +00:00
2020-07-06 07:23:54 +00:00
return gulp . src ( [ ` ${ CORDOVA _DIST _DIR } config.xml ` ] )
2020-07-03 14:18:55 +00:00
. pipe ( xmlTransformer ( [
2021-08-22 06:54:34 +00:00
{ path : '//xmlns:name' , text : metadata . productName } ,
{ path : '//xmlns:description' , text : metadata . description } ,
{ path : '//xmlns:author' , text : metadata . author } ,
2020-07-03 14:18:55 +00:00
] , 'http://www.w3.org/ns/widgets' ) )
. pipe ( xmlTransformer ( [
2021-08-22 06:54:34 +00:00
{ path : '.' , attr : { 'id' : ` com.betaflight. ${ androidName } ` } } ,
2021-08-22 06:54:34 +00:00
{ path : '.' , attr : { 'version' : metadata . storeVersion ? metadata . storeVersion : metadata . version } } ,
2020-07-03 14:18:55 +00:00
] ) )
2020-07-06 07:23:54 +00:00
. pipe ( gulp . dest ( CORDOVA _DIST _DIR ) ) ;
2020-07-03 14:18:55 +00:00
}
2021-08-22 06:54:34 +00:00
2021-08-22 06:54:34 +00:00
function cordova _rename _build _json ( ) {
return gulp . src ( ` ${ CORDOVA _DIR } build_template.json ` )
. pipe ( rename ( 'build.json' ) )
. pipe ( gulp . dest ( CORDOVA _DIST _DIR ) ) ;
}
2020-07-03 14:18:55 +00:00
function cordova _depedencies ( ) {
2021-08-22 06:54:34 +00:00
process . chdir ( CORDOVA _DIST _DIR ) ;
2020-07-03 14:18:55 +00:00
return gulp . src ( [ './package.json' , './yarn.lock' ] )
. pipe ( gulp . dest ( './' ) )
. pipe ( yarn ( {
production : true ,
} ) ) ;
}
2021-08-22 06:54:34 +00:00
2020-07-03 14:18:55 +00:00
function cordova _platforms ( ) {
return cordova . platform ( 'add' , [ 'android' ] ) ;
}
2021-08-22 06:54:34 +00:00
2020-07-03 14:18:55 +00:00
function cordova _debug ( ) {
cordova . run ( ) ;
}
2021-08-22 06:54:34 +00:00
2021-08-22 06:54:34 +00:00
function cordova _debug _build ( done ) {
2020-07-03 14:18:55 +00:00
cordova . build ( {
'platforms' : [ 'android' ] ,
'options' : {
2021-08-22 06:54:34 +00:00
release : false ,
2020-07-03 14:18:55 +00:00
buildConfig : 'build.json' ,
} ,
} ) . then ( function ( ) {
process . chdir ( '../' ) ;
2021-08-22 06:54:34 +00:00
console . log ( ` APK has been generated at ${ CORDOVA _DIST _DIR } platforms/android/app/build/outputs/apk/release/app-release.apk ` ) ;
done ( ) ;
2020-07-03 14:18:55 +00:00
} ) ;
}
2021-08-22 06:54:34 +00:00
2021-08-22 06:54:34 +00:00
function cordova _build ( done ) {
let storePassword = '' ;
return gulp . series ( function password _prompt ( ) {
return gulp . src ( '.' )
. pipe ( prompt . prompt ( {
type : 'password' ,
name : 'storePassword' ,
message : 'Please enter the keystore password:' ,
} , function ( res ) {
storePassword = res . storePassword ;
} ) ) ;
} , function set _password ( ) {
return gulp . src ( ` build.json ` )
. pipe ( jeditor ( {
'android' : {
'release' : {
'storePassword' : storePassword ,
} ,
} ,
} ) )
. pipe ( gulp . dest ( './' ) ) ;
} , function build ( done2 ) {
return cordova . build ( {
'platforms' : [ 'android' ] ,
'options' : {
release : true ,
buildConfig : 'build.json' ,
} ,
} ) . then ( function ( ) {
// Delete the file containing the store password
del ( [ 'build.json' ] , { force : true } ) ;
process . chdir ( '../' ) ;
console . log ( 'AAB has been generated at dist_cordova/platforms/android/app/build/outputs/bundle/release/app.aab' ) ;
done2 ( ) ;
} ) ;
} ) ( done ) ;
}
async function cordova _debug _release ( ) {
const filename = getReleaseFilename ( 'android' , 'apk' ) ;
2020-07-03 14:18:55 +00:00
console . log ( ` Release APK : release/ ${ filename } ` ) ;
2021-08-22 06:54:34 +00:00
return gulp . src ( ` ${ CORDOVA _DIST _DIR } platforms/android/app/build/outputs/apk/debug/app-debug.apk ` )
. pipe ( rename ( filename ) )
. pipe ( gulp . dest ( RELEASE _DIR ) ) ;
}
async function cordova _release ( ) {
const filename = getReleaseFilename ( 'android' , 'aab' ) ;
console . log ( ` Release AAB : release/ ${ filename } ` ) ;
return gulp . src ( ` ${ CORDOVA _DIST _DIR } platforms/android/app/build/outputs/bundle/release/app.aab ` )
2020-07-03 14:18:55 +00:00
. pipe ( rename ( filename ) )
. pipe ( gulp . dest ( RELEASE _DIR ) ) ;
}