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