Skip to content

Commit 6c5415b

Browse files
committed
base code for import parse
1 parent e1a9023 commit 6c5415b

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

lib/core/file.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,19 @@ class File {
1313
this.resolver = options.resolver;
1414
}
1515

16-
downloadFile (callback) {
16+
parseFileForImport(content, callback) {
17+
if (this.filename.indexOf('.sol') < 0) {
18+
// Only supported in Solidity
19+
return callback();
20+
}
21+
const regex = /import "([a-zA-Z0-9_\-.\\\/]+)";/g;
22+
let matches;
23+
while ((matches = regex.exec(content))) {
24+
console.log('Need to download', matches[1]);
25+
}
26+
}
27+
28+
downloadFile (filename, callback) {
1729
const self = this;
1830
async.waterfall([
1931
function makeTheDir(next) {
@@ -41,6 +53,11 @@ class File {
4153
},
4254
function readFile(next) {
4355
fs.readFile(self.path, next);
56+
},
57+
function parseForImports(content, next) {
58+
self.parseFileForImport(content, (err) => {
59+
next(err, content);
60+
});
4461
}
4562
], (err, content) => {
4663
if (err) {

test/contracts/simple_storage.sol

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
pragma solidity ^0.4.7;
22
contract SimpleStorage {
33
uint public storedData;
4+
import "ownable.sol";
5+
import "ownable2.sol";
46

57
function SimpleStorage(uint initialValue) {
68
storedData = initialValue;

test/file.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/*globals describe, it*/
2+
const File = require('../lib/core/file');
3+
const fs = require('fs-extra');
4+
5+
describe('embark.File', function () {
6+
describe('parseFileForImport', () => {
7+
it('should find all the imports', function () {
8+
const contract = fs.readFileSync('./test/contracts/simple_storage.sol').toString();
9+
const file = new File({filename: 'simple_storage.sol'});
10+
file.parseFileForImport(contract);
11+
});
12+
});
13+
});

0 commit comments

Comments
 (0)