Merge pull request #3285 from ethereum/foundry

fix remixd
pull/3301/head
bunsenstraat 2 years ago committed by GitHub
commit 25e0d6b8be
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      .circleci/config.yml
  2. 74
      libs/remixd/src/services/foundryClient.ts
  3. 58
      libs/remixd/src/services/hardhatClient.ts
  4. 72
      libs/remixd/src/services/truffleClient.ts

@ -316,4 +316,4 @@ workflows:
branches:
only: remix_beta
# VS Code Extension Version: 1.5.0
# VS Code Extension Version: 1.5.1

@ -14,10 +14,17 @@ export class FoundryClient extends PluginClient {
warnlog: boolean
buildPath: string
cachePath: string
logTimeout: NodeJS.Timeout
processingTimeout: NodeJS.Timeout
constructor(private readOnly = false) {
super()
this.methods = ['compile', 'sync']
this.onActivation = () => {
console.log('Foundry plugin activated')
this.call('terminal', 'log', { type: 'log', value: 'Foundry plugin activated' })
this.startListening()
}
}
setWebSocket(websocket: WS): void {
@ -32,10 +39,33 @@ export class FoundryClient extends PluginClient {
this.currentSharedFolder = currentSharedFolder
this.buildPath = utils.absolutePath('out', this.currentSharedFolder)
this.cachePath = utils.absolutePath('cache', this.currentSharedFolder)
}
startListening() {
if (fs.existsSync(this.buildPath) && fs.existsSync(this.cachePath)) {
this.listenOnFoundryCompilation()
} else {
this.listenOnFoundryFolder()
}
}
listenOnFoundryFolder() {
console.log('Foundry out folder doesn\'t exist... waiting for the compilation.')
try {
if(this.watcher) this.watcher.close()
this.watcher = chokidar.watch(this.currentSharedFolder, { depth: 1, ignorePermissionErrors: true, ignoreInitial: true })
// watch for new folders
this.watcher.on('addDir', () => {
if (fs.existsSync(this.buildPath) && fs.existsSync(this.cachePath)) {
this.listenOnFoundryCompilation()
}
})
} catch (e) {
console.log(e)
}
}
compile (configPath: string) {
compile() {
return new Promise((resolve, reject) => {
if (this.readOnly) {
const errMsg = '[Foundry Compilation]: Cannot compile in read-only mode'
@ -62,10 +92,20 @@ export class FoundryClient extends PluginClient {
})
}
checkPath() {
if (!fs.existsSync(this.buildPath) || !fs.existsSync(this.cachePath)) {
this.listenOnFoundryFolder()
return false
}
if (!fs.existsSync(join(this.cachePath, 'solidity-files-cache.json'))) return false
return true
}
private async processArtifact() {
if (!this.checkPath()) return
const folderFiles = await fs.readdir(this.buildPath) // "out" folder
try {
const cache = JSON.parse(await fs.readFile(join(this.cachePath, 'solidity-files-cache.json'), { encoding: 'utf-8' }))
// name of folders are file names
for (const file of folderFiles) {
const path = join(this.buildPath, file) // out/Counter.sol/
@ -81,21 +121,33 @@ export class FoundryClient extends PluginClient {
await this.readContract(path, compilationResult, cache)
this.emit('compilationFinished', compilationResult.compilationTarget, { sources: compilationResult.input }, 'soljson', compilationResult.output, compilationResult.solcVersion)
}
if (!this.warnlog) {
clearTimeout(this.logTimeout)
this.logTimeout = setTimeout(() => {
// @ts-ignore
this.call('terminal', 'log', { type: 'log', value: 'receiving compilation result from Foundry' })
this.warnlog = true
this.call('terminal', 'log', { type: 'log', value: `receiving compilation result from Foundry` })
console.log('Syncing compilation result from Foundry')
}, 1000)
} catch (e) {
console.log(e)
}
}
async triggerProcessArtifact() {
// prevent multiple calls
clearTimeout(this.processingTimeout)
this.processingTimeout = setTimeout(async () => await this.processArtifact(), 1000)
}
listenOnFoundryCompilation() {
try {
if(this.watcher) this.watcher.close()
this.watcher = chokidar.watch(this.cachePath, { depth: 0, ignorePermissionErrors: true, ignoreInitial: true })
this.watcher.on('change', async (f: string) => this.processArtifact())
this.watcher.on('add', async (f: string) => this.processArtifact())
this.watcher.on('change', async () => await this.triggerProcessArtifact())
this.watcher.on('add', async () => await this.triggerProcessArtifact())
// process the artifact on activation
setTimeout(() => this.processArtifact(), 1000)
this.triggerProcessArtifact()
} catch (e) {
console.log(e)
}
@ -164,9 +216,7 @@ export class FoundryClient extends PluginClient {
}
async sync() {
console.log('syncing from Foundry')
console.log('syncing Foundry with Remix...')
this.processArtifact()
// @ts-ignore
this.call('terminal', 'log', { type: 'log', value: 'synced with Foundry'})
}
}

@ -13,10 +13,17 @@ export class HardhatClient extends PluginClient {
watcher: chokidar.FSWatcher
warnLog: boolean
buildPath: string
logTimeout: NodeJS.Timeout
processingTimeout: NodeJS.Timeout
constructor(private readOnly = false) {
super()
this.methods = ['compile', 'sync']
this.onActivation = () => {
console.log('Hardhat plugin activated')
this.call('terminal', 'log', { type: 'log', value: 'Hardhat plugin activated' })
this.startListening()
}
}
setWebSocket(websocket: WS): void {
@ -30,14 +37,15 @@ export class HardhatClient extends PluginClient {
sharedFolder(currentSharedFolder: string): void {
this.currentSharedFolder = currentSharedFolder
this.buildPath = utils.absolutePath('artifacts/contracts', this.currentSharedFolder)
}
startListening() {
if (fs.existsSync(this.buildPath)) {
this.listenOnHardhatCompilation()
} else {
console.log('Hardhat artifacts folder doesn\'t exist... waiting for the first compilation.')
console.log('If you are using Hardhat, run `npx hardhat compile` or run the compilation with `Enable Hardhat Compilation` checked from the Remix IDE.')
this.listenOnHardHatFolder()
}
}
compile(configPath: string) {
@ -67,7 +75,16 @@ export class HardhatClient extends PluginClient {
})
}
checkPath() {
if (!fs.existsSync(this.buildPath)) {
this.listenOnHardHatFolder()
return false
}
return true
}
private async processArtifact() {
if (!this.checkPath()) return
// resolving the files
const folderFiles = await fs.readdir(this.buildPath)
const targetsSynced = []
@ -111,23 +128,32 @@ export class HardhatClient extends PluginClient {
}
}
}
if (!this.warnLog) {
clearTimeout(this.logTimeout)
this.logTimeout = setTimeout(() => {
this.call('terminal', 'log', { value: 'receiving compilation result from Hardhat', type: 'log' })
this.warnLog = true
}
if (targetsSynced.length) {
console.log(`Processing artifacts for files: ${[...new Set(targetsSynced)].join(', ')}`)
// @ts-ignore
this.call('terminal', 'log', { type: 'log', value: `synced with Hardhat: ${[...new Set(targetsSynced)].join(', ')}` })
} else {
console.log('No artifacts to process')
// @ts-ignore
this.call('terminal', 'log', { type: 'log', value: 'No artifacts from Hardhat to process' })
}
}, 1000)
}
listenOnHardHatFolder() {
console.log('Hardhat artifacts folder doesn\'t exist... waiting for the compilation.')
try {
this.watcher = chokidar.watch(this.currentSharedFolder, { depth: 1, ignorePermissionErrors: true, ignoreInitial: true })
if(this.watcher) this.watcher.close()
this.watcher = chokidar.watch(this.currentSharedFolder, { depth: 2, ignorePermissionErrors: true, ignoreInitial: true })
// watch for new folders
this.watcher.on('addDir', (path) => {
if (path.endsWith('artifacts/contracts')) {
this.buildPath = path
this.watcher.on('addDir', () => {
if (fs.existsSync(this.buildPath)) {
this.listenOnHardhatCompilation()
}
})
@ -136,15 +162,21 @@ export class HardhatClient extends PluginClient {
}
}
async triggerProcessArtifact() {
// prevent multiple calls
clearTimeout(this.processingTimeout)
this.processingTimeout = setTimeout(async () => await this.processArtifact(), 1000)
}
listenOnHardhatCompilation() {
try {
console.log('listening on Hardhat compilation...')
if(this.watcher) this.watcher.close()
this.watcher = chokidar.watch(this.buildPath, { depth: 1, ignorePermissionErrors: true, ignoreInitial: true })
this.watcher.on('change', () => this.processArtifact())
this.watcher.on('add', () => this.processArtifact())
this.watcher.on('change', async () => await this.triggerProcessArtifact())
this.watcher.on('add', async () => await this.triggerProcessArtifact())
// process the artifact on activation
setTimeout(() => this.processArtifact(), 1000)
this.processArtifact()
} catch (e) {
console.log(e)
}

@ -13,10 +13,17 @@ export class TruffleClient extends PluginClient {
watcher: chokidar.FSWatcher
warnLog: boolean
buildPath: string
logTimeout: NodeJS.Timeout
processingTimeout: NodeJS.Timeout
constructor(private readOnly = false) {
super()
this.methods = ['compile', 'sync']
this.onActivation = () => {
console.log('Truffle plugin activated')
this.call('terminal', 'log', { type: 'log', value: 'Truffle plugin activated' })
this.startListening()
}
}
setWebSocket(websocket: WS): void {
@ -30,8 +37,33 @@ export class TruffleClient extends PluginClient {
sharedFolder(currentSharedFolder: string): void {
this.currentSharedFolder = currentSharedFolder
this.buildPath = utils.absolutePath('build/contracts', this.currentSharedFolder)
}
startListening() {
if (fs.existsSync(this.buildPath)) {
this.listenOnTruffleCompilation()
}
else {
this.listenOnTruffleFolder()
}
}
listenOnTruffleFolder() {
console.log('Truffle build folder doesn\'t exist... waiting for the compilation.')
try {
if (this.watcher) this.watcher.close()
this.watcher = chokidar.watch(this.currentSharedFolder, { depth: 2, ignorePermissionErrors: true, ignoreInitial: true })
// watch for new folders
this.watcher.on('addDir', () => {
if (fs.existsSync(this.buildPath)) {
this.listenOnTruffleCompilation()
}
})
} catch (e) {
console.log(e)
}
}
compile(configPath: string) {
return new Promise((resolve, reject) => {
@ -60,8 +92,19 @@ export class TruffleClient extends PluginClient {
})
}
checkPath() {
if (!fs.existsSync(this.buildPath)) {
this.listenOnTruffleFolder()
return false
}
return true
}
private async processArtifact() {
if (!this.checkPath()) return
const folderFiles = await fs.readdir(this.buildPath)
const filesFound = folderFiles.filter(file => file.endsWith('.json'))
// name of folders are file names
for (const file of folderFiles) {
if (file.endsWith('.json')) {
@ -79,21 +122,33 @@ export class TruffleClient extends PluginClient {
this.emit('compilationFinished', compilationResult.compilationTarget, { sources: compilationResult.input }, 'soljson', compilationResult.output, compilationResult.solcVersion)
}
}
if (!this.warnLog) {
clearTimeout(this.logTimeout)
this.logTimeout = setTimeout(() => {
if (filesFound.length === 0) {
// @ts-ignore
this.call('terminal', 'log', { type: 'log', value: 'receiving compilation result from Truffle' })
this.warnLog = true
this.call('terminal', 'log', { value: 'No contract found in the Truffle build folder', type: 'log' })
} else {
// @ts-ignore
this.call('terminal', 'log', { value: 'receiving compilation result from Truffle', type: 'log' })
console.log('Syncing compilation result from Truffle')
}
}, 1000)
}
async triggerProcessArtifact() {
// prevent multiple calls
clearTimeout(this.processingTimeout)
this.processingTimeout = setTimeout(async () => await this.processArtifact(), 1000)
}
listenOnTruffleCompilation() {
try {
if (this.watcher) this.watcher.close()
this.watcher = chokidar.watch(this.buildPath, { depth: 3, ignorePermissionErrors: true, ignoreInitial: true })
this.watcher.on('change', async (f: string) => this.processArtifact())
this.watcher.on('add', async (f: string) => this.processArtifact())
this.watcher.on('change', async () => await this.triggerProcessArtifact())
this.watcher.on('add', async () => await this.triggerProcessArtifact())
// process the artifact on activation
setTimeout(() => this.processArtifact(), 1000)
this.triggerProcessArtifact()
} catch (e) {
console.log(e)
}
@ -138,9 +193,6 @@ export class TruffleClient extends PluginClient {
}
async sync() {
console.log('syncing from Truffle')
this.processArtifact()
// @ts-ignore
this.call('terminal', 'log', { type: 'log', value: 'synced with Truffle' })
}
}

Loading…
Cancel
Save