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