Merge pull request #359 from embark-framework/node_module_import

better mappings
This commit is contained in:
Iuri Matias 2018-04-12 20:41:52 -04:00 committed by GitHub
commit b56f20db8a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 91 additions and 4 deletions

View File

@ -47,6 +47,7 @@ Config.prototype.loadConfigFiles = function(options) {
this.loadPipelineConfigFile();
this.loadContractsConfigFile();
this.loadExternalContractsFiles();
this.loadWebServerConfigFile();
this.loadChainTrackerFile();
this.loadPluginContractFiles();
@ -60,6 +61,7 @@ Config.prototype.reloadConfig = function() {
this.loadContractsConfigFile();
this.loadPipelineConfigFile();
this.loadContractsConfigFile();
this.loadExternalContractsFiles();
this.loadChainTrackerFile();
};
@ -140,6 +142,23 @@ Config.prototype.loadContractsConfigFile = function() {
this.contractsConfig = this._mergeConfig(configFilePath, configObject, this.env);
};
Config.prototype.loadExternalContractsFiles = function() {
let contracts = this.contractsConfig.contracts;
for (let contractName in contracts) {
let contract = contracts[contractName];
if (!contract.file) {
continue;
}
if (fs.existsSync(contract.file)) {
this.contractsFiles.push(new File({filename: contract.file, type: "dapp_file", basedir: '', path: contract.file}));
} else if (fs.existsSync(path.join('./node_modules/', contract.file))) {
this.contractsFiles.push(new File({filename: path.join('./node_modules/', contract.file), type: "dapp_file", basedir: '', path: path.join('./node_modules/', contract.file)}));
} else {
this.logger.error("contract file not found: " + contract.file);
}
}
};
Config.prototype.loadStorageConfigFile = function() {
var versions = utils.recursiveMerge({"ipfs-api": "17.2.4"}, this.embarkConfig.versions || {});

View File

@ -22,7 +22,8 @@ class Solidity {
let filename = file.filename;
for (let directory of self.contractDirectories) {
filename = filename.replace(directory, '');
let match = new RegExp("^" + directory);
filename = filename.replace(match, '');
}
file.content(function(fileContent) {

View File

@ -1,5 +1,18 @@
let solc;
let fs = require('fs-extra');
let path = require('path');
function findImports(filename) {
if (fs.existsSync(filename)) {
return {contents: fs.readFileSync(filename).toString()};
}
if (fs.existsSync(path.join('./node_modules/', filename))) {
return {contents: fs.readFileSync(path.join('./node_modules/', filename)).toString()};
}
return {error: 'File not found'};
}
process.on('message', function (msg) {
if (msg.action === 'loadCompiler') {
solc = require(msg.solcLocation);
@ -8,7 +21,7 @@ process.on('message', function (msg) {
if (msg.action === 'compile') {
// TODO: only available in 0.4.11; need to make versions warn about this
let output = solc.compileStandardWrapper(JSON.stringify(msg.jsonObj));
let output = solc.compileStandardWrapper(JSON.stringify(msg.jsonObj), findImports);
process.send({result: "compilation", output: output});
}
});

View File

@ -0,0 +1,21 @@
pragma solidity ^0.4.17;
contract SimpleStorageTest2 {
uint public storedData;
function() public payable { }
function SimpleStorage(uint initialValue) public {
storedData = initialValue;
}
function set(uint x) public {
storedData = x;
}
function get() public view returns (uint retVal) {
return storedData;
}
}

View File

@ -62,6 +62,15 @@
["$MyToken2", "$SimpleStorage"],
100
]
},
"ERC20": {
"file": "zeppelin-solidity/contracts/token/ERC20/ERC20.sol"
},
"SimpleStorageTest": {
"file": "./some_folder/test_contract.sol",
"args": [
1000
]
}
},
"afterDeploy": [

View File

@ -16,7 +16,7 @@
"config": "config/",
"versions": {
"web3.js": "1.0.0-beta.27",
"solc": "0.4.17",
"solc": "0.4.18",
"ipfs-api": "17.2.6"
},
"plugins": {

View File

@ -18,6 +18,7 @@
"jquery": "^1.11.3",
"react": "^16.0.0",
"react-bootstrap": "^0.32.0",
"react-dom": "^16.2.0"
"react-dom": "^16.2.0",
"zeppelin-solidity": "^1.8.0"
}
}

View File

@ -0,0 +1,23 @@
pragma solidity ^0.4.17;
import "another_folder/another_test.sol";
import "zeppelin-solidity/contracts/ownership/Ownable.sol";
contract SimpleStorageTest is Ownable {
uint public storedData;
function() public payable { }
function SimpleStorage(uint initialValue) public {
storedData = initialValue;
}
function set(uint x) public {
storedData = x;
}
function get() public view returns (uint retVal) {
return storedData;
}
}