]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - jssource/minify.php
Release 6.4.0
[Github/sugarcrm.git] / jssource / minify.php
1 <?php
2 if(!defined('sugarEntry'))define('sugarEntry', true);
3
4 //assumes jsmin.php is in same directory
5     if(isset($_REQUEST['root_directory'])){
6         require_once('jssource/minify_utils.php');
7     }else{
8         require_once('minify_utils.php');
9     }
10
11 //if we are coming from browser
12
13 if(isset($_REQUEST['root_directory'])){
14         if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
15
16     require_once('include/utils/sugar_file_utils.php');
17
18     //get the root directory to process
19     $from = $_REQUEST['root_directory'];
20     $forceReb = false;
21     //make sure that the rebuild option has been chosen
22     if(isset($_REQUEST['js_rebuild_concat'])){
23         if($_REQUEST['js_rebuild_concat'] == 'rebuild'){
24              //rebuild if files have changed
25              $js_groupings = array();
26             if(isset($_REQUEST['root_directory'])){
27                 require('jssource/JSGroupings.php');
28                 require_once('jssource/minify_utils.php');
29             }else{
30                 require('JSGroupings.php');
31                 require_once('minify_utils.php');
32             }
33
34             //iterate through array of grouped files
35             $grp_array = $js_groupings;//from JSGroupings.php;
36
37             //for each item in array, concatenate the source files
38             foreach($grp_array as $grp){
39                 foreach($grp as $original =>$concat){
40                     $concat = sugar_cached($concat);
41                     //make sure both files are still valid
42                     if(is_file($original)  &&  is_file($concat)){
43                         //if individual file has been modifed date later than modified date of
44                         //concatenated file, then force a rebuild
45                         if(filemtime($original) > filemtime($concat)){
46                             $forceReb = true;
47                             //no need to continue, we will rebuild
48                             break;
49                         }
50                     }else{
51                         //if files are not valid, rebuild as one file could have been deleted
52                         $forceReb = true;
53                         //no need to continue, we will rebuild
54                         break;
55                     }
56                 }
57             }
58
59         }
60         //if boolean has been set, concatenate files
61         if($forceReb){
62         ConcatenateFiles("$from");
63
64         }
65
66     }else{
67         //We are only allowing rebuilding of concat files from browser.
68
69     }
70     return;
71 }else{
72     //run via command line
73     //print_r($argv);
74     $from="";
75
76     if(isset($argv[1]) && !empty($argv[1])){
77          $from = $argv[1];
78     }else{
79      //Root Directory was not specified
80      echo 'Root Directory Input was not provided';
81      return;
82     }
83
84     if(!function_exists('sugar_cached')) {
85         function sugar_cached($dir) { return "cache/$dir"; }
86     }
87
88     if($argv[1] == '-?'){
89         $argv[2] = '-?';
90     }
91
92     //if second argument is set, then process commands
93     if(!empty($argv[2])){
94
95            if($argv[2] == '-r'){
96                 //replace the compressed scripts with the backed up version
97                 reverseScripts("$from/jssource/src_files",$from);
98
99            }elseif($argv[2] == '-m'){
100                 //replace the scripts, and then minify the scripts again
101                 reverseScripts("$from/jssource/src_files",$from);
102                 BackUpAndCompressScriptFiles($from,"",false,true);
103
104            }elseif($argv[2] == '-c'){
105                 //replace the scripts, concatenate the files, and then minify the scripts again
106                 reverseScripts("$from/jssource/src_files",$from);
107                 BackUpAndCompressScriptFiles($from,"",false,true);
108                 ConcatenateFiles($from,true);
109            }elseif($argv[2] == '-mo'){
110                 //do not replace the scriptsjust minify the existing scripts again
111                 BackUpAndCompressScriptFiles($from,"",false,true);
112
113            }elseif($argv[2] == '-co'){
114                 //concatenate the files only
115                 ConcatenateFiles($from,true);
116
117            }elseif($argv[2] == '-?'){
118                 die("
119     Usage : minify <root path> [[-r]|[-m]|[-c]]
120
121     <root path> = path of directory to process.  Should be root of sugar instance.
122      -r  = replace javascript of root with scripts from backed up jssource/src_files directory
123      -m  = same as r, only the script is minified and then copied
124      -c  = same as m, only the concatenated files are processed again.
125      -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.
126      -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.
127
128     *** note that options are mutually exclusive.  You would use -r OR -m OR -c
129
130     examples: say your patch is located in 'c:/sugar'
131     You wish to have files from root directory concatenated according to file grouping array, as well as all js files compressed and backed up:
132         minify 'c:/sugar'
133
134     You wish to have backed up jssource files replace your current javascript files:
135         minify 'c:/sugar' -r
136
137     You wish to have backed up jssource files minified, and replace your current javascript files:
138         minify 'c:/sugar' -m
139
140     You wish to have backed up jssource files concatenated, minified, and replace your current javascript files:
141         minify 'c:/sugar' -c
142                                         ");
143
144            }
145
146     }else{
147         //default is to concatenate the files, then back up and compress them
148         if(empty($from)){
149             echo("directory root to process was not specified");
150         }
151
152         BackUpAndCompressScriptFiles($from, '', true, true);
153         ConcatenateFiles($from,true);
154
155     }
156 }
157
158
159 ?>