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