refactor(@embark/compiler): refactor/simplify compiler module

This commit is contained in:
Iuri Matias 2018-12-14 19:09:06 -05:00
parent 0c5e725b68
commit a7ff02997b
2 changed files with 35 additions and 31 deletions

View File

@ -1,11 +1,6 @@
const async = require("../../utils/async_extend.js");
import { Embark } from "../../../typings/embark";
import { Plugins } from "../../../typings/plugins";
interface CompilerPluginObject {
extension: string;
cb: any;
}
import { CompilerPluginObject, Plugins } from "../../../typings/plugins";
class Compiler {
private logger: any;
@ -21,52 +16,56 @@ class Compiler {
}
private compile_contracts(contractFiles: any[], options: any, cb: any) {
const available_compilers: { [index:string] : any } = {};
if (contractFiles.length === 0) {
return cb(null, {});
}
this.plugins.getPluginsProperty("compilers", "compilers").forEach((compilerObject: CompilerPluginObject) => {
available_compilers[compilerObject.extension] = compilerObject.cb;
});
const compiledObject: {[index: string]: any} = {};
const compilerOptions = {
disableOptimizations: this.disableOptimizations || options.disableOptimizations,
};
async.eachObject(available_compilers,
(extension: string, compiler: any, callback: any) => {
const matchingFiles = contractFiles.filter((file: any) => {
const fileMatch = file.filename.match(/\.[0-9a-z]+$/);
if (fileMatch && (fileMatch[0] === extension)) {
file.compiled = true;
return true;
async.eachObject(this.getAvailableCompilers(),
(extension: string, compiler: any, next: any) => {
const matchingFiles = contractFiles.filter(this.filesMatchingExtension(extension));
if (matchingFiles.length === 0) {
return next();
}
return false;
});
if (!matchingFiles || !matchingFiles.length) {
return callback();
}
compiler.call(compiler, matchingFiles, compilerOptions, (err: any, compileResult: any) => {
Object.assign(compiledObject, compileResult);
callback(err, compileResult);
next(err, compileResult);
});
},
(err: any) => {
contractFiles.forEach((file: any) => {
if (!file.compiled) {
contractFiles.filter((f: any) => !f.compiled).forEach((file: any) => {
this.logger.warn(__("%s doesn't have a compatible contract compiler. Maybe a plugin exists for it.", file.filename));
}
});
cb(err, compiledObject);
},
);
}
private getAvailableCompilers() {
const available_compilers: { [index: string]: any } = {};
this.plugins.getPluginsProperty("compilers", "compilers").forEach((compilerObject: CompilerPluginObject) => {
available_compilers[compilerObject.extension] = compilerObject.cb;
});
return available_compilers;
}
private filesMatchingExtension(extension: string) {
return (file: any) => {
const fileMatch = file.filename.match(/\.[0-9a-z]+$/);
if (fileMatch && (fileMatch[0] === extension)) {
file.compiled = true;
return true;
}
return false;
};
}
}
module.exports = Compiler;

View File

@ -7,3 +7,8 @@ export interface Plugins {
loadInternalPlugin(name: string, options: any): void;
getPluginsProperty(prop: string, name: string): any[];
}
export interface CompilerPluginObject {
extension: string;
cb: any;
}