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      * packScript:
69      *
70      * @param {String} basedir absolute path to files
71      * @param {Array}  list of files (ontop of basedir) 
72      *
73      *
74      *
75      */
76     
77     
78     function packScript($basedir, $files,  $output_url)
79     {
80         // this outputs <script tags..>
81         // either for just the original files,
82         // or the compressed version.
83         // first expand files..
84         
85         $arfiles = array();
86         $ofiles = array();
87         foreach($files as $f) {
88              if (!file_exists($basedir .'/' .$f)) {
89                 continue;
90             }
91             if (!is_dir($basedir .'/' .$f)) {
92                 
93                 $arfiles[$basedir .'/' .$f] = filemtime($basedir .'/' .$f);
94                  $ofiles[] = $f;
95                 continue;
96             }
97             foreach(glob($basedir .'/' .$f.'/*.js') as $fx) {
98                 $arfiles[$fx] = filemtime($fx);
99                 $ofiles [] = $f . '/'. basename($fx);
100             }
101         }
102         
103         $output = md5(serialize($arfiles)) .'.js';
104         
105         if (!file_exists($basedir.'/_cache_/'.$output)) {
106             $this->pack($arfiles,$basedir.'/_cache_/'.$output);
107         }
108         
109         if (file_exists($basedir.'/_cache_/'.$output)) {
110             
111             echo '<script type="text/javascript" src="'.$output_url.'/_cache_/'. $output.'"></script>';
112             return;
113         }
114         foreach($ofiles as $f) {
115             echo '<script type="text/javascript" src="'.$output_url.'/'.$f.'"></script>'."\n";
116             
117         }
118          
119         
120     }
121     function packCss($basedir, $files,   $output_url)
122     {
123         // this outputs <script tags..>
124         // either for just the original files,
125         // or the compressed version.
126         // first expand files..
127         
128         $arfiles = array();
129         $ofiles = array();
130         //print_R($files);
131         foreach($files as $f) {
132             if (!file_exists($basedir .'/' .$f)) {
133                 continue;
134             }
135             if (!is_dir($basedir .'/' .$f)) {
136                 $arfiles[$basedir .'/' .$f] = filemtime($basedir .'/' .$f);
137                 $ofiles[] = $f;
138                 continue;
139             }
140             foreach(glob($basedir .'/' .$f.'/*.css') as $fx) {
141                 $arfiles[$fx] = filemtime($fx);
142                 $ofiles [] = $f . '/'. basename($fx);
143             }
144         }
145         
146         $output = md5(serialize($arfiles)) .'.css';
147         
148         if (!file_exists($basedir.'/_cache_/'.$output)) {
149             $this->packCssCore($arfiles,$basedir.'/_cache_/'.$output);
150         }
151         //var_dump()$basedir. '/_cache_/'.$output);
152         if (file_exists($basedir. '/_cache_/'.$output)) {
153             echo '<link type="text/css" rel="stylesheet" media="screen" href="'.$output_url. '/_cache_/'. $output.'" />';
154             return;
155         }
156         foreach($ofiles as $f ) {
157             echo '<link type="text/css" rel="stylesheet" media="screen" href="'.$output_url.'/'.$f.'" />'."\n";
158              
159         }
160          
161         
162     }
163      /**
164      * wrapper arroudn packer...
165      * @param {Array} map of $files => filemtime the files to pack
166      * @param {String} $output name fo file to output
167      *
168      */
169     function packCssCore($files, $output)
170     {
171         
172          
173         $o = HTML_FlexyFramework::get()->Pman_Core;
174         
175         if (empty($o['cssminify']) || !file_exists($o['cssminify'])) {
176             echo '<!-- jspacker not set -->';
177             return false;
178         }
179         require_once 'System.php';
180         $seed= System::which('seed');
181         if (!$seed) {
182             echo '<!-- seed not installed -->';
183             return false;
184             
185         }
186         $targetm = file_exists($output) ? filemtime($output) : 0;
187         $max = 0;
188         $ofiles = array();
189         foreach($files as $f => $mt) {
190             $max = max($max,$mt);
191             $ofiles[] = escapeshellarg($f);
192         }
193         if ($max < $targetm)  {
194             return true;
195         }
196         if (!file_exists(dirname($output))) {
197             mkdir(dirname($output), 0755, true);
198         }
199         $eoutput = escapeshellarg($output);
200         $cmd = "$seed {$o['cssminify']}  $eoutput " . implode($ofiles, ' ');
201         //echo "<PRE>$cmd\n"; echo `$cmd`; exit;
202         `$cmd`;
203         
204         
205         // we should do more checking.. return val etc..
206         if (file_exists($output) && ($max < filemtime($output) ) ) {
207             return true;
208         }
209         return false;
210         
211     }
212     /**
213      * wrapper arroudn packer...
214      * @param {Array} map of $files => filemtime the files to pack
215      * @param {String} $output name fo file to output
216      *
217      */
218     function pack($files, $output)
219     {
220         
221          
222         $o = HTML_FlexyFramework::get()->Pman_Core;
223         
224         if (empty($o['jspacker']) || !file_exists($o['jspacker'].'/pack.js')) {
225             echo '<!-- JS COMPILE ERROR: option: Pman_Core[jspacker] not set -->';
226             return false;
227             
228         }
229         require_once 'System.php';
230         $seed= System::which('seed');
231         if (!$seed) {
232             echo '<!-- JS COMPILE ERROR: seed not installed -->';
233             return false;
234             
235         }
236         $targetm = file_exists($output) ? filemtime($output) : 0;
237         $max = 0;
238         $ofiles = array();
239         foreach($files as $f => $mt) {
240             $max = max($max,$mt);
241             $ofiles[] = escapeshellarg($f);
242         }
243         if ($max < $targetm)  {
244             return true;
245         }
246         //var_dump($output);
247         if (!file_exists(dirname($output))) {
248             mkdir(dirname($output), 0755, true);
249         }
250         sort($ofiles);
251         $eoutput = escapeshellarg($output);
252         $cmd = "$seed {$o['jspacker']}/pack.js  -o $eoutput " . implode($ofiles, ' ') . ' 2>&1';
253         //echo "<PRE>$cmd\n";
254         //echo `$cmd`;
255         $res = `$cmd`;
256         //exit;
257         
258         // since this only appears when we change.. it's ok to dump it out..
259           echo "<!-- Compiled javascript
260           
261             " . htmlspecialchars($cmd) . "
262             
263             " . htmlspecialchars($res) . "
264             
265             -->";
266             
267         // we should do more checking.. return val etc..
268         if (file_exists($output) && ($max < filemtime($output) ) ) {
269             
270             return true;
271         }
272         echo "<!-- JS COMPILE ERROR: packed file did not exist  -->";
273         return false;
274         
275     }
276     
277     
278     /***
279      * build:
280      *
281      * @param {String} $proj name of Pman component to build
282      * runs pack.js -m {proj} -a $src/*.js
283      * 
284      *
285      */
286       
287     function build($proj) 
288     {
289         echo "Building $proj\n";
290        // var_dump($proj);
291         if (empty($proj)) {
292             $this->err = "no project";
293             if ($this->cli) echo $this->err;
294             return;
295         }
296         // first item in path is always the app start directory..
297         $src= array_shift(explode(PATH_SEPARATOR, ini_get('include_path'))) .'/Pman/'. $proj;
298         
299         
300         
301        //$tmp = ini_get('session.save_path')."/{$proj}_". posix_getuid(). '_'.md5($src);
302         
303         
304         require_once 'System.php';
305         $seed= System::which('seed');
306         if (!$seed) {
307             $this->err ="no seed installed";
308             if ($this->cli) echo $this->err;
309             return false;
310         }
311         
312         $o = HTML_FlexyFramework::get()->Pman_Core;
313         
314         if (empty($o['jspacker']) || !file_exists($o['jspacker'].'/pack.js')) {
315             $this->err ="no jstoolkit path set [Pman_Core][jspacker] to the
316                     introspection documentation directory where pack.js is located.";
317             if ($this->cli) echo $this->err;
318             return false;
319         }  
320         
321         // should we be more specirfic!??!?!?
322          
323         $cmd = "$seed {$o['jspacker']}/pack.js -m $proj  -a  $src/*.js";
324         echo "$cmd\n";
325         passthru($cmd);
326         // technically we should trash old compiled files.. 
327         // or we move towards a 'cache in session directory model..'
328         
329         
330         /*
331         
332         $ret = $tmp . '/'. $proj . '.js';
333         if ($this->cli) {
334             echo "BUILT:  $ret \n";
335             exit;
336         }
337         return $ret;
338         */
339         
340     }
341     // link {PROJECT}/compiled/{PROJECT}.js to _compiled_ folder to make it live.
342         
343     function install($proj) 
344     {
345        
346         $base = dirname(realpath($_SERVER["SCRIPT_FILENAME"]));
347         if (empty($base )) {
348             $base = getcwd();
349         }
350         var_dump($base .'/Pman/'. $proj.'/compiled/'.$proj .'.js');
351         $src =  realpath($base .'/Pman/'. $proj.'/compiled/'.$proj .'.js');
352         if (!$src) {
353             echo "SKIP : no js file $proj\n";
354             return;
355         }
356         if (!file_exists("$base/_compiled_")) {
357             mkdir ("$base/_compiled_", 0755, true);
358         }
359         $target = "$base/_compiled_/".$proj .'.js';
360         print_R(array($src,$target));
361         if (file_exists($target)) {
362             return; // already installed.
363         }
364         
365         symlink($src, $target);
366         
367         
368     }
369     
370     function gatherProjects() {
371         $src= array_shift(explode(PATH_SEPARATOR, ini_get('include_path'))) .'/Pman';
372         
373         $ret = array();
374         foreach(scandir($src) as $f) {
375             if (!strlen($f) || $f[0] == '.') {
376                 continue;
377             }
378             
379             $fp = "$src/$f";
380             if (!is_dir($fp)) {
381                 continue;
382             }
383             if ($f == 'templates') {
384                 continue;
385             }
386             $ret[] = $f;
387             
388             
389         }
390         return $ret;
391     }
392 }
393