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