From 0781ec7423d1a75cc47dd9ded00df0e023fdad1c Mon Sep 17 00:00:00 2001 From: Aniket-Engg Date: Tue, 26 Apr 2022 18:41:10 +0530 Subject: [PATCH] truffle compilation with custom config --- .../solidity-compiler/src/app/compiler-api.ts | 4 ++-- libs/remix-lib/src/types/ICompilerApi.ts | 2 +- .../src/lib/logic/compileTabLogic.ts | 22 ++++++++++++++++++- libs/remixd/src/services/truffleClient.ts | 4 ++-- 4 files changed, 26 insertions(+), 6 deletions(-) diff --git a/apps/solidity-compiler/src/app/compiler-api.ts b/apps/solidity-compiler/src/app/compiler-api.ts index e69bd8ef95..ade46d3223 100644 --- a/apps/solidity-compiler/src/app/compiler-api.ts +++ b/apps/solidity-compiler/src/app/compiler-api.ts @@ -105,8 +105,8 @@ export const CompilerApiMixin = (Base) => class extends Base { return this.call('hardhat', 'compile', configFile) } - compileWithTruffle () { - return this.call('truffle', 'compile') + compileWithTruffle (configFile) { + return this.call('truffle', 'compile', configFile) } logToTerminal (content) { diff --git a/libs/remix-lib/src/types/ICompilerApi.ts b/libs/remix-lib/src/types/ICompilerApi.ts index 4b5dbd4380..8e43892fa0 100644 --- a/libs/remix-lib/src/types/ICompilerApi.ts +++ b/libs/remix-lib/src/types/ICompilerApi.ts @@ -42,7 +42,7 @@ export interface ICompilerApi { logToTerminal: (log: terminalLog) => void compileWithHardhat: (configPath: string) => Promise - compileWithTruffle: () => Promise + compileWithTruffle: (configPath: string) => Promise statusChanged: (data: { key: string, title?: string, type?: string }) => void, emit?: (key: string, ...payload: any) => void } diff --git a/libs/remix-ui/solidity-compiler/src/lib/logic/compileTabLogic.ts b/libs/remix-ui/solidity-compiler/src/lib/logic/compileTabLogic.ts index 8ae5a0871e..05e47fcd17 100644 --- a/libs/remix-ui/solidity-compiler/src/lib/logic/compileTabLogic.ts +++ b/libs/remix-ui/solidity-compiler/src/lib/logic/compileTabLogic.ts @@ -141,11 +141,31 @@ export class CompileTabLogic { }) } } else if (externalCompType === 'truffle') { - this.api.compileWithTruffle().then((result) => { + const { currentVersion, optimize, runs, evmVersion } = this.compiler.state + if (currentVersion) { + const fileContent = `module.exports = { + compilers: { + solc: { + version: '${currentVersion.substring(0, currentVersion.indexOf('+commit'))}', + settings: { + optimizer: { + enabled: ${optimize}, + runs: ${runs}, + }, + evmVersion: ${evmVersion} + } + } + } + }` + const configFilePath = 'remix-compiler.config.js' + this.api.writeFile(configFilePath, fileContent) + _paq.push(['trackEvent', 'compiler', 'compileWithTruffle']) + this.api.compileWithTruffle(configFilePath).then((result) => { this.api.logToTerminal({ type: 'info', value: result }) }).catch((error) => { this.api.logToTerminal({ type: 'error', value: error }) }) + } } } // TODO readd saving current file diff --git a/libs/remixd/src/services/truffleClient.ts b/libs/remixd/src/services/truffleClient.ts index 0b6a223309..45aa381d76 100644 --- a/libs/remixd/src/services/truffleClient.ts +++ b/libs/remixd/src/services/truffleClient.ts @@ -20,13 +20,13 @@ export class TruffleClient extends PluginClient { this.currentSharedFolder = currentSharedFolder } - compile () { + compile (configPath: string) { return new Promise((resolve, reject) => { if (this.readOnly) { const errMsg = '[Truffle Compilation]: Cannot compile in read-only mode' return reject(new Error(errMsg)) } - const cmd = `truffle compile` + const cmd = `truffle compile --config ${configPath}` const options = { cwd: this.currentSharedFolder, shell: true } const child = spawn(cmd, options) let result = ''