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