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