BrickUp API Service for Docker version.

builder.js 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. var crypto = require('crypto');
  2. var fs = require('fs');
  3. var fse = require('fs-extra');
  4. var path = require('path');
  5. var child_process = require('child_process');
  6. var co = require('co');
  7. var Builder = module.exports = function(basePath, sources) {
  8. this.config = {};
  9. this.id = null;
  10. this.libPath = path.join(__dirname, '..', 'libraries');
  11. this.libs = [];
  12. this.basePath = basePath;
  13. this.sources = sources || {};
  14. this.workingPath = '';
  15. this.platform = 'uno'
  16. };
  17. Builder.prototype.init = function(callback) {
  18. var self = this;
  19. this.id = crypto.randomBytes(16).toString('hex').substr(0, 8) + Date.now();
  20. this.workingPath = path.join(this.basePath, this.id);
  21. // Create working directory
  22. fse.mkdirs(this.workingPath, function(err) {
  23. if (callback)
  24. callback(err);
  25. });
  26. };
  27. Builder.prototype.scanLibraries = function(callback) {
  28. var self = this;
  29. co(function *() {
  30. try {
  31. // Scanning system libraries
  32. var systemLibs = yield function(done) {
  33. fs.readdir('/usr/share/arduino/libraries', done);
  34. };
  35. self.libs.concat(systemLibs);
  36. } catch(e) {}
  37. try {
  38. // Scanning user libraries
  39. var userLibs = yield function(done) {
  40. fs.readdir(self.libPath, done);
  41. };
  42. self.libs.concat(userLibs);
  43. } catch(e) {}
  44. callback();
  45. });
  46. };
  47. Builder.prototype.generateMakefile = function(callback) {
  48. var content = [
  49. 'BOARD_TAG = uno',
  50. 'USER_LIB_PATH = ' + path.join(__dirname, '..', 'libraries'),
  51. 'ARDUINO_LIBS = ' + this.libs.join(' '),
  52. 'ARDUINO_DIR = /usr/share/arduino',
  53. 'include /usr/share/arduino/Arduino.mk'
  54. ];
  55. fse.outputFile(path.join(this.workingPath, 'Makefile'), content.join('\n'), callback);
  56. };
  57. Builder.prototype.generateSources = function(callback) {
  58. var self = this;
  59. co(function *() {
  60. // Generating all source files
  61. for (var filename in self.sources) {
  62. var filepath = path.join(self.workingPath, filename);
  63. yield function(done) {
  64. fse.outputFile(filepath, self.sources[filename], done);
  65. };
  66. }
  67. // Everything was done
  68. if (callback)
  69. callback();
  70. });
  71. };
  72. Builder.prototype.compile = function(callback) {
  73. var self = this;
  74. child_process.exec('make -C ' + this.workingPath, function(err, cmdout, cmderr) {
  75. if (err) {
  76. callback(err);
  77. } else if (callback) {
  78. return callback(null, path.join(self.workingPath, 'build-uno', self.id + '.hex'));
  79. }
  80. });
  81. };