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     function build($proj) 
62     {
63         echo "Building $proj\n";
64        // var_dump($proj);
65         if (empty($proj)) {
66             $this->err = "no project";
67             if ($this->cli) echo $this->err;
68             return;
69         }
70         
71         
72         $src = realpath(dirname(__FILE__).'/../'. $proj);
73         
74         
75         $tmp = ini_get('session.save_path')."/{$proj}_". posix_getuid(). '_'.md5($src);
76         
77         
78         require_once 'System.php';
79         $seed= System::which('seed');
80         if (!$seed) {
81             $this->err ="no seed installed";
82             if ($this->cli) echo $this->err;
83             return false;
84         }
85         
86         $o = PEAR::getStaticProperty('Pman_Core','options');
87         if (empty($o['jspacker']) || !file_exists($o['jspacker'].'/pack.js')) {
88             $this->err ="no jstoolkit path set [Pman_Core][jspacker] to the introspection documentation directory where pack.js is located.";
89             if ($this->cli) echo $this->err;
90             return false;
91         }  
92         
93         // should we be more specirfic!??!?!?
94         
95         $buildjs = 
96         $cmd = "$seed {$o['jspacker']}/pack.js -m$proj  -a  $src/*.js -o $tmp";
97         echo $cmd;
98         passthru($cmd);
99         
100         
101         exit;
102         // copy into the revision controlled area.
103         
104         
105         
106         
107         
108         
109         
110         $src = realpath(dirname(__FILE__).'/../../_compiled_tmp_/'.$proj .'.js');
111         if (!$src) {
112             return;
113         }
114         $pdir = realpath(dirname(__FILE__).'/../'. $proj);
115         if (!file_exists($pdir.'/compiled')) {
116             mkdir($pdir.'/compiled', 0755, true);
117             
118         }
119         copy($src , $pdir.'/compiled/'. $proj .'.js');
120         
121         // copy the translation strings.
122         $src = realpath(dirname(__FILE__).'/../../_compiled_tmp_/'.$proj .'/build/_translation_.js');
123        // var_dump($src);
124         
125         $pdir = realpath(dirname(__FILE__).'/../'. $proj);
126        
127         copy($src , $pdir.'/compiled/_translation_.js');
128         
129         if ($svn) {
130             $base = getcwd();
131             chdir($pdir);
132             $cmd = "$svn add compiled";
133             `$cmd`;
134             $cmd = "$svn add ". escapeshellarg('compiled/'.$proj .'.js'); 
135             $cmd = "$svn add ". escapeshellarg('compiled/_translation_.js'); 
136             
137             `$cmd`;
138             `$svn commit -m 'update compiled version'`;
139             chdir($base);
140         }
141         
142         
143         
144         /*
145         
146         $ret = $tmp . '/'. $proj . '.js';
147         if ($this->cli) {
148             echo "BUILT:  $ret \n";
149             exit;
150         }
151         return $ret;
152         */
153         
154     }
155     // link {PROJECT}/compiled/{PROJECT}.js to _compiled_ folder to make it live.
156         
157     function install($proj) 
158     {
159        
160         $base = dirname(realpath($_SERVER["SCRIPT_FILENAME"]));
161         if (empty($base )) {
162             $base = getcwd();
163         }
164         var_dump($base .'/Pman/'. $proj.'/compiled/'.$proj .'.js');
165         $src =  realpath($base .'/Pman/'. $proj.'/compiled/'.$proj .'.js');
166         if (!$src) {
167             echo "SKIP : no js file $proj\n";
168             return;
169         }
170         if (!file_exists("$base/_compiled_")) {
171             mkdir ("$base/_compiled_", 0755, true);
172         }
173         $target = "$base/_compiled_/".$proj .'.js';
174         print_R(array($src,$target));
175         if (file_exists($target)) {
176             return; // already installed.
177         }
178         
179         symlink($src, $target);
180         
181         
182     }
183     
184     function gatherProjects() {
185         $src =  realpath(dirname(__FILE__).'/../');
186         $ret = array();
187         foreach(scandir($src) as $f) {
188             if (!strlen($f) || $f[0] == '.') {
189                 continue;
190             }
191             
192             $fp = "$src/$f";
193             if (!is_dir($fp)) {
194                 continue;
195             }
196             if ($f == 'templates') {
197                 continue;
198             }
199             $ret[] = $f;
200             
201             
202         }
203         return $ret;
204     }
205 }
206