JsCompile.php
[Pman.Core] / JsCompile.php
1 <?php
2
3 /**
4 * wrapper code around js builder...
5
6 *  -- we will use this later to compile on the fly...
7 *
8 *
9 * For general usage:
10 *  $x = new Pman_Core_JsCompile();
11 *  $x->pack('/path/to/files/', 'destination')
12 *  
13 */
14 require_once 'Pman.php';
15
16
17 class Pman_Core_JsCompile  extends Pman
18 {
19     var $cli = false;
20     function getAuth()
21     {
22         // command line only ATM
23         $this->cli = HTML_FlexyFramework::get()->cli;
24       //  var_dump($this->cli);
25         if ($this->cli) {
26             return true;
27         }
28         return  false;
29     }
30     
31     
32     function get($proj, $args)
33     {
34         if (empty($args)) {
35             die("missing action : eg. build or install");
36         }
37         // list of projects.
38         if (empty($args[1])) {
39             
40             $ar = $this->gatherProjects();
41              echo "SELECT Component to build\n";
42             print_r($ar);
43             exit;
44         } else {
45             $ar = array($args[1]); 
46             //$ar = $args;
47             //array_shift($ar);
48         }
49         
50         switch ($args[0]) {
51             case 'build':
52              
53                 foreach($ar as $p) {
54                     $this->build($p);
55                     //$this->install($p);
56                 }
57                 break;
58             case 'install' :     // needed for install on remote sites..
59                 die('not yet..');
60                 foreach($ar as $p) {
61                     $this->install($p);
62                 }
63                 break;
64         }
65         exit;
66     }
67     
68     
69     
70     function packScript($basedir, $files,  $output_path, $output_url)
71     {
72         // this outputs <script tags..>
73         // either for just the original files,
74         // or the compressed version.
75         // first expand files..
76         
77         $arfiles = array();
78         foreach($files as $f) {
79             if (!is_dir($basedir .'/' .$f)) {
80                 $arfiles[$basedir .'/' .$f] = filemtime($basedir .'/' .$f);
81                 continue;
82             }
83             foreach(glob($basedir .'/' .$f.'/*.js') as $fx) {
84                 $arfiles[$fx] = filemtime($fx);
85             }
86         }
87         
88         $output = md5(serialize($arfiles)) .'.js';
89         
90         if (!file_exists($output_path.'/_cache_/'.$output)) {
91             $this->pack($arfiles,$output_path.'/_cache_/'.$output);
92         }
93         
94         if (file_exists($output_path.'/'.$output)) {
95             
96             echo '<script type="text/javascript" src="'.$output_url.'/_cache_/'. $output.'"></script>';
97             return;
98         }
99         foreach($files as $f=>$t) {
100             echo '<script type="text/javascript" src="'.$output_url.'/'.$f.'"></script>';
101             
102         }
103          
104         
105     }
106     function packCss($basedir, $files,  $output_path, $output_url)
107     {
108         // this outputs <script tags..>
109         // either for just the original files,
110         // or the compressed version.
111         // first expand files..
112         
113         $arfiles = array();
114         foreach($files as $f) {
115             if (!is_dir($basedir .'/' .$f)) {
116                 $arfiles[$basedir .'/' .$f] = filemtime($basedir .'/' .$f);
117                 continue;
118             }
119             foreach(glob($basedir .'/' .$f.'/*.css') as $fx) {
120                 $arfiles[$fx] = filemtime($fx);
121             }
122         }
123         
124         $output = md5(serialize($arfiles)) .'.css';
125         
126         if (!file_exists($output_path.'/_cache_/'.$output)) {
127             $this->packCssCore($arfiles,$output_path.'/_cache_/'.$output);
128         }
129         
130         if (file_exists($output_path.'/'.$output)) {
131             echo '<link type="text/css" rel="stylesheet" media="screen" href="'.$output_url. '/_cache_/'. $output.'" />';
132             return;
133         }
134         foreach($arfiles as $f=>$t) {
135             echo '<link type="text/css" rel="stylesheet" media="screen" href="'.$output_url.'/'.$f.'" />'."\n";
136             
137             
138         }
139          
140         
141     }
142      /**
143      * wrapper arroudn packer...
144      * @param {Array} map of $files => filemtime the files to pack
145      * @param {String} $output name fo file to output
146      *
147      */
148     function packCssCore($files, $output)
149     {
150         
151          
152         $o = HTML_FlexyFramework::get()->Pman_Core;
153         
154         if (empty($o['cssminify']) || !file_exists($o['cssminify'])) {
155             echo '<!-- jspacker not set -->';
156             return false;
157         }
158         require_once 'System.php';
159         $seed= System::which('seed');
160         if (!$seed) {
161             echo '<!-- seed not installed -->';
162             return false;
163             
164         }
165         $targetm = file_exists($output) ? filemtime($output) : 0;
166         $max = 0;
167         $ofiles = array();
168         foreach($files as $f => $mt) {
169             $max = max($max,$mt);
170             $ofiles[] = escapeshellarg($f);
171         }
172         if ($max < $targetm)  {
173             return true;
174         }
175         if (!file_exists(dirname($output))) {
176             mkdir(dirname($output), 0755, true);
177         }
178         $eoutput = escapeshellarg($output);
179         $cmd = "$seed {$o['cssminify']}  $eoutput " . implode($ofiles, ' ');
180         //echo "<PRE>$cmd\n";
181         //echo `$cmd`;
182         `$cmd`;
183         
184         
185         // we should do more checking.. return val etc..
186         if (file_exists($output) && ($max < filemtime($output) ) ) {
187             return true;
188         }
189         return false;
190         
191     }
192     /**
193      * wrapper arroudn packer...
194      * @param {Array} map of $files => filemtime the files to pack
195      * @param {String} $output name fo file to output
196      *
197      */
198     function pack($files, $output)
199     {
200         
201          
202         $o = HTML_FlexyFramework::get()->Pman_Core;
203         
204         if (empty($o['jspacker']) || !file_exists($o['jspacker'].'/pack.js')) {
205             echo '<!-- jspacker not set -->';
206             return false;
207             
208         }
209         require_once 'System.php';
210         $seed= System::which('seed');
211         if (!$seed) {
212             echo '<!-- seed not installed -->';
213             return false;
214             
215         }
216         $targetm = file_exists($output) ? filemtime($output) : 0;
217         $max = 0;
218         $ofiles = array();
219         foreach($files as $f => $mt) {
220             $max = max($max,$mt);
221             $ofiles[] = escapeshellarg($f);
222         }
223         if ($max < $targetm)  {
224             return true;
225         }
226         //var_dump($output);
227         if (!file_exists(dirname($output))) {
228             mkdir(dirname($output), 0755, true);
229         }
230         $eoutput = escapeshellarg($output);
231         $cmd = "$seed {$o['jspacker']}/pack.js  -o $eoutput " . implode($ofiles, ' ');
232         //echo "<PRE>$cmd\n";
233         //echo `$cmd`;
234         `$cmd`;
235         
236         
237         // we should do more checking.. return val etc..
238         if (file_exists($output) && ($max < filemtime($output) ) ) {
239             return true;
240         }
241         return false;
242         
243     }
244     
245     
246     /***
247      * build:
248      *
249      * @param {String} $proj name of Pman component to build
250      * runs pack.js -m {proj} -a $src/*.js
251      * 
252      *
253      */
254       
255     function build($proj) 
256     {
257         echo "Building $proj\n";
258        // var_dump($proj);
259         if (empty($proj)) {
260             $this->err = "no project";
261             if ($this->cli) echo $this->err;
262             return;
263         }
264         // first item in path is always the app start directory..
265         $src= array_shift(explode(PATH_SEPARATOR, ini_get('include_path'))) .'/Pman/'. $proj;
266         
267         
268         
269        //$tmp = ini_get('session.save_path')."/{$proj}_". posix_getuid(). '_'.md5($src);
270         
271         
272         require_once 'System.php';
273         $seed= System::which('seed');
274         if (!$seed) {
275             $this->err ="no seed installed";
276             if ($this->cli) echo $this->err;
277             return false;
278         }
279         
280         $o = HTML_FlexyFramework::get()->Pman_Core;
281         
282         if (empty($o['jspacker']) || !file_exists($o['jspacker'].'/pack.js')) {
283             $this->err ="no jstoolkit path set [Pman_Core][jspacker] to the
284                     introspection documentation directory where pack.js is located.";
285             if ($this->cli) echo $this->err;
286             return false;
287         }  
288         
289         // should we be more specirfic!??!?!?
290          
291         $cmd = "$seed {$o['jspacker']}/pack.js -m $proj  -a  $src/*.js";
292         echo "$cmd\n";
293         passthru($cmd);
294         // technically we should trash old compiled files.. 
295         // or we move towards a 'cache in session directory model..'
296         
297         
298         /*
299         
300         $ret = $tmp . '/'. $proj . '.js';
301         if ($this->cli) {
302             echo "BUILT:  $ret \n";
303             exit;
304         }
305         return $ret;
306         */
307         
308     }
309     // link {PROJECT}/compiled/{PROJECT}.js to _compiled_ folder to make it live.
310         
311     function install($proj) 
312     {
313        
314         $base = dirname(realpath($_SERVER["SCRIPT_FILENAME"]));
315         if (empty($base )) {
316             $base = getcwd();
317         }
318         var_dump($base .'/Pman/'. $proj.'/compiled/'.$proj .'.js');
319         $src =  realpath($base .'/Pman/'. $proj.'/compiled/'.$proj .'.js');
320         if (!$src) {
321             echo "SKIP : no js file $proj\n";
322             return;
323         }
324         if (!file_exists("$base/_compiled_")) {
325             mkdir ("$base/_compiled_", 0755, true);
326         }
327         $target = "$base/_compiled_/".$proj .'.js';
328         print_R(array($src,$target));
329         if (file_exists($target)) {
330             return; // already installed.
331         }
332         
333         symlink($src, $target);
334         
335         
336     }
337     
338     function gatherProjects() {
339         $src= array_shift(explode(PATH_SEPARATOR, ini_get('include_path'))) .'/Pman';
340         
341         $ret = array();
342         foreach(scandir($src) as $f) {
343             if (!strlen($f) || $f[0] == '.') {
344                 continue;
345             }
346             
347             $fp = "$src/$f";
348             if (!is_dir($fp)) {
349                 continue;
350             }
351             if ($f == 'templates') {
352                 continue;
353             }
354             $ret[] = $f;
355             
356             
357         }
358         return $ret;
359     }
360 }
361