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>';
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 '<!-- jspacker not set -->';
226             return false;
227             
228         }
229         require_once 'System.php';
230         $seed= System::which('seed');
231         if (!$seed) {
232             echo '<!-- 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         // we should do more checking.. return val etc..
259         if (file_exists($output) && ($max < filemtime($output) ) ) {
260             
261             return true;
262         }
263         echo "<!-- packed file did not exist
264             " . htmlspecialchars($res) . "
265             " . htmlspecialchars($cmd) . "
266             -->";
267         return false;
268         
269     }
270     
271     
272     /***
273      * build:
274      *
275      * @param {String} $proj name of Pman component to build
276      * runs pack.js -m {proj} -a $src/*.js
277      * 
278      *
279      */
280       
281     function build($proj) 
282     {
283         echo "Building $proj\n";
284        // var_dump($proj);
285         if (empty($proj)) {
286             $this->err = "no project";
287             if ($this->cli) echo $this->err;
288             return;
289         }
290         // first item in path is always the app start directory..
291         $src= array_shift(explode(PATH_SEPARATOR, ini_get('include_path'))) .'/Pman/'. $proj;
292         
293         
294         
295        //$tmp = ini_get('session.save_path')."/{$proj}_". posix_getuid(). '_'.md5($src);
296         
297         
298         require_once 'System.php';
299         $seed= System::which('seed');
300         if (!$seed) {
301             $this->err ="no seed installed";
302             if ($this->cli) echo $this->err;
303             return false;
304         }
305         
306         $o = HTML_FlexyFramework::get()->Pman_Core;
307         
308         if (empty($o['jspacker']) || !file_exists($o['jspacker'].'/pack.js')) {
309             $this->err ="no jstoolkit path set [Pman_Core][jspacker] to the
310                     introspection documentation directory where pack.js is located.";
311             if ($this->cli) echo $this->err;
312             return false;
313         }  
314         
315         // should we be more specirfic!??!?!?
316          
317         $cmd = "$seed {$o['jspacker']}/pack.js -m $proj  -a  $src/*.js";
318         echo "$cmd\n";
319         passthru($cmd);
320         // technically we should trash old compiled files.. 
321         // or we move towards a 'cache in session directory model..'
322         
323         
324         /*
325         
326         $ret = $tmp . '/'. $proj . '.js';
327         if ($this->cli) {
328             echo "BUILT:  $ret \n";
329             exit;
330         }
331         return $ret;
332         */
333         
334     }
335     // link {PROJECT}/compiled/{PROJECT}.js to _compiled_ folder to make it live.
336         
337     function install($proj) 
338     {
339        
340         $base = dirname(realpath($_SERVER["SCRIPT_FILENAME"]));
341         if (empty($base )) {
342             $base = getcwd();
343         }
344         var_dump($base .'/Pman/'. $proj.'/compiled/'.$proj .'.js');
345         $src =  realpath($base .'/Pman/'. $proj.'/compiled/'.$proj .'.js');
346         if (!$src) {
347             echo "SKIP : no js file $proj\n";
348             return;
349         }
350         if (!file_exists("$base/_compiled_")) {
351             mkdir ("$base/_compiled_", 0755, true);
352         }
353         $target = "$base/_compiled_/".$proj .'.js';
354         print_R(array($src,$target));
355         if (file_exists($target)) {
356             return; // already installed.
357         }
358         
359         symlink($src, $target);
360         
361         
362     }
363     
364     function gatherProjects() {
365         $src= array_shift(explode(PATH_SEPARATOR, ini_get('include_path'))) .'/Pman';
366         
367         $ret = array();
368         foreach(scandir($src) as $f) {
369             if (!strlen($f) || $f[0] == '.') {
370                 continue;
371             }
372             
373             $fp = "$src/$f";
374             if (!is_dir($fp)) {
375                 continue;
376             }
377             if ($f == 'templates') {
378                 continue;
379             }
380             $ret[] = $f;
381             
382             
383         }
384         return $ret;
385     }
386 }
387