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