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