Add remapping when parse file to support embark-solc

This commit is contained in:
bakaoh 2018-10-02 17:32:54 +07:00 committed by Pascal Precht
parent bdd5852b6c
commit 109152730f
No known key found for this signature in database
GPG Key ID: 0EE28D8D6FD85D7D
3 changed files with 22 additions and 1 deletions

View File

@ -438,7 +438,7 @@ Config.prototype.loadPluginContractFiles = function() {
contractsPlugins.forEach(function(plugin) { contractsPlugins.forEach(function(plugin) {
plugin.contractsFiles.forEach(function(file) { plugin.contractsFiles.forEach(function(file) {
var filename = file.replace('./',''); var filename = file.replace('./','');
self.contractsFiles.push(new File({filename: filename, type: File.types.custom, path: filename, resolver: function(callback) { self.contractsFiles.push(new File({filename: filename, pluginPath: plugin.pluginPath, type: File.types.custom, path: filename, resolver: function(callback) {
callback(plugin.loadPluginFile(file)); callback(plugin.loadPluginFile(file));
}})); }}));
}); });

View File

@ -12,7 +12,9 @@ class File {
this.path = options.path; this.path = options.path;
this.basedir = options.basedir; this.basedir = options.basedir;
this.resolver = options.resolver; this.resolver = options.resolver;
this.pluginPath = options.pluginPath ? options.pluginPath : '';
this.downloadedImports = false; this.downloadedImports = false;
this.importRemappings = []; // mapping downloaded imports to local file
} }
parseFileForImport(content, isHttpContract, callback) { parseFileForImport(content, isHttpContract, callback) {
@ -36,6 +38,21 @@ class File {
fileRelativePath: path.join(path.dirname(self.filename), matches[1]), fileRelativePath: path.join(path.dirname(self.filename), matches[1]),
url: `${pathWithoutFile}/${matches[1]}` url: `${pathWithoutFile}/${matches[1]}`
}; };
var target = matches[1];
if (httpFileObj) {
target = httpFileObj.filePath;
} else if (fs.existsSync(path.join(path.dirname(self.filename), matches[1]))) {
target = path.join(path.dirname(self.filename), matches[1]);
} else if (fs.existsSync(path.join("node_modules", matches[1]))) {
target = path.join("node_modules", matches[1]);
}
self.importRemappings.push({
prefix: matches[1],
target: fs.dappPath(target)
});
if (httpFileObj) { if (httpFileObj) {
// Replace http import by filePath import in content // Replace http import by filePath import in content
newContent = newContent.replace(matches[1], httpFileObj.filePath); newContent = newContent.replace(matches[1], httpFileObj.filePath);

View File

@ -77,6 +77,8 @@ describe('embark.Config', function () {
"filename": ".embark/contracts/embark-framework/embark/master/test_app/app/contracts/simple_storage.sol", "filename": ".embark/contracts/embark-framework/embark/master/test_app/app/contracts/simple_storage.sol",
"type": "http", "type": "http",
"path": "https://raw.githubusercontent.com/embark-framework/embark/master/test_app/app/contracts/simple_storage.sol", "path": "https://raw.githubusercontent.com/embark-framework/embark/master/test_app/app/contracts/simple_storage.sol",
"pluginPath": '',
"importRemappings": [],
"basedir": "", "basedir": "",
"resolver": undefined, "resolver": undefined,
"downloadedImports": false "downloadedImports": false
@ -85,6 +87,8 @@ describe('embark.Config', function () {
"filename": ".embark/contracts/status-im/contracts/master/contracts/identity/ERC725.sol", "filename": ".embark/contracts/status-im/contracts/master/contracts/identity/ERC725.sol",
"type": "http", "type": "http",
"path": "https://raw.githubusercontent.com/status-im/contracts/master/contracts/identity/ERC725.sol", "path": "https://raw.githubusercontent.com/status-im/contracts/master/contracts/identity/ERC725.sol",
"pluginPath": '',
"importRemappings": [],
"basedir": "", "basedir": "",
"resolver": undefined, "resolver": undefined,
"downloadedImports": false "downloadedImports": false