Skip to content

Commit f45ce96

Browse files
committed
add file type so files are loaded when needed; support to configure web3 and solc versions
1 parent 9b98980 commit f45ce96

File tree

14 files changed

+267
-120
lines changed

14 files changed

+267
-120
lines changed

lib/contracts/compiler.js

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ class Compiler {
66
constructor(options) {
77
this.plugins = options.plugins;
88
this.logger = options.logger;
9+
this.solcVersion = options.solcVersion;
910
}
1011

1112
compile_contracts(contractFiles, cb) {
@@ -53,16 +54,28 @@ class Compiler {
5354
let solcW;
5455
async.waterfall([
5556
function prepareInput(callback) {
56-
for (let i = 0; i < contractFiles.length; i++) {
57-
// TODO: this depends on the config
58-
let filename = contractFiles[i].filename.replace('app/contracts/', '');
59-
input[filename] = contractFiles[i].content.toString();
60-
}
61-
callback();
57+
async.each(contractFiles,
58+
function(file, fileCb) {
59+
let filename = file.filename.replace('app/contracts/', '');
60+
file.content(function(fileContent) {
61+
input[filename] = fileContent;
62+
fileCb();
63+
});
64+
},
65+
function (err) {
66+
callback(err);
67+
}
68+
);
69+
//for (let i = 0; i < contractFiles.length; i++) {
70+
// // TODO: this depends on the config
71+
// let filename = contractFiles[i].filename.replace('app/contracts/', '');
72+
// input[filename] = contractFiles[i].content.toString();
73+
//}
74+
//callback();
6275
},
6376
function loadCompiler(callback) {
6477
// TODO: there ino need to load this twice
65-
solcW = new SolcW();
78+
solcW = new SolcW(self.solcVersion);
6679
if (solcW.isCompilerLoaded()) {
6780
return callback();
6881
}

lib/contracts/contracts.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class ContractsManager {
1212
this.contracts = {};
1313
this.logger = options.logger;
1414
this.plugins = options.plugins;
15+
this.solcVersion = options.contractsConfig.versions["solc"];
1516

1617
this.contractDependencies = {};
1718
}
@@ -20,7 +21,7 @@ class ContractsManager {
2021
let self = this;
2122
async.waterfall([
2223
function compileContracts(callback) {
23-
let compiler = new Compiler({plugins: self.plugins, logger: self.logger});
24+
let compiler = new Compiler({plugins: self.plugins, logger: self.logger, solcVersion: self.solcVersion});
2425
compiler.compile_contracts(self.contractFiles, function (err, compiledObject) {
2526
self.compiledContracts = compiledObject;
2627
callback(err);

lib/contracts/solcP.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ let solc;
22

33
process.on('message', function (msg) {
44
if (msg.action === 'loadCompiler') {
5-
solc = require('solc');
5+
//solc = require('solc');
6+
//console.log("requiring compiler at " + msg.solcLocation);
7+
solc = require(msg.solcLocation);
68
process.send({result: "loadedCompiler"});
79
}
810

lib/contracts/solcW.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
let utils = require('../utils/utils.js');
22
let solcProcess;
33
let compilerLoaded = false;
4+
var npm = require('../pipeline/npm.js');
5+
let path = require('path');
46

57
class SolcW {
68

9+
constructor(version) {
10+
this.solcVersion = version;
11+
}
12+
713
load_compiler(done) {
814
if (compilerLoaded) {
915
done();
@@ -16,7 +22,13 @@ class SolcW {
1622
compilerLoaded = true;
1723
done();
1824
});
19-
solcProcess.send({action: 'loadCompiler'});
25+
npm.getPackageVersion('solc', '0.4.10', false, function(location) {
26+
console.log("new compiler installed at " + location);
27+
//let requirePath = path.join(process.env.PWD, location.substr(2));
28+
let requirePath = path.join(process.env.PWD, location);
29+
console.log(requirePath);
30+
solcProcess.send({action: 'loadCompiler', solcLocation: requirePath});
31+
});
2032
}
2133

2234
isCompilerLoaded() {

lib/core/config.js

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var fs = require('./fs.js');
2+
var File = require('./file.js');
23
var Plugins = require('./plugins.js');
34
var utils = require('../utils/utils.js');
45
var npm = require('../pipeline/npm.js');
@@ -45,9 +46,9 @@ Config.prototype.loadConfigFiles = function(options) {
4546
this.loadStorageConfigFile();
4647
this.loadCommunicationConfigFile();
4748

49+
this.loadContractsConfigFile();
4850
this.loadPipelineConfigFile();
4951

50-
this.loadContractsConfigFile();
5152
this.loadWebServerConfigFile();
5253
this.loadChainTrackerFile();
5354
this.loadPluginContractFiles();
@@ -58,8 +59,8 @@ Config.prototype.reloadConfig = function() {
5859
this.loadBlockchainConfigFile();
5960
this.loadStorageConfigFile();
6061
this.loadCommunicationConfigFile();
61-
this.loadPipelineConfigFile();
6262
this.loadContractsConfigFile();
63+
this.loadPipelineConfigFile();
6364
this.loadChainTrackerFile();
6465
};
6566

@@ -234,34 +235,38 @@ Config.prototype.loadFiles = function(files) {
234235
if (file === 'embark.js') {
235236

236237
if (self.blockchainConfig.enabled || self.communicationConfig.provider === 'whisper' || self.communicationConfig.available_providers.indexOf('whisper') >= 0) {
237-
readFiles.push({filename: 'web3.js', content: fs.readFileSync(fs.embarkPath("js/web3.js")).toString(), path: fs.embarkPath("js/web3.js")});
238+
let web3Version = self.contractsConfig.versions["web3.js"];
239+
if (web3Version) {
240+
//if (false) {
241+
//readFiles.push(new File({filename: 'web3-' + web3Version + '.js', type: 'custom', resolver: function(callback) {
242+
readFiles.push(new File({filename: 'web3.js', type: 'custom', resolver: function(callback) {
243+
npm.getPackageVersion('web3', web3Version, 'dist/web3.js', function(web3Content) {
244+
callback(web3Content);
245+
});
246+
}}));
247+
} else {
248+
readFiles.push(new File({filename: 'web3.js', type: 'embark_internal', path: "js/web3.js"}));
249+
}
238250
}
239251

240252
if (self.storageConfig.enabled && (self.storageConfig.provider === 'ipfs' || self.storageConfig.available_providers.indexOf('ipfs') >= 0)) {
241-
readFiles.push({filename: 'ipfs.js', content: fs.readFileSync(fs.embarkPath("js/ipfs.js")).toString(), path: fs.embarkPath("js/ipfs.js")});
253+
readFiles.push(new File({filename: 'ipfs.js', type: 'embark_internal', path: "js/ipfs.js"}));
242254
}
243255

244256
if (self.communicationConfig.enabled && (self.communicationConfig.provider === 'orbit' || self.communicationConfig.available_providers.indexOf('orbit') >= 0)) {
245257
// TODO: remove duplicated files if functionality is the same for storage and orbit
246-
readFiles.push({filename: 'ipfs-api.js', content: fs.readFileSync(fs.embarkPath("js/ipfs-api.min.js")).toString(), path: fs.embarkPath("js/ipfs-api.min.js")});
247-
readFiles.push({filename: 'orbit.js', content: fs.readFileSync(fs.embarkPath("js/orbit.min.js")).toString(), path: fs.embarkPath("js/orbit.min.js")});
258+
readFiles.push(new File({filename: 'ipfs-api.js', type: 'embark_internal', path: "js/ipfs-api.min.js"}));
259+
readFiles.push(new File({filename: 'orbit.js', type: 'embark_internal', path: "js/orbit.min.js"}));
248260
}
249261

250-
readFiles.push({filename: 'embark.js', content: fs.readFileSync(fs.embarkPath("js/build/embark.bundle.js")).toString(), path: fs.embarkPath("js/build/embark.bundle.js")});
262+
readFiles.push(new File({filename: 'embark.js', type: 'embark_internal', path: "js/build/embark.bundle.js"}));
251263
}
252264
if (file === '$EMBARK_JS') {
253-
readFiles.push({filename: '$EMBARK_JS', content: fs.readFileSync(fs.embarkPath("js/build/embark.bundle.js")).toString(), path: fs.embarkPath("js/build/embark.bundle.js")});
254-
}
255-
if (file.indexOf("web3-") === 0) {
256-
let web3Version = file.split('web3-')[1].split(".js")[0];
257-
npm.getPackageVersion('web3', web3Version, function(web3Content) {
258-
self.logger.error('downloaded web3');
259-
readFiles.push({filename: file, content: web3Content, path: fs.embarkPath("js/web3.js")});
260-
});
261-
}
262-
if (file === "web3.js") {
263-
readFiles.push({filename: 'web3.js', content: fs.readFileSync(fs.embarkPath("js/web3.js")).toString(), path: fs.embarkPath("js/web3.js")});
265+
readFiles.push(new File({filename: '$EMBARK_JS', type: 'embark_internal', path: "js/build/embark.bundle.js"}));
264266
}
267+
//if (file === "web3.js") {
268+
// readFiles.push(new File({filename: 'web3.js', type: 'embark_internal', path: "js/web3.js"));
269+
//}
265270
});
266271

267272
// get plugins
@@ -298,13 +303,13 @@ Config.prototype.loadFiles = function(files) {
298303
return;
299304
} else if (file.indexOf("web3") === 0) {
300305
return;
301-
} else if (file === 'abi.js') {
302-
readFiles.push({filename: file, content: "", path: file});
306+
//} else if (file === 'abi.js') {
307+
// readFiles.push({filename: file, content: "", path: file});
303308
} else {
304309
if (file.indexOf('.') >= 0) {
305-
readFiles.push({filename: file, content: fs.readFileSync(file).toString(), path: file});
310+
readFiles.push(new File({filename: file, type: "dapp_file", path: file}));
306311
} else if (file[0] === '$') {
307-
readFiles.push({filename: file, content: "", path: file});
312+
//readFiles.push(new File({filename: file, type: 'embark_internal', path: file}));
308313
}
309314
}
310315
});
@@ -320,7 +325,10 @@ Config.prototype.loadPluginContractFiles = function() {
320325
contractsPlugins.forEach(function(plugin) {
321326
plugin.contractsFiles.forEach(function(file) {
322327
var filename = file.replace('./','');
323-
self.contractsFiles.push({filename: filename, content: plugin.loadPluginFile(file), path: plugin.pathToFile(file)});
328+
//self.contractsFiles.push({filename: filename, content: plugin.loadPluginFile(file), path: plugin.pathToFile(file)});
329+
self.contractsFiles.push(new File({filename: filename, type: 'custom', resolver: function(callback) {
330+
callback(plugin.loadPluginFile(file));
331+
}}));
324332
});
325333
});
326334
}

lib/core/engine.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,9 @@ class Engine {
8383
this.events.on('abi', function (abi, contractsJSON) {
8484
self.currentAbi = abi;
8585
self.contractsJSON = contractsJSON;
86-
pipeline.build(abi, contractsJSON);
87-
self.events.emit('outputDone');
86+
pipeline.build(abi, contractsJSON, null, function() {
87+
self.events.emit('outputDone');
88+
});
8889
});
8990
// TODO: still need to redeploy contracts because the original contracts
9091
// config is being corrupted

lib/core/file.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
let fs = require('./fs.js');
2+
3+
class File {
4+
5+
constructor(options) {
6+
this.filename = options.filename;
7+
this.type = options.type;
8+
this.path = options.path;
9+
this.resolver = options.resolver;
10+
}
11+
12+
content(callback) {
13+
if (this.type === 'embark_internal') {
14+
return callback(fs.readFileSync(fs.embarkPath(this.path)).toString());
15+
} else if (this.type === 'dapp_file') {
16+
return callback(fs.readFileSync(this.path).toString());
17+
} else if (this.type === 'custom') {
18+
return this.resolver(callback);
19+
} else {
20+
throw new Error("unknown file: " + this.filename);
21+
}
22+
}
23+
24+
}
25+
26+
module.exports = File;

lib/core/fs.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ function embarkPath(fileOrDir) {
3434
return utils.joinPath(__dirname, '/../../', fileOrDir);
3535
}
3636

37+
function dappPath() {
38+
return process.env.PWD;
39+
}
40+
3741
module.exports = {
3842
mkdirpSync: mkdirpSync,
3943
copySync: copySync,

lib/pipeline/npm.js

Lines changed: 54 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,70 @@
11
let utils = require('../utils/utils.js');
2+
let fs = require('../core/fs.js');
3+
let o_fs = require('fs-extra');
4+
5+
let http = require('follow-redirects').http;
6+
let https = require('follow-redirects').https;
7+
var tar = require('tar');
28

39
module.exports = {
4-
// TODO: need to refactor to make it truly generalistic, perhaps by
5-
// downloading the tarball itself
6-
getPackageVersion: function(packageName, version, callback) {
7-
let npmRegistry = "http://registry.npmjs.org/" + packageName + "/" + version;
10+
getPackageVersion: function(packageName, version, returnContent, callback) {
11+
let npmRegistry = "https://registry.npmjs.org/" + packageName + "/" + version;
812

9-
utils.httpGet(npmRegistry, function (res) {
13+
utils.httpsGet(npmRegistry, function (res) {
1014

1115
let body = '';
1216
res.on('data', function (d) {
1317
body += d;
1418
});
1519
res.on('end', function () {
1620
let registryJSON = JSON.parse(body);
17-
let gitHash = registryJSON.gitHead;
18-
let repo = registryJSON.homepage.split("github.com/")[1];
19-
20-
let gitUrl = "http://raw.githubusercontent.com/" + repo + "/" + gitHash + "/dist/" + packageName + ".js";
21-
utils.httpGet(gitUrl, function (res2) {
22-
let body = '';
23-
res2.on('data', function (d) {
24-
body += d;
21+
22+
let tarball = registryJSON.dist.tarball;
23+
24+
var download = function(url, dest, cb) {
25+
var file = o_fs.createWriteStream(dest);
26+
var request = (url.substring(0,5) === 'https' ? https : http).get(url, function(response) {
27+
response.pipe(file);
28+
file.on('finish', function() {
29+
file.close(cb); // close() is async, call cb after close completes.
30+
});
31+
}).on('error', function(err) { // Handle errors
32+
fs.unlink(dest); // Delete the file async. (But we don't check the result)
33+
if (cb) cb(err.message);
2534
});
26-
res2.on('end', function () {
27-
callback(body);
35+
};
36+
37+
let packageDirectory = './.embark/versions/' + packageName + '/' + version + '/';
38+
39+
if (fs.existsSync(packageDirectory + "/downloaded_package.tgz")) {
40+
if (returnContent) {
41+
let distFile = packageDirectory + returnContent;
42+
callback(fs.readFileSync(distFile).toString());
43+
} else {
44+
callback(packageDirectory);
45+
}
46+
} else {
47+
fs.mkdirpSync(packageDirectory);
48+
//self.logger.info("downloading " + packageName + " " + version + "....");
49+
50+
download(tarball, packageDirectory + "/downloaded_package.tgz", function() {
51+
o_fs.createReadStream(packageDirectory + '/downloaded_package.tgz').pipe(
52+
tar.x({
53+
strip: 1,
54+
C: packageDirectory
55+
}).on('end', function() {
56+
if (returnContent) {
57+
let distFile = packageDirectory + returnContent;
58+
callback(fs.readFileSync(distFile).toString());
59+
} else {
60+
callback(packageDirectory);
61+
}
62+
})
63+
);
2864
});
29-
});
65+
66+
}
67+
3068
});
3169
});
3270
}

0 commit comments

Comments
 (0)