UNPKG

@truffle/compile-solidity

Version:
181 lines 8.5 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; const debug = require("debug")("compile"); const findContracts = require("@truffle/contract-sources"); const Config = require("@truffle/config"); const Profiler = require("./profiler"); const { CompilerSupplier } = require("./compilerSupplier"); const { run } = require("./run"); const { normalizeOptions } = require("./normalizeOptions"); const { compileWithPragmaAnalysis } = require("./compileWithPragmaAnalysis"); const { reportSources } = require("./reportSources"); const { Compilations } = require("@truffle/compile-common"); const expect = require("@truffle/expect"); const partition = require("lodash/partition"); const fs = require("fs-extra"); function compileYulPaths(yulPaths, options) { return __awaiter(this, void 0, void 0, function* () { let yulCompilations = []; for (const path of yulPaths) { const yulOptions = options.with({ compilationTargets: [path] }); //load up Yul sources, since they weren't loaded up earlier //(we'll just use FS for this rather than going through the resolver, //for simplicity, since there are no imports to worry about) const yulSource = fs.readFileSync(path, { encoding: "utf8" }); debug("Compiling Yul"); const compilation = yield run({ [path]: yulSource }, yulOptions, { language: "Yul" }); debug("Yul compiled successfully"); if (compilation.contracts.length > 0) { yulCompilations.push(compilation); } } if (yulPaths.length > 0 && !options.quiet) { //replacement for individual Yul warnings options.logger.log("> Warning: Yul is still experimental. Avoid using it in live deployments."); } return yulCompilations; }); } const Compile = { // this takes an object with keys being the name and values being source // material as well as an options object // NOTE: this function does *not* transform the source path prefix to // "project:/" before passing to the compiler! sources({ sources, options }) { return __awaiter(this, void 0, void 0, function* () { options = Config.default().merge(options); options = normalizeOptions(options); //note: "solidity" here includes JSON as well! const [yulNames, solidityNames] = partition(Object.keys(sources), name => name.endsWith(".yul")); const soliditySources = Object.assign({}, ...solidityNames.map(name => ({ [name]: sources[name] }))); let solidityCompilations = []; let yulCompilations = []; if (solidityNames.length > 0) { debug("Compiling Solidity (specified sources)"); const compilation = yield run(soliditySources, options, { noTransform: true }); debug("Compiled Solidity"); if (compilation.contracts.length > 0) { solidityCompilations = [compilation]; } } for (const name of yulNames) { debug("Compiling Yul (specified sources)"); const compilation = yield run({ [name]: sources[name] }, options, { language: "Yul", noTransform: true }); debug("Compiled Yul"); yulCompilations.push(compilation); } const compilations = [...solidityCompilations, ...yulCompilations]; return Compilations.promoteCompileResult({ compilations }); }); }, all(options) { return __awaiter(this, void 0, void 0, function* () { const paths = [ ...new Set([ ...(yield findContracts(options.contracts_directory)), ...(options.files || []) ]) ]; return yield Compile.sourcesWithDependencies({ paths, options }); }); }, necessary(options) { return __awaiter(this, void 0, void 0, function* () { options.logger = options.logger || console; const paths = yield Profiler.updated(options); return yield Compile.sourcesWithDependencies({ paths, options }); }); }, // this takes an array of paths and options sourcesWithDependencies({ paths, options }) { return __awaiter(this, void 0, void 0, function* () { if (options.compilers.solc.version === "pragma") { return this.sourcesWithPragmaAnalysis({ paths, options }); } options.logger = options.logger || console; options.contracts_directory = options.contracts_directory || process.cwd(); debug("paths: %O", paths); expect.options(options, [ "working_directory", "contracts_directory", "resolver" ]); options = Config.default().merge(options); options = normalizeOptions(options); const supplier = new CompilerSupplier({ events: options.events, solcConfig: options.compilers.solc }); const { solc } = yield supplier.load(); //note: solidityPaths here still includes JSON as well! const [yulPaths, solidityPaths] = partition(paths, path => path.endsWith(".yul")); debug("invoking profiler"); //only invoke profiler on Solidity, not Yul const { allSources, compilationTargets } = yield Profiler.requiredSources(options.with({ paths: solidityPaths, base_path: options.contracts_directory, resolver: options.resolver, compiler: { name: "solc", version: solc.version() } })); debug("compilationTargets: %O", compilationTargets); // we can exit if there are no Solidity/Yul files to compile since // it indicates that we only have Vyper-related JSON const solidityTargets = compilationTargets.filter(fileName => fileName.endsWith(".sol")); if (solidityTargets.length === 0 && yulPaths.length === 0) { return Compilations.emptyWorkflowCompileResult(); } reportSources({ paths: [...compilationTargets, ...yulPaths], options }); let solidityCompilations = []; // only call run if there are sources to run on! if (Object.keys(allSources).length > 0) { const solidityOptions = options.with({ compilationTargets }); debug("Compiling Solidity"); const compilation = yield run(allSources, solidityOptions, { solc }); debug("Solidity compiled successfully"); if (compilation.contracts.length > 0) { solidityCompilations = [compilation]; } } const yulCompilations = yield compileYulPaths(yulPaths, options); const compilations = [...solidityCompilations, ...yulCompilations]; return Compilations.promoteCompileResult({ compilations }); }); }, sourcesWithPragmaAnalysis({ paths, options }) { return __awaiter(this, void 0, void 0, function* () { const compilationResult = yield compileWithPragmaAnalysis({ paths, options }); return Compilations.promoteCompileResult(compilationResult); }); } }; module.exports = { Compile, CompilerSupplier }; //# sourceMappingURL=index.js.map