Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@michaelsbradleyjr
Created September 6, 2019 20:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michaelsbradleyjr/8339402aee74636fa29bda49bf2e3226 to your computer and use it in GitHub Desktop.
Save michaelsbradleyjr/8339402aee74636fa29bda49bf2e3226 to your computer and use it in GitHub Desktop.
private compile_contracts(contractFiles: any[], cb: Callback<any>) {
if (contractFiles.length === 0) {
return cb(null, {});
}
async.waterfall(
[
(next: Callback<any[]>) => {
this.plugins.runActionsForEvent(
"compiler:contracts:compile:before",
contractFiles,
(err?: Error | null, files: any[] = []) => {
if (err) {
return next(err);
}
next(
null,
files.map((file: any) =>
file instanceof File
? file
: new File({
originalPath: file.path,
path: file.path,
resolver: (callback: Callback<any>) => {
fs.readFile(
file.path,
{ encoding: "utf-8" },
callback
);
},
type: Types.dappFile
})
)
);
}
);
},
(files: any[], next: Callback<any>) => {
const compiledObject: { [index: string]: any } = {};
const compilerOptions = {
isCoverage: this.isCoverage
};
async.eachObject(
this.getAvailableCompilers(),
(extension: string, compilers: any, nextObj: any) => {
const matchingFiles = files.filter(
this.filesMatchingExtension(extension)
);
if (matchingFiles.length === 0) {
return nextObj();
}
async.someLimit(
compilers,
1,
(compiler: any, someCb: Callback<boolean>) => {
compiler.call(
compiler,
matchingFiles,
compilerOptions,
(err: any, compileResult: any) => {
if (err) {
return someCb(err);
}
if (compileResult === false) {
// Compiler not compatible, trying the next one
return someCb(null, false);
}
Object.assign(compiledObject, compileResult);
someCb(null, true);
}
);
},
(err: Error, result: boolean) => {
if (err) {
return nextObj(err);
}
if (!result) {
// No compiler was compatible
return nextObj(
new Error(
__(
"No installed compiler was compatible with your version of %s files",
extension
)
)
);
}
nextObj();
}
);
},
(err?: Error | null) => {
if (err) {
return next(err);
}
files
.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.path
)
);
});
next(null, compiledObject);
}
);
},
(compiledObject: any, next: Callback<any>) => {
this.plugins.runActionsForEvent(
"compiler:contracts:compile:after",
compiledObject,
(err?: Error | null, compiledObj: any = {}) => {
if (err) {
return next(err);
}
next(null, compiledObj);
}
);
}
],
(err?: Error | null, compiledObject?: any) => {
if (err) {
return cb(err);
}
cb(null, compiledObject || {});
}
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment