Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/constants.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"httpContractsDirectory": ".embark/contracts/"
}
28 changes: 18 additions & 10 deletions lib/core/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ const File = require('./file.js');
const Plugins = require('./plugins.js');
const utils = require('../utils/utils.js');
const path = require('path');

const httpContractDir = '.embark/contracts/';
const constants = require('../constants');

var Config = function(options) {
this.env = options.env;
Expand Down Expand Up @@ -152,7 +151,7 @@ Config.prototype.getExternalContractUrl = function (contract) {
const match = contract.file.match(/https:\/\/github\.[a-z]+\/(.*)/);
if (!match) {
this.logger.error(MALFORMED_ERROR + contract.file);
return '';
return null;
}
url = `${RAW_URL}${match[1].replace('blob/', '')}`;
} else if (contract.file.startsWith('git')) {
Expand All @@ -164,11 +163,11 @@ Config.prototype.getExternalContractUrl = function (contract) {
// [4] path
// [5] branch
const match = contract.file.match(
/(git:\/\/)?github\.[a-z]+\/([a-zA-Z0-9_\-.]+)\/([a-zA-Z0-9_\-7]+)\/([a-zA-Z0-9_\-\/.]+)#?([a-zA-Z0-1_\-.]*)?/
/(git:\/\/)?github\.[a-z]+\/([a-zA-Z0-9_\-.]+)\/([a-zA-Z0-9_\-]+)\/([a-zA-Z0-9_\-\/.]+)#?([a-zA-Z0-1_\-.]*)?/
);
if (!match) {
this.logger.error(MALFORMED_ERROR + contract.file);
return '';
return null;
}
let branch = match[5];
if (!branch) {
Expand All @@ -178,7 +177,13 @@ Config.prototype.getExternalContractUrl = function (contract) {
} else {
url = contract.file;
}
return url;
const match = url.match(
/\.[a-z]+\/([a-zA-Z0-9_\-\/.]+)/
);
return {
url,
filePath: match[1]
};
};

Config.prototype.loadExternalContractsFiles = function() {
Expand All @@ -189,9 +194,12 @@ Config.prototype.loadExternalContractsFiles = function() {
continue;
}
if (contract.file.startsWith('http') || contract.file.startsWith('git')) {
const url = this.getExternalContractUrl(contract);
const localFile = httpContractDir + path.basename(url);
this.contractsFiles.push(new File({filename: localFile, type: File.types.http, basedir: '', path: url}));
const fileObj = this.getExternalContractUrl(contract);
if (!fileObj) {
return this.logger.error("HTTP contract file not found: " + contract.file);
}
const localFile = constants.httpContractsDirectory + fileObj.filePath;
this.contractsFiles.push(new File({filename: localFile, type: File.types.http, basedir: '', path: fileObj.url}));
} else if (fs.existsSync(contract.file)) {
this.contractsFiles.push(new File({filename: contract.file, type: File.types.dapp_file, basedir: '', path: contract.file}));
} else if (fs.existsSync(path.join('./node_modules/', contract.file))) {
Expand Down Expand Up @@ -259,7 +267,7 @@ Config.prototype.loadEmbarkConfigFile = function() {
}).map((dir) => {
return dir.split("*.")[0];
});
this.contractDirectories.push(httpContractDir);
this.contractDirectories.push(constants.httpContractsDirectory);

this.buildDir = this.embarkConfig.buildDir;
this.configDir = this.embarkConfig.config;
Expand Down
54 changes: 43 additions & 11 deletions lib/core/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,34 +13,59 @@ class File {
this.resolver = options.resolver;
}

downloadFile (callback) {
parseFileForImport(content, callback) {
const self = this;
if (self.filename.indexOf('.sol') < 0) {
// Only supported in Solidity
return callback();
}
const regex = /import "([a-zA-Z0-9_\-.\\\/]+)";/g;
let matches;
const filesToDownload = [];
const pathWithoutFile = path.dirname(self.path);
while ((matches = regex.exec(content))) {
filesToDownload.push({
fileRelativePath: path.join(path.dirname(self.filename), matches[1]),
url: `${pathWithoutFile}/${matches[1]}`
});
}

async.each(filesToDownload, ((fileObj, eachCb) => {
self.downloadFile(fileObj.fileRelativePath, fileObj.url, (_content) => {
eachCb();
});
}), callback);
}

downloadFile (filename, url, callback) {
const self = this;
async.waterfall([
function makeTheDir(next) {
fs.mkdirp(path.dirname(self.filename), (err) => {
fs.mkdirp(path.dirname(filename), (err) => {
if (err) {
return next(err);
}
next();
});
},
function downloadTheFile(next) {
request(self.path)
request(url)
.on('response', function (response) {
if (response.statusCode !== 200) {
next('Getting file returned code ' + response.statusCode);
}
})
.on('error', next)
.pipe(fs.createWriteStream(self.filename))
.on('finish', () => {
self.path = self.filename;
self.type = File.types.dapp_file;
next();
});
.pipe(fs.createWriteStream(filename))
.on('finish', next);
},
function readFile(next) {
fs.readFile(self.path, next);
fs.readFile(filename, next);
},
function parseForImports(content, next) {
self.parseFileForImport(content, (err) => {
next(err, content);
});
}
], (err, content) => {
if (err) {
Expand All @@ -59,7 +84,14 @@ class File {
} else if (this.type === File.types.custom) {
return this.resolver(callback);
} else if (this.type === File.types.http) {
this.downloadFile(callback);
this.downloadFile(this.filename, this.path, (content) => {
if (!content) {
return callback(content);
}
this.path = this.filename;
this.type = File.types.dapp_file;
callback(content);
});
} else {
throw new Error("unknown file: " + this.filename);
}
Expand Down
8 changes: 6 additions & 2 deletions lib/modules/solidity/solcP.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
let solc;

let fs = require('fs-extra');
let path = require('path');
const fs = require('fs-extra');
const path = require('path');
const constants = require('../../constants');

function findImports(filename) {
if (fs.existsSync(filename)) {
Expand All @@ -10,6 +11,9 @@ function findImports(filename) {
if (fs.existsSync(path.join('./node_modules/', filename))) {
return {contents: fs.readFileSync(path.join('./node_modules/', filename)).toString()};
}
if (fs.existsSync(path.join(constants.httpContractsDirectory, filename))) {
return {contents: fs.readFileSync(path.join('./.embark/contracts', filename)).toString()};
}
return {error: 'File not found'};
}

Expand Down
118 changes: 92 additions & 26 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,6 @@
"matchdep": "^1.0.1",
"mocha": "^3.2.0",
"mocha-sinon": "^1.1.4",
"sinon": "^1.15.4"
"sinon": "^4.5.0"
}
}
Loading