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);
106 print_r(glob($basedir .'/' .$f.'/*.js'));
107 foreach(glob($basedir .'/' .$f.'/*.js') as $fx) {
109 $arfiles[$fx] = filemtime($fx);
110 $ofiles [] = $f . '/'. basename($fx);
114 $output = md5(serialize($arfiles)) .'.js';
116 if ( $compile && !file_exists($basedir.'/_cache_/'.$output)) {
117 $this->pack($arfiles,$basedir.'/_cache_/'.$output);
120 if ($compile && file_exists($basedir.'/_cache_/'.$output)) {
122 echo '<script type="text/javascript" src="'.$output_url.'/_cache_/'. $output.'"></script>';
125 foreach($ofiles as $f) {
126 echo '<script type="text/javascript" src="'.$output_url.'/'.$f.'"></script>'."\n";
132 function packCss($basedir, $files, $output_url)
134 // this outputs <script tags..>
135 // either for just the original files,
136 // or the compressed version.
137 // first expand files..
142 foreach($files as $f) {
143 if (!file_exists($basedir .'/' .$f)) {
146 if (!is_dir($basedir .'/' .$f)) {
147 $arfiles[$basedir .'/' .$f] = filemtime($basedir .'/' .$f);
151 foreach(glob($basedir .'/' .$f.'/*.css') as $fx) {
152 $arfiles[$fx] = filemtime($fx);
153 $ofiles [] = $f . '/'. basename($fx);
157 $output = md5(serialize($arfiles)) .'.css';
159 if (!file_exists($basedir.'/_cache_/'.$output)) {
160 $this->packCssCore($arfiles,$basedir.'/_cache_/'.$output);
162 //var_dump()$basedir. '/_cache_/'.$output);
163 if (file_exists($basedir. '/_cache_/'.$output)) {
164 echo '<link type="text/css" rel="stylesheet" media="screen" href="'.$output_url. '/_cache_/'. $output.'" />';
167 foreach($ofiles as $f ) {
168 echo '<link type="text/css" rel="stylesheet" media="screen" href="'.$output_url.'/'.$f.'" />'."\n";
175 * wrapper arroudn packer...
176 * @param {Array} map of $files => filemtime the files to pack
177 * @param {String} $output name fo file to output
180 function packCssCore($files, $output)
184 $o = HTML_FlexyFramework::get()->Pman_Core;
186 if (empty($o['cssminify']) || !file_exists($o['cssminify'])) {
187 echo '<!-- cssminify not set -->';
190 require_once 'System.php';
192 $seed= System::which('seed');
193 $gjs = System::which('gjs');
195 if (!$seed && !$gjs) {
196 echo '<!-- seed or gjs are not installed -->';
200 $targetm = file_exists($output) ? filemtime($output) : 0;
203 foreach($files as $f => $mt) {
204 $max = max($max,$mt);
205 $ofiles[] = escapeshellarg($f);
207 if ($max < $targetm) {
210 if (!file_exists(dirname($output))) {
211 mkdir(dirname($output), 0755, true);
213 $eoutput = escapeshellarg($output);
215 ("$seed {$o['cssminify']} $eoutput " . implode($ofiles, ' ')) :
216 ("$gjs {$o['cssminify']} -- -- $eoutput " . implode($ofiles, ' '));
217 //echo "<PRE>$cmd\n"; echo `$cmd`; exit;
221 // we should do more checking.. return val etc..
222 if (file_exists($output) && ($max < filemtime($output) ) ) {
229 * wrapper arround packer...
230 * uses the translation module & puts index in __tra
232 * @param {Array} map of $files => filemtime the files to pack
233 * @param {String} $output name fo file to output
236 function pack($files, $output, $translation_base=false)
240 $o = HTML_FlexyFramework::get()->Pman_Core;
242 if (empty($o['jspacker']) || !file_exists($o['jspacker'].'/pack.js')) {
243 echo '<!-- JS COMPILE ERROR: option: Pman_Core[jspacker] not set to directory -->';
247 require_once 'System.php';
248 $seed= System::which('seed');
249 $gjs = System::which('gjs');
251 if (!$seed && !$gjs) {
252 echo '<!-- seed or gjs are not installed -->';
256 $targetm = file_exists($output) && filesize($output) ? filemtime($output) : 0;
259 foreach($files as $f => $mt) {
260 $max = max($max,$mt);
261 $ofiles[] = escapeshellarg($f);
263 if ($max < $targetm) {
264 echo '<!-- use cached compile. -->';
268 if (!file_exists(dirname($output))) {
269 mkdir(dirname($output), 0755, true);
271 $lsort = create_function('$a,$b','return strlen($a) > strlen($b) ? 1 : -1;');
272 usort($ofiles, $lsort);
274 //$eoutput = " -k -o " . escapeshellarg($output) ; // with whitespace..
275 $eoutput = " -o " . escapeshellarg($output) ;
277 if ( $translation_base) {
278 $toutput = " -t ". escapeshellarg(preg_replace('/\.js$/', '.__translation__.js', $output)) .
279 " -p " . escapeshellarg($translation_base) ;//." -k "; // this kills the compression.
285 "$seed {$o['jspacker']}/pack.js " :
286 "$gjs -I {$o['jspacker']} -I {$o['jspacker']}/JSDOC {$o['jspacker']}/pack.js -- -- "
288 ) . " $eoutput $toutput " . implode($ofiles, ' ') . ' 2>&1';
289 //echo "<PRE>$cmd\n";
292 echo "<!-- Compile javascript
294 " . htmlspecialchars($cmd) . "
302 file_put_contents($output.'.log', $cmd."\n\n". $res);
303 // since this only appears when we change.. it's ok to dump it out..
304 echo "<!-- Compiled javascript
305 " . htmlspecialchars($res) . "
308 // we should do more checking.. return val etc..
309 if (file_exists($output) && ($max < filemtime($output) ) ) {
313 echo "<!-- JS COMPILE ERROR: packed file did not exist -->";
322 * @param {String} $proj name of Pman component to build
323 * runs pack.js -m {proj} -a $src/*.js
328 function build($proj)
330 echo "Building $proj\n";
333 $this->err = "no project";
334 if ($this->cli) echo $this->err;
337 // first item in path is always the app start directory..
338 $src= array_shift(explode(PATH_SEPARATOR, ini_get('include_path'))) .'/Pman/'. $proj;
342 //$tmp = ini_get('session.save_path')."/{$proj}_". posix_getuid(). '_'.md5($src);
345 require_once 'System.php';
346 $seed= System::which('seed');
348 $this->err ="no seed installed";
349 if ($this->cli) echo $this->err;
353 $o = HTML_FlexyFramework::get()->Pman_Core;
355 if (empty($o['jspacker']) || !file_exists($o['jspacker'].'/pack.js')) {
356 $this->err ="no jstoolkit path set [Pman_Core][jspacker] to the
357 introspection documentation directory where pack.js is located.";
358 if ($this->cli) echo $this->err;
362 // should we be more specirfic!??!?!?
364 $cmd = "$seed {$o['jspacker']}/pack.js -m $proj -a $src/*.js";
367 // technically we should trash old compiled files..
368 // or we move towards a 'cache in session directory model..'
373 $ret = $tmp . '/'. $proj . '.js';
375 echo "BUILT: $ret \n";
382 // link {PROJECT}/compiled/{PROJECT}.js to _compiled_ folder to make it live.
384 function install($proj)
387 $base = dirname(realpath($_SERVER["SCRIPT_FILENAME"]));
391 var_dump($base .'/Pman/'. $proj.'/compiled/'.$proj .'.js');
392 $src = realpath($base .'/Pman/'. $proj.'/compiled/'.$proj .'.js');
394 echo "SKIP : no js file $proj\n";
397 if (!file_exists("$base/_compiled_")) {
398 mkdir ("$base/_compiled_", 0755, true);
400 $target = "$base/_compiled_/".$proj .'.js';
401 print_R(array($src,$target));
402 if (file_exists($target)) {
403 return; // already installed.
406 symlink($src, $target);
411 function gatherProjects() {
412 $src= array_shift(explode(PATH_SEPARATOR, ini_get('include_path'))) .'/Pman';
415 foreach(scandir($src) as $f) {
416 if (!strlen($f) || $f[0] == '.') {
424 if ($f == 'templates') {