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