fix foundry

pull/5370/head
filip mertens 2 years ago committed by Aniket
parent be8596086d
commit 5b177de770
  1. 48
      libs/remixd/src/services/foundryClient.ts

@ -15,12 +15,12 @@ export class FoundryClient extends PluginClient {
buildPath: string buildPath: string
cachePath: string cachePath: string
constructor (private readOnly = false) { constructor(private readOnly = false) {
super() super()
this.methods = ['compile', 'sync'] this.methods = ['compile', 'sync']
} }
setWebSocket (websocket: WS): void { setWebSocket(websocket: WS): void {
this.websocket = websocket this.websocket = websocket
this.websocket.addEventListener('close', () => { this.websocket.addEventListener('close', () => {
this.warnlog = false this.warnlog = false
@ -28,14 +28,35 @@ export class FoundryClient extends PluginClient {
}) })
} }
sharedFolder (currentSharedFolder: string): void { sharedFolder(currentSharedFolder: string): void {
this.currentSharedFolder = currentSharedFolder this.currentSharedFolder = currentSharedFolder
this.buildPath = utils.absolutePath('out', this.currentSharedFolder) this.buildPath = utils.absolutePath('out', this.currentSharedFolder)
this.cachePath = utils.absolutePath('cache', this.currentSharedFolder) this.cachePath = utils.absolutePath('cache', this.currentSharedFolder)
if (fs.existsSync(this.buildPath) && fs.existsSync(this.cachePath)) {
this.listenOnFoundryCompilation() this.listenOnFoundryCompilation()
} else {
console.log('Foundry out folder doesn\'t exist... waiting for the first compilation.')
this.listenOFoundryFolder()
}
}
listenOFoundryFolder() {
try {
this.watcher = chokidar.watch(this.currentSharedFolder, { depth: 1, ignorePermissionErrors: true, ignoreInitial: true })
// watch for new folders
this.watcher.on('addDir', (path) => {
if (fs.existsSync(this.buildPath) && fs.existsSync(this.cachePath)) {
this.buildPath = path
this.listenOnFoundryCompilation()
}
})
} catch (e) {
console.log(e)
}
} }
compile (configPath: string) { compile(configPath: string) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (this.readOnly) { if (this.readOnly) {
const errMsg = '[Foundry Compilation]: Cannot compile in read-only mode' const errMsg = '[Foundry Compilation]: Cannot compile in read-only mode'
@ -62,8 +83,10 @@ export class FoundryClient extends PluginClient {
}) })
} }
private async processArtifact () { private async processArtifact() {
const folderFiles = await fs.readdir(this.buildPath) // "out" folder const folderFiles = await fs.readdir(this.buildPath) // "out" folder
if (!fs.existsSync(join(this.cachePath, 'solidity-files-cache.json'))) return
try {
const cache = JSON.parse(await fs.readFile(join(this.cachePath, 'solidity-files-cache.json'), { encoding: 'utf-8' })) const cache = JSON.parse(await fs.readFile(join(this.cachePath, 'solidity-files-cache.json'), { encoding: 'utf-8' }))
// name of folders are file names // name of folders are file names
@ -79,16 +102,19 @@ export class FoundryClient extends PluginClient {
compilationTarget: null compilationTarget: null
} }
await this.readContract(path, compilationResult, cache) await this.readContract(path, compilationResult, cache)
this.emit('compilationFinished', compilationResult.compilationTarget, { sources: compilationResult.input } , 'soljson', compilationResult.output, compilationResult.solcVersion) this.emit('compilationFinished', compilationResult.compilationTarget, { sources: compilationResult.input }, 'soljson', compilationResult.output, compilationResult.solcVersion)
} }
if (!this.warnlog) { if (!this.warnlog) {
// @ts-ignore // @ts-ignore
this.call('terminal', 'log', { type: 'log', value: 'receiving compilation result from Foundry' }) this.call('terminal', 'log', { type: 'log', value: 'receiving compilation result from Foundry' })
this.warnlog = true this.warnlog = true
} }
} catch (e) {
console.log(e)
}
} }
listenOnFoundryCompilation () { listenOnFoundryCompilation() {
try { try {
this.watcher = chokidar.watch(this.cachePath, { depth: 0, ignorePermissionErrors: true, ignoreInitial: true }) this.watcher = chokidar.watch(this.cachePath, { depth: 0, ignorePermissionErrors: true, ignoreInitial: true })
@ -101,7 +127,7 @@ export class FoundryClient extends PluginClient {
} }
} }
async readContract (contractFolder, compilationResultPart, cache) { async readContract(contractFolder, compilationResultPart, cache) {
const files = await fs.readdir(contractFolder) const files = await fs.readdir(contractFolder)
for (const file of files) { for (const file of files) {
@ -111,7 +137,7 @@ export class FoundryClient extends PluginClient {
} }
} }
async feedContractArtifactFile (path, content, compilationResultPart, cache) { async feedContractArtifactFile(path, content, compilationResultPart, cache) {
const contentJSON = JSON.parse(content) const contentJSON = JSON.parse(content)
const contractName = basename(path).replace('.json', '') const contractName = basename(path).replace('.json', '')
@ -163,10 +189,10 @@ export class FoundryClient extends PluginClient {
} }
} }
async sync () { async sync() {
console.log('syncing from Foundry') console.log('syncing from Foundry')
this.processArtifact() this.processArtifact()
// @ts-ignore // @ts-ignore
this.call('terminal', 'log', { type: 'log', value: 'synced with Foundry'}) this.call('terminal', 'log', { type: 'log', value: 'synced with Foundry' })
} }
} }

Loading…
Cancel
Save