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 foreach(glob($basedir .'/' .$f.'/*.js') as $fx) {
107 $arfiles[$fx] = filemtime($fx);
108 $ofiles [] = $f . '/'. basename($fx);
112 $output = md5(serialize($arfiles)) .'.js';
114 if ( $compile && !file_exists($basedir.'/_cache_/'.$output)) {
115 $this->pack($arfiles,$basedir.'/_cache_/'.$output);
118 if ($compile && file_exists($basedir.'/_cache_/'.$output)) {
120 echo '<script type="text/javascript" src="'.$output_url.'/_cache_/'. $output.'"></script>';
123 foreach($ofiles as $f) {
124 echo '<script type="text/javascript" src="'.$output_url.'/'.$f.'"></script>'."\n";
130 function packCss($basedir, $files, $output_url)
132 // this outputs <script tags..>
133 // either for just the original files,
134 // or the compressed version.
135 // first expand files..
140 foreach($files as $f) {
141 if (!file_exists($basedir .'/' .$f)) {
144 if (!is_dir($basedir .'/' .$f)) {
145 $arfiles[$basedir .'/' .$f] = filemtime($basedir .'/' .$f);
149 foreach(glob($basedir .'/' .$f.'/*.css') as $fx) {
150 $arfiles[$fx] = filemtime($fx);
151 $ofiles [] = $f . '/'. basename($fx);
155 $output = md5(serialize($arfiles)) .'.css';
157 if (!file_exists($basedir.'/_cache_/'.$output)) {
158 $this->packCssCore($arfiles,$basedir.'/_cache_/'.$output);
160 //var_dump()$basedir. '/_cache_/'.$output);
161 if (file_exists($basedir. '/_cache_/'.$output)) {
162 echo '<link type="text/css" rel="stylesheet" media="screen" href="'.$output_url. '/_cache_/'. $output.'" />';
165 foreach($ofiles as $f ) {
166 echo '<link type="text/css" rel="stylesheet" media="screen" href="'.$output_url.'/'.$f.'" />'."\n";
173 * wrapper arroudn packer...
174 * @param {Array} map of $files => filemtime the files to pack
175 * @param {String} $output name fo file to output
178 function packCssCore($files, $output)
182 $o = HTML_FlexyFramework::get()->Pman_Core;
184 if (empty($o['cssminify']) || !file_exists($o['cssminify'])) {
185 echo '<!-- cssminify not set -->';
188 require_once 'System.php';
189 $seed= System::which('seed');
191 echo '<!-- seed not installed -->';
195 $targetm = file_exists($output) ? filemtime($output) : 0;
198 foreach($files as $f => $mt) {
199 $max = max($max,$mt);
200 $ofiles[] = escapeshellarg($f);
202 if ($max < $targetm) {
205 if (!file_exists(dirname($output))) {
206 mkdir(dirname($output), 0755, true);
208 $eoutput = escapeshellarg($output);
209 $cmd = "$seed {$o['cssminify']} $eoutput " . implode($ofiles, ' ');
210 //echo "<PRE>$cmd\n"; echo `$cmd`; exit;
214 // we should do more checking.. return val etc..
215 if (file_exists($output) && ($max < filemtime($output) ) ) {
222 * wrapper arround packer...
223 * uses the translation module & puts index in __tra
225 * @param {Array} map of $files => filemtime the files to pack
226 * @param {String} $output name fo file to output
229 function pack($files, $output, $translation_base=false)
233 $o = HTML_FlexyFramework::get()->Pman_Core;
235 if (empty($o['jspacker']) || !file_exists($o['jspacker'].'/pack.js')) {
236 echo '<!-- JS COMPILE ERROR: option: Pman_Core[jspacker] not set to directory -->';
240 require_once 'System.php';
241 $seed= System::which('seed');
243 echo '<!-- JS COMPILE ERROR: seed not installed -->';
247 $targetm = file_exists($output) && filesize($output) ? filemtime($output) : 0;
250 foreach($files as $f => $mt) {
251 $max = max($max,$mt);
252 $ofiles[] = escapeshellarg($f);
254 if ($max < $targetm) {
255 echo '<!-- use cached compile. -->';
259 if (!file_exists(dirname($output))) {
260 mkdir(dirname($output), 0755, true);
262 $lsort = create_function('$a,$b','return strlen($a) > strlen($b) ? 1 : -1;');
263 usort($ofiles, $lsort);
265 $eoutput = " -o " . escapeshellarg($output) ;
268 if ( $translation_base) {
269 $toutput = "-t ". escapeshellarg(preg_replace('/\.js$/', '.__translation__.js', $output)) .
270 " -p " . escapeshellarg($translation_base) ;//." -k "; // this kills the compression.
275 $cmd = "$seed {$o['jspacker']}/pack.js $eoutput $toutput " . implode($ofiles, ' ') . ' 2>&1';
276 //echo "<PRE>$cmd\n";
279 echo "<!-- Compile javascript
281 " . htmlspecialchars($cmd) . "
289 file_put_contents($output.'.log', $cmd."\n\n". $res);
290 // since this only appears when we change.. it's ok to dump it out..
291 echo "<!-- Compiled javascript
292 " . htmlspecialchars($res) . "
295 // we should do more checking.. return val etc..
296 if (file_exists($output) && ($max < filemtime($output) ) ) {
300 echo "<!-- JS COMPILE ERROR: packed file did not exist -->";
309 * @param {String} $proj name of Pman component to build
310 * runs pack.js -m {proj} -a $src/*.js
315 function build($proj)
317 echo "Building $proj\n";
320 $this->err = "no project";
321 if ($this->cli) echo $this->err;
324 // first item in path is always the app start directory..
325 $src= array_shift(explode(PATH_SEPARATOR, ini_get('include_path'))) .'/Pman/'. $proj;
329 //$tmp = ini_get('session.save_path')."/{$proj}_". posix_getuid(). '_'.md5($src);
332 require_once 'System.php';
333 $seed= System::which('seed');
335 $this->err ="no seed installed";
336 if ($this->cli) echo $this->err;
340 $o = HTML_FlexyFramework::get()->Pman_Core;
342 if (empty($o['jspacker']) || !file_exists($o['jspacker'].'/pack.js')) {
343 $this->err ="no jstoolkit path set [Pman_Core][jspacker] to the
344 introspection documentation directory where pack.js is located.";
345 if ($this->cli) echo $this->err;
349 // should we be more specirfic!??!?!?
351 $cmd = "$seed {$o['jspacker']}/pack.js -m $proj -a $src/*.js";
354 // technically we should trash old compiled files..
355 // or we move towards a 'cache in session directory model..'
360 $ret = $tmp . '/'. $proj . '.js';
362 echo "BUILT: $ret \n";
369 // link {PROJECT}/compiled/{PROJECT}.js to _compiled_ folder to make it live.
371 function install($proj)
374 $base = dirname(realpath($_SERVER["SCRIPT_FILENAME"]));
378 var_dump($base .'/Pman/'. $proj.'/compiled/'.$proj .'.js');
379 $src = realpath($base .'/Pman/'. $proj.'/compiled/'.$proj .'.js');
381 echo "SKIP : no js file $proj\n";
384 if (!file_exists("$base/_compiled_")) {
385 mkdir ("$base/_compiled_", 0755, true);
387 $target = "$base/_compiled_/".$proj .'.js';
388 print_R(array($src,$target));
389 if (file_exists($target)) {
390 return; // already installed.
393 symlink($src, $target);
398 function gatherProjects() {
399 $src= array_shift(explode(PATH_SEPARATOR, ini_get('include_path'))) .'/Pman';
402 foreach(scandir($src) as $f) {
403 if (!strlen($f) || $f[0] == '.') {
411 if ($f == 'templates') {