diff --git a/apps/remix-ide/src/app.js b/apps/remix-ide/src/app.js index 35c31e862a..6927f20c4b 100644 --- a/apps/remix-ide/src/app.js +++ b/apps/remix-ide/src/app.js @@ -5,6 +5,7 @@ import { RemixAppManager } from './remixAppManager' import { ThemeModule } from './app/tabs/theme-module' import { NetworkModule } from './app/tabs/network-module' import { Web3ProviderModule } from './app/tabs/web3-provider' +import { IntelligentScriptExecutor } from './app/tabs/intelligent-script-executor' import { SidePanel } from './app/components/side-panel' import { HiddenPanel } from './app/components/hidden-panel' import { VerticalIcons } from './app/components/vertical-icons' @@ -179,7 +180,8 @@ class AppComponent { api: offsetToLineColumnConverter, name: 'offsettolinecolumnconverter' }) - + // ----------------- run script after each compilation results ----------- + const intelligentScriptExecutor = new IntelligentScriptExecutor() // -------------------Terminal---------------------------------------- makeUdapp(blockchain, compilersArtefacts, domEl => terminal.logHtml(domEl)) const terminal = new Terminal( @@ -222,6 +224,7 @@ class AppComponent { contextualListener, terminal, web3Provider, + intelligentScriptExecutor, fetchAndCompile, dGitProvider, storagePlugin, diff --git a/apps/remix-ide/src/app/tabs/intelligent-script-executor.js b/apps/remix-ide/src/app/tabs/intelligent-script-executor.js new file mode 100644 index 0000000000..074f12d947 --- /dev/null +++ b/apps/remix-ide/src/app/tabs/intelligent-script-executor.js @@ -0,0 +1,40 @@ +import { Plugin } from '@remixproject/engine' +import * as packageJson from '../../../../../package.json' + +export const profile = { + name: 'IntelligentScriptExecutor', + displayName: 'Intelligent Script Executor', + description: 'after each compilation, run the script defined in Natspec.', + methods: [], + version: packageJson.version, + kind: 'none' +} + +export class IntelligentScriptExecutor extends Plugin { + constructor () { + super(profile) + } + + onActivation () { + this.on('solidity', 'compilationFinished', async (file, source, languageVersion, data, input, version) => { + const currentFile = await this.call('fileManager', 'file') + if (data && data.sources[currentFile] && + data.sources[currentFile].ast && data.sources[currentFile].ast.nodes && data.sources[currentFile].ast.nodes.length) { + const nodes = data.sources[currentFile].ast.nodes + for (let node of nodes) { + if (node.documentation && node.documentation.text && node.documentation.text.startsWith('@custom:dev-run-script')) { + const text = node.documentation.text.replace('@custom:dev-run-script', '').trim() + await this.call('terminal', 'log', `running ${text} ...`) + const content = await this.call('fileManager', 'readFile', text) + await this.call('udapp', 'clearAllInstances') + await this.call('scriptRunner', 'execute', content) + } + } + } + }) + } + + onDeactivation () { + this.off('solidity', 'compilationFinished') + } +}