Created
January 22, 2019 09:39
-
-
Save drauggres/ff643913354df3e63a435383309d67e1 to your computer and use it in GitHub Desktop.
Titanium build hook for android module
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const path = require('path'); | |
const spawn = require('child_process').spawn; | |
/** The plugin's identifier */ | |
exports.id = 'android.module.hook'; | |
/** The Titanium CLI version that this hook is compatible with */ | |
exports.cliVersion = '>=3.2'; | |
function checkLine(line, logger) { | |
const re = new RegExp( // eslint-disable-line security/detect-non-literal-regexp | |
'(?:\u001b\\[\\d+m)?\\[?(' | |
+ logger.getLevels().join('|') | |
+ ')\\]?\\s*(?:\u001b\\[\\d+m)?(.*)', 'i' | |
); | |
if (line) { | |
const m = line.match(re); | |
if (m) { | |
logger[m[1].toLowerCase()](m[2].trim()); | |
} else { | |
logger.debug(line); | |
} | |
} | |
} | |
function runCommand(cmd, args, logger) { | |
// when calling on Windows, we need to escape ampersands in the command | |
if (process.platform === 'win32') { | |
cmd.replace(/&/g, '^&'); | |
} | |
try { | |
return new Promise(function(resolve, reject) { | |
const str = 'Run "' + cmd + ' ' + args.join(' ') + '"'; | |
logger.log(str.yellow); | |
const child = spawn(cmd, args); | |
child.stdout.on('data', function(data) { | |
data.toString().split('\n').forEach(function(line) { | |
checkLine(line, logger); | |
}); | |
}); | |
child.stderr.on('data', function(data) { | |
data.toString().split('\n').forEach(function(line) { | |
checkLine(line, logger); | |
}); | |
}); | |
child.on('close', function(code) { | |
if (code) { | |
const msg = 'Failed to run command ' + args[0]; | |
let error = new Error(msg); | |
error.code = code; | |
return reject(error); | |
} | |
resolve(); | |
}); | |
}) | |
} catch (e) { | |
return Promise.reject(e); | |
} | |
} | |
/** | |
* Initialize the hook. | |
* | |
* @param {Object} logger - The logger instance | |
* @param {Object} config - The CLI config object | |
* @param {CLI} cli - The CLI instance | |
* @param {Object} appc - The node-appc library | |
*/ | |
exports.init = function init(logger, config, cli, appc) { | |
cli.on('cli:post-validate', function(build, finished) { | |
if (!build.cli || !build.cli.timodule) { | |
return finished(); | |
} | |
return Promise.resolve() | |
.then(function() { | |
return runCommand('git', ['submodule', 'init'], logger) | |
}) | |
.then(function() { | |
return runCommand('git', ['submodule', 'update'], logger) | |
}) | |
.then(finished) | |
.catch(function(e) { | |
logger.error(e.message); | |
if (e.code) { | |
process.exit(e.code); | |
} | |
process.exit(1); | |
}) | |
}); | |
cli.on('build.module.pre.compile', function(build) { | |
if (!build.cli || !build.cli.timodule) { | |
return; | |
} | |
const javaSrc = path.join(build.projectDir, 'External', 'src', 'main', 'java', 'com'); | |
const javaDst = path.join(build.projectDir, 'src', 'com'); | |
appc.fs.copyDirSyncRecursive(javaSrc, javaDst, | |
{ | |
preserve: true, | |
logger: logger.debug | |
} | |
); | |
const cppSrc = path.join(build.projectDir, 'External', 'jni'); | |
const cppDst = path.join(build.projectDir, 'native'); | |
appc.fs.copyDirSyncRecursive(cppSrc, cppDst, | |
{ | |
preserve: true, | |
logger: logger.debug | |
} | |
); | |
const moduleGenTemplateDir = path.join(build.projectDir, 'templates', 'module', 'generated'); | |
build.androidMkTemplateFile = path.join(moduleGenTemplateDir, 'Android.mk.ejs'); | |
const str = 'Inject custom "Android.mk" template (' + build.androidMkTemplateFile + ')'; | |
logger.log(str.yellow); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment