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