embark/lib/contracts/compiler.js

57 lines
1.6 KiB
JavaScript
Raw Normal View History

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