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