1 | const path = require("path");
|
2 | const { exec } = require("child_process");
|
3 | const fs = require("fs");
|
4 |
|
5 | const async = require("async");
|
6 | const colors = require("colors");
|
7 | const minimatch = require("minimatch");
|
8 |
|
9 | const Common = require("@truffle/compile-common");
|
10 |
|
11 | const compiler = {
|
12 | name: "ligo",
|
13 | version: "next"
|
14 | };
|
15 |
|
16 | const LIGO_PATTERN = "**/*.{ligo,mligo}";
|
17 |
|
18 |
|
19 |
|
20 | const compile = Object.assign({}, Common);
|
21 |
|
22 |
|
23 |
|
24 |
|
25 | function checkLigo(callback) {
|
26 | exec(
|
27 | "docker run --rm -i ligolang/ligo:next --help",
|
28 | (err, stdout, stderr) => {
|
29 | if (err)
|
30 | return callback(`${colors.red("Error executing ligo:")}\n${stderr}`);
|
31 |
|
32 | callback(null);
|
33 | }
|
34 | );
|
35 | }
|
36 |
|
37 |
|
38 | function execLigo(sourcePath, entryPoint, callback) {
|
39 | const command = `docker run -v $PWD:$PWD --rm -i ligolang/ligo:next compile-contract --michelson-format=json ${sourcePath} ${entryPoint}`;
|
40 |
|
41 | exec(command, { maxBuffer: 600 * 1024 }, (err, stdout, stderr) => {
|
42 | if (err)
|
43 | return callback(
|
44 | `${stderr}\n${colors.red(
|
45 | `Compilation of ${sourcePath} failed. See above.`
|
46 | )}`
|
47 | );
|
48 |
|
49 | const jsonContractOutput = stdout.trim();
|
50 |
|
51 | callback(null, jsonContractOutput);
|
52 | });
|
53 | }
|
54 |
|
55 |
|
56 | function compileAll(options, callback) {
|
57 | const entryPoint = options._[0] || "main";
|
58 | options.logger = options.logger || console;
|
59 |
|
60 | compile.display(options.paths, options, entryPoint);
|
61 |
|
62 | async.map(
|
63 | options.paths,
|
64 | (sourcePath, c) => {
|
65 | execLigo(sourcePath, entryPoint, (err, compiledContract) => {
|
66 | if (err) return c(err);
|
67 |
|
68 |
|
69 | const extension = path.extname(sourcePath);
|
70 | const basename = path.basename(sourcePath, extension);
|
71 |
|
72 | const contractName = basename;
|
73 |
|
74 | const sourceBuffer = fs.readFileSync(sourcePath);
|
75 | const sourceContents = sourceBuffer.toString();
|
76 |
|
77 | const contractDefinition = {
|
78 | contractName,
|
79 | abi: [],
|
80 | sourcePath,
|
81 | source: sourceContents,
|
82 | code: compiledContract,
|
83 | compiler
|
84 | };
|
85 |
|
86 | c(null, contractDefinition);
|
87 | });
|
88 | },
|
89 | (err, contracts) => {
|
90 | if (err) return callback(err);
|
91 |
|
92 | const result = contracts.reduce((result, contract) => {
|
93 | result[contract.contractName] = contract;
|
94 |
|
95 | return result;
|
96 | }, {});
|
97 |
|
98 | callback(null, result, options.paths, compiler);
|
99 | }
|
100 | );
|
101 | }
|
102 |
|
103 |
|
104 | function compileLigo(options, callback) {
|
105 |
|
106 | options.paths = options.paths.filter(path => minimatch(path, LIGO_PATTERN));
|
107 |
|
108 |
|
109 | if (options.paths.length === 0) return callback(null, {}, []);
|
110 |
|
111 | checkLigo(err => {
|
112 | if (err) return callback(err);
|
113 |
|
114 | return compileAll(options, callback);
|
115 | });
|
116 | }
|
117 |
|
118 |
|
119 | compileLigo.all = (options, callback) =>
|
120 | compile.all(
|
121 | compile,
|
122 | compile.updateContractsDirectory(options, LIGO_PATTERN),
|
123 | callback
|
124 | );
|
125 |
|
126 |
|
127 | compileLigo.necessary = (options, callback) =>
|
128 | compile.necessary(
|
129 | compile,
|
130 | compile.updateContractsDirectory(options, LIGO_PATTERN),
|
131 | callback
|
132 | );
|
133 |
|
134 | compile.with_dependencies = compileLigo;
|
135 | module.exports = compileLigo;
|