+<<<<<<< HEAD
/**
- * Eosコマンドをエミュレートするクラス
- * @varructor
- * @returns {object}
- * function execute(command, params) {
+ *
+ * Class variables
*/
-function Eos() {
-
- /**
- *
- * Class variables
- */
-
- // include all Eos command's info.
- // For seaching with O(n), the object key name is command name.
- var commandReference = {
- mrcImageInfo: { },
- dcdFilePrint: { }
- };
-
- // for unit test
- var workspace = ['file1.txt', 'file2.txt'];
-
-
- /**
- *
- * Class variables
- */
-
-
- /**
- * validate
- * コマンドとオプションのバリデーション
- * @param command
- * @param params
- * @returns {valid: boolean, message: string}
- */
- function validate(command, options) {
- var result = { hasDone: false, // true | false
- comment: ''// string
- };
- var ocf; // Array
- var ocfObj = {}; // key-value
-
- try {
- /**
- * Check of command name
- */
- if(typeof command !== 'string') {
- errorMsg = 'Command parameter need to be string';
- throw new Error(errorMsg);
- }
-
- var hasCommand = Object.keys(commandReference).indexOf(command) > -1;
- if(!hasCommand) {
- errorMsg = 'Command name is invalid';
- throw new Error(errorMsg);
- }
-
- /**
- * Check of options
- */
+=======
+/** * * Class variables */
+>>>>>>> 6b2b2b88511733893d2c6e7848c389abfcd53ba6
+// include all Eos command's info.
+// For seaching with O(n), the object key name is command name.
+var db = require('./DB.js').instance;
+var commandList = require(__dirname + '/../../user-specific-files/OptionControlFile/command_list.json');
+var ocfReference = {};
+
+commandList.forEach(function(c) {
+ ocfReference[c.name] = require(__dirname + '/../../user-specific-files/OptionControlFile/commands/' + c.name);
+});
+var spawn = require('child_process').spawn;
+
+
+function hasOneProperty(options) {
+ return new Promise(function(resolve, reject) {
+ if(options.length === 0) {
+ var errorMsg = 'At least one option is required.';
+ throw new Error(errorMsg);
+ } else {
+ resolve();
+ }
+ });
+}
- if(!(Array.isArray(options))) {
- errorMsg = 'Options need to be Array';
- throw new Error(errorMsg);
+function matchOption(options, ocf) {
+ return new Promise(function(resolve, reject) {
+ var ok = {};
+ var notIncludingRequiredOptions = [];
+ options.forEach(function(o) {
+ ok[o.name] = o.arguments;
+ });
+ ocf.forEach(function(o) {
+ if(o.optionProperties && !ok[o.option]) {
+ notIncludingRequiredOptions.push(o.option);
}
+ });
+ // check whether all required option exist
+ if(notIncludingRequiredOptions.length > 0) {
+ var errorMsg = 'Option ' + notIncludingRequiredOptions.toString() + ' are required';
+ reject(new Error(errorMsg));
+ } else {
+ resolve();
+ }
+ });
+}
- if(options.length === 0) {
- errorMsg = 'At least one option is required.';
- throw new Error(errorMsg);
+function validArgumentsNumber(options, ocfObj) {
+ return new Promise(function(resolve, reject) {
+ options.forEach(function(o) {
+ // option number
+ var expectNum = ocfObj[o.name].optionNumber;
+ var actualNum = o.arguments.length;
+ if(expectNum !== actualNum) {
+ reject(new Error('Invalid arguments number'));
}
+ });
+ resolve();
+ });
+};
- // translate options to key-value and check whether options include correct member
- var optionsObj = {};
- var hasCorrectMember = true;
- var isArgumentsArray = true;
-
- options.forEach(function(o) {
- if(!(o.name) && !(o.arguments)) {
- hasCorrectMember = false;
+function validArgumentsType(options, ocfObj, workspace) {
+ return new Promise(function(resolve, reject) {
+ options.forEach(function(o) {
+ o.arguments.forEach(function(arg,i) {
+ // argType
+ var formType = ocfObj[o.name].arg[i].formType
+ if(formType === 'select') { // This argument is filename
+ var exist = workspace.indexOf(arg) > -1;
+ if(!exist) {
+ reject(new Error(arg + ' doesn\'t exist.'));
+ }
} else {
- if(Array.isArray(o.arguments)) {
- optionsObj[o.name] = o.arguments;
- } else {
- isArgumentsArray = false;
+ var expectType = formType === 'text' ? 'string' : 'number';
+ var actualType = typeof arg;
+ if(expectType !== actualType) {
+ reject(new Error('argType is invalid'));
}
}
});
+ });
+ resolve();
+ });
+}
- // check each object has proberties "name" and "argumets"
- if(!hasCorrectMember) {
- errorMsg = 'Options need to include Object which have properties "name" and "arguments"';
- throw new Error(errorMsg);
- }
-
- // check each "argumets" properties is Array
- if(!isArgumentsArray) {
- errorMsg = 'Each "arguments" properties needs to be Array';
- throw new Error(errorMsg);
- }
-
- // Read OptionControlFile info of command
- ocf = require(__dirname + '/../../user-specific-files/OptionControlFile/commands/' + command);
+function validOutfileName(options, ocfObj, workspace) {
+ return new Promise(function(resolve, reject) {
+ // output file Regexp
+ var outRegExp = /out|append/;
- // translate ocf info to key-value
- var notIncludingRequiredOptions = [];
- ocf.forEach(function(o) {
- if(o.optionProperties && Object.keys(optionsObj).indexOf(o.option) < 0) {
- notIncludingRequiredOptions.push(o.option);
+ options.forEach(function(o) {
+ o.arguments.forEach(function(arg,i) {
+ // outFile name
+ if(outRegExp.test(ocfObj[o.name].arg[i].argType)) {
+ if(workspace.indexOf(o.arguments[i]) > -1) {
+ reject(new Error('Invalid outfile name.'));
+ }
}
- ocfObj[o.option] = o;
});
+ });
+ resolve();
+ });
+}
- // check whether all required option exist
- if(notIncludingRequiredOptions.length > 0) {
- errorMsg = 'Option ' + notIncludingRequiredOptions.toString() + ' are required';
- throw new Error(errorMsg);
- }
-
- var invalidArgumentsNumber= [];
- var invalidArgumentType = [];
- var invalidOutputFileName = [];
+/**
+ * validate
+ * コマンドとオプションのバリデーション
+ * @param command
+ * @param params
+ * @returns {valid: boolean, message: string}
+ */
+function validate(command, options, workspaceId) {
+ return new Promise(function(resolve, reject) {
+
+ var ocf;
+ if(ocfReference[command]) {
+ ocf = ocfReference[command];
+ } else {
+ var errorMsg = 'Command name is invalid';
+ reject(new Error(errorMsg));
+ }
- // output file Regexp
- var outRegExp = /out|append/;
+ var ocfObj = {};
+ ocf.forEach(function(o) {
+ ocfObj[o.option] = o;
+ });
+ var optionsObj = {};
+ if(Array.isArray(options)) {
options.forEach(function(o) {
- // option number
- var expectNum = ocfObj[o.name].optionNumber;
- var actualNum = o.arguments.length;
- if(expectNum !== actualNum) {
- invalidArgumentsNumber.push({name: o.name, expect: expectNum, actual: actualNum});
- }
-
-
- // argType and outFile name
- o.arguments.forEach(function(arg,i) {
- // argType
- var formType = ocfObj[o.name].arg[i].formType
- if(formType === 'select') { // This argument is filename
- var exist = workspace.indexOf(arg) > -1;
- if(!exist) {
- invalidArgumentType.push({name: o.name, file: arg});
- }
+ if(o.name && o.arguments) {
+ if(Array.isArray(o.arguments)) {
+ optionsObj[o.name] = o.arguments;
} else {
- var expectType = formType === 'text' ? 'string' : 'number';
- var actualType = typeof arg;
- if(expectType !== actualType) {
- invalidArgumentType.push({name: o.name, expect: expectType, actual: actualType});
- }
- }
-
- // outFile name
- if(outRegExp.test(ocfObj[o.name].arg[i].argType)) {
- if(workspace.indexOf(o.arguments[i]) > -1) {
- invalidOutputFileName.push({name: o.name, file: arg});
- }
+ var errorMsg = 'Each "arguments" properties needs to be Array';
+ reject(new Error(errorMsg));
}
- });
+ } else {
+ var errorMsg = 'Options need to include Object which have properties "name" and "arguments"';
+ reject(new Error(errorMsg));
+ }
});
+ } else {
+ var errorMsg = 'Options need to be Array';
+ reject(new Error(errorMsg));
+ }
- // check arguments number value
- if(invalidArgumentsNumber.length > 0) {
- errorMsg = '"arguments" properties is invalid number.\n';
- invalidArgumentsNumber.forEach(function(i) {
- errorMsg += i.name + ' expect to ' + i.expect + ', but actual ' + i.actual + '.\n';
- });
- throw new Error(errorMsg);
- }
+ getFiles(workspaceId)
+ .then(function(workspace) {
+
+ // validate function
+ var promises = [];
+ promises.push(hasOneProperty(options));
+ promises.push(matchOption(options, ocf));
+ promises.push(validArgumentsNumber(options, ocfObj));
+ promises.push(validArgumentsType(options, ocfObj, workspace));
+ promises.push(validOutfileName(options, ocfObj, workspace));
+
+ // do validation
+ return Promise.all(promises)
+ })
+ .catch(function(error) {
+ reject(error);
+ })
+ .then(function(r) {
+ resolve();
+ })
+ });
+}
- // check arguments type
- if(invalidArgumentType.length > 0) {
- errorMsg = '"arguments" type is invalid.\n';
- invalidArgumentType.forEach(function(i) {
- if(i.file) {
- errorMsg += i.name + ' ' + i.file + ' does not exist.\n';
- } else {
- errorMsg += i.name + ' expect to ' + i.expect + ', but actual ' + i.actual + '.\n';
- }
- });
- throw new Error(errorMsg);
- }
+/**
+ * toExecString
+ *
+ * @param command
+ * @param options
+ * @returns {string}
+ */
+function toExecString(command, options, workspaceId) {
+ var ocf = ocfReference[command]; // Array
+ var finalOptions = {};
+<<<<<<< HEAD
+=======
+ //var execStr = '/Users/hiratakengo/Eos/bin/X86MAC64/'+command + ' ';
+>>>>>>> 6b2b2b88511733893d2c6e7848c389abfcd53ba6
+ var execStr = command + ' ';
+ var ocfObj = {};
+ ocf.forEach(function(o) {
+ ocfObj[o.option] = o;
+ });
+
+
+ if(workspaceId === "1f83f620-c1ed-11e5-9657-7942989daa00") { // root
+ var root = __dirname + '/../../user-specific-files/workspace/';
+ }
- // check outFile name
- if(invalidOutputFileName.length > 0) {
- errorMsg = 'output file name is invalid.\n';
- invalidOutputFileName.forEach(function(i) {
- errorMsg += i.name + ' ' + i.file + ' has already existed.\n';
- });
- throw new Error(errorMsg);
+ // set default parameters
+ ocf.forEach(function(o) {
+ o.arg.forEach(function(arg) {
+ if(!(arg.initialValue === "") && arg.initialValue) {
+ if(!(finalOptions[o.option])) {
+ finalOptions[o.option] = [];
+ finalOptions[o.option].push(arg.initialValue);
+ } else {
+ finalOptions[o.option].push(arg.initialValue);
+ }
}
- } catch(e) {
- result.message = e.message;
- return result;
- }
- }
+ });
+ });
+
+ // set user setting parameters
+ options.forEach(function(o) {
+ var s = [];
+ var outRegExp = /out|append/;
+ o.arguments.forEach(function(arg, i) {
+ if(ocfObj[o.name].arg[i].formType === 'select' || outRegExp.test(ocfObj[o.name].arg[i].argType)) {
+ s.push(root+arg);
+ } else {
+ s.push(arg);
+ }
+ });
+ finalOptions[o.name] = s;
+ });
+
+ // set execution string
+ Object.keys(finalOptions).forEach(function(key) {
+ execStr += key + ' ';
+ finalOptions[key].forEach(function(arg) {
+ execStr += arg + ' ';
+ });
+ });
+
+ // remove last blank
+ execStr = execStr.slice(0,execStr.length-1);
+
+ return execStr;
+}
- /**
- * toExecString
- *
- * @param command
- * @param options
- * @returns {string}
- */
- function toExecString(command, options) {
- var ocf = require(__dirname + '/../../user-specific-files/OptionControlFile/commands/' + command); // Array
+/**
+ * toExecArray
+ *
+ * @param {fileId}
+ * @returns {string}
+ */
+function toExecArray(command, options, workspaceId) {
+ return new Promise(function(resolve, reject) {
+ var ocf = ocfReference[command]; // Array
var finalOptions = {};
- var execStr = command + ' ';
+ var ocfObj = {};
+ ocf.forEach(function(o) {
+ ocfObj[o.option] = o;
+ });
// set default parameters
ocf.forEach(function(o) {
});
});
- // set user setting parameters
- options.forEach(function(o) {
- finalOptions[o.name] = o.arguments;
- });
-
- // set execution string
- Object.keys(finalOptions).forEach(function(key) {
- execStr += key + ' ';
- finalOptions[key].forEach(function(arg) {
- execStr += arg + ' ';
+ getUUIDs(workspaceId)
+ .then(function(uuids) {
+ // set user setting parameters
+ options.forEach(function(o) {
+ var s = [];
+ var outRegExp = /out|append/;
+ o.arguments.forEach(function(arg, i) {
+ if(ocfObj[o.name].arg[i].formType === 'select') {
+<<<<<<< HEAD
+ s.push(uuids[arg]);
+ //s.push(arg);
+ console.log('input:' + uuids[arg]);
+=======
+ //s.push(uuids[arg]);
+ s.push(arg);
+>>>>>>> 6b2b2b88511733893d2c6e7848c389abfcd53ba6
+ } else {
+ s.push(arg);
+ }
+ });
+ finalOptions[o.name] = s;
});
+ var array = Object.keys(finalOptions).reduce(function(a,b) {
+ a.push(b);
+ finalOptions[b].forEach(function(v) {
+ a.push(v);
+ });
+ return a;
+ },[]);
+ resolve(array);
});
+ });
+}
+
+/**
+ * execute
+ *
+ * @param command
+ * @param params
+ * @returns {object}
+ */
+function execute(command, optionsArray) {
+ return new Promise(function(resolve, reject) {
+ var workspace;
+ if(process.env.NODE_ENV === 'debug') {
+ workspace = __dirname + '/../../user-specific-files/workspace.debug';
+ } else {
+<<<<<<< HEAD
+ workspace = __dirname + '/../../user-specific-files/workspace';
+=======
+ workspace = _dirname + '/../../user-specific-files/workspace';
+>>>>>>> 6b2b2b88511733893d2c6e7848c389abfcd53ba6
+ }
- // remove last blank
- execStr = execStr.slice(0,execStr.length-1);
+ var config = {
+ cwd: workspace
+ };
+<<<<<<< HEAD
+ var runner = spawn(command,optionsArray,config);
+ //var runner = spawn(command,optionsArray,'/');
+ console.log('spawn');
+=======
+ //var runner = spawn('/Users/hiratakengo/Eos/bin/X86MAC64/'+command,optionsArray,config);
+ var runner = spawn(command,optionsArray,config);
+ //var runner = spawn(command,optionsArray);
+ //commandRet = spawn('ls', ['-lh', '/usr']);
+ //commandRet = spawn(command, ['-h']);
+
+ var commandRet = runner;
+
+ commandRet.stdout.on('data', function (data) {
+ console.log('stdout: ' + data);
+ });
+
+ commandRet.stderr.on('data', function (data) {
+ console.log('stderr: ' + data);
+ });
+>>>>>>> 6b2b2b88511733893d2c6e7848c389abfcd53ba6
+
+ runner.on('close', function() {
+ resolve();
+ });
+ });
+}
- return execStr;
- }
+/**
+ * getFiles
+ * @param fileId
+ * @returns {promise} resolve(Array)
+ */
+function getFiles(fileId) {
+ return new Promise(function(resolve, reject) {
+ if(process.env.NODE_ENV === 'test') {
+ resolve(['file1.txt', 'file2.txt']);
+ } else {
+ db.getFiles(fileId)
+ .then(function(r) {
+ var workspace = r.map(function(f) { return f.name });
+ resolve(workspace);
+ });
+ }
+ });
+}
+/**
+ * getUUID
+ * @param fileId
+ * @returns {object} key: filename, value: uuid
+ */
+function getUUIDs(fileId) {
+ return new Promise(function(resolve) {
+ db.getFiles(fileId)
+ .then(function(r) {
+ var uuids = {};
+ r.forEach(function(v) {
+ uuids[v.dataValues.name] = v.dataValues.fileId;
+ });
+ resolve(uuids);
+ });
+ });
+}
- return {
- /**
- * execute
- *
- * @param command
- * @param params
- * @returns {object}
- */
- execute: function(command, options) {
-
- /**
- * End in Success
- */
- result.hasDone = true;
- result.message = command + ' has done.';
- return result;
- },
-
- // For unit test
- validate: validate,
- toExecString: toExecString
- }
+/**
+ * Eosコマンドをエミュレートするクラス
+ * @varructor
+ * @returns {object}
+ * function execute(command, params) {
+ */
+var eos = {
+ validate: validate,
+ toExecString: toExecString,
+ execute: execute,
+ getFiles: getFiles,
+ getUUIDs: getUUIDs,
+ toExecArray: toExecArray
}
-module.exports = Eos;
+module.exports = { instance: eos };