var crypto = require('crypto'); var fs = require('fs'); var fse = require('fs-extra'); var path = require('path'); var child_process = require('child_process'); var co = require('co'); var Builder = module.exports = function(basePath, sources) { this.config = {}; this.id = null; this.libPath = path.join(__dirname, '..', 'libraries'); this.libs = []; this.basePath = basePath; this.sources = sources || {}; this.workingPath = ''; this.platform = 'uno' }; Builder.prototype.init = function(callback) { var self = this; this.id = crypto.randomBytes(16).toString('hex').substr(0, 8) + Date.now(); this.workingPath = path.join(this.basePath, this.id); // Create working directory fse.mkdirs(this.workingPath, function(err) { if (callback) callback(err); }); }; Builder.prototype.scanLibraries = function(callback) { var self = this; co(function *() { try { // Scanning system libraries var systemLibs = yield function(done) { fs.readdir('/usr/share/arduino/libraries', done); }; self.libs.concat(systemLibs); } catch(e) {} try { // Scanning user libraries var userLibs = yield function(done) { fs.readdir(self.libPath, done); }; self.libs.concat(userLibs); } catch(e) {} callback(); }); }; Builder.prototype.generateMakefile = function(callback) { var content = [ 'BOARD_TAG = uno', 'USER_LIB_PATH = ' + path.join(__dirname, '..', 'libraries'), 'ARDUINO_LIBS = ' + this.libs.join(' '), 'ARDUINO_DIR = /usr/share/arduino', 'include /usr/share/arduino/Arduino.mk' ]; fse.outputFile(path.join(this.workingPath, 'Makefile'), content.join('\n'), callback); }; Builder.prototype.generateSources = function(callback) { var self = this; co(function *() { // Generating all source files for (var filename in self.sources) { var filepath = path.join(self.workingPath, filename); yield function(done) { fse.outputFile(filepath, self.sources[filename], done); }; } // Everything was done if (callback) callback(); }); }; Builder.prototype.compile = function(callback) { var self = this; child_process.exec('make -C ' + this.workingPath, function(err, cmdout, cmderr) { if (err) { callback(err); } else if (callback) { return callback(null, path.join(self.workingPath, 'build-uno', self.id + '.hex')); } }); };