Merge pull request #1328 from ethereum/truffleImport

Import module from `installed_contracts` and `node_modules` folder
pull/3094/head
yann300 7 years ago committed by GitHub
commit a4bb894ef5
  1. 34
      src/app.js
  2. 29
      src/app/compiler/compiler-imports.js
  3. 3
      src/app/files/fileManager.js

@ -36,7 +36,7 @@ var modalDialogCustom = require('./app/ui/modal-dialog-custom')
var TxLogger = require('./app/execution/txLogger') var TxLogger = require('./app/execution/txLogger')
var Txlistener = remixLib.execution.txListener var Txlistener = remixLib.execution.txListener
var EventsDecoder = remixLib.execution.EventsDecoder var EventsDecoder = remixLib.execution.EventsDecoder
var handleImports = require('./app/compiler/compiler-imports') var CompilerImport = require('./app/compiler/compiler-imports')
var FileManager = require('./app/files/fileManager') var FileManager = require('./app/files/fileManager')
var ContextualListener = require('./app/editor/contextualListener') var ContextualListener = require('./app/editor/contextualListener')
var ContextView = require('./app/editor/contextView') var ContextView = require('./app/editor/contextView')
@ -135,6 +135,7 @@ class App {
self._api.filesProviders['ipfs'] = new BasicReadOnlyExplorer('ipfs') self._api.filesProviders['ipfs'] = new BasicReadOnlyExplorer('ipfs')
self._view = {} self._view = {}
self._components = {} self._components = {}
self._components.compilerImport = new CompilerImport()
self.data = { self.data = {
_layout: { _layout: {
right: { right: {
@ -235,7 +236,7 @@ Please make a backup of your contracts and start using http://remix.ethereum.org
} }
function importExternal (url, cb) { function importExternal (url, cb) {
handleImports.import(url, self._components.compilerImport.import(url,
(loadingMsg) => { (loadingMsg) => {
toolTip(loadingMsg) toolTip(loadingMsg)
}, },
@ -249,22 +250,34 @@ Please make a backup of your contracts and start using http://remix.ethereum.org
}) })
} }
// ----------------- Compiler ----------------- function importFileCb (url, filecb) {
var compiler = new Compiler((url, cb) => {
var provider = fileManager.fileProviderOf(url) var provider = fileManager.fileProviderOf(url)
if (provider) { if (provider) {
provider.exists(url, (error, exist) => { provider.exists(url, (error, exist) => {
if (error) return cb(error) if (error) return filecb(error)
if (exist) { if (exist) {
return provider.get(url, cb) return provider.get(url, filecb)
} else { } else {
importExternal(url, cb) importExternal(url, filecb)
} }
}) })
} else if (self._components.compilerImport.isRelativeImport(url)) {
// try to resolve localhost modules (aka truffle imports)
var splitted = /([^/]+)\/(.*)$/g.exec(url)
async.tryEach([
(cb) => { importFileCb('localhost/installed_contracts/' + url, cb) },
(cb) => { if (!splitted) { cb('url not parseable' + url) } else { importFileCb('localhost/installed_contracts/' + splitted[1] + '/contracts/' + splitted[2], cb) } },
(cb) => { importFileCb('localhost/node_modules/' + url, cb) },
(cb) => { if (!splitted) { cb('url not parseable' + url) } else { importFileCb('localhost/node_modules/' + splitted[1] + '/contracts/' + splitted[2], cb) } }],
(error, result) => { filecb(error, result) }
)
} else { } else {
importExternal(url, cb) importExternal(url, filecb)
} }
}) }
// ----------------- Compiler -----------------
var compiler = new Compiler(importFileCb)
var offsetToLineColumnConverter = new OffsetToLineColumnConverter(compiler.event) var offsetToLineColumnConverter = new OffsetToLineColumnConverter(compiler.event)
// ----------------- UniversalDApp ----------------- // ----------------- UniversalDApp -----------------
@ -553,7 +566,8 @@ Please make a backup of your contracts and start using http://remix.ethereum.org
var fileManager = new FileManager({ var fileManager = new FileManager({
config: config, config: config,
editor: editor, editor: editor,
filesProviders: filesProviders filesProviders: filesProviders,
compilerImport: self._components.compilerImport
}) })
// Add files received from remote instance (i.e. another remix-ide) // Add files received from remote instance (i.e. another remix-ide)

@ -3,9 +3,12 @@ var base64 = require('js-base64').Base64
var swarmgw = require('swarmgw') var swarmgw = require('swarmgw')
var request = require('request') var request = require('request')
module.exports = { module.exports = class CompilerImports {
previouslyHandled: {}, // cache import so we don't make the request at each compilation. constructor () {
handleGithubCall: function (root, path, cb) { this.previouslyHandled = {} // cache import so we don't make the request at each compilation.
}
handleGithubCal (root, path, cb) {
return request.get( return request.get(
{ {
url: 'https://api.github.com/repos/' + root + '/contents/' + path, url: 'https://api.github.com/repos/' + root + '/contents/' + path,
@ -26,15 +29,15 @@ module.exports = {
cb('Content not received') cb('Content not received')
} }
}) })
}, }
handleSwarmImport: function (url, cleanUrl, cb) { handleSwarmImport (url, cleanUrl, cb) {
swarmgw.get(url, function (err, content) { swarmgw.get(url, function (err, content) {
cb(err, content, cleanUrl) cb(err, content, cleanUrl)
}) })
}, }
handleIPFS: function (url, cb) { handleIPFS (url, cb) {
// replace ipfs:// with /ipfs/ // replace ipfs:// with /ipfs/
url = url.replace(/^ipfs:\/\/?/, 'ipfs/') url = url.replace(/^ipfs:\/\/?/, 'ipfs/')
@ -51,17 +54,21 @@ module.exports = {
} }
cb(null, data, url) cb(null, data, url)
}) })
}, }
handlers: function () { handlers () {
return [ return [
{ type: 'github', match: /^(https?:\/\/)?(www.)?github.com\/([^/]*\/[^/]*)\/(.*)/, handler: (match, cb) => { this.handleGithubCall(match[3], match[4], cb) } }, { type: 'github', match: /^(https?:\/\/)?(www.)?github.com\/([^/]*\/[^/]*)\/(.*)/, handler: (match, cb) => { this.handleGithubCall(match[3], match[4], cb) } },
{ type: 'swarm', match: /^(bzz[ri]?:\/\/?(.*))$/, handler: (match, cb) => { this.handleSwarmImport(match[1], match[2], cb) } }, { type: 'swarm', match: /^(bzz[ri]?:\/\/?(.*))$/, handler: (match, cb) => { this.handleSwarmImport(match[1], match[2], cb) } },
{ type: 'ipfs', match: /^(ipfs:\/\/?.+)/, handler: (match, cb) => { this.handleIPFS(match[1], cb) } } { type: 'ipfs', match: /^(ipfs:\/\/?.+)/, handler: (match, cb) => { this.handleIPFS(match[1], cb) } }
] ]
}, }
isRelativeImport (url) {
return /^([A-Za-z0-9]+)/.exec(url)
}
import: function (url, loadingCb, cb) { import (url, loadingCb, cb) {
var self = this var self = this
var imported = this.previouslyHandled[url] var imported = this.previouslyHandled[url]
if (imported) { if (imported) {

@ -4,7 +4,6 @@ var $ = require('jquery')
var remixLib = require('remix-lib') var remixLib = require('remix-lib')
var yo = require('yo-yo') var yo = require('yo-yo')
var EventManager = remixLib.EventManager var EventManager = remixLib.EventManager
var imports = require('../compiler/compiler-imports')
/* /*
attach to files event (removed renamed) attach to files event (removed renamed)
@ -161,7 +160,7 @@ class FileManager {
if (provider !== null && this.opt.filesProviders[provider[0]]) { if (provider !== null && this.opt.filesProviders[provider[0]]) {
return this.opt.filesProviders[provider[0]] return this.opt.filesProviders[provider[0]]
} else { } else {
for (var handler of imports.handlers()) { for (var handler of this.opt.compilerImport.handlers()) {
if (handler.match.exec(file)) { if (handler.match.exec(file)) {
return this.opt.filesProviders[handler.type] return this.opt.filesProviders[handler.type]
} }

Loading…
Cancel
Save