2018-05-07 18:30:30 +00:00
|
|
|
const fs = require('../core/fs.js');
|
|
|
|
const async = require('async');
|
2018-05-10 14:10:09 +00:00
|
|
|
const child_process = require('child_process');
|
2018-05-07 18:30:30 +00:00
|
|
|
const utils = require('../utils/utils.js');
|
|
|
|
const constants = require('../constants');
|
2016-08-21 16:02:02 +00:00
|
|
|
|
2018-01-17 00:17:52 +00:00
|
|
|
require("babel-preset-react");
|
|
|
|
require("babel-preset-es2015");
|
|
|
|
require("babel-preset-es2016");
|
|
|
|
require("babel-preset-es2017");
|
|
|
|
|
2017-03-30 11:12:39 +00:00
|
|
|
class Pipeline {
|
2017-01-15 19:30:41 +00:00
|
|
|
|
2017-03-30 11:12:39 +00:00
|
|
|
constructor(options) {
|
|
|
|
this.buildDir = options.buildDir;
|
|
|
|
this.contractsFiles = options.contractsFiles;
|
|
|
|
this.assetFiles = options.assetFiles;
|
2018-05-10 14:10:09 +00:00
|
|
|
this.events = options.events;
|
|
|
|
this.logger = options.logger;
|
2018-05-10 15:14:25 +00:00
|
|
|
this.normalizeInput = options.normalizeInput;
|
2018-05-10 14:10:09 +00:00
|
|
|
this.plugins = options.plugins;
|
2018-05-10 14:48:06 +00:00
|
|
|
this.pipelinePlugins = this.plugins.getPluginsFor('pipeline');
|
2017-03-30 11:12:39 +00:00
|
|
|
}
|
2017-01-15 19:30:41 +00:00
|
|
|
|
2017-07-05 12:35:51 +00:00
|
|
|
build(abi, contractsJSON, path, callback) {
|
2017-03-30 11:12:39 +00:00
|
|
|
let self = this;
|
2018-05-08 13:36:50 +00:00
|
|
|
const importsList = {};
|
2017-06-27 22:18:29 +00:00
|
|
|
|
2018-05-08 13:36:50 +00:00
|
|
|
async.waterfall([
|
|
|
|
function buildTheContracts(next) {
|
|
|
|
self.buildContracts(contractsJSON, next);
|
|
|
|
},
|
|
|
|
function buildWeb3(next) {
|
2018-05-15 21:57:56 +00:00
|
|
|
self.events.request('code-generator:web3js', next);
|
2018-05-08 13:36:50 +00:00
|
|
|
},
|
|
|
|
function createImportList(next) {
|
|
|
|
importsList["Embark/EmbarkJS"] = fs.dappPath(".embark", 'embark.js');
|
|
|
|
importsList["Embark/web3"] = fs.dappPath(".embark", 'web3_instance.js');
|
2017-06-27 22:18:29 +00:00
|
|
|
|
2018-05-10 14:10:09 +00:00
|
|
|
self.plugins.getPluginsProperty('imports', 'imports').forEach(function (importObject) {
|
2018-05-08 13:36:50 +00:00
|
|
|
let [importName, importLocation] = importObject;
|
|
|
|
importsList[importName] = importLocation;
|
|
|
|
});
|
2017-12-12 21:10:12 +00:00
|
|
|
|
2018-05-08 13:36:50 +00:00
|
|
|
next();
|
|
|
|
},
|
|
|
|
function writeContracts(next) {
|
|
|
|
async.each(Object.keys(contractsJSON), (contractName, eachCb) => {
|
2018-05-08 13:41:19 +00:00
|
|
|
self.buildContractJS(contractName, (err, contractCode) => {
|
|
|
|
let filePath = fs.dappPath(".embark", contractName + '.js');
|
|
|
|
importsList["Embark/contracts/" + contractName] = filePath;
|
|
|
|
fs.writeFile(filePath, contractCode, eachCb);
|
|
|
|
});
|
2018-05-08 13:36:50 +00:00
|
|
|
}, next);
|
|
|
|
},
|
|
|
|
function assetFileWrite(next) {
|
2018-05-10 14:43:10 +00:00
|
|
|
async.eachOf(self.assetFiles, function (files, targetFile, cb) {
|
|
|
|
async.map(files,
|
2018-05-08 13:36:50 +00:00
|
|
|
function (file, fileCb) {
|
2018-05-10 14:10:09 +00:00
|
|
|
self.logger.trace("reading " + file.filename);
|
2018-05-08 13:36:50 +00:00
|
|
|
|
2018-05-10 14:48:06 +00:00
|
|
|
// Not a JS file
|
2018-05-08 13:36:50 +00:00
|
|
|
if (file.filename.indexOf('.js') < 0) {
|
|
|
|
return file.content(function (fileContent) {
|
|
|
|
self.runPlugins(file, fileContent, fileCb);
|
|
|
|
});
|
|
|
|
}
|
2018-04-09 20:29:49 +00:00
|
|
|
|
2018-05-08 13:36:50 +00:00
|
|
|
// JS files
|
|
|
|
async.waterfall([
|
|
|
|
function runWebpack(next) {
|
2018-05-10 14:10:09 +00:00
|
|
|
const webpackProcess = child_process.fork(utils.joinPath(__dirname, 'webpackProcess.js'));
|
|
|
|
webpackProcess.send({action: constants.pipeline.init, options: {}});
|
|
|
|
webpackProcess.send({action: constants.pipeline.build, file, importsList});
|
|
|
|
|
|
|
|
webpackProcess.on('message', function (msg) {
|
|
|
|
if (msg.result === constants.pipeline.built) {
|
|
|
|
webpackProcess.disconnect();
|
2018-05-10 14:48:06 +00:00
|
|
|
return next(msg.error);
|
2018-05-10 14:10:09 +00:00
|
|
|
}
|
2018-05-08 13:25:37 +00:00
|
|
|
|
2018-05-10 14:10:09 +00:00
|
|
|
if (msg.result === constants.pipeline.log) {
|
|
|
|
if (self.logger[msg.type]) {
|
|
|
|
return self.logger[msg.type](self.normalizeInput(msg.message));
|
|
|
|
}
|
|
|
|
self.logger.debug(self.normalizeInput(msg.message));
|
|
|
|
}
|
|
|
|
});
|
2018-05-08 13:36:50 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
function readFile(next) {
|
|
|
|
fs.readFile('./.embark/' + file.filename, (err, data) => {
|
|
|
|
if (err) {
|
|
|
|
return next(err);
|
|
|
|
}
|
|
|
|
next(null, data.toString());
|
|
|
|
});
|
|
|
|
},
|
2018-05-08 13:25:37 +00:00
|
|
|
|
2018-05-08 13:36:50 +00:00
|
|
|
function runPluginsOnContent(fileContent, next) {
|
|
|
|
self.runPlugins(file, fileContent, next);
|
|
|
|
}
|
2018-05-08 13:25:37 +00:00
|
|
|
|
2018-05-08 13:36:50 +00:00
|
|
|
], function (err, contentFile) {
|
2018-05-08 13:04:53 +00:00
|
|
|
if (err) {
|
2018-05-10 14:10:09 +00:00
|
|
|
self.logger.error(err);
|
2018-05-08 13:36:50 +00:00
|
|
|
return fileCb(err);
|
2018-05-08 13:04:53 +00:00
|
|
|
}
|
|
|
|
|
2018-05-08 13:36:50 +00:00
|
|
|
fileCb(null, contentFile);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
function (err, contentFiles) {
|
|
|
|
if (err) {
|
2018-05-08 21:49:46 +00:00
|
|
|
self.logger.error(__('errors found while generating') + ' ' + targetFile);
|
2018-05-08 13:36:50 +00:00
|
|
|
}
|
|
|
|
let dir = targetFile.split('/').slice(0, -1).join('/');
|
2018-05-10 14:10:09 +00:00
|
|
|
self.logger.trace("creating dir " + self.buildDir + dir);
|
2018-05-08 13:36:50 +00:00
|
|
|
fs.mkdirpSync(self.buildDir + dir);
|
2018-05-08 13:02:46 +00:00
|
|
|
|
2018-05-08 13:36:50 +00:00
|
|
|
// if it's a directory
|
|
|
|
if (targetFile.slice(-1) === '/' || targetFile.indexOf('.') === -1) {
|
|
|
|
let targetDir = targetFile;
|
2018-05-08 13:02:46 +00:00
|
|
|
|
2018-05-08 13:36:50 +00:00
|
|
|
if (targetDir.slice(-1) !== '/') {
|
|
|
|
targetDir = targetDir + '/';
|
2018-05-08 13:25:37 +00:00
|
|
|
}
|
2018-05-08 13:04:53 +00:00
|
|
|
|
2018-05-08 13:36:50 +00:00
|
|
|
async.each(contentFiles, function (file, mapCb) {
|
|
|
|
let filename = file.filename.replace(file.basedir + '/', '');
|
2018-05-10 14:10:09 +00:00
|
|
|
self.logger.info("writing file " + (self.buildDir + targetDir + filename).bold.dim);
|
2018-05-08 13:25:37 +00:00
|
|
|
|
2018-05-08 13:36:50 +00:00
|
|
|
fs.copy(file.path, self.buildDir + targetDir + filename, {overwrite: true}, mapCb);
|
|
|
|
}, cb);
|
|
|
|
return;
|
2018-05-08 13:25:37 +00:00
|
|
|
}
|
2018-05-08 13:36:50 +00:00
|
|
|
|
|
|
|
let content = contentFiles.map(function (file) {
|
|
|
|
if (file === undefined) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
return file.content;
|
|
|
|
}).join("\n");
|
|
|
|
|
2018-05-08 21:49:46 +00:00
|
|
|
self.logger.info(__("writing file") + " " + (self.buildDir + targetFile).bold.dim);
|
2018-05-08 13:36:50 +00:00
|
|
|
fs.writeFile(self.buildDir + targetFile, content, cb);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
next);
|
|
|
|
}
|
|
|
|
], callback);
|
2017-12-12 17:20:57 +00:00
|
|
|
}
|
|
|
|
|
2018-02-28 23:09:10 +00:00
|
|
|
runPlugins(file, fileContent, fileCb) {
|
|
|
|
const self = this;
|
2018-05-10 14:48:06 +00:00
|
|
|
if (self.pipelinePlugins.length <= 0) {
|
2018-05-10 14:10:09 +00:00
|
|
|
return fileCb(null, {content: fileContent, filename: file.filename, path: file.path, basedir: file.basedir, modified: true});
|
2018-02-28 23:09:10 +00:00
|
|
|
}
|
2018-05-10 14:48:06 +00:00
|
|
|
async.eachSeries(self.pipelinePlugins,
|
2018-05-10 14:10:09 +00:00
|
|
|
function(plugin, pluginCB) {
|
2018-02-28 23:09:10 +00:00
|
|
|
if (file.options && file.options.skipPipeline) {
|
|
|
|
return pluginCB();
|
|
|
|
}
|
|
|
|
|
|
|
|
fileContent = plugin.runPipeline({targetFile: file.filename, source: fileContent});
|
|
|
|
file.modified = true;
|
|
|
|
pluginCB();
|
|
|
|
},
|
|
|
|
function (err) {
|
|
|
|
if (err) {
|
2018-05-10 14:10:09 +00:00
|
|
|
self.logger.error(err.message);
|
2018-02-28 23:09:10 +00:00
|
|
|
}
|
2018-05-10 14:10:09 +00:00
|
|
|
return fileCb(null, {content: fileContent, filename: file.filename, path: file.path, basedir: file.basedir, modified: true});
|
2018-02-28 23:09:10 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-05-08 13:36:50 +00:00
|
|
|
buildContracts(contractsJSON, callback) {
|
|
|
|
fs.mkdirp(fs.dappPath(this.buildDir, 'contracts'), (err) => {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
async.each(Object.keys(contractsJSON), (className, eachCb) => {
|
|
|
|
let contract = contractsJSON[className];
|
|
|
|
fs.writeJson(fs.dappPath(this.buildDir, 'contracts', className + ".json"), contract, {spaces: 2}, eachCb);
|
|
|
|
}, callback);
|
|
|
|
});
|
2016-08-21 16:02:02 +00:00
|
|
|
}
|
2017-06-28 00:27:24 +00:00
|
|
|
|
2018-05-08 13:41:19 +00:00
|
|
|
buildContractJS(contractName, callback) {
|
|
|
|
fs.readFile(fs.dappPath(this.buildDir, 'contracts', contractName + '.json'), (err, contractJSON) => {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
contractJSON = contractJSON.toString();
|
2017-12-12 17:20:57 +00:00
|
|
|
|
2018-05-08 13:41:19 +00:00
|
|
|
let contractCode = "";
|
|
|
|
contractCode += "import web3 from 'Embark/web3';\n";
|
|
|
|
contractCode += "import EmbarkJS from 'Embark/EmbarkJS';\n";
|
|
|
|
contractCode += "let " + contractName + "JSONConfig = " + contractJSON + ";\n";
|
|
|
|
contractCode += "let " + contractName + " = new EmbarkJS.Contract(" + contractName + "JSONConfig);\n";
|
2017-12-12 17:20:57 +00:00
|
|
|
|
2018-05-08 13:41:19 +00:00
|
|
|
contractCode += "\n__embarkContext.execWhenReady(function() {\n";
|
|
|
|
contractCode += "\n" + contractName + ".setProvider(web3.currentProvider);\n";
|
2018-05-11 19:39:45 +00:00
|
|
|
contractCode += "\n" + contractName + ".options.from = web3.eth.defaultAccount;\n";
|
2018-05-08 13:41:19 +00:00
|
|
|
contractCode += "\n});\n";
|
2017-06-28 00:27:24 +00:00
|
|
|
|
2018-05-08 13:41:19 +00:00
|
|
|
contractCode += "export default " + contractName + ";\n";
|
|
|
|
callback(null, contractCode);
|
|
|
|
});
|
2017-06-28 00:27:24 +00:00
|
|
|
}
|
2017-12-12 19:45:20 +00:00
|
|
|
|
2017-03-30 11:12:39 +00:00
|
|
|
}
|
2016-08-21 16:02:02 +00:00
|
|
|
|
2018-05-10 14:10:09 +00:00
|
|
|
module.exports = Pipeline;
|