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