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