Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 159 lines (127 sloc) 5.953 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
<?php
if(!defined('sugarEntry'))define('sugarEntry', true);

//assumes jsmin.php is in same directory
    if(isset($_REQUEST['root_directory'])){
        require_once('jssource/minify_utils.php');
    }else{
        require_once('minify_utils.php');
    }

//if we are coming from browser

if(isset($_REQUEST['root_directory'])){
if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');

    require_once('include/utils/sugar_file_utils.php');

    //get the root directory to process
    $from = $_REQUEST['root_directory'];
    $forceReb = false;
    //make sure that the rebuild option has been chosen
    if(isset($_REQUEST['js_rebuild_concat'])){
        if($_REQUEST['js_rebuild_concat'] == 'rebuild'){
             //rebuild if files have changed
             $js_groupings = array();
            if(isset($_REQUEST['root_directory'])){
                require('jssource/JSGroupings.php');
                require_once('jssource/minify_utils.php');
            }else{
                require('JSGroupings.php');
                require_once('minify_utils.php');
            }

            //iterate through array of grouped files
            $grp_array = $js_groupings;//from JSGroupings.php;

            //for each item in array, concatenate the source files
            foreach($grp_array as $grp){
                foreach($grp as $original =>$concat){
                    $concat = sugar_cached($concat);
                    //make sure both files are still valid
                    if(is_file($original) && is_file($concat)){
                        //if individual file has been modifed date later than modified date of
                        //concatenated file, then force a rebuild
                        if(filemtime($original) > filemtime($concat)){
                            $forceReb = true;
                            //no need to continue, we will rebuild
                            break;
                        }
                    }else{
                        //if files are not valid, rebuild as one file could have been deleted
                        $forceReb = true;
                        //no need to continue, we will rebuild
                        break;
                    }
                }
            }

        }
        //if boolean has been set, concatenate files
        if($forceReb){
        ConcatenateFiles("$from");

        }

    }else{
        //We are only allowing rebuilding of concat files from browser.

    }
    return;
}else{
    //run via command line
    //print_r($argv);
    $from="";

    if(isset($argv[1]) && !empty($argv[1])){
         $from = $argv[1];
    }else{
     //Root Directory was not specified
     echo 'Root Directory Input was not provided';
     return;
    }

    if(!function_exists('sugar_cached')) {
        function sugar_cached($dir) { return "cache/$dir"; }
    }

    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("
Usage : minify <root path> [[-r]|[-m]|[-c]]

<root path> = path of directory to process. Should be root of sugar instance.
-r = replace javascript of root with scripts from backed up jssource/src_files directory
-m = same as r, only the script is minified and then copied
-c = same as m, only the concatenated files are processed again.
-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.
-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.

*** note that options are mutually exclusive. You would use -r OR -m OR -c

examples: say your patch is located in 'c:/sugar'
You wish to have files from root directory concatenated according to file grouping array, as well as all js files compressed and backed up:
minify 'c:/sugar'

You wish to have backed up jssource files replace your current javascript files:
minify 'c:/sugar' -r

You wish to have backed up jssource files minified, and replace your current javascript files:
minify 'c:/sugar' -m

You wish to have backed up jssource files concatenated, minified, and replace your current javascript files:
minify 'c:/sugar' -c
");

           }

    }else{
        //default is to concatenate the files, then back up and compress them
        if(empty($from)){
            echo("directory root to process was not specified");
        }

        BackUpAndCompressScriptFiles($from, '', true, true);
        ConcatenateFiles($from,true);

    }
}


?>
Something went wrong with that request. Please try again.