Skip to content

Commit 05b1f61

Browse files
committed
download files in full path of url
1 parent 6c5415b commit 05b1f61

File tree

5 files changed

+86
-49
lines changed

5 files changed

+86
-49
lines changed

lib/core/config.js

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ Config.prototype.getExternalContractUrl = function (contract) {
152152
const match = contract.file.match(/https:\/\/github\.[a-z]+\/(.*)/);
153153
if (!match) {
154154
this.logger.error(MALFORMED_ERROR + contract.file);
155-
return '';
155+
return null;
156156
}
157157
url = `${RAW_URL}${match[1].replace('blob/', '')}`;
158158
} else if (contract.file.startsWith('git')) {
@@ -164,11 +164,11 @@ Config.prototype.getExternalContractUrl = function (contract) {
164164
// [4] path
165165
// [5] branch
166166
const match = contract.file.match(
167-
/(git:\/\/)?github\.[a-z]+\/([a-zA-Z0-9_\-.]+)\/([a-zA-Z0-9_\-7]+)\/([a-zA-Z0-9_\-\/.]+)#?([a-zA-Z0-1_\-.]*)?/
167+
/(git:\/\/)?github\.[a-z]+\/([a-zA-Z0-9_\-.]+)\/([a-zA-Z0-9_\-]+)\/([a-zA-Z0-9_\-\/.]+)#?([a-zA-Z0-1_\-.]*)?/
168168
);
169169
if (!match) {
170170
this.logger.error(MALFORMED_ERROR + contract.file);
171-
return '';
171+
return null;
172172
}
173173
let branch = match[5];
174174
if (!branch) {
@@ -178,7 +178,13 @@ Config.prototype.getExternalContractUrl = function (contract) {
178178
} else {
179179
url = contract.file;
180180
}
181-
return url;
181+
const match = url.match(
182+
/\.[a-z]+\/([a-zA-Z0-9_\-\/.]+)/
183+
);
184+
return {
185+
url,
186+
filePath: match[1]
187+
};
182188
};
183189

184190
Config.prototype.loadExternalContractsFiles = function() {
@@ -189,9 +195,12 @@ Config.prototype.loadExternalContractsFiles = function() {
189195
continue;
190196
}
191197
if (contract.file.startsWith('http') || contract.file.startsWith('git')) {
192-
const url = this.getExternalContractUrl(contract);
193-
const localFile = httpContractDir + path.basename(url);
194-
this.contractsFiles.push(new File({filename: localFile, type: File.types.http, basedir: '', path: url}));
198+
const fileObj = this.getExternalContractUrl(contract);
199+
if (!fileObj) {
200+
return this.logger.error("HTTP contract file not found: " + contract.file);
201+
}
202+
const localFile = httpContractDir + fileObj.filePath;
203+
this.contractsFiles.push(new File({filename: localFile, type: File.types.http, basedir: '', path: fileObj.url}));
195204
} else if (fs.existsSync(contract.file)) {
196205
this.contractsFiles.push(new File({filename: contract.file, type: File.types.dapp_file, basedir: '', path: contract.file}));
197206
} else if (fs.existsSync(path.join('./node_modules/', contract.file))) {

lib/core/file.js

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,45 +20,48 @@ class File {
2020
}
2121
const regex = /import "([a-zA-Z0-9_\-.\\\/]+)";/g;
2222
let matches;
23+
const filesToDownload = [];
24+
const pathWithoutFile = path.dirname(this.path);
2325
while ((matches = regex.exec(content))) {
2426
console.log('Need to download', matches[1]);
27+
filesToDownload.push({
28+
fileRelativePath: matches[1],
29+
url: path.join(pathWithoutFile, matches[1])
30+
});
2531
}
32+
//async.each(filesToDownload, (file))
2633
}
2734

28-
downloadFile (filename, callback) {
29-
const self = this;
35+
downloadFile (filename, url, callback) {
36+
// const self = this;
3037
async.waterfall([
3138
function makeTheDir(next) {
32-
fs.mkdirp(path.dirname(self.filename), (err) => {
39+
fs.mkdirp(path.dirname(filename), (err) => {
3340
if (err) {
3441
return next(err);
3542
}
3643
next();
3744
});
3845
},
3946
function downloadTheFile(next) {
40-
request(self.path)
47+
request(url)
4148
.on('response', function (response) {
4249
if (response.statusCode !== 200) {
4350
next('Getting file returned code ' + response.statusCode);
4451
}
4552
})
4653
.on('error', next)
47-
.pipe(fs.createWriteStream(self.filename))
48-
.on('finish', () => {
49-
self.path = self.filename;
50-
self.type = File.types.dapp_file;
51-
next();
52-
});
54+
.pipe(fs.createWriteStream(filename))
55+
.on('finish', next);
5356
},
5457
function readFile(next) {
55-
fs.readFile(self.path, next);
56-
},
57-
function parseForImports(content, next) {
58+
fs.readFile(filename, next);
59+
}// ,
60+
/*function parseForImports(content, next) {
5861
self.parseFileForImport(content, (err) => {
5962
next(err, content);
6063
});
61-
}
64+
}*/
6265
], (err, content) => {
6366
if (err) {
6467
console.error('Error while downloading the file', err);
@@ -76,7 +79,14 @@ class File {
7679
} else if (this.type === File.types.custom) {
7780
return this.resolver(callback);
7881
} else if (this.type === File.types.http) {
79-
this.downloadFile(callback);
82+
this.downloadFile(this.filename, this.path, (content) => {
83+
if (!content) {
84+
return callback(content);
85+
}
86+
this.path = this.filename;
87+
this.type = File.types.dapp_file;
88+
callback(content);
89+
});
8090
} else {
8191
throw new Error("unknown file: " + this.filename);
8292
}

test/config.js

Lines changed: 41 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -59,72 +59,89 @@ describe('embark.Config', function () {
5959

6060
describe('#getExternalContractUrl', function () {
6161
it('should get the right url for a https://github file', function () {
62-
const url = config.getExternalContractUrl(
62+
const fileObj = config.getExternalContractUrl(
6363
{file: 'https://github.com/embark-framework/embark/blob/master/test_app/app/contracts/simple_storage.sol'}
6464
);
65-
assert.strictEqual(url,
66-
'https://raw.githubusercontent.com/embark-framework/embark/master/test_app/app/contracts/simple_storage.sol');
65+
assert.deepEqual(fileObj,
66+
{
67+
filePath: 'embark-framework/embark/master/test_app/app/contracts/simple_storage.sol',
68+
url: 'https://raw.githubusercontent.com/embark-framework/embark/master/test_app/app/contracts/simple_storage.sol'
69+
});
6770
});
6871

6972
it('should fail for a malformed https://github file', function () {
70-
const url = config.getExternalContractUrl(
73+
const fileObj = config.getExternalContractUrl(
7174
{file: 'https://github/embark-framework/embark/blob/master/test_app/app/contracts/simple_storage.sol'}
7275
);
73-
assert.strictEqual(url, '');
76+
assert.strictEqual(fileObj, null);
7477
});
7578

7679
it('should get the right url for a git:// file with no branch #', function () {
77-
const url = config.getExternalContractUrl(
80+
const fileObj = config.getExternalContractUrl(
7881
{file: 'git://github.com/status-im/contracts/contracts/identity/ERC725.sol'}
7982
);
80-
console.log(url);
81-
assert.strictEqual(url,
82-
'https://raw.githubusercontent.com/status-im/contracts/master/contracts/identity/ERC725.sol');
83+
assert.deepEqual(fileObj,
84+
{
85+
filePath: 'status-im/contracts/master/contracts/identity/ERC725.sol',
86+
url: 'https://raw.githubusercontent.com/status-im/contracts/master/contracts/identity/ERC725.sol'
87+
});
8388
});
8489

8590
it('should get the right url for a git:// file with a branch #', function () {
86-
const url = config.getExternalContractUrl(
91+
const fileObj = config.getExternalContractUrl(
8792
{file: 'git://github.com/status-im/contracts/contracts/identity/ERC725.sol#myBranch'}
8893
);
89-
assert.strictEqual(url,
90-
'https://raw.githubusercontent.com/status-im/contracts/myBranch/contracts/identity/ERC725.sol');
94+
assert.deepEqual(fileObj,
95+
{
96+
filePath: 'status-im/contracts/myBranch/contracts/identity/ERC725.sol',
97+
url: 'https://raw.githubusercontent.com/status-im/contracts/myBranch/contracts/identity/ERC725.sol'
98+
});
9199
});
92100

93101
it('should fail when the git:// file is malformed', function () {
94-
const url = config.getExternalContractUrl(
102+
const fileObj = config.getExternalContractUrl(
95103
{file: 'git://github.com/identity/ERC725.sol#myBranch'}
96104
);
97-
assert.strictEqual(url, '');
105+
assert.strictEqual(fileObj, null);
98106
});
99107

100108
it('should get the right url with a github.com file without branch #', function () {
101-
const url = config.getExternalContractUrl(
109+
const fileObj = config.getExternalContractUrl(
102110
{file: 'github.com/status-im/contracts/contracts/identity/ERC725.sol'}
103111
);
104-
assert.strictEqual(url,
105-
'https://raw.githubusercontent.com/status-im/contracts/master/contracts/identity/ERC725.sol');
112+
assert.deepEqual(fileObj,
113+
{
114+
filePath: 'status-im/contracts/master/contracts/identity/ERC725.sol',
115+
url: 'https://raw.githubusercontent.com/status-im/contracts/master/contracts/identity/ERC725.sol'
116+
});
106117
});
107118

108119
it('should get the right url with a github.com file with branch #', function () {
109-
const url = config.getExternalContractUrl(
120+
const fileObj = config.getExternalContractUrl(
110121
{file: 'github.com/status-im/contracts/contracts/identity/ERC725.sol#theBranch'}
111122
);
112-
assert.strictEqual(url,
113-
'https://raw.githubusercontent.com/status-im/contracts/theBranch/contracts/identity/ERC725.sol');
123+
assert.deepEqual(fileObj,
124+
{
125+
filePath: 'status-im/contracts/theBranch/contracts/identity/ERC725.sol',
126+
url: 'https://raw.githubusercontent.com/status-im/contracts/theBranch/contracts/identity/ERC725.sol'
127+
});
114128
});
115129

116130
it('should fail with a malformed github.com url', function () {
117-
const url = config.getExternalContractUrl(
131+
const fileObj = config.getExternalContractUrl(
118132
{file: 'github/status-im/contracts/contracts/identity/ERC725.sol#theBranch'}
119133
);
120-
assert.strictEqual(url, '');
134+
assert.strictEqual(fileObj, null);
121135
});
122136

123137
it('should succeed with a generic http url', function () {
124-
const url = config.getExternalContractUrl(
138+
const fileObj = config.getExternalContractUrl(
125139
{file: 'http://myurl.com/myFile.sol'}
126140
);
127-
assert.strictEqual(url, 'http://myurl.com/myFile.sol');
141+
assert.deepEqual(fileObj, {
142+
filePath: 'myFile.sol',
143+
url: 'http://myurl.com/myFile.sol'
144+
});
128145
});
129146
});
130147

test/contracts/simple_storage.sol

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

77
function SimpleStorage(uint initialValue) {
88
storedData = initialValue;

test/file.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ describe('embark.File', function () {
66
describe('parseFileForImport', () => {
77
it('should find all the imports', function () {
88
const contract = fs.readFileSync('./test/contracts/simple_storage.sol').toString();
9-
const file = new File({filename: 'simple_storage.sol'});
9+
const file = new File({filename: 'simple_storage.sol',
10+
path: 'https://raw.githubusercontent.com/embark-framework/embark/develop/test_apps/test_app/app/contracts/simple_storage.sol'});
1011
file.parseFileForImport(contract);
1112
});
1213
});

0 commit comments

Comments
 (0)