Author: marvintriebel
Date: Thu Mar 13 16:50:47 2014
New Revision: 9241

URL: http://svn.gna.org/viewcvs/service-tech?rev=9241&view=rev
Log:
file extraction from case study, instable version

Added:
    trunk/_meta/live2/frontend/generation/toolFilesFromCaseStudies.js

Added: trunk/_meta/live2/frontend/generation/toolFilesFromCaseStudies.js
URL: 
http://svn.gna.org/viewcvs/service-tech/trunk/_meta/live2/frontend/generation/toolFilesFromCaseStudies.js?rev=9241&view=auto
==============================================================================
--- trunk/_meta/live2/frontend/generation/toolFilesFromCaseStudies.js   (added)
+++ trunk/_meta/live2/frontend/generation/toolFilesFromCaseStudies.js   Thu Mar 
13 16:50:47 2014
@@ -0,0 +1,158 @@
+/*jshint esnext: true*/
+/*jslint node: true */
+(function(){
+"use strict";
+
+// requires
+const fs = require("fs"),
+// sys = require("sys"),
+exec = require("child_process").exec,
+
+// consts
+reBaseLink = /\$LIVE_FILES([^\s]+)/,
+baseLink = 
"http://svn.gna.org/svn/service-tech/trunk/_meta/live2/frontend/generation/files/";;
+
+
+// 1st: REQUIRED ARGUMENT: relative path to casestudy folder
+if (process.argv.length === 2) {
+    console.log("Please provide ONE argument: relative path to casestudy 
folder");
+    return 1;
+}
+
+const relCaseStudyPath = process.argv[2];
+console.log("relative casestudies path: " + relCaseStudyPath);
+
+// this is the object we will create
+var toolFiles = { },
+allFiles;
+
+// 2nd: OPTIONAL ARGUMENT: relative path to output folder
+const relOutputPath = process.argv[3] || "";
+
+// TODO: check for relOutputpath/backend_list.json
+// TODO: check for relOutputpath/allFiles.json
+// TODO: check if output/generatedFiles/ exists...
+
+fs.readFile(relOutputPath + "/backend_list.json", function(err, data) {
+    var list = JSON.parse(data);
+    // iterate through all tools
+    list.forEach(function(cur) {
+        // create object
+        toolFiles[cur.name] = {};
+    });
+    fs.readFile(relOutputPath + "/allFiles.json", function(err, data) {
+        
+        var fileStack = [JSON.parse(data), ""];
+        function pushToStack(prefix) {
+            return function(item) {
+                fileStack.push([item, prefix]);
+            };
+        }
+        while(fileStack.length) {
+           const tos = fileStack.pop(),
+           curItem = tos[0],
+           prefix = tos[1];
+           if(curItem.type === "file") {
+               // put to file hash
+               allFiles[prefix + curItem.name] = true;
+           } else if (curItem.type === "folder") {
+               curItem.files.forEach( pushToStack(prefix + curItem.name));
+           }
+        }
+        openAllCaseStudies();
+    });
+});
+
+function openAllCaseStudies() {
+    // collect all casestudy files
+    exec("find " + relCaseStudyPath + " -type f", function(err, stdout) {
+        var csFiles = stdout.split("\n"),
+        numReadFiles = 0;
+        const csFiles_length = csFiles.length;
+        csFiles.forEach(function(curCsFile) {
+            // keep track of number of files
+            if(!curCsFile) {
+                ++numReadFiles;
+                return;
+            }
+            // open case study
+            console.log("found casestudy file: " + curCsFile);
+            fs.readFile(curCsFile, function read(err, data) {
+                numReadFiles++;
+                if(err) {
+                    throw err;
+                }
+                parseCaseStudy(data);
+                // if all files parsed
+                if(numReadFiles === csFiles_length) {
+                    // convert to tree, ready for live
+                    toolFiles.forEach(function(t) {
+                        var fileTree = { files: [] },
+                        fnOut;
+                        t.forEach(function(cs, csName) {
+                            var filesInCs = [];
+                            cs.forEach(function(fileInCs) {
+                                filesInCs.push(fileInCs);
+                            });
+                            fileTree.files.push({
+                                name  : csName,
+                                type  : "folder",
+                                files : filesInCs
+                            });
+                        });
+                        // now add "all other files"
+                        fileTree.files.push(allFiles);
+                        // each tool gets a json files
+                        fnOut = relOutputPath + "/generated_files/files." + t 
+ ".json";
+                        fs.writeFile(fnOut, JSON.stringify(fileTree, null, "   
 "));
+                        console.log("written file: " + fnOut);
+                    });
+                }
+            });
+        });
+    });
+}
+
+function parseCaseStudy(input) {
+    const inputLines = (input + "").split("\n");
+    var name,
+    c, i = 0,
+    toolName;
+    while ( (c = inputLines[i++]) || typeof c !== "undefined") {
+        if (c.match(/^\s*\#(.*)$/i)) {
+            // first line is assumed to be the name of the case study
+            if (!name) {
+                name = RegExp.$1;
+                // TODO: recognize md-markup
+                name = name.replace(/^(\s*\#)+/gi, "");
+            }
+            // console.log('comment line: ' + c);
+            // TODO: handle comment line
+        } else if (!c) {
+            continue;
+        } else {
+            toolName = c.replace(/^\s*(\w+)\s.*$/, "$1");
+            if(!toolFiles[toolName]) {
+                // if tool file is not in backend list...
+                continue;
+                // toolFiles[toolName] = {};
+            }
+            if(!toolFiles[toolName][name]) {
+                toolFiles[toolName][name] = {};
+            }
+            while (c.match(reBaseLink)) {
+                //let f, fn;
+                c = c.replace(reBaseLink, "");
+                const f = RegExp.$1;
+                const fn = f.replace(/^.*[\\\/]([^\\\/]+)$/gi, "$1");
+                toolFiles[toolName][name][fn] = {
+                    type: "file",
+                    name: fn,
+                    link: baseLink + f
+                };
+            }
+        }
+    }
+}
+
+})();


-- 
You received this e-mail, because you subscribed the mailing list 
"service-tech-commits" which will forward you any e-mail addressed to 
[email protected]. If you want to unsubscribe or make any changes to 
your subscription, please go to
https://mail.gna.org/listinfo/service-tech-commits.

Reply via email to