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