embark-area-51/lib/modules/compiler/index.js

65 lines
1.8 KiB
JavaScript
Raw Normal View History

2018-07-20 17:22:12 +00:00
let async = require('../../utils/async_extend.js');
2017-02-17 12:14:44 +00:00
2017-03-30 11:12:39 +00:00
class Compiler {
2018-07-20 17:22:12 +00:00
constructor(embark, options) {
const self = this;
2017-03-30 11:12:39 +00:00
this.plugins = options.plugins;
2018-07-20 17:22:12 +00:00
this.events = embark.events;
this.logger = embark.logger;
this.events.setCommandHandler("compiler:contracts", function(contractFiles, cb) {
self.compile_contracts(contractFiles, cb);
});
2017-03-30 11:12:39 +00:00
}
2017-01-29 02:31:09 +00:00
2017-03-30 11:12:39 +00:00
compile_contracts(contractFiles, cb) {
const self = this;
2017-12-16 20:39:30 +00:00
let available_compilers = {};
2017-01-29 02:31:09 +00:00
if (contractFiles.length === 0) {
return cb(null, {});
}
let pluginCompilers = self.plugins.getPluginsProperty('compilers', 'compilers');
2018-07-20 18:28:46 +00:00
console.dir(pluginCompilers);
2017-12-29 13:26:31 +00:00
pluginCompilers.forEach(function (compilerObject) {
available_compilers[compilerObject.extension] = compilerObject.cb;
});
2017-01-29 02:31:09 +00:00
2017-03-30 11:12:39 +00:00
let compiledObject = {};
2017-01-29 02:31:09 +00:00
2017-03-30 11:12:39 +00:00
async.eachObject(available_compilers,
function (extension, compiler, callback) {
let matchingFiles = contractFiles.filter(function (file) {
2017-06-29 11:39:12 +00:00
let fileMatch = file.filename.match(/\.[0-9a-z]+$/);
if (fileMatch && (fileMatch[0] === extension)) {
file.compiled = true;
return true;
}
return false;
2017-03-30 11:12:39 +00:00
});
2016-08-14 12:04:34 +00:00
2018-05-17 14:02:53 +00:00
if (!matchingFiles || !matchingFiles.length) {
return callback();
}
compiler.call(compiler, matchingFiles, function (err, compileResult) {
2017-03-30 11:12:39 +00:00
Object.assign(compiledObject, compileResult);
callback(err, compileResult);
});
},
function (err) {
contractFiles.forEach(file => {
if (!file.compiled) {
2018-05-08 21:49:46 +00:00
self.logger.warn(__("%s doesn't have a compatible contract compiler. Maybe a plugin exists for it.", file.filename));
}
});
2017-03-30 11:12:39 +00:00
cb(err, compiledObject);
}
2017-03-30 11:12:39 +00:00
);
}
}
2016-08-14 12:04:34 +00:00
module.exports = Compiler;