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 require_once 'Pman.php';
9
10
11 class Pman_Core_JsCompile  extends Pman
12 {
13     var $cli = false;
14     function getAuth()
15     {
16         // command line only ATM
17         $this->cli = HTML_FlexyFramework::get()->cli;
18       //  var_dump($this->cli);
19         if ($this->cli) {
20             return true;
21         }
22         return  false;
23     }
24     
25     
26     function get($proj, $args)
27     {
28         if (empty($args)) {
29             die("missing action : eg. build or install");
30         }
31         // list of projects.
32         if (empty($args[1])) {
33             
34             $ar = $this->gatherProjects();
35              echo "SELECT Component to build\n";
36             print_r($ar);
37             exit;
38         } else {
39             $ar = array($args[1]); 
40             //$ar = $args;
41             //array_shift($ar);
42         }
43         
44         switch ($args[0]) {
45             case 'build':
46              
47                 foreach($ar as $p) {
48                     $this->build($p);
49                     //$this->install($p);
50                 }
51                 break;
52             case 'install' :     // needed for install on remote sites..
53                 die('not yet..');
54                 foreach($ar as $p) {
55                     $this->install($p);
56                 }
57                 break;
58         }
59         exit;
60     }
61     
62     /**
63      * wrapper arroudn packer...
64      *
65      */
66     function pack($files, $output) {
67         
68         $o = HTML_FlexyFramework::get()->Pman_Core;
69         
70         if (empty($o['jspacker']) || !file_exists($o['jspacker'].'/pack.js')) {
71             throw new Exception("no jstoolkit path set [Pman_Core][jspacker] to the
72                     introspection documentation directory where pack.js is located.");
73             
74         }
75         require_once 'System.php';
76         $seed= System::which('seed');
77         if (!$seed) {
78             throw new Exception(" no seed installed");
79         }
80         $max = 0;
81         foreach($files as $i => $f) {
82             $max = max($max,filemtime($f));
83             $files[$i] = escapeshellarg($f);
84         }
85         if ($max < filemtime($output)) {
86             return true;
87         }
88         
89         $eoutput = escapeshellarg($f);
90         $cmd = "$seed {$o['jspacker']}/pack.js  -o $eoutput " . implode($files, ' ');
91         //echo "$cmd\n";
92         `$cmd`;
93         // we should do more checking.. return val etc..
94         if (file_exists($output) && ($max < filemtime($output) ) ) {
95             return true;
96         }
97         
98         
99     }
100     
101     
102     /***
103      * build:
104      *
105      * @param {String} $proj name of Pman component to build
106      * runs pack.js -m {proj} -a $src/*.js
107      * 
108      *
109      */
110       
111     function build($proj) 
112     {
113         echo "Building $proj\n";
114        // var_dump($proj);
115         if (empty($proj)) {
116             $this->err = "no project";
117             if ($this->cli) echo $this->err;
118             return;
119         }
120         // first item in path is always the app start directory..
121         $src= array_shift(explode(PATH_SEPARATOR, ini_get('include_path'))) .'/Pman/'. $proj;
122         
123         
124         
125        //$tmp = ini_get('session.save_path')."/{$proj}_". posix_getuid(). '_'.md5($src);
126         
127         
128         require_once 'System.php';
129         $seed= System::which('seed');
130         if (!$seed) {
131             $this->err ="no seed installed";
132             if ($this->cli) echo $this->err;
133             return false;
134         }
135         
136         $o = HTML_FlexyFramework::get()->Pman_Core;
137         
138         if (empty($o['jspacker']) || !file_exists($o['jspacker'].'/pack.js')) {
139             $this->err ="no jstoolkit path set [Pman_Core][jspacker] to the
140                     introspection documentation directory where pack.js is located.";
141             if ($this->cli) echo $this->err;
142             return false;
143         }  
144         
145         // should we be more specirfic!??!?!?
146          
147         $cmd = "$seed {$o['jspacker']}/pack.js -m $proj  -a  $src/*.js";
148         echo "$cmd\n";
149         passthru($cmd);
150         // technically we should trash old compiled files.. 
151         // or we move towards a 'cache in session directory model..'
152         
153         
154         /*
155         
156         $ret = $tmp . '/'. $proj . '.js';
157         if ($this->cli) {
158             echo "BUILT:  $ret \n";
159             exit;
160         }
161         return $ret;
162         */
163         
164     }
165     // link {PROJECT}/compiled/{PROJECT}.js to _compiled_ folder to make it live.
166         
167     function install($proj) 
168     {
169        
170         $base = dirname(realpath($_SERVER["SCRIPT_FILENAME"]));
171         if (empty($base )) {
172             $base = getcwd();
173         }
174         var_dump($base .'/Pman/'. $proj.'/compiled/'.$proj .'.js');
175         $src =  realpath($base .'/Pman/'. $proj.'/compiled/'.$proj .'.js');
176         if (!$src) {
177             echo "SKIP : no js file $proj\n";
178             return;
179         }
180         if (!file_exists("$base/_compiled_")) {
181             mkdir ("$base/_compiled_", 0755, true);
182         }
183         $target = "$base/_compiled_/".$proj .'.js';
184         print_R(array($src,$target));
185         if (file_exists($target)) {
186             return; // already installed.
187         }
188         
189         symlink($src, $target);
190         
191         
192     }
193     
194     function gatherProjects() {
195         $src= array_shift(explode(PATH_SEPARATOR, ini_get('include_path'))) .'/Pman';
196         
197         $ret = array();
198         foreach(scandir($src) as $f) {
199             if (!strlen($f) || $f[0] == '.') {
200                 continue;
201             }
202             
203             $fp = "$src/$f";
204             if (!is_dir($fp)) {
205                 continue;
206             }
207             if ($f == 'templates') {
208                 continue;
209             }
210             $ret[] = $f;
211             
212             
213         }
214         return $ret;
215     }
216 }
217