Guest User

Untitled

a guest
Jan 22nd, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.14 KB | None | 0 0
  1. 'use strict';
  2.  
  3. const path = require('path');
  4. const spawn = require('child_process').spawn;
  5.  
  6. /** The plugin's identifier */
  7. exports.id = 'android.module.hook';
  8.  
  9. /** The Titanium CLI version that this hook is compatible with */
  10. exports.cliVersion = '>=3.2';
  11.  
  12. function checkLine(line, logger) {
  13. const re = new RegExp( // eslint-disable-line security/detect-non-literal-regexp
  14. '(?:\u001b\\[\\d+m)?\\[?('
  15. + logger.getLevels().join('|')
  16. + ')\\]?\\s*(?:\u001b\\[\\d+m)?(.*)', 'i'
  17. );
  18.  
  19. if (line) {
  20. const m = line.match(re);
  21. if (m) {
  22. logger[m[1].toLowerCase()](m[2].trim());
  23. } else {
  24. logger.debug(line);
  25. }
  26. }
  27. }
  28.  
  29. function runCommand(cmd, args, logger) {
  30. // when calling on Windows, we need to escape ampersands in the command
  31. if (process.platform === 'win32') {
  32. cmd.replace(/&/g, '^&');
  33. }
  34. try {
  35. return new Promise(function(resolve, reject) {
  36. const str = 'Run "' + cmd + ' ' + args.join(' ') + '"';
  37. logger.log(str.yellow);
  38. const child = spawn(cmd, args);
  39. child.stdout.on('data', function(data) {
  40. data.toString().split('\n').forEach(function(line) {
  41. checkLine(line, logger);
  42. });
  43. });
  44.  
  45. child.stderr.on('data', function(data) {
  46. data.toString().split('\n').forEach(function(line) {
  47. checkLine(line, logger);
  48. });
  49. });
  50.  
  51. child.on('close', function(code) {
  52. if (code) {
  53. const msg = 'Failed to run command ' + args[0];
  54. let error = new Error(msg);
  55. error.code = code;
  56. return reject(error);
  57. }
  58.  
  59. resolve();
  60. });
  61. })
  62.  
  63. } catch (e) {
  64. return Promise.reject(e);
  65. }
  66. }
  67.  
  68. /**
  69. * Initialize the hook.
  70. *
  71. * @param {Object} logger - The logger instance
  72. * @param {Object} config - The CLI config object
  73. * @param {CLI} cli - The CLI instance
  74. * @param {Object} appc - The node-appc library
  75. */
  76. exports.init = function init(logger, config, cli, appc) {
  77. cli.on('cli:post-validate', function(build, finished) {
  78. if (!build.cli || !build.cli.timodule) {
  79. return finished();
  80. }
  81. return Promise.resolve()
  82. .then(function() {
  83. return runCommand('git', ['submodule', 'init'], logger)
  84. })
  85. .then(function() {
  86. return runCommand('git', ['submodule', 'update'], logger)
  87. })
  88. .then(finished)
  89. .catch(function(e) {
  90. logger.error(e.message);
  91. if (e.code) {
  92. process.exit(e.code);
  93. }
  94. process.exit(1);
  95. })
  96. });
  97. cli.on('build.module.pre.compile', function(build) {
  98. if (!build.cli || !build.cli.timodule) {
  99. return;
  100. }
  101. const javaSrc = path.join(build.projectDir, 'External', 'src', 'main', 'java', 'com');
  102. const javaDst = path.join(build.projectDir, 'src', 'com');
  103. appc.fs.copyDirSyncRecursive(javaSrc, javaDst,
  104. {
  105. preserve: true,
  106. logger: logger.debug
  107. }
  108. );
  109. const cppSrc = path.join(build.projectDir, 'External', 'jni');
  110. const cppDst = path.join(build.projectDir, 'native');
  111. appc.fs.copyDirSyncRecursive(cppSrc, cppDst,
  112. {
  113. preserve: true,
  114. logger: logger.debug
  115. }
  116. );
  117.  
  118. const moduleGenTemplateDir = path.join(build.projectDir, 'templates', 'module', 'generated');
  119. build.androidMkTemplateFile = path.join(moduleGenTemplateDir, 'Android.mk.ejs');
  120. const str = 'Inject custom "Android.mk" template (' + build.androidMkTemplateFile + ')';
  121. logger.log(str.yellow);
  122. });
  123. };
Add Comment
Please, Sign In to add comment