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