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)
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,create_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(dirname($output))) {
291 mkdir(dirname($output), 0755, true);
293 $lsort = create_function('$a,$b','return strlen($a) > strlen($b) ? 1 : -1;');
294 usort($ofiles, $lsort);
296 //$eoutput = " -k -o " . escapeshellarg($output) ; // with whitespace..
297 $eoutput = " -t " . escapeshellarg($output) ;
299 // no support for translation any more?
300 //if ( $translation_base) {
301 // $toutput = " -t ". escapeshellarg(preg_replace('/\.js$/', '.__translation__.js', $output)) .
302 // " -p " . escapeshellarg($translation_base) ;//." -k "; // this kills the compression.
307 $cmd = "$packer $eoutput -f " . implode($ofiles, ' -f ') . ' 2>&1';
308 //echo "<PRE>$cmd\n";
311 echo "<!-- Compile javascript
313 " . htmlspecialchars($cmd) . "
321 file_put_contents($output.'.log', $cmd."\n\n". $res);
322 // since this only appears when we change.. it's ok to dump it out..
323 echo "<!-- Compiled javascript
324 " . htmlspecialchars($res) . "
327 // we should do more checking.. return val etc..
328 if (file_exists($output) && ($max < filemtime($output) ) ) {
332 echo "<!-- JS COMPILE ERROR: packed file did not exist -->";
337 // depricated verison using seed.
338 function packSeed($files, $output, $translation_base=false)
342 $o = HTML_FlexyFramework::get()->Pman_Core;
344 if (empty($o['packseed']) || !file_exists($o['jspacker'].'/pack.js')) {
345 echo '<!-- JS COMPILE ERROR: option: Pman_Core[jspacker] not set to directory -->';
349 require_once 'System.php';
350 $seed= System::which('seed');
351 $gjs = System::which('gjs');
353 if (!$seed && !$gjs) {
354 echo '<!-- seed or gjs are not installed -->';
358 $targetm = file_exists($output) && filesize($output) ? filemtime($output) : 0;
361 foreach($files as $f => $mt) {
362 $max = max($max,$mt);
363 $ofiles[] = escapeshellarg($f);
365 if ($max < $targetm) {
366 echo '<!-- use cached compile. -->';
370 if (!file_exists(dirname($output))) {
371 mkdir(dirname($output), 0755, true);
373 $lsort = create_function('$a,$b','return strlen($a) > strlen($b) ? 1 : -1;');
374 usort($ofiles, $lsort);
376 //$eoutput = " -k -o " . escapeshellarg($output) ; // with whitespace..
377 $eoutput = " -o " . escapeshellarg($output) ;
379 if ( $translation_base) {
380 $toutput = " -t ". escapeshellarg(preg_replace('/\.js$/', '.__translation__.js', $output)) .
381 " -p " . escapeshellarg($translation_base) ;//." -k "; // this kills the compression.
387 "$seed {$o['packseed']}/pack.js " :
388 "$gjs -I {$o['packseed']} -I {$o['packseed']}/JSDOC {$o['packseed']}/pack.js -- -- "
390 ) . " $eoutput $toutput " . implode($ofiles, ' ') . ' 2>&1';
391 //echo "<PRE>$cmd\n";
394 echo "<!-- Compile javascript
396 " . htmlspecialchars($cmd) . "
404 file_put_contents($output.'.log', $cmd."\n\n". $res);
405 // since this only appears when we change.. it's ok to dump it out..
406 echo "<!-- Compiled javascript
407 " . htmlspecialchars($res) . "
410 // we should do more checking.. return val etc..
411 if (file_exists($output) && ($max < filemtime($output) ) ) {
415 echo "<!-- JS COMPILE ERROR: packed file did not exist -->";
424 * @param {String} $proj name of Pman component to build
425 * runs pack.js -m {proj} -a $src/*.js
430 function build($proj)
432 echo "Building $proj\n";
435 $this->err = "no project";
436 if ($this->cli) echo $this->err;
439 // first item in path is always the app start directory..
440 $src= array_shift(explode(PATH_SEPARATOR, ini_get('include_path'))) .'/Pman/'. $proj;
444 //$tmp = ini_get('session.save_path')."/{$proj}_". posix_getuid(). '_'.md5($src);
447 require_once 'System.php';
448 $seed= System::which('seed');
450 $this->err ="no seed installed";
451 if ($this->cli) echo $this->err;
455 $o = HTML_FlexyFramework::get()->Pman_Core;
457 if (empty($o['packseed']) || !file_exists($o['packseed'].'/pack.js')) {
458 $this->err ="no jstoolkit path set [Pman_Core][packseed] to the
459 introspection documentation directory where pack.js is located.";
460 if ($this->cli) echo $this->err;
464 // should we be more specirfic!??!?!?
466 $cmd = "$seed {$o['packseed']}/pack.js -m $proj -a $src/*.js";
469 // technically we should trash old compiled files..
470 // or we move towards a 'cache in session directory model..'
475 $ret = $tmp . '/'. $proj . '.js';
477 echo "BUILT: $ret \n";
484 // link {PROJECT}/compiled/{PROJECT}.js to _compiled_ folder to make it live.
486 function install($proj)
489 $base = dirname(realpath($_SERVER["SCRIPT_FILENAME"]));
493 var_dump($base .'/Pman/'. $proj.'/compiled/'.$proj .'.js');
494 $src = realpath($base .'/Pman/'. $proj.'/compiled/'.$proj .'.js');
496 echo "SKIP : no js file $proj\n";
499 if (!file_exists("$base/_compiled_")) {
500 mkdir ("$base/_compiled_", 0755, true);
502 $target = "$base/_compiled_/".$proj .'.js';
503 print_R(array($src,$target));
504 if (file_exists($target)) {
505 return; // already installed.
508 symlink($src, $target);
513 function gatherProjects() {
514 $src= array_shift(explode(PATH_SEPARATOR, ini_get('include_path'))) .'/Pman';
517 foreach(scandir($src) as $f) {
518 if (!strlen($f) || $f[0] == '.') {
526 if ($f == 'templates') {