diff --git a/.gitignore b/.gitignore index a39a2abc1..6e7222b28 100755 --- a/.gitignore +++ b/.gitignore @@ -37,3 +37,4 @@ temp.txt update.sh workflow/public_html/translations/ build-prod.zip +node_modules diff --git a/Rakefile b/Rakefile index 8257a0088..412773015 100644 --- a/Rakefile +++ b/Rakefile @@ -354,7 +354,6 @@ def getJsIncludeFiles "gulliver/js/tinymce/jscripts/tiny_mce/plugins/visualchars/editor_plugin.js", "gulliver/js/tinymce/jscripts/tiny_mce/plugins/xhtmlxtras/editor_plugin.js", "gulliver/js/tinymce/jscripts/tiny_mce/plugins/wordcount/editor_plugin.js", - "gulliver/js/tinymce/jscripts/tiny_mce/plugins/visualblocks/editor_plugin.js", "gulliver/js/tinymce/jscripts/tiny_mce/plugins/table/editor_plugin.js", "gulliver/js/tinymce/jscripts/tiny_mce/plugins/template/editor_plugin.js", "gulliver/js/tinymce/jscripts/tiny_mce/plugins/visualblocks/editor_plugin.js", diff --git a/config/build.json b/config/build.json new file mode 100644 index 000000000..f6441c1f1 --- /dev/null +++ b/config/build.json @@ -0,0 +1,523 @@ +[ + { + "id": "pmui", + "description": "Building PMUI library", + "steps": [ + { + "type": "rake", + "rake_dir": "vendor/colosa/pmUI", + "rake_task": "js" + }, + { + "type": "rake", + "rake_dir": "vendor/colosa/pmUI", + "rake_task": "compileTheme[../MichelangeloFE/themes/mafe]" + }, + { + "type": "copy", + "src_dir": "vendor/colosa/pmUI", + "dest_dir": "workflow/public_html/lib", + "files": [ + { + "src": "build/js/pmui-0.1.1.js", + "dest_dir": "pmUI/", + "dest_name": "pmui.min.js" + }, + { + "src": "img/**", + "dest_dir": "img" + }, + { + "src": "libraries/restclient/restclient-min.js", + "dest_dir": "js/", + "dest_name": "restclient.min.js" + }, + { + "src": "themes/mafe/fonts/**/*", + "dest_dir": "fonts" + } + ] + }, + { + "type": "copy", + "src_dir": "vendor/colosa/MichelangeloFE/themes/mafe/build", + "dest_dir": "workflow/public_html/lib", + "files": [ + { + "src": "pmui-mafe.css", + "dest_dir": "pmUI/", + "dest_name": "pmui.min.css" + }, + { + "src": "images/*", + "dest_dir": "css/images/" + } + ] + } + ] + }, + { + "id": "pmDynaform", + "description": "Building PMDynaform library", + "steps": [ + { + "type": "rake", + "rake_dir": "vendor/colosa/pmDynaform", + "rake_task": "default" + }, + { + "type": "dir", + "dir": "workflow/public_html/lib/pmdynaform", + "operation": "clean" + }, + { + "type": "copy", + "src_dir": "vendor/colosa/pmDynaform/", + "dest_dir": "workflow/public_html/lib/pmdynaform/", + "files": [ + { + "src": "build/**", + "dest_dir": "build/" + }, + { + "src": "libs/**", + "dest_dir": "libs/" + } + ] + }, + { + "type": "read", + "src": "vendor/colosa/pmDynaform/config/templates.json", + "variable": "FILES" + }, + { + "type": "parse", + "data": "%FILES%", + "parseTo": "json", + "path": "0/files", + "variable": "FILES" + }, + { + "type": "replace_string", + "target": "%FILES%", + "replacements": [ + { + "search": "src/templates", + "replaceBy": "vendor/colosa/pmDynaform/src/templates" + } + ], + "variable": "FILES" + }, + { + "type": "concatenate", + "files": "%FILES%", + "variable": "CONCATENATED" + }, + { + "type": "replaceFileContents", + "orig_file": "workflow/engine/templates/cases/pmdynaform.html", + "dest_dir": "workflow/public_html/lib/pmdynaform/build", + "dest_file": "pmdynaform.html", + "replacements": [ + { + "search": "###TEMPLATES###", + "replaceBy": "%CONCATENATED%" + } + ] + } + ] + }, + { + "id": "pmDynaformZip", + "description": "Building pmDynaform Zip version...", + "steps": [ + { + "type": "rake", + "rake_dir": "vendor/colosa/pmDynaform", + "rake_task": "mobile" + }, + { + "type": "copy", + "src_dir": "vendor/colosa/pmDynaform/build-prod-zip/", + "dest_dir": "workflow/public_html", + "file_mode": 777, + "files": [ + { + "src": "build-prod.zip" + } + ] + } + ] + }, + { + "id": "mafe", + "description": "Building PM Michelangelo FE", + "steps": [ + { + "type": "rake", + "rake_dir": "vendor/colosa/MichelangeloFE", + "rake_task": "rmdir" + }, + { + "type": "rake", + "rake_dir": "vendor/colosa/MichelangeloFE", + "rake_task": "dir" + }, + { + "type": "rake", + "rake_dir": "vendor/colosa/MichelangeloFE", + "rake_task": "compass" + }, + { + "type": "rake", + "rake_dir": "vendor/colosa/MichelangeloFE", + "rake_task": "compress_js_files" + }, + { + "type": "rake", + "rake_dir": "vendor/colosa/MichelangeloFE", + "rake_task": "compress_app_files" + }, + { + "type": "copy", + "src_dir": "vendor/colosa/pmDynaform/build-prod-zip/", + "dest_dir": "workflow/public_html", + "file_mode": 777, + "files": [ + { + "src": "build-prod.zip" + } + ] + }, + { + "type": "copy", + "src_dir": "vendor/colosa/MichelangeloFE/", + "dest_dir": "workflow/public_html/lib/", + "files": [ + { + "src": "lib/jQueryUI/images/*.png", + "dest_dir": "css/images/" + }, + { + "src": "build/js/designer.js", + "dest_dir": "mafe/", + "dest_name": "designer.min.js" + }, + { + "src": "build/js/mafe.js", + "dest_dir": "mafe/", + "dest_name": "mafe.min.js" + }, + { + "src": "build/css/mafe.css", + "dest_dir": "mafe/", + "dest_name": "mafe.min.css" + }, + { + "src": "img/*.*", + "dest_dir": "img" + } + ] + }, + { + "type": "copy", + "src_dir": "vendor/colosa/MichelangeloFE/lib", + "dest_dir": "workflow/public_html/lib/js/", + "files": [ + { + "src": "wz_jsgraphics/wz_jsgraphics.js", + "dest_name": "wz_jsgraphics.js" + }, + { + "src": "jQuery/jquery-1.10.2.min.js", + "dest_name": "jquery-1.10.2.min.js" + }, + { + "src": "underscore/underscore-min.js", + "dest_name": "underscore-min.js" + }, + { + "src": "jQueryUI/jquery-ui-1.10.3.custom.min.js", + "dest_name": "jquery-ui-1.10.3.custom.min.js" + }, + { + "src": "jQueryLayout/jquery.layout.min.js", + "dest_name": "jquery.layout.min.js" + }, + { + "src": "modernizr/modernizr.js", + "dest_name": "modernizr.js" + } + ] + }, + { + "type": "copy", + "src_dir": "vendor/colosa/MichelangeloFE/src/formDesigner/img/", + "dest_dir": "workflow/public_html/lib/", + "files": [ + { + "src": "*", + "dest_dir": "img" + } + ] + } + ] + }, + { + "id": "log", + "description": "Creating log, hash, and version files...", + "steps": [ + { + "type": "exec", + "working_dir": "vendor/colosa/pmUI", + "command": "git", + "arguments": [ + "rev-parse", + "--short", + "HEAD" + ], + "variable": "PMUI_HASH" + }, + { + "type": "exec", + "working_dir": "vendor/colosa/MichelangeloFE", + "command": "git", + "arguments": [ + "rev-parse", + "--short", + "HEAD" + ], + "variable": "MAFE_HASH" + }, + { + "type": "exec", + "working_dir": "vendor/colosa/pmDynaform", + "command": "git", + "arguments": [ + "rev-parse", + "--short", + "HEAD" + ], + "variable": "PMDYNAFORM_HASH" + }, + { + "type": "concatenate", + "dest_dir": "workflow/public_html/lib/js", + "dest_name": "$[\"mafe-\", \"%PMUI_HASH%\", \"-\", \"%MAFE_HASH%\", \".js\"]$", + "files": [ + "workflow/public_html/lib/js/wz_jsgraphics.js", + "workflow/public_html/lib/js/jquery-1.10.2.min.js", + "workflow/public_html/lib/js/underscore-min.js", + "workflow/public_html/lib/js/jquery-ui-1.10.3.custom.min.js", + "workflow/public_html/lib/js/jquery.layout.min.js", + "workflow/public_html/lib/js/modernizr.js", + "workflow/public_html/lib/js/restclient.min.js", + "workflow/public_html/lib/pmUI/pmui.min.js", + "workflow/public_html/lib/mafe/mafe.min.js", + "workflow/public_html/lib/mafe/designer.min.js", + "gulliver/js/tinymce/jscripts/tiny_mce/tiny_mce.js", + + "gulliver/js/tinymce/jscripts/tiny_mce/plugins/pmGrids/editor_plugin.js", + "gulliver/js/tinymce/jscripts/tiny_mce/plugins/pmSimpleUploader/editor_plugin.js", + "gulliver/js/tinymce/jscripts/tiny_mce/plugins/pmVariablePicker/editor_plugin.js", + "gulliver/js/tinymce/jscripts/tiny_mce/plugins/visualchars/editor_plugin.js", + "gulliver/js/tinymce/jscripts/tiny_mce/plugins/xhtmlxtras/editor_plugin.js", + "gulliver/js/tinymce/jscripts/tiny_mce/plugins/wordcount/editor_plugin.js", + "gulliver/js/tinymce/jscripts/tiny_mce/plugins/table/editor_plugin.js", + "gulliver/js/tinymce/jscripts/tiny_mce/plugins/template/editor_plugin.js", + "gulliver/js/tinymce/jscripts/tiny_mce/plugins/visualblocks/editor_plugin.js", + "gulliver/js/tinymce/jscripts/tiny_mce/plugins/preview/editor_plugin.js", + "gulliver/js/tinymce/jscripts/tiny_mce/plugins/print/editor_plugin.js", + "gulliver/js/tinymce/jscripts/tiny_mce/plugins/style/editor_plugin.js", + "gulliver/js/tinymce/jscripts/tiny_mce/plugins/save/editor_plugin.js", + "gulliver/js/tinymce/jscripts/tiny_mce/plugins/tabfocus/editor_plugin.js", + "gulliver/js/tinymce/jscripts/tiny_mce/plugins/searchreplace/editor_plugin.js", + "gulliver/js/tinymce/jscripts/tiny_mce/plugins/paste/editor_plugin.js", + "gulliver/js/tinymce/jscripts/tiny_mce/plugins/media/editor_plugin.js", + "gulliver/js/tinymce/jscripts/tiny_mce/plugins/lists/editor_plugin.js", + "gulliver/js/tinymce/jscripts/tiny_mce/plugins/insertdatetime/editor_plugin.js", + "gulliver/js/tinymce/jscripts/tiny_mce/plugins/example/editor_plugin.js", + "gulliver/js/tinymce/jscripts/tiny_mce/plugins/pagebreak/editor_plugin.js", + "gulliver/js/tinymce/jscripts/tiny_mce/plugins/example_dependency/editor_plugin.js", + "gulliver/js/tinymce/jscripts/tiny_mce/plugins/noneditable/editor_plugin.js", + "gulliver/js/tinymce/jscripts/tiny_mce/plugins/fullpage/editor_plugin.js", + "gulliver/js/tinymce/jscripts/tiny_mce/plugins/layer/editor_plugin.js", + "gulliver/js/tinymce/jscripts/tiny_mce/plugins/legacyoutput/editor_plugin.js", + "gulliver/js/tinymce/jscripts/tiny_mce/plugins/fullscreen/editor_plugin.js", + "gulliver/js/tinymce/jscripts/tiny_mce/plugins/iespell/editor_plugin.js", + "gulliver/js/tinymce/jscripts/tiny_mce/plugins/inlinepopups/editor_plugin.js", + "gulliver/js/tinymce/jscripts/tiny_mce/plugins/autoresize/editor_plugin.js", + "gulliver/js/tinymce/jscripts/tiny_mce/plugins/contextmenu/editor_plugin.js", + "gulliver/js/tinymce/jscripts/tiny_mce/plugins/advlist/editor_plugin.js", + "gulliver/js/tinymce/jscripts/tiny_mce/plugins/autolink/editor_plugin.js", + "gulliver/js/tinymce/jscripts/tiny_mce/plugins/directionality/editor_plugin.js", + "gulliver/js/tinymce/jscripts/tiny_mce/plugins/emotions/editor_plugin.js", + "gulliver/js/tinymce/jscripts/tiny_mce/themes/advanced/editor_template.js", + "gulliver/js/tinymce/jscripts/tiny_mce/plugins/advhr/editor_plugin.js", + "gulliver/js/tinymce/jscripts/tiny_mce/plugins/advlink/editor_plugin.js", + "gulliver/js/tinymce/jscripts/tiny_mce/plugins/advimage/editor_plugin.js", + "gulliver/js/tinymce/jscripts/tiny_mce/plugins/nonbreaking/editor_plugin.js", + + "gulliver/js/codemirror/lib/codemirror.js", + "gulliver/js/codemirror/addon/hint/show-hint.js", + "gulliver/js/codemirror/addon/hint/javascript-hint.js", + "gulliver/js/codemirror/addon/hint/sql-hint.js", + "gulliver/js/codemirror/addon/hint/php-hint.js", + "gulliver/js/codemirror/addon/hint/html-hint.js", + "gulliver/js/codemirror/mode/javascript/javascript.js", + "gulliver/js/codemirror/addon/edit/matchbrackets.js", + "gulliver/js/codemirror/mode/htmlmixed/htmlmixed.js", + "gulliver/js/codemirror/mode/xml/xml.js", + "gulliver/js/codemirror/mode/css/css.js", + "gulliver/js/codemirror/mode/clike/clike.js", + "gulliver/js/codemirror/mode/php/php.js", + "gulliver/js/codemirror/mode/sql/sql.js" + ] + }, + { + "type": "concatenate", + "dest_dir": "workflow/public_html/lib/css", + "dest_name": "$[\"mafe-\", \"%PMUI_HASH%\", \"-\", \"%MAFE_HASH%\", \".css\"]$", + "files": [ + "gulliver/js/codemirror/lib/codemirror.css", + "gulliver/js/codemirror/addon/hint/show-hint.css", + "workflow/public_html/lib/pmUI/pmui.min.css", + "workflow/public_html/lib/mafe/mafe.min.css" + ] + }, + { + "type": "write", + "contents": "$[\"%PMUI_HASH%\", \"-\", \"%MAFE_HASH%\"]$", + "dest_dir": "workflow/public_html/lib/", + "dest_name": "buildhash" + }, + { + "type": "rake", + "rake_dir": "vendor/colosa/pmUI", + "rake_task": "version", + "variable": "PMUI_VERSION" + }, + { + "type": "rake", + "rake_dir": "vendor/colosa/MichelangeloFE", + "rake_task": "version", + "variable": "MAFE_VERSION" + }, + { + "type": "rake", + "rake_dir": "vendor/colosa/pmDynaform", + "rake_task": "version", + "variable": "PMDYNAFORM_VERSION" + }, + { + "type": "replace_string", + "target": "{\"pmui_ver\":\"##PMUI_VERSION##\",\"pmui_hash\":\"##PMUI_HASH##\",\"mafe_ver\":\"##MAFE_VERSION##\",\"mafe_hash\":\"##MAFE_HASH##\",\"pmdynaform_ver\":\"##PMDYNAFORM_VERSION##\",\"pmdynaform_hash\":\"##PMDYNAFORM_HASH##\"}", + "replacements": [ + { + "search": "##PMUI_VERSION##", + "replaceBy": "%PMUI_VERSION%" + }, + { + "search": "##PMUI_HASH##", + "replaceBy": "%PMUI_HASH%" + }, + { + "search": "##MAFE_VERSION##", + "replaceBy": "%MAFE_VERSION%" + }, + { + "search": "##MAFE_HASH##", + "replaceBy": "%MAFE_HASH%" + }, + { + "search": "##PMDYNAFORM_VERSION##", + "replaceBy": "%PMDYNAFORM_VERSION%" + }, + { + "search": "##PMDYNAFORM_HASH##", + "replaceBy": "%PMDYNAFORM_HASH%" + } + ], + "variable": "VERSIONS" + }, + { + "type": "write", + "contents": "%VERSIONS%", + "dest_dir": "workflow/public_html/lib/", + "dest_name": "versions" + }, + { + "type": "exec", + "working_dir": "vendor/colosa/pmUI", + "command": "git", + "arguments": [ + "log", + "-30", + "--pretty=[%cr] %h %d %s <%an>", + "--no-merges" + ], + "variable": "PMUI_LOG" + }, + { + "type": "exec", + "working_dir": "vendor/colosa/MichelangeloFE", + "command": "git", + "arguments": [ + "log", + "-30", + "--pretty=[%cr] %h %d %s <%an>", + "--no-merges" + ], + "variable": "MAFE_LOG" + }, + { + "type": "exec", + "working_dir": "vendor/colosa/pmDynaform", + "command": "git", + "arguments": [ + "log", + "-30", + "--pretty=[%cr] %h %d %s <%an>", + "--no-merges" + ], + "variable": "PMDYNAFORM_LOG" + }, + { + "type": "exec", + "working_dir": "./", + "command": "git", + "arguments": [ + "log", + "-30", + "--pretty=[%cr] %h %d %s <%an>", + "--no-merges" + ], + "variable": "PM_LOG" + }, + { + "type": "write", + "contents": "%PMUI_LOG%", + "dest_dir": "workflow/public_html/lib/", + "dest_name": "lib-pmui.log" + }, + { + "type": "write", + "contents": "%MAFE_LOG%", + "dest_dir": "workflow/public_html/lib/", + "dest_name": "lib-mafe.log" + }, + { + "type": "write", + "contents": "%PMDYNAFORM_LOG%", + "dest_dir": "workflow/public_html/lib/", + "dest_name": "lib-pmdynaform.log" + }, + { + "type": "write", + "contents": "%PM_LOG%", + "dest_dir": "workflow/public_html/lib/", + "dest_name": "processmaker.log" + } + ] + } +] \ No newline at end of file diff --git a/gulpfile.js b/gulpfile.js new file mode 100644 index 000000000..85dcb13b8 --- /dev/null +++ b/gulpfile.js @@ -0,0 +1,483 @@ +const path = require('path'), + gchmod = require('gulp-chmod'), + CWD = process.cwd() + gconcat = require('gulp-concat'), + grename = require('gulp-rename'), + _ = require('underscore'), + gutil = require('gulp-util'), + grename = require('gulp-rename'), + del = require('del'), + exec = require('child_process').exec, + execFile = require('child_process').execFile, + fs = require('fs'), + mkdirp = require('mkdirp'), + replace = require('gulp-replace'), + isWIN = /^win/.test(process.platform); + +var gulp = require('gulp'), + config = require('./config/build.json'); + +function getVariableName(varName) { + if (/^\%.+\%$/.test(varName)) { + return varName.match(/^\%(.+)\%$/)[1]; + } + return null; +} + +function getValue(key, vars) { + var value = this[key], + finalValue = '', + variable, + i; + + if (typeof value !== 'string') { + return value; + } + + if (/^\$\[(.*)\]\$$/.test(value)) { + value = JSON.parse(value.match(/^\$(\[.*\])\$$/)[1]); + + for (i = 0; i < value.length; i += 1) { + if (variable = getVariableName(value[i])) { + finalValue += vars[variable]; + } else { + finalValue += value[i]; + } + } + } else if (variable = getVariableName(value)) { + finalValue = vars[variable]; + } else { + finalValue = value; + } + + return finalValue; +} + +function executeSequence(sequence, callback) { + var current; + if (sequence.length) { + current = sequence.shift(); + current(function (err, returnValue) { + if (err) { + callback(err); + return; + } + executeSequence(sequence, callback); + }); + } else { + callback(); + } +} + +function validatePath(dir) { + // for the sake of security absolute paths are not allowed + if (path.isAbsolute(dir)) { + throw new Error('Absolute paths are not allowed (used path: ' + dir + ').'); + } + + // for the sake of security paths directioning outside the current directory are not allowed + if (/\.\./.test(path.relative(CWD, dir))) { + throw new Error('Paths poiting outside ProcessMaker project are not allowed (used path: ' + dir + ').'); + } +} + +function cleanDirectory(dir, callback) { + validatePath(dir); + del.sync([ + dir + ], {force: true}); + + return mkdirp(dir, function (err) { + if (typeof callback === 'function') { + callback(err); + } + }); +} + +function executeRake(cb, vars) { + var task = this.rake_task.trim(), + dir = this.rake_dir.trim(), + that = this; + + validatePath(dir); + + gutil.log(gutil.colors.yellow('Executing Rake ' + (task ? '(task: ' + task + ') ' : '') + 'in ' + this.rake_dir)); + exec('rake ' + task, { + cwd: path.join(__dirname , dir) + }, function (err, stdout, stderr) { + if (err) { + cb(err); + } else { + if (typeof that.variable === 'string') { + vars[that.variable] = stdout.trim(); + } + cb(); + } + }); +} + +function executeCopy(cb) { + var src_dir = this['src_dir'] || '', + dest_dir = this['dest_dir'] || '', + file_mode = this['file_mode'], + stream, + file, + file_dest_dir, + file_dest_name, + files_length = this['files'].length, + origin, + destination, + i, + copied = 0, + finishCopy = function () { + copied ++; + if (copied === files_length) { + cb(); + } + }, + showError = function (e) { + gutil.log(gutil.colors.red(e)); + }; + + gutil.log(gutil.colors.yellow('Executing copy of files from ' + src_dir + ' to ' + dest_dir)); + + for (i = 0; i < this['files'].length; i += 1) { + file = this.files[i]; + file_dest_dir = file['dest_dir'] || ''; + file_dest_name = file['dest_name'] || ''; + + origin = path.join(src_dir, file['src']); + destination = path.join(dest_dir, file_dest_dir); + + validatePath(destination); + + gutil.log('copying: ' + origin + ' to ' + path.join(destination, file_dest_name)); + + if (file_dest_name) { + stream = gulp.src([origin]) + .pipe(grename(file_dest_name)) + .pipe(gulp.dest(destination)) + .on('error', showError) + .on('end', finishCopy); + } else { + stream = gulp.src([origin]) + .pipe(gulp.dest(destination)) + .on('error', showError) + .on('end', finishCopy); + } + + if (file_mode) { + stream.pipe(gchmod(file_mode)); + } + } +} + +function executeDir(cb) { + gutil.log(gutil.colors.yellow('Executing directory operation (' + this.operation + ') over ' + this.dir)); + validatePath(this.dir); + switch (this.operation) { + case 'clean': + cleanDirectory(this.dir, cb); + } +} + +function executeRead(cb, vars) { + var that = this, + src = getValue.call(this, 'src', vars); + gutil.log(gutil.colors.yellow('Executing read operation from ' + src)); + + validatePath(src); + + fs.readFile(src, function (err, data) { + if (err) { + return cb(err); + } + + data = data.toString(); + + if (typeof that.variable === 'string') { + vars[that.variable] = data; + } + cb(); + }); +} + +function executeParse(cb, vars) { + var data, path, i; + + gutil.log(gutil.colors.yellow('Executing parsing (' + this.parseTo + ')')); + + switch (this.parseTo) { + case 'json': + path = this.path || ""; + path = path.split("/"); + data = JSON.parse(getValue.call(this, 'data', vars)); + + for (i = 0; i < path.length; i += 1) { + if (path[i]) { + data = data[path[i]]; + } + } + break; + default: + break; + } + + if (typeof this.variable === 'string') { + vars[this.variable] = data; + } + cb(); +} + +function executeReplaceFileContents(cb, vars) { + var originalFile = getValue.call(this, 'orig_file', vars), // It can be string or array + replacements = getValue.call(this, 'replacements', vars), + dest_dir = getValue.call(this, 'dest_dir', vars), + dest_file = getValue.call(this, 'dest_file', vars), + completePath = path.join(dest_dir, dest_file), + that = this, + replacement, + stream, + i; + + gutil.log(gutil.colors.yellow('Executing content replacement of file ' + originalFile + ' files to ' + completePath)); + + if (!_.isArray(replacements)) { + return cb("The replacements must be an array."); + } + + validatePath(completePath); + + stream = gulp.src(originalFile); + + for (i = 0; i < replacements.length; i += 1) { + replacement = replacements[i]; + stream = stream.pipe(replace( + getValue.call(replacement, 'search', vars), + getValue.call(replacement, 'replaceBy', vars) + )); + } + + stream.pipe(grename(dest_file)) + .pipe(gulp.dest(dest_dir)) + .on('end', function () { + if (typeof this.variable === 'string') { + vars[this.variable] = data; + } + cb(); + }); +} + +function executeReplaceString(cb, vars) { + var target = getValue.call(this, 'target', vars), + replacement, + result = [], + isString = false, + i, + j; + + gutil.log(gutil.colors.yellow('Executing replacing')); + + if (typeof target === 'string') { + target = [target]; + isString = true; + } + + for (j = 0; j < target.length; j += 1) { + result[j] = target[j]; + for (i = 0; i < this.replacements.length; i += 1) { + replacement = this.replacements[i]; + result[j] = result[j].replace(getValue.call(replacement, "search", vars), getValue.call(replacement, "replaceBy", vars)); + } + } + + result = isString ? result[0] : result; + + if (typeof this.variable === 'string') { + vars[this.variable] = result; + } + cb(); +} + +function executeConcatenate(cb, vars) { + var files = getValue.call(this, 'files', vars), + name = getValue.call(this, 'dest_name', vars), + completePath, + result = "", + that = this; + + if (this.dest_dir && this.dest_name) { + completePath = path.join(this.dest_dir, name); + gutil.log(gutil.colors.yellow('Executing concatenation of ' + files.length + ' files to ' + completePath)); + validatePath(completePath); + gulp.src(files) + .on('error', function (err) { + cb(err); + }) + .on('data', function (chunk) { + result += chunk.contents; + }) + .on('end', function () { + if (typeof that.variable === 'string') { + vars[that.variable] = result; + } + cb(); + }) + .pipe(gconcat(name)) + .pipe(gulp.dest(this['dest_dir'])); + } else { + gutil.log(gutil.colors.yellow('Executing concatenation of ' + files.length + ' files')); + + gulp.src(files) + .pipe(gconcat('__TMP__')) + .on('error', function (err) { + cb(err); + }) + .on('data', function (chunk) { + result += chunk.contents; + }) + .on('end', function () { + if (typeof that.variable === 'string') { + vars[that.variable] = result; + } + cb(); + }); + } +} + +function executeWrite(cb, vars) { + var thePath = path.join( + getValue.call(this, "dest_dir", vars), + getValue.call(this, "dest_name", vars) + ); + + gutil.log(gutil.colors.yellow('Executing writing of file ' + thePath)); + + validatePath(thePath); + + fs.writeFile(thePath, getValue.call(this, "contents", vars), function (err) { + cb(err); + }); +} + +function executeExec(cb, vars) { + var that = this, + working_dir = path.join(this['working_dir']), // This modifies the path to POSIX/Windows + args = this['arguments'] || [], + opts = {}; + + if (isWIN) { + opts['shell'] = 'git-bash.exe'; + } + + gutil.log(gutil.colors.yellow('Executing shell command: ') + gutil.colors.bold.yellow(this['command'] + ' ' + args.join(' ')) + ' in ' + gutil.colors.bold.yellow(working_dir)); + validatePath(working_dir); + + execFile(this['command'], args, { + cwd: working_dir + }, function (err, stdout, stderr) { + if (err) { + return cb(err); + } + if (typeof that.variable === 'string') { + vars[that.variable] = stdout.trim(); + } + cb(); + }); +} + +function executeVariable(cb, vars) { + var varName = getValue.call(this, 'name', vars); + gutil.log(gutil.colors.yellow('Executing assignation into variable ' + varName)); + vars[varName] = getValue.call(this, 'value', vars); + cb(); +} + +function processTask(callback, stepIndex, variables) { + var step, fn, i, that = this; + + stepIndex = stepIndex || 0; + variables = variables || {}; + + if (stepIndex === 0) { + gutil.log(gutil.colors.green(this.description)); + } + + try { + if (stepIndex < this.steps.length) { + step = this.steps[stepIndex]; + + if (step) { + switch (step.type) { + case 'rake': + fn = executeRake; + break; + case 'copy': + fn = executeCopy; + break; + case 'dir': + fn = executeDir; + break; + case 'read': + fn = executeRead; + break; + case 'parse': + fn = executeParse; + break; + case 'replaceFileContents': + fn = executeReplaceFileContents; + break; + case 'replace_string': + fn = executeReplaceString; + break; + case 'concatenate': + fn = executeConcatenate; + break; + case 'write': + fn = executeWrite; + break; + case 'exec': + fn = executeExec; + break; + case 'variable': + fn = executeVariable; + } + + if (fn) { + fn.call(step, function (err) { + if (err) { + callback(err); + return; + } + processTask.call(that, callback, stepIndex + 1, variables); + }, variables); + } else { + processTask.call(this, callback, stepIndex + 1, variables); + } + } + } else { + gutil.log(gutil.colors.green('DONE!')); + callback(); + } + } catch (e) { + gutil.log(gutil.colors.red('Error at processing ' + this.id + ', step #' + (stepIndex + 1) + ' (' + step.type + '): ' + e.message)); + process.exit(); + } +} + +gulp.task('clean', function () { + gutil.log(gutil.colors.green('Cleaning directories...')); + + cleanDirectory('workflow/public_html/lib'); +}); + +gulp.task('default', ['clean'], function (cb) { + var i, tasks = []; + + gutil.log(gutil.colors.green('Initializing ProcessMaker building...')); + + for (i = 0; i < config.length; i += 1) { + tasks.push(_.bind(processTask, config[i])); + } + executeSequence(tasks, cb); +}); \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 000000000..3c0045485 --- /dev/null +++ b/package.json @@ -0,0 +1,33 @@ +{ + "name": "processmaker-builder", + "version": "1.0.0", + "description": "The gulp task runner for ProcessMaker building", + "main": "gulpfile.js", + "directories": { + "doc": "docs", + "test": "tests" + }, + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+ssh://git@bitbucket.org/duhnnie/processmaker.git" + }, + "author": "ProcessMaker", + "license": "ISC", + "homepage": "https://bitbucket.org/duhnnie/processmaker#readme", + "devDependencies": { + "gulp": "^3.9.1" + }, + "dependencies": { + "del": "^2.2.1", + "gulp-chmod": "^1.3.0", + "gulp-concat": "^2.6.0", + "gulp-exec": "^2.1.2", + "gulp-rename": "^1.2.2", + "gulp-replace": "^0.5.4", + "replace": "^0.3.0", + "underscore": "^1.8.3" + } +}