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