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