mirror of https://github.com/embarklabs/embark.git
refactor(@embark/compiler): refactor/simplify compiler module
This commit is contained in:
parent
0c5e725b68
commit
a7ff02997b
|
@ -1,11 +1,6 @@
|
||||||
const async = require("../../utils/async_extend.js");
|
const async = require("../../utils/async_extend.js");
|
||||||
import { Embark } from "../../../typings/embark";
|
import { Embark } from "../../../typings/embark";
|
||||||
import { Plugins } from "../../../typings/plugins";
|
import { CompilerPluginObject, Plugins } from "../../../typings/plugins";
|
||||||
|
|
||||||
interface CompilerPluginObject {
|
|
||||||
extension: string;
|
|
||||||
cb: any;
|
|
||||||
}
|
|
||||||
|
|
||||||
class Compiler {
|
class Compiler {
|
||||||
private logger: any;
|
private logger: any;
|
||||||
|
@ -21,52 +16,56 @@ class Compiler {
|
||||||
}
|
}
|
||||||
|
|
||||||
private compile_contracts(contractFiles: any[], options: any, cb: any) {
|
private compile_contracts(contractFiles: any[], options: any, cb: any) {
|
||||||
const available_compilers: { [index:string] : any } = {};
|
|
||||||
|
|
||||||
if (contractFiles.length === 0) {
|
if (contractFiles.length === 0) {
|
||||||
return cb(null, {});
|
return cb(null, {});
|
||||||
}
|
}
|
||||||
|
|
||||||
this.plugins.getPluginsProperty("compilers", "compilers").forEach((compilerObject: CompilerPluginObject) => {
|
const compiledObject: {[index: string]: any} = {};
|
||||||
available_compilers[compilerObject.extension] = compilerObject.cb;
|
|
||||||
});
|
|
||||||
|
|
||||||
const compiledObject: { [index:string] : any } = {};
|
|
||||||
|
|
||||||
const compilerOptions = {
|
const compilerOptions = {
|
||||||
disableOptimizations: this.disableOptimizations || options.disableOptimizations,
|
disableOptimizations: this.disableOptimizations || options.disableOptimizations,
|
||||||
};
|
};
|
||||||
|
|
||||||
async.eachObject(available_compilers,
|
async.eachObject(this.getAvailableCompilers(),
|
||||||
(extension: string, compiler: any, callback: any) => {
|
(extension: string, compiler: any, next: any) => {
|
||||||
const matchingFiles = contractFiles.filter((file: any) => {
|
const matchingFiles = contractFiles.filter(this.filesMatchingExtension(extension));
|
||||||
const fileMatch = file.filename.match(/\.[0-9a-z]+$/);
|
if (matchingFiles.length === 0) {
|
||||||
if (fileMatch && (fileMatch[0] === extension)) {
|
return next();
|
||||||
file.compiled = true;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!matchingFiles || !matchingFiles.length) {
|
|
||||||
return callback();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
compiler.call(compiler, matchingFiles, compilerOptions, (err: any, compileResult: any) => {
|
compiler.call(compiler, matchingFiles, compilerOptions, (err: any, compileResult: any) => {
|
||||||
Object.assign(compiledObject, compileResult);
|
Object.assign(compiledObject, compileResult);
|
||||||
callback(err, compileResult);
|
next(err, compileResult);
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
(err: any) => {
|
(err: any) => {
|
||||||
contractFiles.forEach((file: any) => {
|
contractFiles.filter((f: any) => !f.compiled).forEach((file: any) => {
|
||||||
if (!file.compiled) {
|
this.logger.warn(__("%s doesn't have a compatible contract compiler. Maybe a plugin exists for it.", file.filename));
|
||||||
this.logger.warn(__("%s doesn't have a compatible contract compiler. Maybe a plugin exists for it.", file.filename));
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
cb(err, compiledObject);
|
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;
|
module.exports = Compiler;
|
||||||
|
|
|
@ -7,3 +7,8 @@ export interface Plugins {
|
||||||
loadInternalPlugin(name: string, options: any): void;
|
loadInternalPlugin(name: string, options: any): void;
|
||||||
getPluginsProperty(prop: string, name: string): any[];
|
getPluginsProperty(prop: string, name: string): any[];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface CompilerPluginObject {
|
||||||
|
extension: string;
|
||||||
|
cb: any;
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue