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
85 function packScript($basedir, $files, $output_url, $compile=true)
87 // this outputs <script tags..>
88 // either for just the original files,
89 // or the compressed version.
90 // first expand files..
92 echo "<!-- compiling $basedir -->\n";
96 foreach($files as $f) {
97 if (!file_exists($basedir .'/' .$f)) {
100 if (!is_dir($basedir .'/' .$f)) {
102 $arfiles[$basedir .'/' .$f] = filemtime($basedir .'/' .$f);
107 foreach(glob($basedir .'/' .$f.'/*.js') as $fx) {
109 $arfiles[$fx] = filemtime($fx);
110 $ofiles [] = $f . '/'. basename($fx);
114 usort($ofiles,function($a,$b) {
115 $a = substr($a, 0, -3);
116 $b= substr($b, 0, -3);
120 return ($a > $b) ? +1 : -1;
124 $output = md5(serialize($arfiles)) .'.js';
126 if ( $compile && !file_exists($basedir.'/_cache_/'.$output)) {
127 $this->pack($arfiles,$basedir.'/_cache_/'.$output);
130 if ($compile && file_exists($basedir.'/_cache_/'.$output)) {
132 echo '<script type="text/javascript" src="'.$output_url.'/_cache_/'. $output.'"></script>';
135 foreach($ofiles as $f) {
136 echo '<script type="text/javascript" src="'.$output_url.'/'.$f.'"></script>'."\n";
142 function packCss($basedir, $files, $output_url)
144 // this outputs <script tags..>
145 // either for just the original files,
146 // or the compressed version.
147 // first expand files..
152 foreach($files as $f) {
153 if (!file_exists($basedir .'/' .$f)) {
156 if (!is_dir($basedir .'/' .$f)) {
157 $arfiles[$basedir .'/' .$f] = filemtime($basedir .'/' .$f);
161 foreach(glob($basedir .'/' .$f.'/*.css') as $fx) {
162 $arfiles[$fx] = filemtime($fx);
163 $ofiles [] = $f . '/'. basename($fx);
167 $output = md5(serialize($arfiles)) .'.css';
169 if (!file_exists($basedir.'/_cache_/'.$output)) {
170 $this->packCssCore($arfiles,$basedir.'/_cache_/'.$output);
172 //var_dump()$basedir. '/_cache_/'.$output);
173 if (file_exists($basedir. '/_cache_/'.$output)) {
174 echo '<link type="text/css" rel="stylesheet" media="screen" href="'.$output_url. '/_cache_/'. $output.'" />';
177 foreach($ofiles as $f ) {
178 echo '<link type="text/css" rel="stylesheet" media="screen" href="'.$output_url.'/'.$f.'" />'."\n";
185 * wrapper arroudn packer...
186 * @param {Array} map of $files => filemtime the files to pack
187 * @param {String} $output name fo file to output
190 function packCssCore($files, $output)
194 $o = HTML_FlexyFramework::get()->Pman_Core;
196 if (empty($o['cssminify']) || !file_exists($o['cssminify'])) {
197 echo '<!-- cssminify not set -->';
200 require_once 'System.php';
202 $seed= System::which('seed');
203 $gjs = System::which('gjs');
205 if (!$seed && !$gjs) {
206 echo '<!-- seed or gjs are not installed -->';
210 $targetm = file_exists($output) ? filemtime($output) : 0;
213 foreach($files as $f => $mt) {
214 $max = max($max,$mt);
215 $ofiles[] = escapeshellarg($f);
217 if ($max < $targetm) {
220 if (!file_exists(dirname($output))) {
221 mkdir(dirname($output), 0755, true);
223 $eoutput = escapeshellarg($output);
225 ("$seed {$o['cssminify']} $eoutput " . implode($ofiles, ' ')) :
226 ("$gjs {$o['cssminify']} -- -- $eoutput " . implode($ofiles, ' '));
227 //echo "<PRE>$cmd\n"; echo `$cmd`; exit;
231 // we should do more checking.. return val etc..
232 if (file_exists($output) && ($max < filemtime($output) ) ) {
239 * wrapper arround packer...
240 * uses the translation module & puts index in __tra
242 * @param {Array} map of $files => filemtime the files to pack
243 * @param {String} $output name fo file to output
246 function pack($files, $output, $translation_base=false)
250 $o = HTML_FlexyFramework::get()->Pman_Core;
252 if (empty($o['jspacker']) || !file_exists($o['jspacker'].'/pack.js')) {
253 echo '<!-- JS COMPILE ERROR: option: Pman_Core[jspacker] not set to directory -->';
257 require_once 'System.php';
258 $seed= System::which('seed');
259 $gjs = System::which('gjs');
261 if (!$seed && !$gjs) {
262 echo '<!-- seed or gjs are not installed -->';
266 $targetm = file_exists($output) && filesize($output) ? filemtime($output) : 0;
269 foreach($files as $f => $mt) {
270 $max = max($max,$mt);
271 $ofiles[] = escapeshellarg($f);
273 if ($max < $targetm) {
274 echo '<!-- use cached compile. -->';
278 if (!file_exists(dirname($output))) {
279 mkdir(dirname($output), 0755, true);
281 $lsort = create_function('$a,$b','return strlen($a) > strlen($b) ? 1 : -1;');
282 usort($ofiles, $lsort);
284 //$eoutput = " -k -o " . escapeshellarg($output) ; // with whitespace..
285 $eoutput = " -o " . escapeshellarg($output) ;
287 if ( $translation_base) {
288 $toutput = " -t ". escapeshellarg(preg_replace('/\.js$/', '.__translation__.js', $output)) .
289 " -p " . escapeshellarg($translation_base) ;//." -k "; // this kills the compression.
295 "$seed {$o['jspacker']}/pack.js " :
296 "$gjs -I {$o['jspacker']} -I {$o['jspacker']}/JSDOC {$o['jspacker']}/pack.js -- -- "
298 ) . " $eoutput $toutput " . implode($ofiles, ' ') . ' 2>&1';
299 //echo "<PRE>$cmd\n";
302 echo "<!-- Compile javascript
304 " . htmlspecialchars($cmd) . "
312 file_put_contents($output.'.log', $cmd."\n\n". $res);
313 // since this only appears when we change.. it's ok to dump it out..
314 echo "<!-- Compiled javascript
315 " . htmlspecialchars($res) . "
318 // we should do more checking.. return val etc..
319 if (file_exists($output) && ($max < filemtime($output) ) ) {
323 echo "<!-- JS COMPILE ERROR: packed file did not exist -->";
332 * @param {String} $proj name of Pman component to build
333 * runs pack.js -m {proj} -a $src/*.js
338 function build($proj)
340 echo "Building $proj\n";
343 $this->err = "no project";
344 if ($this->cli) echo $this->err;
347 // first item in path is always the app start directory..
348 $src= array_shift(explode(PATH_SEPARATOR, ini_get('include_path'))) .'/Pman/'. $proj;
352 //$tmp = ini_get('session.save_path')."/{$proj}_". posix_getuid(). '_'.md5($src);
355 require_once 'System.php';
356 $seed= System::which('seed');
358 $this->err ="no seed installed";
359 if ($this->cli) echo $this->err;
363 $o = HTML_FlexyFramework::get()->Pman_Core;
365 if (empty($o['jspacker']) || !file_exists($o['jspacker'].'/pack.js')) {
366 $this->err ="no jstoolkit path set [Pman_Core][jspacker] to the
367 introspection documentation directory where pack.js is located.";
368 if ($this->cli) echo $this->err;
372 // should we be more specirfic!??!?!?
374 $cmd = "$seed {$o['jspacker']}/pack.js -m $proj -a $src/*.js";
377 // technically we should trash old compiled files..
378 // or we move towards a 'cache in session directory model..'
383 $ret = $tmp . '/'. $proj . '.js';
385 echo "BUILT: $ret \n";
392 // link {PROJECT}/compiled/{PROJECT}.js to _compiled_ folder to make it live.
394 function install($proj)
397 $base = dirname(realpath($_SERVER["SCRIPT_FILENAME"]));
401 var_dump($base .'/Pman/'. $proj.'/compiled/'.$proj .'.js');
402 $src = realpath($base .'/Pman/'. $proj.'/compiled/'.$proj .'.js');
404 echo "SKIP : no js file $proj\n";
407 if (!file_exists("$base/_compiled_")) {
408 mkdir ("$base/_compiled_", 0755, true);
410 $target = "$base/_compiled_/".$proj .'.js';
411 print_R(array($src,$target));
412 if (file_exists($target)) {
413 return; // already installed.
416 symlink($src, $target);
421 function gatherProjects() {
422 $src= array_shift(explode(PATH_SEPARATOR, ini_get('include_path'))) .'/Pman';
425 foreach(scandir($src) as $f) {
426 if (!strlen($f) || $f[0] == '.') {
434 if ($f == 'templates') {