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