Expose "compile" method to remix-plugin
pull/3094/head
yann300 6 years ago committed by GitHub
commit 1dca17a8b3
  1. 2
      package.json
  2. 20
      src/app/tabs/compile-tab.js
  3. 44
      src/app/tabs/compileTab/compileTab.js
  4. 2
      test-browser/helpers/contracts.js

@ -68,7 +68,7 @@
},
"dependencies": {
"http-server": "^0.11.1",
"remix-plugin": "0.0.2-alpha.9",
"remix-plugin": "0.0.2-alpha.10",
"remixd": "0.1.8-alpha.6"
},
"repository": {

@ -132,7 +132,7 @@ class CompileTab extends CompilerApi {
// Update contract Selection
let contractMap = {}
if (success) this.compiler.visitContracts((contract) => { contractMap[contract.name] = contract })
let contractSelection = this.contractSelection(Object.keys(contractMap) || [], source.target)
let contractSelection = this.contractSelection(contractMap)
yo.update(this._view.contractSelection, contractSelection)
if (data['error']) {
@ -173,6 +173,11 @@ class CompileTab extends CompilerApi {
return this.compileTabLogic.compiler.lastCompilationResult
}
// This function is used by remix-plugin
compile (fileName) {
return this.compileTabLogic.compileFile(fileName)
}
/*********
* SUB-COMPONENTS
*/
@ -181,13 +186,22 @@ class CompileTab extends CompilerApi {
* Section to select the compiled contract
* @param {string[]} contractList Names of the compiled contracts
*/
contractSelection (contractList = [], sourceFile) {
contractSelection (contractMap) {
// Return the file name of a path: ex "browser/ballot.sol" -> "ballot.sol"
const getFileName = (path) => {
const part = path.split('/')
return part[part.length - 1]
}
const contractList = contractMap ? Object.keys(contractMap).map((key) => ({
name: key,
file: getFileName(contractMap[key].file)
})) : []
let selectEl = yo`
<select
onchange="${e => this.selectContract(e.target.value)}"
id="compiledContracts" class="custom-select"
>
${contractList.map((name) => yo`<option value="${name}">${name}</option>`)}
${contractList.map(({name, file}) => yo`<option value="${name}">${name} (${file})</option>`)}
</select>
`
let result = contractList.length

@ -33,28 +33,38 @@ class CompileTab {
this.compiler.setOptimize(this.optimize)
}
runCompiler () {
this.fileManager.saveCurrentFile()
this.editor.clearAnnotations()
var currentFile = this.config.get('currentFile')
if (!currentFile) return
if (!/\.sol/.exec(currentFile)) return
/**
* Compile a specific file of the file manager
* @param {string} target the path to the file to compile
*/
compileFile (target) {
if (!target) throw new Error('No target provided for compiliation')
if (!/\.sol/.exec(target)) throw new Error(`${target} is not a solidity file. It cannot be compiled with solidity compiler`)
// only compile *.sol file.
var target = currentFile
var sources = {}
var provider = this.fileManager.fileProviderOf(currentFile)
if (!provider) return console.log('cannot compile ' + currentFile + '. Does not belong to any explorer')
provider.get(target, (error, content) => {
if (error) return console.log(error)
sources[target] = { content }
this.event.emit('startingCompilation')
setTimeout(() => {
const provider = this.fileManager.fileProviderOf(target)
if (!provider) throw new Error(`cannot compile ${target}. Does not belong to any explorer`)
return new Promise((resolve, reject) => {
provider.get(target, (error, content) => {
if (error) return reject(error)
const sources = { [target]: { content } }
this.event.emit('startingCompilation')
// setTimeout fix the animation on chrome... (animation triggered by 'staringCompilation')
this.compiler.compile(sources, target)
}, 100)
setTimeout(() => this.compiler.compile(sources, target), 100)
})
})
}
runCompiler () {
try {
this.fileManager.saveCurrentFile()
this.editor.clearAnnotations()
var currentFile = this.config.get('currentFile')
return this.compileFile(currentFile)
} catch (err) {
console.error(err)
}
}
importExternal (url, cb) {
this.compilerImport.import(url,

@ -42,7 +42,7 @@ function getCompiledContracts (browser, compiled, callback) {
} else {
var ret = []
for (var c = 0; c < contracts.length; c++) {
ret.push(contracts[c].innerHTML)
ret.push(contracts[c].value)
}
return ret
}

Loading…
Cancel
Save