4 * wrapper code around js builder...
6 * -- we will use this later to compile on the fly...
10 * $x = new Pman_Core_JsCompile();
11 * $x->pack('/path/to/files/', 'destination')
14 require_once 'Pman.php';
17 class Pman_Core_JsCompile extends Pman
20 static $cli_desc = "Wrapper around Javascript compression tools";
25 // command line only ATM
26 $this->cli = HTML_FlexyFramework::get()->cli;
27 // var_dump($this->cli);
35 function get($proj, $args)
38 die("missing action : eg. build or install");
41 if (empty($args[1])) {
43 $ar = $this->gatherProjects();
44 echo "SELECT Component to build\n";
48 $ar = array($args[1]);
61 case 'install' : // needed for install on remote sites..
73 * @param {String} basedir absolute path to files
74 * @param {Array} list of files (ontop of basedir)
75 * @param {String} output url (path to basedir basically), or false
82 function packScript($basedir, $files, $output_url, $compile=true)
84 // this outputs <script tags..>
85 // either for just the original files,
86 // or the compressed version.
87 // first expand files..
91 foreach($files as $f) {
92 if (!file_exists($basedir .'/' .$f)) {
95 if (!is_dir($basedir .'/' .$f)) {
97 $arfiles[$basedir .'/' .$f] = filemtime($basedir .'/' .$f);
101 foreach(glob($basedir .'/' .$f.'/*.js') as $fx) {
102 $arfiles[$fx] = filemtime($fx);
103 $ofiles [] = $f . '/'. basename($fx);
107 $output = md5(serialize($arfiles)) .'.js';
109 if ( $compile && !file_exists($basedir.'/_cache_/'.$output)) {
110 $this->pack($arfiles,$basedir.'/_cache_/'.$output);
113 if ($compile && file_exists($basedir.'/_cache_/'.$output)) {
115 echo '<script type="text/javascript" src="'.$output_url.'/_cache_/'. $output.'"></script>';
118 foreach($ofiles as $f) {
119 echo '<script type="text/javascript" src="'.$output_url.'/'.$f.'"></script>'."\n";
125 function packCss($basedir, $files, $output_url)
127 // this outputs <script tags..>
128 // either for just the original files,
129 // or the compressed version.
130 // first expand files..
135 foreach($files as $f) {
136 if (!file_exists($basedir .'/' .$f)) {
139 if (!is_dir($basedir .'/' .$f)) {
140 $arfiles[$basedir .'/' .$f] = filemtime($basedir .'/' .$f);
144 foreach(glob($basedir .'/' .$f.'/*.css') as $fx) {
145 $arfiles[$fx] = filemtime($fx);
146 $ofiles [] = $f . '/'. basename($fx);
150 $output = md5(serialize($arfiles)) .'.css';
152 if (!file_exists($basedir.'/_cache_/'.$output)) {
153 $this->packCssCore($arfiles,$basedir.'/_cache_/'.$output);
155 //var_dump()$basedir. '/_cache_/'.$output);
156 if (file_exists($basedir. '/_cache_/'.$output)) {
157 echo '<link type="text/css" rel="stylesheet" media="screen" href="'.$output_url. '/_cache_/'. $output.'" />';
160 foreach($ofiles as $f ) {
161 echo '<link type="text/css" rel="stylesheet" media="screen" href="'.$output_url.'/'.$f.'" />'."\n";
168 * wrapper arroudn packer...
169 * @param {Array} map of $files => filemtime the files to pack
170 * @param {String} $output name fo file to output
173 function packCssCore($files, $output)
177 $o = HTML_FlexyFramework::get()->Pman_Core;
179 if (empty($o['cssminify']) || !file_exists($o['cssminify'])) {
180 echo '<!-- cssminify not set -->';
183 require_once 'System.php';
184 $seed= System::which('seed');
186 echo '<!-- seed not installed -->';
190 $targetm = file_exists($output) ? filemtime($output) : 0;
193 foreach($files as $f => $mt) {
194 $max = max($max,$mt);
195 $ofiles[] = escapeshellarg($f);
197 if ($max < $targetm) {
200 if (!file_exists(dirname($output))) {
201 mkdir(dirname($output), 0755, true);
203 $eoutput = escapeshellarg($output);
204 $cmd = "$seed {$o['cssminify']} $eoutput " . implode($ofiles, ' ');
205 //echo "<PRE>$cmd\n"; echo `$cmd`; exit;
209 // we should do more checking.. return val etc..
210 if (file_exists($output) && ($max < filemtime($output) ) ) {
217 * wrapper arroudn packer...
218 * @param {Array} map of $files => filemtime the files to pack
219 * @param {String} $output name fo file to output
222 function pack($files, $output)
226 $o = HTML_FlexyFramework::get()->Pman_Core;
228 if (empty($o['jspacker']) || !file_exists($o['jspacker'].'/pack.js')) {
229 echo '<!-- JS COMPILE ERROR: option: Pman_Core[jspacker] not set to directory -->';
233 require_once 'System.php';
234 $seed= System::which('seed');
236 echo '<!-- JS COMPILE ERROR: seed not installed -->';
240 $targetm = file_exists($output) ? filemtime($output) : 0;
243 foreach($files as $f => $mt) {
244 $max = max($max,$mt);
245 $ofiles[] = escapeshellarg($f);
247 if ($max < $targetm) {
251 if (!file_exists(dirname($output))) {
252 mkdir(dirname($output), 0755, true);
254 $lsort = create_function('$a,$b','return strlen($a) > strlen($b) ? 1 : -1;');
255 usort($ofiles, $lsort);
257 $eoutput = escapeshellarg($output);
258 $cmd = "$seed {$o['jspacker']}/pack.js -o $eoutput " . implode($ofiles, ' ') . ' 2>&1';
259 //echo "<PRE>$cmd\n";
262 echo "<!-- Compile javascript
264 " . htmlspecialchars($cmd) . "
272 file_put_contents($output.'.log'. $cmd."\n\n". $res);
273 // since this only appears when we change.. it's ok to dump it out..
274 echo "<!-- Compiled javascript
275 " . htmlspecialchars($res) . "
278 // we should do more checking.. return val etc..
279 if (file_exists($output) && ($max < filemtime($output) ) ) {
283 echo "<!-- JS COMPILE ERROR: packed file did not exist -->";
292 * @param {String} $proj name of Pman component to build
293 * runs pack.js -m {proj} -a $src/*.js
298 function build($proj)
300 echo "Building $proj\n";
303 $this->err = "no project";
304 if ($this->cli) echo $this->err;
307 // first item in path is always the app start directory..
308 $src= array_shift(explode(PATH_SEPARATOR, ini_get('include_path'))) .'/Pman/'. $proj;
312 //$tmp = ini_get('session.save_path')."/{$proj}_". posix_getuid(). '_'.md5($src);
315 require_once 'System.php';
316 $seed= System::which('seed');
318 $this->err ="no seed installed";
319 if ($this->cli) echo $this->err;
323 $o = HTML_FlexyFramework::get()->Pman_Core;
325 if (empty($o['jspacker']) || !file_exists($o['jspacker'].'/pack.js')) {
326 $this->err ="no jstoolkit path set [Pman_Core][jspacker] to the
327 introspection documentation directory where pack.js is located.";
328 if ($this->cli) echo $this->err;
332 // should we be more specirfic!??!?!?
334 $cmd = "$seed {$o['jspacker']}/pack.js -m $proj -a $src/*.js";
337 // technically we should trash old compiled files..
338 // or we move towards a 'cache in session directory model..'
343 $ret = $tmp . '/'. $proj . '.js';
345 echo "BUILT: $ret \n";
352 // link {PROJECT}/compiled/{PROJECT}.js to _compiled_ folder to make it live.
354 function install($proj)
357 $base = dirname(realpath($_SERVER["SCRIPT_FILENAME"]));
361 var_dump($base .'/Pman/'. $proj.'/compiled/'.$proj .'.js');
362 $src = realpath($base .'/Pman/'. $proj.'/compiled/'.$proj .'.js');
364 echo "SKIP : no js file $proj\n";
367 if (!file_exists("$base/_compiled_")) {
368 mkdir ("$base/_compiled_", 0755, true);
370 $target = "$base/_compiled_/".$proj .'.js';
371 print_R(array($src,$target));
372 if (file_exists($target)) {
373 return; // already installed.
376 symlink($src, $target);
381 function gatherProjects() {
382 $src= array_shift(explode(PATH_SEPARATOR, ini_get('include_path'))) .'/Pman';
385 foreach(scandir($src) as $f) {
386 if (!strlen($f) || $f[0] == '.') {
394 if ($f == 'templates') {