Show
Ignore:
Timestamp:
02/06/08 22:58:28 (10 months ago)
Author:
rambo
Message:

scripted whitespace normalization, see r14772

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/midcom/build/packJavascripts.php

    r12302 r14773  
    1616class packJavascripts extends Task 
    1717{ 
    18        function __construct() 
    19        
    20  
    21        
    22  
    23        protected $returnProperty; // name of property to set to return value 
    24  
    25        /** 
    26         * The root path to where the files are stored. 
    27         */ 
    28        private $path = null; 
    29          
    30        /** 
    31         * List of files to be packed 
    32         */ 
    33        private $js_files = array(); 
    34        /** 
    35         * List of packed files 
    36         */ 
    37        private $js_packed_files = array(); 
    38          
    39        private $statistics = null; 
    40          
    41        private $js_src_files = array(); 
    42          
    43        private $js_file_names = array(); 
    44          
    45        private $no_source = true; 
    46          
    47        private $action = 'pack'; 
    48          
    49        /** 
    50         * The target directory where the packed files should be saved. 
    51         */ 
    52        protected $target_dir = null; 
    53          
    54        /** 
    55         * The setter for the attribute "target_dir" 
    56         */ 
    57        public function setTarget_dir($str) 
    58        
    59                $this->target_dir = $str; 
    60        
    61          
    62        public function setPath($str) 
    63        
    64                $this->path = $str; 
    65        
    66          
    67        public function setAction($str) 
    68        
    69                $this->action = $str; 
    70        
    71          
    72        /** 
    73         * Sets property name to set with return value of function or expression. 
    74         */ 
    75        public function setReturnProperty($r) 
    76        
    77                $this->returnProperty = $r; 
    78        
    79  
    80        protected $copyfiles = array (); 
    81  
    82        /** 
    83         * The init method: Do init steps. 
    84         */ 
    85        public function init() 
    86        
    87                // nothing to do here 
    88        
    89  
    90        /** 
    91         * The main entry point method. 
    92         */ 
    93        public function main() 
    94        
    95            $this->directory_list_js_files($this->path); 
    96             
    97            $this->prepare_files(); 
    98             
    99            if ($this->action == 'pack') 
    100            { 
    101             $this->pack_files();                
    102            } 
    103            else 
    104            { 
    105             $this->unpack_files();              
    106            } 
    107                     
    108            $this->project->setProperty($this->returnProperty, $this->statistics); 
    109     } 
    110          
    111        /** 
    112         * Generate the filelist 
    113         * @param array $config File listing configuration 
    114         * @return string File XML list 
    115         */ 
    116        function directory_list_js_files($path) 
    117        
    118                $directory = dir($path); 
    119  
    120                // List contents 
    121                while (false !== ($entry = $directory->read())) 
    122                
    123                        if (substr($entry, 0, 1) == '.') 
    124                        
    125                                // Ignore dotfiles 
    126                                continue; 
    127                        
    128                        if ($entry == '.svn') 
    129                        
    130                                // Ignore SVN directories 
    131                                continue; 
    132                        
    133  
    134                        // Check for js files 
    135                        $path_parts = pathinfo($entry); 
    136                        switch ($path_parts['extension']) 
    137                        
    138                                case 'js': 
    139                                    $clean_filename = str_replace('.src', '', $path_parts['filename']); 
    140                                    $clean_filename = str_replace('.pack', '', $clean_filename); 
    141                                     
    142                            if (! in_array($path_parts['filename'], $this->js_file_names)) 
    143                            { 
    144                                if (   strpos($path_parts['filename'], '.src') === false 
    145                                        && strpos($path_parts['filename'], '.pack') === false) 
    146                                    { 
    147                                        $this->js_file_names[$clean_filename] = $path_parts['filename']; 
    148                                        $this->js_files[$clean_filename] = $path . "/" . $entry; 
    149                                    } 
    150                            } 
    151                                if (strpos($path_parts['filename'], '.pack') !== false) 
    152                                    { 
    153                                        $this->js_packed_files[$clean_filename] = $path . "/" . $entry; 
    154                                    } 
    155                                if (strpos($path_parts['filename'], '.src') !== false) 
    156                                    { 
    157                                        $this->js_src_files[$clean_filename] = $path . "/" . $entry; 
    158                                    } 
    159                                        break; 
    160                                default: 
    161                                        break; 
    162                        
    163  
    164                        if (is_dir("{$path}/{$entry}")) 
    165                        
    166                                // List the subdirectory 
    167                                $subpath = "{$path}/{$entry}"; 
    168                                $this->directory_list_js_files($subpath); 
    169                        
    170                
    171        
    172          
    173        function prepare_files() 
    174        
    175            foreach ($this->js_file_names as $clean_name => $file) 
    176            { 
    177                $old_packed = $this->js_packed_files[$clean_name]; 
    178                $src_file = $this->js_src_files[$clean_name]; 
    179                $sel_source = ''; 
    180                //echo "\nClean name: {$clean_name}, file: {$file}\n"; 
    181                //echo "\nPacked: {$old_packed}\n"; 
    182                //echo "\nSource: {$src_file}\n"; 
    183                 
    184                $orig_file_parts = pathinfo($this->js_files[$clean_name]); 
    185                $new_packed_file = $orig_file_parts['dirname'] . "/" . $orig_file_parts['filename'] . ".pack.js"; 
    186                $new_source_file = $orig_file_parts['dirname'] . "/" . $orig_file_parts['filename'] . ".src.js"; 
    187                                 
    188                if (empty($src_file)) 
    189                { 
    190                    //echo "\nWe have no separate source file, assume the original is the source.\n"; 
    191                    $this->no_source = true; 
    192                    $sel_source = $this->js_files[$clean_name]; 
    193                } 
    194                else 
    195                { 
    196                    //echo "\nWe have source file. Then we also must have either packed file, and/or the original is packed.\n"; 
    197                    $this->no_source = false; 
    198                    $sel_source = $src_file; 
    199  
    200                if (! empty($old_packed)) 
    201                { 
    202                    //echo "\nWe have old packed file. Delete this.\n"; 
    203                    unlink($old_packed); 
    204                } 
    205                }                
    206                 
    207                //echo "\nSelected source file {$sel_source}\n"; 
    208                 
    209                if (! $this->no_source) 
    210                { 
    211                    //Separate source file 
    212                    //echo "\nRename current packed file {$this->js_files[$clean_name]} to .pack\n"; 
    213                    rename($this->js_files[$clean_name], $new_packed_file); 
    214                    //echo "\nRename current source file {$sel_source} to {$this->js_files[$clean_name]}\n"; 
    215                    rename($sel_source, $this->js_files[$clean_name]); 
    216                } 
    217            } 
    218        
    219          
    220        function pack_files() 
    221        
    222            $t1 = microtime(true); 
    223             
    224            $file_count = count($this->js_files); 
    225            foreach ($this->js_files as $key => $src_file) 
    226            { 
    227                $src_file_contents = file_get_contents($src_file); 
    228                 
    229                $src_file_parts = pathinfo($src_file); 
    230                $src_new_file = $src_file_parts['dirname'] . "/" . $src_file_parts['filename'] . ".src.js"; 
    231                $trgt_file = $src_file; 
    232                 
    233                if (file_exists($src_file)) 
    234                { 
    235                    rename($src_file, $src_new_file); 
    236                    //echo "\nRename source file {$src_file} to {$src_new_file}\n"; 
    237                    $this->statistics .= "\nrename {$src_file} to {$src_new_file}\n"; 
     18    function __construct() 
     19   
     20 
     21   
     22 
     23    protected $returnProperty; // name of property to set to return value 
     24 
     25    /** 
     26    * The root path to where the files are stored. 
     27    */ 
     28    private $path = null; 
     29     
     30    /** 
     31    * List of files to be packed 
     32    */ 
     33    private $js_files = array(); 
     34    /** 
     35    * List of packed files 
     36    */ 
     37    private $js_packed_files = array(); 
     38     
     39    private $statistics = null; 
     40     
     41    private $js_src_files = array(); 
     42     
     43    private $js_file_names = array(); 
     44     
     45    private $no_source = true; 
     46     
     47    private $action = 'pack'; 
     48     
     49    /** 
     50    * The target directory where the packed files should be saved. 
     51    */ 
     52    protected $target_dir = null; 
     53     
     54    /** 
     55    * The setter for the attribute "target_dir" 
     56    */ 
     57    public function setTarget_dir($str) 
     58   
     59        $this->target_dir = $str; 
     60   
     61     
     62    public function setPath($str) 
     63   
     64        $this->path = $str; 
     65   
     66     
     67    public function setAction($str) 
     68   
     69        $this->action = $str; 
     70   
     71     
     72    /** 
     73    * Sets property name to set with return value of function or expression. 
     74    */ 
     75    public function setReturnProperty($r) 
     76   
     77        $this->returnProperty = $r; 
     78   
     79 
     80    protected $copyfiles = array (); 
     81 
     82    /** 
     83    * The init method: Do init steps. 
     84    */ 
     85    public function init() 
     86   
     87        // nothing to do here 
     88   
     89 
     90    /** 
     91    * The main entry point method. 
     92    */ 
     93    public function main() 
     94   
     95        $this->directory_list_js_files($this->path); 
     96         
     97        $this->prepare_files(); 
     98         
     99        if ($this->action == 'pack') 
     100        { 
     101            $this->pack_files();             
     102        } 
     103        else 
     104        { 
     105            $this->unpack_files();             
     106        } 
     107                 
     108        $this->project->setProperty($this->returnProperty, $this->statistics); 
     109    } 
     110     
     111    /** 
     112    * Generate the filelist 
     113    * @param array $config File listing configuration 
     114    * @return string File XML list 
     115    */ 
     116    function directory_list_js_files($path) 
     117   
     118        $directory = dir($path); 
     119 
     120        // List contents 
     121        while (false !== ($entry = $directory->read())) 
     122       
     123            if (substr($entry, 0, 1) == '.') 
     124           
     125                // Ignore dotfiles 
     126                continue; 
     127           
     128            if ($entry == '.svn') 
     129           
     130                // Ignore SVN directories 
     131                continue; 
     132           
     133 
     134            // Check for js files 
     135            $path_parts = pathinfo($entry); 
     136            switch ($path_parts['extension']) 
     137           
     138                case 'js': 
     139                    $clean_filename = str_replace('.src', '', $path_parts['filename']); 
     140                    $clean_filename = str_replace('.pack', '', $clean_filename); 
     141                     
     142                    if (! in_array($path_parts['filename'], $this->js_file_names)) 
     143                    { 
     144                        if (   strpos($path_parts['filename'], '.src') === false 
     145                            && strpos($path_parts['filename'], '.pack') === false) 
     146                        { 
     147                            $this->js_file_names[$clean_filename] = $path_parts['filename']; 
     148                            $this->js_files[$clean_filename] = $path . "/" . $entry; 
     149                        } 
     150                    } 
     151                    if (strpos($path_parts['filename'], '.pack') !== false) 
     152                    { 
     153                        $this->js_packed_files[$clean_filename] = $path . "/" . $entry; 
     154                    } 
     155                    if (strpos($path_parts['filename'], '.src') !== false) 
     156                    { 
     157                        $this->js_src_files[$clean_filename] = $path . "/" . $entry; 
     158                    } 
     159                    break; 
     160                default: 
     161                    break; 
     162           
     163 
     164            if (is_dir("{$path}/{$entry}")) 
     165           
     166                // List the subdirectory 
     167                $subpath = "{$path}/{$entry}"; 
     168                $this->directory_list_js_files($subpath); 
     169           
     170       
     171   
     172     
     173    function prepare_files() 
     174   
     175        foreach ($this->js_file_names as $clean_name => $file) 
     176        { 
     177            $old_packed = $this->js_packed_files[$clean_name]; 
     178            $src_file = $this->js_src_files[$clean_name]; 
     179            $sel_source = ''; 
     180            //echo "\nClean name: {$clean_name}, file: {$file}\n"; 
     181            //echo "\nPacked: {$old_packed}\n"; 
     182            //echo "\nSource: {$src_file}\n"; 
     183             
     184            $orig_file_parts = pathinfo($this->js_files[$clean_name]); 
     185            $new_packed_file = $orig_file_parts['dirname'] . "/" . $orig_file_parts['filename'] . ".pack.js"; 
     186            $new_source_file = $orig_file_parts['dirname'] . "/" . $orig_file_parts['filename'] . ".src.js"; 
     187                         
     188            if (empty($src_file)) 
     189            { 
     190                //echo "\nWe have no separate source file, assume the original is the source.\n"; 
     191                $this->no_source = true; 
     192                $sel_source = $this->js_files[$clean_name]; 
     193            } 
     194            else 
     195            { 
     196                //echo "\nWe have source file. Then we also must have either packed file, and/or the original is packed.\n"; 
     197                $this->no_source = false; 
     198                $sel_source = $src_file; 
     199 
     200                if (! empty($old_packed)) 
     201                { 
     202                    //echo "\nWe have old packed file. Delete this.\n"; 
     203                    unlink($old_packed); 
     204                } 
     205            }             
     206             
     207            //echo "\nSelected source file {$sel_source}\n"; 
     208             
     209            if (! $this->no_source) 
     210            { 
     211                //Separate source file 
     212                //echo "\nRename current packed file {$this->js_files[$clean_name]} to .pack\n"; 
     213                rename($this->js_files[$clean_name], $new_packed_file); 
     214                //echo "\nRename current source file {$sel_source} to {$this->js_files[$clean_name]}\n"; 
     215                rename($sel_source, $this->js_files[$clean_name]); 
     216            } 
     217        } 
     218   
     219     
     220    function pack_files() 
     221   
     222        $t1 = microtime(true); 
     223         
     224        $file_count = count($this->js_files); 
     225        foreach ($this->js_files as $key => $src_file) 
     226        { 
     227            $src_file_contents = file_get_contents($src_file); 
     228             
     229            $src_file_parts = pathinfo($src_file); 
     230            $src_new_file = $src_file_parts['dirname'] . "/" . $src_file_parts['filename'] . ".src.js"; 
     231            $trgt_file = $src_file; 
     232             
     233            if (file_exists($src_file)) 
     234            { 
     235                rename($src_file, $src_new_file); 
     236                //echo "\nRename source file {$src_file} to {$src_new_file}\n"; 
     237                $this->statistics .= "\nrename {$src_file} to {$src_new_file}\n"; 
    238238            } 
    239239            else 
     
    244244             
    245245            $this->js_src_files[$key] = $src_new_file; 
    246                $this->js_packed_files[$key] = $trgt_file; 
    247                 
    248                $this->statistics .= "Packing file: {$src_new_file} to {$trgt_file}... "; 
    249  
    250                $packer = new JavaScriptPacker($src_file_contents, 'Normal', true, false); 
     246            $this->js_packed_files[$key] = $trgt_file; 
     247             
     248            $this->statistics .= "Packing file: {$src_new_file} to {$trgt_file}... "; 
     249 
     250            $packer = new JavaScriptPacker($src_file_contents, 'Normal', true, false); 
    251251            $packed = $packer->pack(); 
    252252             
     
    254254            file_put_contents($trgt_file, $packed); 
    255255            $this->statistics .= "DONE\n"; 
    256            } 
    257             
     256        } 
     257         
    258258        $t2 = microtime(true); 
    259259        $time = sprintf('%.4f', ($t2 - $t1) ); 
    260260        $this->statistics .= "{$file_count} files packed in {$time} seconds. \n"; 
    261        
    262          
    263        function unpack_files() 
    264        
    265            $t1 = microtime(true); 
    266             
    267            $file_count = count($this->js_files); 
    268            foreach ($this->js_files as $key => $file) 
    269            { 
    270                $file_parts = pathinfo($file); 
    271                $src_file = $this->js_src_files[$key]; 
    272                $packed_file = $this->js_packed_files[$key]; 
    273                $new_src_file = $file; 
    274                $new_packed_file = $file_parts['dirname'] . "/" . $file_parts['filename'] . ".pack.js"; 
    275                 
    276                if (file_exists($packed_file)) 
    277                { 
    278                if (   file_exists($new_packed_file) 
    279                    && $packed_file != $new_packed_file) 
    280                { 
    281                    //echo "\nWe have old packed file. Delete this.\n"; 
    282                    unlink($new_packed_file); 
    283                    $this->statistics .= "\nunlink {$new_packed_file}\n"; 
    284                } 
    285                 
    286                //echo "\nrename {$packed_file} to {$new_packed_file}\n"; 
    287                rename($packed_file, $new_packed_file); 
    288                $this->statistics .= "\nrename {$packed_file} to {$new_packed_file}\n"; 
    289             } 
    290                 
    291                if (file_exists($src_file)) 
    292                { 
    293                //echo "\nrename {$src_file} to {$new_src_file}\n"; 
    294                rename($src_file, $new_src_file); 
    295                $this->statistics .= "\nrename {$src_file} to {$new_src_file}\n"; 
    296             } 
    297            } 
    298             
     261   
     262     
     263    function unpack_files() 
     264   
     265        $t1 = microtime(true); 
     266         
     267        $file_count = count($this->js_files); 
     268        foreach ($this->js_files as $key => $file) 
     269        { 
     270            $file_parts = pathinfo($file); 
     271            $src_file = $this->js_src_files[$key]; 
     272            $packed_file = $this->js_packed_files[$key]; 
     273            $new_src_file = $file; 
     274            $new_packed_file = $file_parts['dirname'] . "/" . $file_parts['filename'] . ".pack.js"; 
     275             
     276            if (file_exists($packed_file)) 
     277            { 
     278                if (   file_exists($new_packed_file) 
     279                    && $packed_file != $new_packed_file) 
     280                { 
     281                    //echo "\nWe have old packed file. Delete this.\n"; 
     282                    unlink($new_packed_file); 
     283                    $this->statistics .= "\nunlink {$new_packed_file}\n"; 
     284                } 
     285                 
     286                //echo "\nrename {$packed_file} to {$new_packed_file}\n"; 
     287                rename($packed_file, $new_packed_file); 
     288                $this->statistics .= "\nrename {$packed_file} to {$new_packed_file}\n"; 
     289            } 
     290             
     291            if (file_exists($src_file)) 
     292            { 
     293                //echo "\nrename {$src_file} to {$new_src_file}\n"; 
     294                rename($src_file, $new_src_file); 
     295                $this->statistics .= "\nrename {$src_file} to {$new_src_file}\n"; 
     296            } 
     297        } 
     298         
    299299        $t2 = microtime(true); 
    300300        $time = sprintf('%.4f', ($t2 - $t1) ); 
    301301        $this->statistics .= "\nunpacked {$file_count} files in {$time} seconds. \n"; 
    302        
     302   
    303303     
    304304}