From 6c5415b27f65aa57ebb82003348b6fadfe09d079 Mon Sep 17 00:00:00 2001 From: Jonathan Rainville Date: Wed, 18 Apr 2018 16:32:51 -0400 Subject: [PATCH] base code for import parse --- lib/core/file.js | 19 ++++++++++++++++++- test/contracts/simple_storage.sol | 2 ++ test/file.js | 13 +++++++++++++ 3 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 test/file.js diff --git a/lib/core/file.js b/lib/core/file.js index 09498fc9..b9960561 100644 --- a/lib/core/file.js +++ b/lib/core/file.js @@ -13,7 +13,19 @@ class File { this.resolver = options.resolver; } - downloadFile (callback) { + parseFileForImport(content, callback) { + if (this.filename.indexOf('.sol') < 0) { + // Only supported in Solidity + return callback(); + } + const regex = /import "([a-zA-Z0-9_\-.\\\/]+)";/g; + let matches; + while ((matches = regex.exec(content))) { + console.log('Need to download', matches[1]); + } + } + + downloadFile (filename, callback) { const self = this; async.waterfall([ function makeTheDir(next) { @@ -41,6 +53,11 @@ class File { }, function readFile(next) { fs.readFile(self.path, next); + }, + function parseForImports(content, next) { + self.parseFileForImport(content, (err) => { + next(err, content); + }); } ], (err, content) => { if (err) { diff --git a/test/contracts/simple_storage.sol b/test/contracts/simple_storage.sol index 4b24a2fa..123d8ee8 100644 --- a/test/contracts/simple_storage.sol +++ b/test/contracts/simple_storage.sol @@ -1,6 +1,8 @@ pragma solidity ^0.4.7; contract SimpleStorage { uint public storedData; + import "ownable.sol"; + import "ownable2.sol"; function SimpleStorage(uint initialValue) { storedData = initialValue; diff --git a/test/file.js b/test/file.js new file mode 100644 index 00000000..55c52d8f --- /dev/null +++ b/test/file.js @@ -0,0 +1,13 @@ +/*globals describe, it*/ +const File = require('../lib/core/file'); +const fs = require('fs-extra'); + +describe('embark.File', function () { + describe('parseFileForImport', () => { + it('should find all the imports', function () { + const contract = fs.readFileSync('./test/contracts/simple_storage.sol').toString(); + const file = new File({filename: 'simple_storage.sol'}); + file.parseFileForImport(contract); + }); + }); +});