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