code and test importing the http contract

This commit is contained in:
Jonathan Rainville 2018-04-20 11:39:17 -04:00
parent 9bf06aebce
commit c1bed28c00
7 changed files with 77 additions and 17 deletions

View File

@ -20,16 +20,11 @@ class File {
callback = isHttpContract; callback = isHttpContract;
isHttpContract = false; isHttpContract = false;
} }
if (self.parsedImports) {
// We already parsed this file
return callback();
}
self.parsedImports = true;
if (self.filename.indexOf('.sol') < 0) { if (self.filename.indexOf('.sol') < 0) {
// Only supported in Solidity // Only supported in Solidity
return callback(); return callback(null, content);
} }
const regex = /import "([a-zA-Z0-9_\-.\\\/:]+)";/g; const regex = /import ["|']([a-zA-Z0-9_\-.\\\/:]+)";/g;
let matches; let matches;
const filesToDownload = []; const filesToDownload = [];
const pathWithoutFile = path.dirname(self.path); const pathWithoutFile = path.dirname(self.path);
@ -40,6 +35,9 @@ class File {
url: `${pathWithoutFile}/${matches[1]}` url: `${pathWithoutFile}/${matches[1]}`
}; };
if (httpFileObj) { if (httpFileObj) {
// Replace http import by filePath import in content
content = content.replace(matches[1], httpFileObj.filePath);
fileObj.fileRelativePath = httpFileObj.filePath; fileObj.fileRelativePath = httpFileObj.filePath;
fileObj.url = httpFileObj.url; fileObj.url = httpFileObj.url;
} else if (!isHttpContract) { } else if (!isHttpContract) {
@ -49,11 +47,18 @@ class File {
filesToDownload.push(fileObj); filesToDownload.push(fileObj);
} }
if (self.downloadedImports) {
// We already parsed this file
return callback(null, content);
}
self.downloadedImports = true;
async.each(filesToDownload, ((fileObj, eachCb) => { async.each(filesToDownload, ((fileObj, eachCb) => {
self.downloadFile(fileObj.fileRelativePath, fileObj.url, (_content) => { self.downloadFile(fileObj.fileRelativePath, fileObj.url, (_content) => {
eachCb(); eachCb();
}); });
}), callback); }), (err) => {
callback(err, content);
});
} }
downloadFile (filename, url, callback) { downloadFile (filename, url, callback) {
@ -103,8 +108,8 @@ class File {
content = fs.readFileSync(this.path).toString(); content = fs.readFileSync(this.path).toString();
} else if (this.type === File.types.custom) { } else if (this.type === File.types.custom) {
return this.resolver((theContent) => { return this.resolver((theContent) => {
this.parseFileForImport(content, () => { this.parseFileForImport(theContent, (err, newContent) => {
callback(theContent); callback(newContent);
}); });
}); });
} else if (this.type === File.types.http) { } else if (this.type === File.types.http) {
@ -119,8 +124,8 @@ class File {
} else { } else {
throw new Error("unknown file: " + this.filename); throw new Error("unknown file: " + this.filename);
} }
return this.parseFileForImport(content, () => { return this.parseFileForImport(content, (err, newContent) => {
callback(content); callback(newContent);
}); });
} }

View File

