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