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