@ -3,8 +3,13 @@ let solc;
const fs = require('fs-extra'); const fs = require('fs-extra');
const path = require('path'); const path = require('path');
const constants = require('../../constants'); const constants = require('../../constants');
const Utils = require('../../utils/utils');
function findImports(filename) { function findImports(filename) {
if (filename.startsWith('http') || filename.startsWith('git')) {
const fileObj = Utils.getExternalContractUrl(filename);
filename = fileObj.filePath;
}
if (fs.existsSync(filename)) { if (fs.existsSync(filename)) {
return {contents: fs.readFileSync(filename).toString()}; return {contents: fs.readFileSync(filename).toString()};
} }

View File

@ -1,7 +1,7 @@
pragma solidity ^0.4.7; pragma solidity ^0.4.7;
import "https://github.com/embark-framework/embark/blob/develop/test_apps/contracts_app/contracts/contract_args.sol";
contract SimpleStorage { contract SimpleStorage {
uint public storedData; uint public storedData;
import "https://github.com/embark-framework/embark/blob/develop/test_apps/contracts_app/contracts/contract_args.sol";
function SimpleStorage(uint initialValue) { function SimpleStorage(uint initialValue) {
storedData = initialValue; storedData = initialValue;

View File

@ -1,7 +1,7 @@
pragma solidity ^0.4.7; pragma solidity ^0.4.7;
import "./ownable.sol";
contract SimpleStorage { contract SimpleStorage {
uint public storedData; uint public storedData;
import "./ownable.sol";
function SimpleStorage(uint initialValue) { function SimpleStorage(uint initialValue) {
storedData = initialValue; storedData = initialValue;

View File

@ -0,0 +1,34 @@
pragma solidity ^0.4.17;
import "https://github.com/embark-framework/embark/blob/develop/test_apps/contracts_app/contracts/ownable.sol";
contract SimpleStorageWithHttpImport is Ownable {
uint public storedData;
function() public payable { }
function SimpleStorageWithHttpImport(uint initialValue) public {
storedData = initialValue;
}
function set(uint x) public {
storedData = x;
for(uint i = 0; i < 1000; i++) {
storedData += i;
}
}
function set2(uint x, uint unusedGiveWarning) public onlyOwner {
storedData = x;
}
function get() public view returns (uint retVal) {
return storedData;
}
function getS() public pure returns (string d) {
return "hello";
}
}

View File

@ -74,6 +74,12 @@
}, },
"Identity": { "Identity": {
"file": "https://github.com/status-im/contracts/blob/master/contracts/identity/Identity.sol" "file": "https://github.com/status-im/contracts/blob/master/contracts/identity/Identity.sol"
},
"SimpleStorageWithHttpImport": {
"fromIndex": 0,
"args": [
100
]
} }
}, },
"afterDeploy": [ "afterDeploy": [

View File

@ -3,10 +3,9 @@ const fs = require('fs-extra');
const assert = require('assert'); const assert = require('assert');
describe('http contracts', () => { describe('http contracts', () => {
const contractPath = '.embark/contracts/status-im/contracts/master/contracts/identity/Identity.sol';
const contractImportPath = '.embark/contracts/status-im/contracts/master/contracts/identity/ERC725.sol';
it('should have downloaded the file in .embark/contracts', (done) => { it('should have downloaded the file in .embark/contracts', (done) => {
const contractPath = '.embark/contracts/status-im/contracts/master/contracts/identity/Identity.sol';
fs.access(contractPath, (err) => { fs.access(contractPath, (err) => {
if (err) { if (err) {
assert.fail(contractPath + ' was not downloaded'); assert.fail(contractPath + ' was not downloaded');
@ -16,9 +15,20 @@ describe('http contracts', () => {
}); });
it('should have downloaded the file import file too', (done) => { it('should have downloaded the file import file too', (done) => {
const contractImportPath = '.embark/contracts/status-im/contracts/master/contracts/identity/ERC725.sol';
fs.access(contractImportPath, (err) => { fs.access(contractImportPath, (err) => {
if (err) { if (err) {
assert.fail(contractPath + ' was not downloaded'); assert.fail(contractImportPath + ' was not downloaded');
}
done();
});
});
it('should have downloaded the http import in SimpleStorageWithHttpImport', (done) => {
const contractImportPath = '.embark/contracts/embark-framework/embark/develop/test_apps/contracts_app/contracts/ownable.sol';
fs.access(contractImportPath, (err) => {
if (err) {
assert.fail(contractImportPath + ' was not downloaded');
} }
done(); done();
}); });