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