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)
204 echo '<!-- JSCOMPILE - should not be used for CSS packing ?? -->';
206 // if we did.. use this?
208 //require_once 'HTML/CSS/Minify.php';
209 //$x = new HTML_CSS_Minify(substr($relpath,0,-1), $dir, $relfiles);
211 // file_put_contents($compiledir.'/'.$output , $x->minify( $this->baseURL.$asset));
216 * wrapper arround packer...
217 * uses the translation module & puts index in __tra
219 * @param {Array} map of $files => filemtime the files to pack
220 * @param {String} $output name fo file to output
224 function pack($files, $output, $translation_base=false)
231 // if packer is running, then dont compile - just output onebyone...
234 require_once 'System.php';
235 $packer = System::which('roojspacker');
239 echo '<!-- roojspacker is not installed -->';
243 $targetm = file_exists($output) && filesize($output) ? filemtime($output) : 0;
246 foreach($files as $f => $mt) {
247 $max = max($max,$mt);
248 $ofiles[] = escapeshellarg($f);
250 if ($max < $targetm) {
251 echo '<!-- use cached compile. -->';
257 $pg = System::which('pgrep');
259 echo '<!-- install pgrep -->';
263 $cmd = "$pg roojspacker";
266 if (strlen($out) > 0) {
267 echo '<!-- onther process is compiling compile. -->';
272 if (file_exists($output)) {
277 if (!file_exists(dirname($output))) {
278 mkdir(dirname($output), 0755, true);
281 usort($ofiles, function($a,$b) { return strlen($a) > strlen($b) ? 1 : -1; });
283 //$eoutput = " -k -o " . escapeshellarg($output) ; // with whitespace..
284 $eoutput = " -t " . escapeshellarg($output) ;
286 // no support for translation any more?
287 //if ( $translation_base) {
288 // $toutput = " -t ". escapeshellarg(preg_replace('/\.js$/', '.__translation__.js', $output)) .
289 // " -p " . escapeshellarg($translation_base) ;//." -k "; // this kills the compression.
294 $cmd = "$packer $eoutput -f " . implode(' -f ', $ofiles) . ' 2>&1';
295 //echo "<PRE>$cmd\n";
298 echo "<!-- Compile javascript
300 " . htmlspecialchars($cmd) . "
308 file_put_contents($output.'.log', $cmd."\n\n". $res);
309 // since this only appears when we change.. it's ok to dump it out..
310 echo "<!-- Compiled javascript
311 " . htmlspecialchars($res) . "
314 // we should do more checking.. return val etc..
315 if (file_exists($output) && filesize($output) && ($max < filemtime($output) ) ) {
316 echo "<!-- file looks like its been generated -->\n";
319 echo '<script type="text/javascript"> alert('. json_encode("Error: Javascript Compile failed\n" . $res) .');</script>';
322 echo "<!-- JS COMPILE ERROR: packed file did not exist -->";
328 function packIsRunning()
330 require_once 'System.php';
336 // depricated verison using seed.
337 function packSeed($files, $output, $translation_base=false)
341 $o = HTML_FlexyFramework::get()->Pman_Core;
343 if (empty($o['packseed']) || !file_exists($o['jspacker'].'/pack.js')) {
344 echo '<!-- JS COMPILE ERROR: option: Pman_Core[jspacker] not set to directory -->';
348 require_once 'System.php';
349 $seed= System::which('seed');
350 $gjs = System::which('gjs');
352 if (!$seed && !$gjs) {
353 echo '<!-- seed or gjs are not installed -->';
357 $targetm = file_exists($output) && filesize($output) ? filemtime($output) : 0;
360 foreach($files as $f => $mt) {
361 $max = max($max,$mt);
362 $ofiles[] = escapeshellarg($f);
364 if ($max < $targetm) {
365 echo '<!-- use cached compile. -->';
369 if (!file_exists(dirname($output))) {
370 mkdir(dirname($output), 0755, true);
372 $lsort = create_function('$a,$b','return strlen($a) > strlen($b) ? 1 : -1;');
373 usort($ofiles, $lsort);
375 //$eoutput = " -k -o " . escapeshellarg($output) ; // with whitespace..
376 $eoutput = " -o " . escapeshellarg($output) ;
378 if ( $translation_base) {
379 $toutput = " -t ". escapeshellarg(preg_replace('/\.js$/', '.__translation__.js', $output)) .
380 " -p " . escapeshellarg($translation_base) ;//." -k "; // this kills the compression.
386 "$seed {$o['packseed']}/pack.js " :
387 "$gjs -I {$o['packseed']} -I {$o['packseed']}/JSDOC {$o['packseed']}/pack.js -- -- "
389 ) . " $eoutput $toutput " . implode($ofiles, ' ') . ' 2>&1';
390 //echo "<PRE>$cmd\n";
393 echo "<!-- Compile javascript
395 " . htmlspecialchars($cmd) . "
403 file_put_contents($output.'.log', $cmd."\n\n". $res);
404 // since this only appears when we change.. it's ok to dump it out..
405 echo "<!-- Compiled javascript
406 " . htmlspecialchars($res) . "
409 // we should do more checking.. return val etc..
410 if (file_exists($output) && ($max < filemtime($output) ) ) {
416 echo "\n<!-- JS COMPILE ERROR: packed file did not exist -->\n";
425 * @param {String} $proj name of Pman component to build
426 * runs pack.js -m {proj} -a $src/*.js
431 function build($proj)
433 echo "Building $proj\n";
436 $this->err = "no project";
437 if ($this->cli) echo $this->err;
440 // first item in path is always the app start directory..
441 $src= array_shift(explode(PATH_SEPARATOR, ini_get('include_path'))) .'/Pman/'. $proj;
445 //$tmp = ini_get('session.save_path')."/{$proj}_". posix_getuid(). '_'.md5($src);
448 require_once 'System.php';
449 $seed= System::which('seed');
451 $this->err ="no seed installed";
452 if ($this->cli) echo $this->err;
456 $o = HTML_FlexyFramework::get()->Pman_Core;
458 if (empty($o['packseed']) || !file_exists($o['packseed'].'/pack.js')) {
459 $this->err ="no jstoolkit path set [Pman_Core][packseed] to the
460 introspection documentation directory where pack.js is located.";
461 if ($this->cli) echo $this->err;
465 // should we be more specirfic!??!?!?
467 $cmd = "$seed {$o['packseed']}/pack.js -m $proj -a $src/*.js";
470 // technically we should trash old compiled files..
471 // or we move towards a 'cache in session directory model..'
476 $ret = $tmp . '/'. $proj . '.js';
478 echo "BUILT: $ret \n";
485 // link {PROJECT}/compiled/{PROJECT}.js to _compiled_ folder to make it live.
487 function install($proj)
490 $base = dirname(realpath($_SERVER["SCRIPT_FILENAME"]));
494 var_dump($base .'/Pman/'. $proj.'/compiled/'.$proj .'.js');
495 $src = realpath($base .'/Pman/'. $proj.'/compiled/'.$proj .'.js');
497 echo "SKIP : no js file $proj\n";
500 if (!file_exists("$base/_compiled_")) {
501 mkdir ("$base/_compiled_", 0755, true);
503 $target = "$base/_compiled_/".$proj .'.js';
504 print_R(array($src,$target));
505 if (file_exists($target)) {
506 return; // already installed.
509 symlink($src, $target);
514 function gatherProjects() {
515 $src= array_shift(explode(PATH_SEPARATOR, ini_get('include_path'))) .'/Pman';
518 foreach(scandir($src) as $f) {
519 if (!strlen($f) || $f[0] == '.') {
527 if ($f == 'templates') {