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