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.'/'.$output)) {
91             $this->pack($arfiles,$output_path.'/'.$output);
92         }
93         
94         if (file_exists($output_path.'/'.$output)) {
95             
96             echo '<script type="text/javascript" src=">
97             
98             echo "output compressed..";
99             exit;
100         }
101         echo "output original..";
102         exit;
103         
104         
105         
106         
107         
108     }
109     
110     /**
111      * wrapper arroudn packer...
112      * @param {Array} map of $files => filemtime the files to pack
113      * @param {String} $output name fo file to output
114      *
115      */
116     function pack($files, $output)
117     {
118         
119          
120         $o = HTML_FlexyFramework::get()->Pman_Core;
121         
122         if (empty($o['jspacker']) || !file_exists($o['jspacker'].'/pack.js')) {
123             echo '<!-- jspacker not set -->';
124             return false;
125             
126         }
127         require_once 'System.php';
128         $seed= System::which('seed');
129         if (!$seed) {
130             echo '<!-- seed not installed -->';
131             return false;
132             
133         }
134         $targetm = file_exists($output) ? filemtime($output) : 0;
135         $max = 0;
136         $ofiles = array();
137         foreach($files as $f => $mt) {
138             $max = max($max,$mt);
139             $ofiles[] = escapeshellarg($f);
140         }
141         if ($max < $targetm)  {
142             return true;
143         }
144         if (!file_exists(dirname($output))) {
145             mkdir(dirname($output), 0755, true);
146         }
147         $eoutput = escapeshellarg($output);
148         $cmd = "$seed {$o['jspacker']}/pack.js  -o $eoutput " . implode($ofiles, ' ');
149         //echo "<PRE>$cmd\n";
150         //echo `$cmd`;
151         `$cmd`;
152         
153         
154         // we should do more checking.. return val etc..
155         if (file_exists($output) && ($max < filemtime($output) ) ) {
156             return true;
157         }
158         return false;
159         
160     }
161     
162     
163     /***
164      * build:
165      *
166      * @param {String} $proj name of Pman component to build
167      * runs pack.js -m {proj} -a $src/*.js
168      * 
169      *
170      */
171       
172     function build($proj) 
173     {
174         echo "Building $proj\n";
175        // var_dump($proj);
176         if (empty($proj)) {
177             $this->err = "no project";
178             if ($this->cli) echo $this->err;
179             return;
180         }
181         // first item in path is always the app start directory..
182         $src= array_shift(explode(PATH_SEPARATOR, ini_get('include_path'))) .'/Pman/'. $proj;
183         
184         
185         
186        //$tmp = ini_get('session.save_path')."/{$proj}_". posix_getuid(). '_'.md5($src);
187         
188         
189         require_once 'System.php';
190         $seed= System::which('seed');
191         if (!$seed) {
192             $this->err ="no seed installed";
193             if ($this->cli) echo $this->err;
194             return false;
195         }
196         
197         $o = HTML_FlexyFramework::get()->Pman_Core;
198         
199         if (empty($o['jspacker']) || !file_exists($o['jspacker'].'/pack.js')) {
200             $this->err ="no jstoolkit path set [Pman_Core][jspacker] to the
201                     introspection documentation directory where pack.js is located.";
202             if ($this->cli) echo $this->err;
203             return false;
204         }  
205         
206         // should we be more specirfic!??!?!?
207          
208         $cmd = "$seed {$o['jspacker']}/pack.js -m $proj  -a  $src/*.js";
209         echo "$cmd\n";
210         passthru($cmd);
211         // technically we should trash old compiled files.. 
212         // or we move towards a 'cache in session directory model..'
213         
214         
215         /*
216         
217         $ret = $tmp . '/'. $proj . '.js';
218         if ($this->cli) {
219             echo "BUILT:  $ret \n";
220             exit;
221         }
222         return $ret;
223         */
224         
225     }
226     // link {PROJECT}/compiled/{PROJECT}.js to _compiled_ folder to make it live.
227         
228     function install($proj) 
229     {
230        
231         $base = dirname(realpath($_SERVER["SCRIPT_FILENAME"]));
232         if (empty($base )) {
233             $base = getcwd();
234         }
235         var_dump($base .'/Pman/'. $proj.'/compiled/'.$proj .'.js');
236         $src =  realpath($base .'/Pman/'. $proj.'/compiled/'.$proj .'.js');
237         if (!$src) {
238             echo "SKIP : no js file $proj\n";
239             return;
240         }
241         if (!file_exists("$base/_compiled_")) {
242             mkdir ("$base/_compiled_", 0755, true);
243         }
244         $target = "$base/_compiled_/".$proj .'.js';
245         print_R(array($src,$target));
246         if (file_exists($target)) {
247             return; // already installed.
248         }
249         
250         symlink($src, $target);
251         
252         
253     }
254     
255     function gatherProjects() {
256         $src= array_shift(explode(PATH_SEPARATOR, ini_get('include_path'))) .'/Pman';
257         
258         $ret = array();
259         foreach(scandir($src) as $f) {
260             if (!strlen($f) || $f[0] == '.') {
261                 continue;
262             }
263             
264             $fp = "$src/$f";
265             if (!is_dir($fp)) {
266                 continue;
267             }
268             if ($f == 'templates') {
269                 continue;
270             }
271             $ret[] = $f;
272             
273             
274         }
275         return $ret;
276     }
277 }
278