OSDN Git Service

Debug: workspace
[eos/zephyr.git] / server / class / Eos.js
index 7840134..a5efe08 100644 (file)
@@ -1,42 +1,16 @@
-
-/**
- * Eosコマンドをエミュレートするクラス 
- * @varructor
- * @returns {object}
- * function execute(command, params) {
- */
-var eos = {
-    validate: validate,
-    toExecString: toExecString,
-    execute: execute,
-    getFiles: getFiles
-}
-
-
-/**
- *
- * Class variables
- */
-
-// include all Eos command's info.
-// For seaching with O(n), the object key name is command name.
-var db = require('./DB.js').instance;
+/** * * Class variables */ 
+// 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;
 
 
-// for unit test
-
-
-/**
- *
- * Class variables
- */
-
 function hasOneProperty(options) {
     return new Promise(function(resolve, reject) {
         if(options.length === 0) {
@@ -204,6 +178,7 @@ function validate(command, options, workspaceId) {
 function toExecString(command, options, workspaceId) {
     var ocf = ocfReference[command]; // Array
     var finalOptions = {};
+    //var execStr = '/Users/hiratakengo/Eos/bin/X86MAC64/'+command + ' ';
     var execStr = command + ' ';
     var ocfObj = {};
     ocf.forEach(function(o) {
@@ -257,6 +232,61 @@ function toExecString(command, options, workspaceId) {
     return execStr;
 }
 
+/**
+ * toExecArray
+ *
+ * @param {fileId} 
+ * @returns {string}
+ */
+function toExecArray(command, options, workspaceId) {
+    return new Promise(function(resolve, reject) {
+        var ocf = ocfReference[command]; // Array
+        var finalOptions = {};
+        var ocfObj = {};
+        ocf.forEach(function(o) {
+            ocfObj[o.option] = o;
+        });
+
+        // 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);
+                    }
+                }
+            });
+        });
+
+        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') {
+                        s.push(uuids[arg]);
+                    } 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
@@ -265,12 +295,38 @@ function toExecString(command, options, workspaceId) {
  * @param params
  * @returns {object}
  */
-function execute(command, options) {
-    var result = validate(command, options);
-    if(!result.hasDone) {
-    } else {
-        var str = toExecString(command, options);
-    }
+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 {
+            workspace = _dirname + '/../../user-specific-files/workspace';
+        }
+        
+        var config = {
+            cwd: workspace
+        };
+        //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);
+       });
+
+        runner.on('close', function() {
+            resolve();
+        });
+    });
 }
 
 /**
@@ -292,4 +348,37 @@ function getFiles(fileId) {
     });
 }
 
+/**
+ * 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);
+        });
+    });
+}
+
+/**
+ * Eosコマンドをエミュレートするクラス 
+ * @varructor
+ * @returns {object}
+ * function execute(command, params) {
+ */
+var eos = {
+    validate: validate,
+    toExecString: toExecString,
+    execute: execute,
+    getFiles: getFiles,
+    getUUIDs: getUUIDs,
+    toExecArray: toExecArray
+}
+
 module.exports = { instance: eos };