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
21 Runs the javascript compiler - merging all the JS files so the load faster.
22 Note: cfg option Pman_Builder['jspacker'] must be set to location of jstoolkit code
28 // command line only ATM
29 $this->cli = HTML_FlexyFramework::get()->cli;
30 // var_dump($this->cli);
38 function get($proj, $args)
41 die("missing action : eg. build or install");
44 if (empty($args[1])) {
46 $ar = $this->gatherProjects();
47 echo "SELECT Component to build\n";
51 $ar = array($args[1]);
64 case 'install' : // needed for install on remote sites..
76 * @param {String} basedir absolute path to files
77 * @param {Array} list of files (ontop of basedir)
78 * @param {String} output url (path to basedir basically), or false
84 static function jsSort($a,$b)
86 $a = substr($a, 0, -3);
87 $b= substr($b, 0, -3);
91 return ($a > $b) ? +1 : -1;
95 function packScript($basedir, $files, $output_url, $compile=true)
97 // this outputs <script tags..>
98 // either for just the original files,
99 // or the compressed version.
100 // first expand files..
102 echo "<!-- compiling $basedir -->\n";
106 foreach($files as $f) {
107 if (!file_exists($basedir .'/' .$f)) {
110 if (!is_dir($basedir .'/' .$f)) {
112 $arfiles[$basedir .'/' .$f] = filemtime($basedir .'/' .$f);
117 foreach(glob($basedir .'/' .$f.'/*.js') as $fx) {
119 $arfiles[$fx] = filemtime($fx);
120 $ofiles [] = $f . '/'. basename($fx);
125 usort($ofiles,create_function('$a,$b', 'return Pman_Core_JsCompile::jsSort($a,$b);'));
129 $output = md5(serialize($arfiles)) .'.js';
131 if ( $compile && !file_exists($basedir.'/_cache_/'.$output)) {
132 $this->pack($arfiles,$basedir.'/_cache_/'.$output);
135 if ($compile && file_exists($basedir.'/_cache_/'.$output)) {
137 echo '<script type="text/javascript" src="'.$output_url.'/_cache_/'. $output.'"></script>';
140 foreach($ofiles as $f) {
141 echo '<script type="text/javascript" src="'.$output_url.'/'.$f.'"></script>'."\n";
147 function packCss($basedir, $files, $output_url)
149 // this outputs <script tags..>
150 // either for just the original files,
151 // or the compressed version.
152 // first expand files..
157 foreach($files as $f) {
158 if (!file_exists($basedir .'/' .$f)) {
161 if (!is_dir($basedir .'/' .$f)) {
162 $arfiles[$basedir .'/' .$f] = filemtime($basedir .'/' .$f);
166 foreach(glob($basedir .'/' .$f.'/*.css') as $fx) {
167 $arfiles[$fx] = filemtime($fx);
168 $ofiles [] = $f . '/'. basename($fx);
172 $output = md5(serialize($arfiles)) .'.css';
174 if (!file_exists($basedir.'/_cache_/'.$output)) {
175 $this->packCssCore($arfiles,$basedir.'/_cache_/'.$output);
177 //var_dump()$basedir. '/_cache_/'.$output);
178 if (file_exists($basedir. '/_cache_/'.$output)) {
179 echo '<link type="text/css" rel="stylesheet" media="screen" href="'.$output_url. '/_cache_/'. $output.'" />';
182 foreach($ofiles as $f ) {
183 echo '<link type="text/css" rel="stylesheet" media="screen" href="'.$output_url.'/'.$f.'" />'."\n";
190 * wrapper arroudn packer...
191 * @param {Array} map of $files => filemtime the files to pack
192 * @param {String} $output name fo file to output
195 function packCssCore($files, $output)
199 $o = HTML_FlexyFramework::get()->Pman_Core;
201 if (empty($o['cssminify']) || !file_exists($o['cssminify'])) {
202 echo '<!-- cssminify not set -->';
205 require_once 'System.php';
207 $seed= System::which('seed');
208 $gjs = System::which('gjs');
210 if (!$seed && !$gjs) {
211 echo '<!-- seed or gjs are not installed -->';
215 $targetm = file_exists($output) ? filemtime($output) : 0;
218 foreach($files as $f => $mt) {
219 $max = max($max,$mt);
220 $ofiles[] = escapeshellarg($f);
222 if ($max < $targetm) {
225 if (!file_exists(dirname($output))) {
226 mkdir(dirname($output), 0755, true);
228 $eoutput = escapeshellarg($output);
230 ("$seed {$o['cssminify']} $eoutput " . implode($ofiles, ' ')) :
231 ("$gjs {$o['cssminify']} -- -- $eoutput " . implode($ofiles, ' '));
232 //echo "<PRE>$cmd\n"; echo `$cmd`; exit;
236 // we should do more checking.. return val etc..
237 if (file_exists($output) && ($max < filemtime($output) ) ) {
244 * wrapper arround packer...
245 * uses the translation module & puts index in __tra
247 * @param {Array} map of $files => filemtime the files to pack
248 * @param {String} $output name fo file to output
251 function pack($files, $output, $translation_base=false)
255 $o = HTML_FlexyFramework::get()->Pman_Core;
257 if (empty($o['jspacker']) || !file_exists($o['jspacker'].'/pack.js')) {
258 echo '<!-- JS COMPILE ERROR: option: Pman_Core[jspacker] not set to directory -->';
262 require_once 'System.php';
263 $seed= System::which('seed');
264 $gjs = System::which('gjs');
266 if (!$seed && !$gjs) {
267 echo '<!-- seed or gjs are not installed -->';
271 $targetm = file_exists($output) && filesize($output) ? filemtime($output) : 0;
274 foreach($files as $f => $mt) {
275 $max = max($max,$mt);
276 $ofiles[] = escapeshellarg($f);
278 if ($max < $targetm) {
279 echo '<!-- use cached compile. -->';
283 if (!file_exists(dirname($output))) {
284 mkdir(dirname($output), 0755, true);
286 $lsort = create_function('$a,$b','return strlen($a) > strlen($b) ? 1 : -1;');
287 usort($ofiles, $lsort);
289 //$eoutput = " -k -o " . escapeshellarg($output) ; // with whitespace..
290 $eoutput = " -o " . escapeshellarg($output) ;
292 if ( $translation_base) {
293 $toutput = " -t ". escapeshellarg(preg_replace('/\.js$/', '.__translation__.js', $output)) .
294 " -p " . escapeshellarg($translation_base) ;//." -k "; // this kills the compression.
300 "$seed {$o['jspacker']}/pack.js " :
301 "$gjs -I {$o['jspacker']} -I {$o['jspacker']}/JSDOC {$o['jspacker']}/pack.js -- -- "
303 ) . " $eoutput $toutput " . implode($ofiles, ' ') . ' 2>&1';
304 //echo "<PRE>$cmd\n";
307 echo "<!-- Compile javascript
309 " . htmlspecialchars($cmd) . "
317 file_put_contents($output.'.log', $cmd."\n\n". $res);
318 // since this only appears when we change.. it's ok to dump it out..
319 echo "<!-- Compiled javascript
320 " . htmlspecialchars($res) . "
323 // we should do more checking.. return val etc..
324 if (file_exists($output) && ($max < filemtime($output) ) ) {
328 echo "<!-- JS COMPILE ERROR: packed file did not exist -->";
337 * @param {String} $proj name of Pman component to build
338 * runs pack.js -m {proj} -a $src/*.js
343 function build($proj)
345 echo "Building $proj\n";
348 $this->err = "no project";
349 if ($this->cli) echo $this->err;
352 // first item in path is always the app start directory..
353 $src= array_shift(explode(PATH_SEPARATOR, ini_get('include_path'))) .'/Pman/'. $proj;
357 //$tmp = ini_get('session.save_path')."/{$proj}_". posix_getuid(). '_'.md5($src);
360 require_once 'System.php';
361 $seed= System::which('seed');
363 $this->err ="no seed installed";
364 if ($this->cli) echo $this->err;
368 $o = HTML_FlexyFramework::get()->Pman_Core;
370 if (empty($o['jspacker']) || !file_exists($o['jspacker'].'/pack.js')) {
371 $this->err ="no jstoolkit path set [Pman_Core][jspacker] to the
372 introspection documentation directory where pack.js is located.";
373 if ($this->cli) echo $this->err;
377 // should we be more specirfic!??!?!?
379 $cmd = "$seed {$o['jspacker']}/pack.js -m $proj -a $src/*.js";
382 // technically we should trash old compiled files..
383 // or we move towards a 'cache in session directory model..'
388 $ret = $tmp . '/'. $proj . '.js';
390 echo "BUILT: $ret \n";
397 // link {PROJECT}/compiled/{PROJECT}.js to _compiled_ folder to make it live.
399 function install($proj)
402 $base = dirname(realpath($_SERVER["SCRIPT_FILENAME"]));
406 var_dump($base .'/Pman/'. $proj.'/compiled/'.$proj .'.js');
407 $src = realpath($base .'/Pman/'. $proj.'/compiled/'.$proj .'.js');
409 echo "SKIP : no js file $proj\n";
412 if (!file_exists("$base/_compiled_")) {
413 mkdir ("$base/_compiled_", 0755, true);
415 $target = "$base/_compiled_/".$proj .'.js';
416 print_R(array($src,$target));
417 if (file_exists($target)) {
418 return; // already installed.
421 symlink($src, $target);
426 function gatherProjects() {
427 $src= array_shift(explode(PATH_SEPARATOR, ini_get('include_path'))) .'/Pman';
430 foreach(scandir($src) as $f) {
431 if (!strlen($f) || $f[0] == '.') {
439 if ($f == 'templates') {