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