UNPKG

7.42 kBJavaScriptView Raw
1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3 return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5Object.defineProperty(exports, "__esModule", { value: true });
6const chalk_1 = __importDefault(require("chalk"));
7const fs_extra_1 = __importDefault(require("fs-extra"));
8const path_1 = __importDefault(require("path"));
9const artifacts_1 = require("../internal/artifacts");
10const constants_1 = require("../internal/constants");
11const config_env_1 = require("../internal/core/config/config-env");
12const errors_1 = require("../internal/core/errors");
13const errors_list_1 = require("../internal/core/errors-list");
14const compiler_1 = require("../internal/solidity/compiler");
15const compiler_input_1 = require("../internal/solidity/compiler/compiler-input");
16const dependencyGraph_1 = require("../internal/solidity/dependencyGraph");
17const resolver_1 = require("../internal/solidity/resolver");
18const glob_1 = require("../internal/util/glob");
19const global_dir_1 = require("../internal/util/global-dir");
20const strings_1 = require("../internal/util/strings");
21const task_names_1 = require("./task-names");
22const cache_1 = require("./utils/cache");
23async function cacheSolcJsonFiles(config, input, output) {
24 await fs_extra_1.default.ensureDir(config.paths.cache);
25 // TODO: This could be much better. It feels somewhat hardcoded
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}
33function 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}
39function 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}
137exports.default = default_1;
138//# sourceMappingURL=compile.js.map
\No newline at end of file