// require_once($argv[1].'/include/utils/sugar_file_utils.php'); if ($argv[1] == '-?') { $argv[2] = '-?'; } //if second argument is set, then process commands if (!empty($argv[2])) { if ($argv[2] == '-r') { //replace the compressed scripts with the backed up version reverseScripts("{$from}/jssource/src_files", "{$from}"); } elseif ($argv[2] == '-m') { //replace the scripts, and then minify the scripts again reverseScripts("{$from}/jssource/src_files", "{$from}"); BackUpAndCompressScriptFiles("{$from}", "", false, true); } elseif ($argv[2] == '-c') { //replace the scripts, concatenate the files, and then minify the scripts again reverseScripts("{$from}/jssource/src_files", "{$from}"); BackUpAndCompressScriptFiles("{$from}", "", false, true); ConcatenateFiles("{$from}", true); } elseif ($argv[2] == '-mo') { //do not replace the scriptsjust minify the existing scripts again BackUpAndCompressScriptFiles("{$from}", "", false, true); } elseif ($argv[2] == '-co') { //concatenate the files only ConcatenateFiles("{$from}", true); } elseif ($argv[2] == '-?') { die("\n Usage : minify <root path> [[-r]|[-m]|[-c]]\n \n <root path> = path of directory to process. Should be root of sugar instance. \n -r = replace javascript of root with scripts from backed up jssource/src_files directory \n -m = same as r, only the script is minified and then copied \n -c = same as m, only the concatenated files are processed again.\n -co = concatenates only the js files that are to be concatenated. Main use is for development when files that make up a concatenated file have been modified.\n -mo = minifies only the existing js files. Will not use source files and will not back up scripts. Main use is for development, when changes have been made to working javascript and you wish to recompress your scripts.\n \n *** note that options are mutually exclusive. You would use -r OR -m OR -c \n \n examples: say your patch is located in 'c:/sugar'\n You wish to have files from root directory concatenated according to file grouping array, as well as all js files compressed and backed up:\n minify 'c:/sugar' \n \n You wish to have backed up jssource files replace your current javascript files:\n minify 'c:/sugar' -r \n \n You wish to have backed up jssource files minified, and replace your current javascript files:\n minify 'c:/sugar' -m \n \n You wish to have backed up jssource files concatenated, minified, and replace your current javascript files:\n minify 'c:/sugar' -c \n "); } } else { //default is to concatenate the files, then back up and compress them if (empty($from)) { echo "directory root to process was not specified";
function reverseScripts($from_path, $to_path = '') { $from_path = str_replace('\\', '/', $from_path); if (empty($to_path)) { $to_path = $from_path; } $to_path = str_replace('\\', '/', $to_path); //check to see if provided paths are legit if (!file_exists($from_path)) { //log error echo "JS Source directory at {$from_path} Does Not Exist<p>\n"; return; } //get correct path for backup $bu_path = $to_path; $bu_path .= substr($from_path, strlen($to_path . '/jssource/src_files')); //if this is a directory, then read it and process files if (is_dir($from_path)) { //grab file / directory and read it. $handle = opendir($from_path); //loop over the directory and go into each child directory while (false !== ($dir = readdir($handle))) { //make sure you go into directory tree and not out of tree if ($dir != '.' && $dir != '..') { //make recursive call to process this directory reverseScripts($from_path . '/' . $dir, $to_path); } } } //if this is not a directory, then //check if this is a javascript file, then process $path_parts = pathinfo($from_path); if (is_file("{$from_path}") && isset($path_parts['extension']) && $path_parts['extension'] == 'js') { //create backup directory if needed $bu_dir = dirname($bu_path); if (!file_exists($bu_dir)) { //directory does not exist, log it and return echo " directory {$bu_dir} does not exist, could not restore {$bu_path}"; return; } //delete backup src file if it exists already if (file_exists($bu_path)) { unlink($bu_path); } copy($from_path, $bu_path); } }