4 * wrapper code around js builder...
6 * -- we will use this later to compile on the fly...
8 * -- updated to use roojspacker https://github.com/roojs/roojspacker
12 * $x = new Pman_Core_JsCompile();
13 * $x->pack('/path/to/files/', 'destination')
16 require_once 'Pman.php';
19 class Pman_Core_JsCompile extends Pman
22 static $cli_desc = "Wrapper around Javascript compression tools
23 Runs the javascript compiler - merging all the JS files so the load faster.
24 Note: cfg option Pman_Builder['jspacker'] must be set to location of jstoolkit code
30 // command line only ATM
31 $this->cli = HTML_FlexyFramework::get()->cli;
32 // var_dump($this->cli);
40 function get($proj, $args=array())
43 die("missing action : eg. build or install");
46 if (empty($args[1])) {
48 $ar = $this->gatherProjects();
49 echo "SELECT Component to build\n";
53 $ar = array($args[1]);
66 case 'install' : // needed for install on remote sites..
78 * @param {String} basedir absolute path to files
79 * @param {Array} list of files (ontop of basedir)
80 * @param {String} output url (path to basedir basically), or false
86 static function jsSort($a,$b)
88 $a = substr($a, 0, -3);
89 $b= substr($b, 0, -3);
93 return ($a > $b) ? +1 : -1;
97 function packScript($basedir, $files, $output_url, $compile=true)
99 // this outputs <script tags..>
100 // either for just the original files,
101 // or the compressed version.
102 // first expand files..
104 echo "<!-- compiling $basedir -->\n";
108 foreach($files as $f) {
109 if (!file_exists($basedir .'/' .$f)) {
112 if (!is_dir($basedir .'/' .$f)) {
114 $arfiles[$basedir .'/' .$f] = filemtime($basedir .'/' .$f);
119 foreach(glob($basedir .'/' .$f.'/*.js') as $fx) {
121 $arfiles[$fx] = filemtime($fx);
122 $ofiles [] = $f . '/'. basename($fx);
127 usort($ofiles,function($a,$b) { return Pman_Core_JsCompile::jsSort($a,$b); });
131 $output = md5(serialize($arfiles)) .'.js';
133 if ( $compile && !file_exists($basedir.'/_cache_/'.$output)) {
134 $this->pack($arfiles,$basedir.'/_cache_/'.$output);
137 if ($compile && file_exists($basedir.'/_cache_/'.$output)) {
139 echo '<script type="text/javascript" src="'.$output_url.'/_cache_/'. $output.'"></script>';
142 foreach($ofiles as $f) {
143 echo '<script type="text/javascript" src="'.$output_url.'/'.$f.'"></script>'."\n";
149 // this is depricated... - we can use the pear CSS library for this..
151 function packCss($basedir, $files, $output_url)
153 // this outputs <script tags..>
154 // either for just the original files,
155 // or the compressed version.
156 // first expand files..
161 foreach($files as $f) {
162 if (!file_exists($basedir .'/' .$f)) {
165 if (!is_dir($basedir .'/' .$f)) {
166 $arfiles[$basedir .'/' .$f] = filemtime($basedir .'/' .$f);
170 foreach(glob($basedir .'/' .$f.'/*.css') as $fx) {
171 $arfiles[$fx] = filemtime($fx);
172 $ofiles [] = $f . '/'. basename($fx);
176 $output = md5(serialize($arfiles)) .'.css';
178 if (!file_exists($basedir.'/_cache_/'.$output)) {
179 $this->packCssCore($arfiles,$basedir.'/_cache_/'.$output);
181 //var_dump()$basedir. '/_cache_/'.$output);
182 if (file_exists($basedir. '/_cache_/'.$output)) {
183 echo '<link type="text/css" rel="stylesheet" media="screen" href="'.$output_url. '/_cache_/'. $output.'" />';
186 foreach($ofiles as $f ) {
187 echo '<link type="text/css" rel="stylesheet" media="screen" href="'.$output_url.'/'.$f.'" />'."\n";
194 * wrapper arroudn packer...
195 * @param {Array} map of $files => filemtime the files to pack
196 * @param {String} $output name fo file to output
199 function packCssCore($files, $output)
203 $o = HTML_FlexyFramework::get()->Pman_Core;
205 if (empty($o['cssminify']) || !file_exists($o['cssminify'])) {
206 echo '<!-- cssminify not set -->';
209 require_once 'System.php';
211 $seed= System::which('seed');
212 $gjs = System::which('gjs');
214 if (!$seed && !$gjs) {
215 echo '<!-- seed or gjs are not installed -->';
219 $targetm = file_exists($output) ? filemtime($output) : 0;
222 foreach($files as $f => $mt) {
223 $max = max($max,$mt);
224 $ofiles[] = escapeshellarg($f);
226 if ($max < $targetm) {
229 if (!file_exists(dirname($output))) {
230 mkdir(dirname($output), 0755, true);
232 $eoutput = escapeshellarg($output);
234 ("$seed {$o['cssminify']} $eoutput " . implode($ofiles, ' ')) :
235 ("$gjs {$o['cssminify']} -- -- $eoutput " . implode($ofiles, ' '));
236 //echo "<PRE>$cmd\n"; echo `$cmd`; exit;
240 // we should do more checking.. return val etc..
241 if (file_exists($output) && ($max < filemtime($output) ) ) {
248 * wrapper arround packer...
249 * uses the translation module & puts index in __tra
251 * @param {Array} map of $files => filemtime the files to pack
252 * @param {String} $output name fo file to output
256 function pack($files, $output, $translation_base=false)
263 $o = HTML_FlexyFramework::get()->Pman_Core;
264 if (isset($o['packseed'])) {
265 return $this->packSeed($files,$output,$translation_base);
269 require_once 'System.php';
270 $packer = System::which('roojspacker');
274 echo '<!-- roojspacker is not installed -->';
278 $targetm = file_exists($output) && filesize($output) ? filemtime($output) : 0;
281 foreach($files as $f => $mt) {
282 $max = max($max,$mt);
283 $ofiles[] = escapeshellarg($f);
285 if ($max < $targetm) {
286 echo '<!-- use cached compile. -->';
290 if (file_exists($output)) {
295 if (!file_exists(dirname($output))) {
296 mkdir(dirname($output), 0755, true);
299 usort($ofiles, function($a,$b) { return strlen($a) > strlen($b) ? 1 : -1; });
301 //$eoutput = " -k -o " . escapeshellarg($output) ; // with whitespace..
302 $eoutput = " -t " . escapeshellarg($output) ;
304 // no support for translation any more?
305 //if ( $translation_base) {
306 // $toutput = " -t ". escapeshellarg(preg_replace('/\.js$/', '.__translation__.js', $output)) .
307 // " -p " . escapeshellarg($translation_base) ;//." -k "; // this kills the compression.
312 $cmd = "$packer $eoutput -f " . implode(' -f ', $ofiles) . ' 2>&1';
313 //echo "<PRE>$cmd\n";
316 echo "<!-- Compile javascript
318 " . htmlspecialchars($cmd) . "
326 file_put_contents($output.'.log', $cmd."\n\n". $res);
327 // since this only appears when we change.. it's ok to dump it out..
328 echo "<!-- Compiled javascript
329 " . htmlspecialchars($res) . "
332 // we should do more checking.. return val etc..
333 if (file_exists($output) && filesize($output) && ($max < filemtime($output) ) ) {
334 echo "<!-- file looks like its been generated -->\n";
337 echo '<script type="text/javascript"> alert('. json_encode("Error: Javascript Compile failed\n" . $res) .');</script>';
340 echo "<!-- JS COMPILE ERROR: packed file did not exist -->";
345 // depricated verison using seed.
346 function packSeed($files, $output, $translation_base=false)
350 $o = HTML_FlexyFramework::get()->Pman_Core;
352 if (empty($o['packseed']) || !file_exists($o['jspacker'].'/pack.js')) {
353 echo '<!-- JS COMPILE ERROR: option: Pman_Core[jspacker] not set to directory -->';
357 require_once 'System.php';
358 $seed= System::which('seed');
359 $gjs = System::which('gjs');
361 if (!$seed && !$gjs) {
362 echo '<!-- seed or gjs are not installed -->';
366 $targetm = file_exists($output) && filesize($output) ? filemtime($output) : 0;
369 foreach($files as $f => $mt) {
370 $max = max($max,$mt);
371 $ofiles[] = escapeshellarg($f);
373 if ($max < $targetm) {
374 echo '<!-- use cached compile. -->';
378 if (!file_exists(dirname($output))) {
379 mkdir(dirname($output), 0755, true);
381 $lsort = create_function('$a,$b','return strlen($a) > strlen($b) ? 1 : -1;');
382 usort($ofiles, $lsort);
384 //$eoutput = " -k -o " . escapeshellarg($output) ; // with whitespace..
385 $eoutput = " -o " . escapeshellarg($output) ;
387 if ( $translation_base) {
388 $toutput = " -t ". escapeshellarg(preg_replace('/\.js$/', '.__translation__.js', $output)) .
389 " -p " . escapeshellarg($translation_base) ;//." -k "; // this kills the compression.
395 "$seed {$o['packseed']}/pack.js " :
396 "$gjs -I {$o['packseed']} -I {$o['packseed']}/JSDOC {$o['packseed']}/pack.js -- -- "
398 ) . " $eoutput $toutput " . implode($ofiles, ' ') . ' 2>&1';
399 //echo "<PRE>$cmd\n";
402 echo "<!-- Compile javascript
404 " . htmlspecialchars($cmd) . "
412 file_put_contents($output.'.log', $cmd."\n\n". $res);
413 // since this only appears when we change.. it's ok to dump it out..
414 echo "<!-- Compiled javascript
415 " . htmlspecialchars($res) . "
418 // we should do more checking.. return val etc..
419 if (file_exists($output) && ($max < filemtime($output) ) ) {
425 echo "\n<!-- JS COMPILE ERROR: packed file did not exist -->\n";
434 * @param {String} $proj name of Pman component to build
435 * runs pack.js -m {proj} -a $src/*.js
440 function build($proj)
442 echo "Building $proj\n";
445 $this->err = "no project";
446 if ($this->cli) echo $this->err;
449 // first item in path is always the app start directory..
450 $src= array_shift(explode(PATH_SEPARATOR, ini_get('include_path'))) .'/Pman/'. $proj;
454 //$tmp = ini_get('session.save_path')."/{$proj}_". posix_getuid(). '_'.md5($src);
457 require_once 'System.php';
458 $seed= System::which('seed');
460 $this->err ="no seed installed";
461 if ($this->cli) echo $this->err;
465 $o = HTML_FlexyFramework::get()->Pman_Core;
467 if (empty($o['packseed']) || !file_exists($o['packseed'].'/pack.js')) {
468 $this->err ="no jstoolkit path set [Pman_Core][packseed] to the
469 introspection documentation directory where pack.js is located.";
470 if ($this->cli) echo $this->err;
474 // should we be more specirfic!??!?!?
476 $cmd = "$seed {$o['packseed']}/pack.js -m $proj -a $src/*.js";
479 // technically we should trash old compiled files..
480 // or we move towards a 'cache in session directory model..'
485 $ret = $tmp . '/'. $proj . '.js';
487 echo "BUILT: $ret \n";
494 // link {PROJECT}/compiled/{PROJECT}.js to _compiled_ folder to make it live.
496 function install($proj)
499 $base = dirname(realpath($_SERVER["SCRIPT_FILENAME"]));
503 var_dump($base .'/Pman/'. $proj.'/compiled/'.$proj .'.js');
504 $src = realpath($base .'/Pman/'. $proj.'/compiled/'.$proj .'.js');
506 echo "SKIP : no js file $proj\n";
509 if (!file_exists("$base/_compiled_")) {
510 mkdir ("$base/_compiled_", 0755, true);
512 $target = "$base/_compiled_/".$proj .'.js';
513 print_R(array($src,$target));
514 if (file_exists($target)) {
515 return; // already installed.
518 symlink($src, $target);
523 function gatherProjects() {
524 $src= array_shift(explode(PATH_SEPARATOR, ini_get('include_path'))) .'/Pman';
527 foreach(scandir($src) as $f) {
528 if (!strlen($f) || $f[0] == '.') {
536 if ($f == 'templates') {