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);
113 $ofiles = usort($ofiles,function($a,$b) {
119 return ($a > $b) ? +1 : -1;
123 $output = md5(serialize($arfiles)) .'.js';
125 if ( $compile && !file_exists($basedir.'/_cache_/'.$output)) {
126 $this->pack($arfiles,$basedir.'/_cache_/'.$output);
129 if ($compile && file_exists($basedir.'/_cache_/'.$output)) {
131 echo '<script type="text/javascript" src="'.$output_url.'/_cache_/'. $output.'"></script>';
134 foreach($ofiles as $f) {
135 echo '<script type="text/javascript" src="'.$output_url.'/'.$f.'"></script>'."\n";
141 function packCss($basedir, $files, $output_url)
143 // this outputs <script tags..>
144 // either for just the original files,
145 // or the compressed version.
146 // first expand files..
151 foreach($files as $f) {
152 if (!file_exists($basedir .'/' .$f)) {
155 if (!is_dir($basedir .'/' .$f)) {
156 $arfiles[$basedir .'/' .$f] = filemtime($basedir .'/' .$f);
160 foreach(glob($basedir .'/' .$f.'/*.css') as $fx) {
161 $arfiles[$fx] = filemtime($fx);
162 $ofiles [] = $f . '/'. basename($fx);
166 $output = md5(serialize($arfiles)) .'.css';
168 if (!file_exists($basedir.'/_cache_/'.$output)) {
169 $this->packCssCore($arfiles,$basedir.'/_cache_/'.$output);
171 //var_dump()$basedir. '/_cache_/'.$output);
172 if (file_exists($basedir. '/_cache_/'.$output)) {
173 echo '<link type="text/css" rel="stylesheet" media="screen" href="'.$output_url. '/_cache_/'. $output.'" />';
176 foreach($ofiles as $f ) {
177 echo '<link type="text/css" rel="stylesheet" media="screen" href="'.$output_url.'/'.$f.'" />'."\n";
184 * wrapper arroudn packer...
185 * @param {Array} map of $files => filemtime the files to pack
186 * @param {String} $output name fo file to output
189 function packCssCore($files, $output)
193 $o = HTML_FlexyFramework::get()->Pman_Core;
195 if (empty($o['cssminify']) || !file_exists($o['cssminify'])) {
196 echo '<!-- cssminify not set -->';
199 require_once 'System.php';
201 $seed= System::which('seed');
202 $gjs = System::which('gjs');
204 if (!$seed && !$gjs) {
205 echo '<!-- seed or gjs are not installed -->';
209 $targetm = file_exists($output) ? filemtime($output) : 0;
212 foreach($files as $f => $mt) {
213 $max = max($max,$mt);
214 $ofiles[] = escapeshellarg($f);
216 if ($max < $targetm) {
219 if (!file_exists(dirname($output))) {
220 mkdir(dirname($output), 0755, true);
222 $eoutput = escapeshellarg($output);
224 ("$seed {$o['cssminify']} $eoutput " . implode($ofiles, ' ')) :
225 ("$gjs {$o['cssminify']} -- -- $eoutput " . implode($ofiles, ' '));
226 //echo "<PRE>$cmd\n"; echo `$cmd`; exit;
230 // we should do more checking.. return val etc..
231 if (file_exists($output) && ($max < filemtime($output) ) ) {
238 * wrapper arround packer...
239 * uses the translation module & puts index in __tra
241 * @param {Array} map of $files => filemtime the files to pack
242 * @param {String} $output name fo file to output
245 function pack($files, $output, $translation_base=false)
249 $o = HTML_FlexyFramework::get()->Pman_Core;
251 if (empty($o['jspacker']) || !file_exists($o['jspacker'].'/pack.js')) {
252 echo '<!-- JS COMPILE ERROR: option: Pman_Core[jspacker] not set to directory -->';
256 require_once 'System.php';
257 $seed= System::which('seed');
258 $gjs = System::which('gjs');
260 if (!$seed && !$gjs) {
261 echo '<!-- seed or gjs are not installed -->';
265 $targetm = file_exists($output) && filesize($output) ? filemtime($output) : 0;
268 foreach($files as $f => $mt) {
269 $max = max($max,$mt);
270 $ofiles[] = escapeshellarg($f);
272 if ($max < $targetm) {
273 echo '<!-- use cached compile. -->';
277 if (!file_exists(dirname($output))) {
278 mkdir(dirname($output), 0755, true);
280 $lsort = create_function('$a,$b','return strlen($a) > strlen($b) ? 1 : -1;');
281 usort($ofiles, $lsort);
283 //$eoutput = " -k -o " . escapeshellarg($output) ; // with whitespace..
284 $eoutput = " -o " . escapeshellarg($output) ;
286 if ( $translation_base) {
287 $toutput = " -t ". escapeshellarg(preg_replace('/\.js$/', '.__translation__.js', $output)) .
288 " -p " . escapeshellarg($translation_base) ;//." -k "; // this kills the compression.
294 "$seed {$o['jspacker']}/pack.js " :
295 "$gjs -I {$o['jspacker']} -I {$o['jspacker']}/JSDOC {$o['jspacker']}/pack.js -- -- "
297 ) . " $eoutput $toutput " . implode($ofiles, ' ') . ' 2>&1';
298 //echo "<PRE>$cmd\n";
301 echo "<!-- Compile javascript
303 " . htmlspecialchars($cmd) . "
311 file_put_contents($output.'.log', $cmd."\n\n". $res);
312 // since this only appears when we change.. it's ok to dump it out..
313 echo "<!-- Compiled javascript
314 " . htmlspecialchars($res) . "
317 // we should do more checking.. return val etc..
318 if (file_exists($output) && ($max < filemtime($output) ) ) {
322 echo "<!-- JS COMPILE ERROR: packed file did not exist -->";
331 * @param {String} $proj name of Pman component to build
332 * runs pack.js -m {proj} -a $src/*.js
337 function build($proj)
339 echo "Building $proj\n";
342 $this->err = "no project";
343 if ($this->cli) echo $this->err;
346 // first item in path is always the app start directory..
347 $src= array_shift(explode(PATH_SEPARATOR, ini_get('include_path'))) .'/Pman/'. $proj;
351 //$tmp = ini_get('session.save_path')."/{$proj}_". posix_getuid(). '_'.md5($src);
354 require_once 'System.php';
355 $seed= System::which('seed');
357 $this->err ="no seed installed";
358 if ($this->cli) echo $this->err;
362 $o = HTML_FlexyFramework::get()->Pman_Core;
364 if (empty($o['jspacker']) || !file_exists($o['jspacker'].'/pack.js')) {
365 $this->err ="no jstoolkit path set [Pman_Core][jspacker] to the
366 introspection documentation directory where pack.js is located.";
367 if ($this->cli) echo $this->err;
371 // should we be more specirfic!??!?!?
373 $cmd = "$seed {$o['jspacker']}/pack.js -m $proj -a $src/*.js";
376 // technically we should trash old compiled files..
377 // or we move towards a 'cache in session directory model..'
382 $ret = $tmp . '/'. $proj . '.js';
384 echo "BUILT: $ret \n";
391 // link {PROJECT}/compiled/{PROJECT}.js to _compiled_ folder to make it live.
393 function install($proj)
396 $base = dirname(realpath($_SERVER["SCRIPT_FILENAME"]));
400 var_dump($base .'/Pman/'. $proj.'/compiled/'.$proj .'.js');
401 $src = realpath($base .'/Pman/'. $proj.'/compiled/'.$proj .'.js');
403 echo "SKIP : no js file $proj\n";
406 if (!file_exists("$base/_compiled_")) {
407 mkdir ("$base/_compiled_", 0755, true);
409 $target = "$base/_compiled_/".$proj .'.js';
410 print_R(array($src,$target));
411 if (file_exists($target)) {
412 return; // already installed.
415 symlink($src, $target);
420 function gatherProjects() {
421 $src= array_shift(explode(PATH_SEPARATOR, ini_get('include_path'))) .'/Pman';
424 foreach(scandir($src) as $f) {
425 if (!strlen($f) || $f[0] == '.') {
433 if ($f == 'templates') {