Show
Ignore:
Timestamp:
09/15/07 12:41:16 (1 year ago)
Author:
w_i
Message:

Bulletproofing to javascript packing.
Added unpacking.
Added packer to component packing task.
Closes #6

Files:

Legend:

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

    r12078 r12302  
    3939        private $statistics = null; 
    4040         
     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         
    4149        /** 
    4250         * The target directory where the packed files should be saved. 
     
    5765        } 
    5866         
     67        public function setAction($str) 
     68        { 
     69                $this->action = $str; 
     70        } 
     71         
    5972        /** 
    6073         * Sets property name to set with return value of function or expression. 
     
    8295            $this->directory_list_js_files($this->path); 
    8396             
    84             $this->pack_files(); 
    85              
     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                     
    86108            $this->project->setProperty($this->returnProperty, $this->statistics); 
    87109    } 
     
    104126                                continue; 
    105127                        } 
    106                         if ($entry == 'CVS' || $entry == '.svn') 
     128                        if ($entry == '.svn') 
    107129                        { 
    108                                 // Ignore CVS directories 
     130                                // Ignore SVN directories 
    109131                                continue; 
    110132                        } 
     
    114136                        switch ($path_parts['extension']) 
    115137                        { 
    116                                 case 'js' : 
    117                                         $this->js_files[] = $path . "/" . $entry; 
    118                                         //$this->js_packed_files[] = $this->target_dir . "/" . $path_parts['filename'] . ".pack.js"; 
    119                                         $this->js_packed_files[] = $path . "/" . $path_parts['filename'] . ".pack.js"; 
     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                                    } 
    120159                                        break; 
    121160                                default: 
     
    132171        } 
    133172         
     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         
    134220        function pack_files() 
    135221        { 
     
    140226            { 
    141227                $src_file_contents = file_get_contents($src_file); 
    142                 $trgt_file = $this->js_packed_files[$key]; 
    143                 $this->statistics .= "Packing file: {$src_file} to {$trgt_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"; 
     238            } 
     239            else 
     240            { 
     241                echo "\nFATAL ERROR: Could not find source file {$src_file}\n"; 
     242                continue; 
     243            } 
     244             
     245            $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}... "; 
    144249 
    145250                $packer = new JavaScriptPacker($src_file_contents, 'Normal', true, false); 
     
    155260        $this->statistics .= "{$file_count} files packed in {$time} seconds. \n"; 
    156261        } 
     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             
     299        $t2 = microtime(true); 
     300        $time = sprintf('%.4f', ($t2 - $t1) ); 
     301        $this->statistics .= "\nunpacked {$file_count} files in {$time} seconds. \n"; 
     302        } 
    157303     
    158304}