1 | "use strict";
|
2 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
3 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
4 | };
|
5 | Object.defineProperty(exports, "__esModule", { value: true });
|
6 | const chalk_1 = __importDefault(require("chalk"));
|
7 | const fs_extra_1 = __importDefault(require("fs-extra"));
|
8 | const path_1 = __importDefault(require("path"));
|
9 | const artifacts_1 = require("../internal/artifacts");
|
10 | const constants_1 = require("../internal/constants");
|
11 | const config_env_1 = require("../internal/core/config/config-env");
|
12 | const errors_1 = require("../internal/core/errors");
|
13 | const errors_list_1 = require("../internal/core/errors-list");
|
14 | const compiler_1 = require("../internal/solidity/compiler");
|
15 | const compiler_input_1 = require("../internal/solidity/compiler/compiler-input");
|
16 | const dependencyGraph_1 = require("../internal/solidity/dependencyGraph");
|
17 | const resolver_1 = require("../internal/solidity/resolver");
|
18 | const glob_1 = require("../internal/util/glob");
|
19 | const global_dir_1 = require("../internal/util/global-dir");
|
20 | const strings_1 = require("../internal/util/strings");
|
21 | const task_names_1 = require("./task-names");
|
22 | const cache_1 = require("./utils/cache");
|
23 | async function cacheSolcJsonFiles(config, input, output) {
|
24 | await fs_extra_1.default.ensureDir(config.paths.cache);
|
25 |
|
26 | await fs_extra_1.default.writeFile(path_1.default.join(config.paths.cache, constants_1.SOLC_INPUT_FILENAME), JSON.stringify(input, undefined, 2), {
|
27 | encoding: "utf8",
|
28 | });
|
29 | await fs_extra_1.default.writeFile(path_1.default.join(config.paths.cache, constants_1.SOLC_OUTPUT_FILENAME), JSON.stringify(output, undefined, 2), {
|
30 | encoding: "utf8",
|
31 | });
|
32 | }
|
33 | function isConsoleLogError(error) {
|
34 | return (error.type === "TypeError" &&
|
35 | typeof error.message === "string" &&
|
36 | error.message.includes("log") &&
|
37 | error.message.includes("type(library console)"));
|
38 | }
|
39 | function default_1() {
|
40 | config_env_1.internalTask(task_names_1.TASK_COMPILE_GET_SOURCE_PATHS, async (_, { config }) => {
|
41 | return glob_1.glob(path_1.default.join(config.paths.sources, "**/*.sol"));
|
42 | });
|
43 | config_env_1.internalTask(task_names_1.TASK_COMPILE_GET_RESOLVED_SOURCES, async (_, { config, run }) => {
|
44 | const resolver = new resolver_1.Resolver(config.paths.root);
|
45 | const paths = await run(task_names_1.TASK_COMPILE_GET_SOURCE_PATHS);
|
46 | return Promise.all(paths.map((p) => resolver.resolveProjectSourceFile(p)));
|
47 | });
|
48 | config_env_1.internalTask(task_names_1.TASK_COMPILE_GET_DEPENDENCY_GRAPH, async (_, { config, run }) => {
|
49 | const resolver = new resolver_1.Resolver(config.paths.root);
|
50 | const localFiles = await run(task_names_1.TASK_COMPILE_GET_RESOLVED_SOURCES);
|
51 | return dependencyGraph_1.DependencyGraph.createFromResolvedFiles(resolver, localFiles);
|
52 | });
|
53 | config_env_1.internalTask(task_names_1.TASK_COMPILE_GET_COMPILER_INPUT, async (_, { config, run }) => {
|
54 | const dependencyGraph = await run(task_names_1.TASK_COMPILE_GET_DEPENDENCY_GRAPH);
|
55 | return compiler_input_1.getInputFromDependencyGraph(dependencyGraph, config.solc.optimizer, config.solc.evmVersion);
|
56 | });
|
57 | config_env_1.internalTask(task_names_1.TASK_COMPILE_RUN_COMPILER)
|
58 | .addParam("input", "The compiler standard JSON input", undefined, config_env_1.types.json)
|
59 | .setAction(async ({ input }, { config }) => {
|
60 | const compilersCache = await global_dir_1.getCompilersDir();
|
61 | const compiler = new compiler_1.Compiler(config.solc.version, compilersCache);
|
62 | return compiler.compile(input);
|
63 | });
|
64 | config_env_1.internalTask(task_names_1.TASK_COMPILE_COMPILE, async (_, { config, run }) => {
|
65 | const input = await run(task_names_1.TASK_COMPILE_GET_COMPILER_INPUT);
|
66 | console.log("Compiling...");
|
67 | const output = await run(task_names_1.TASK_COMPILE_RUN_COMPILER, { input });
|
68 | let hasErrors = false;
|
69 | let hasConsoleLogErrors = false;
|
70 | if (output.errors) {
|
71 | for (const error of output.errors) {
|
72 | hasErrors = hasErrors || error.severity === "error";
|
73 | if (error.severity === "error") {
|
74 | hasErrors = true;
|
75 | if (isConsoleLogError(error)) {
|
76 | hasConsoleLogErrors = true;
|
77 | }
|
78 | console.error(chalk_1.default.red(error.formattedMessage));
|
79 | }
|
80 | else {
|
81 | console.log("\n");
|
82 | console.warn(chalk_1.default.yellow(error.formattedMessage));
|
83 | }
|
84 | }
|
85 | }
|
86 | if (hasConsoleLogErrors) {
|
87 | console.error(chalk_1.default.red(`The console.log call you made isn’t supported. See https://buidler.dev/console-log for the list of supported methods.`));
|
88 | console.log();
|
89 | }
|
90 | if (hasErrors || !output.contracts) {
|
91 | throw new errors_1.BuidlerError(errors_list_1.ERRORS.BUILTIN_TASKS.COMPILE_FAILURE);
|
92 | }
|
93 | await cacheSolcJsonFiles(config, input, output);
|
94 | await cache_1.cacheBuidlerConfig(config.paths, config.solc);
|
95 | return output;
|
96 | });
|
97 | config_env_1.internalTask(task_names_1.TASK_COMPILE_CHECK_CACHE, async ({ force }, { config, run }) => {
|
98 | if (force) {
|
99 | return false;
|
100 | }
|
101 | const dependencyGraph = await run(task_names_1.TASK_COMPILE_GET_DEPENDENCY_GRAPH);
|
102 | const sourceTimestamps = dependencyGraph
|
103 | .getResolvedFiles()
|
104 | .map((file) => file.lastModificationDate.getTime());
|
105 | return cache_1.areArtifactsCached(sourceTimestamps, config.solc, config.paths);
|
106 | });
|
107 | config_env_1.internalTask(task_names_1.TASK_BUILD_ARTIFACTS, async ({ force }, { config, run }) => {
|
108 | const sources = await run(task_names_1.TASK_COMPILE_GET_SOURCE_PATHS);
|
109 | if (sources.length === 0) {
|
110 | console.log("No Solidity source file available.");
|
111 | return;
|
112 | }
|
113 | const isCached = await run(task_names_1.TASK_COMPILE_CHECK_CACHE, { force });
|
114 | if (isCached) {
|
115 | console.log("All contracts have already been compiled, skipping compilation.");
|
116 | return;
|
117 | }
|
118 | const compilationOutput = await run(task_names_1.TASK_COMPILE_COMPILE);
|
119 | if (compilationOutput === undefined) {
|
120 | return;
|
121 | }
|
122 | await fs_extra_1.default.ensureDir(config.paths.artifacts);
|
123 | let numberOfContracts = 0;
|
124 | for (const file of Object.values(compilationOutput.contracts)) {
|
125 | for (const [contractName, contractOutput] of Object.entries(file)) {
|
126 | const artifact = artifacts_1.getArtifactFromContractOutput(contractName, contractOutput);
|
127 | numberOfContracts += 1;
|
128 | await artifacts_1.saveArtifact(config.paths.artifacts, artifact);
|
129 | }
|
130 | }
|
131 | console.log("Compiled", numberOfContracts, strings_1.pluralize(numberOfContracts, "contract"), "successfully");
|
132 | });
|
133 | config_env_1.task(task_names_1.TASK_COMPILE, "Compiles the entire project, building all artifacts")
|
134 | .addFlag("force", "Force compilation ignoring cache")
|
135 | .setAction(async ({ force: force }, { run }) => run(task_names_1.TASK_BUILD_ARTIFACTS, { force }));
|
136 | }
|
137 | exports.default = default_1;
|
138 |
|
\ | No newline at end of file |