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';
190 $seed= System::which('seed');
191 $gjs = System::which('gjs');
193 if (!$seed && !$gjs) {
194 echo '<!-- seed or gjs are not installed -->';
198 $targetm = file_exists($output) ? filemtime($output) : 0;
201 foreach($files as $f => $mt) {
202 $max = max($max,$mt);
203 $ofiles[] = escapeshellarg($f);
205 if ($max < $targetm) {
208 if (!file_exists(dirname($output))) {
209 mkdir(dirname($output), 0755, true);
211 $eoutput = escapeshellarg($output);
213 ("$seed {$o['cssminify']} $eoutput " . implode($ofiles, ' ')) :
214 ("$gjs {$o['cssminify']} $eoutput " . implode($ofiles, ' '));
215 //echo "<PRE>$cmd\n"; echo `$cmd`; exit;
219 // we should do more checking.. return val etc..
220 if (file_exists($output) && ($max < filemtime($output) ) ) {
227 * wrapper arround packer...
228 * uses the translation module & puts index in __tra
230 * @param {Array} map of $files => filemtime the files to pack
231 * @param {String} $output name fo file to output
234 function pack($files, $output, $translation_base=false)
238 $o = HTML_FlexyFramework::get()->Pman_Core;
240 if (empty($o['jspacker']) || !file_exists($o['jspacker'].'/pack.js')) {
241 echo '<!-- JS COMPILE ERROR: option: Pman_Core[jspacker] not set to directory -->';
245 require_once 'System.php';
246 $seed= System::which('seed');
247 $gjs = System::which('gjs');
249 if (!$seed && !$gjs) {
250 echo '<!-- seed or gjs are not installed -->';
254 $targetm = file_exists($output) && filesize($output) ? filemtime($output) : 0;
257 foreach($files as $f => $mt) {
258 $max = max($max,$mt);
259 $ofiles[] = escapeshellarg($f);
261 if ($max < $targetm) {
262 echo '<!-- use cached compile. -->';
266 if (!file_exists(dirname($output))) {
267 mkdir(dirname($output), 0755, true);
269 $lsort = create_function('$a,$b','return strlen($a) > strlen($b) ? 1 : -1;');
270 usort($ofiles, $lsort);
272 $eoutput = " -o " . escapeshellarg($output) ;
275 if ( $translation_base) {
276 $toutput = "-t ". escapeshellarg(preg_replace('/\.js$/', '.__translation__.js', $output)) .
277 " -p " . escapeshellarg($translation_base) ;//." -k "; // this kills the compression.
282 $cmd = "$seed {$o['jspacker']}/pack.js $eoutput $toutput " . implode($ofiles, ' ') . ' 2>&1';
283 //echo "<PRE>$cmd\n";
286 echo "<!-- Compile javascript
288 " . htmlspecialchars($cmd) . "
296 file_put_contents($output.'.log', $cmd."\n\n". $res);
297 // since this only appears when we change.. it's ok to dump it out..
298 echo "<!-- Compiled javascript
299 " . htmlspecialchars($res) . "
302 // we should do more checking.. return val etc..
303 if (file_exists($output) && ($max < filemtime($output) ) ) {
307 echo "<!-- JS COMPILE ERROR: packed file did not exist -->";
316 * @param {String} $proj name of Pman component to build
317 * runs pack.js -m {proj} -a $src/*.js
322 function build($proj)
324 echo "Building $proj\n";
327 $this->err = "no project";
328 if ($this->cli) echo $this->err;
331 // first item in path is always the app start directory..
332 $src= array_shift(explode(PATH_SEPARATOR, ini_get('include_path'))) .'/Pman/'. $proj;
336 //$tmp = ini_get('session.save_path')."/{$proj}_". posix_getuid(). '_'.md5($src);
339 require_once 'System.php';
340 $seed= System::which('seed');
342 $this->err ="no seed installed";
343 if ($this->cli) echo $this->err;
347 $o = HTML_FlexyFramework::get()->Pman_Core;
349 if (empty($o['jspacker']) || !file_exists($o['jspacker'].'/pack.js')) {
350 $this->err ="no jstoolkit path set [Pman_Core][jspacker] to the
351 introspection documentation directory where pack.js is located.";
352 if ($this->cli) echo $this->err;
356 // should we be more specirfic!??!?!?
358 $cmd = "$seed {$o['jspacker']}/pack.js -m $proj -a $src/*.js";
361 // technically we should trash old compiled files..
362 // or we move towards a 'cache in session directory model..'
367 $ret = $tmp . '/'. $proj . '.js';
369 echo "BUILT: $ret \n";
376 // link {PROJECT}/compiled/{PROJECT}.js to _compiled_ folder to make it live.
378 function install($proj)
381 $base = dirname(realpath($_SERVER["SCRIPT_FILENAME"]));
385 var_dump($base .'/Pman/'. $proj.'/compiled/'.$proj .'.js');
386 $src = realpath($base .'/Pman/'. $proj.'/compiled/'.$proj .'.js');
388 echo "SKIP : no js file $proj\n";
391 if (!file_exists("$base/_compiled_")) {
392 mkdir ("$base/_compiled_", 0755, true);
394 $target = "$base/_compiled_/".$proj .'.js';
395 print_R(array($src,$target));
396 if (file_exists($target)) {
397 return; // already installed.
400 symlink($src, $target);
405 function gatherProjects() {
406 $src= array_shift(explode(PATH_SEPARATOR, ini_get('include_path'))) .'/Pman';
409 foreach(scandir($src) as $f) {
410 if (!strlen($f) || $f[0] == '.') {
418 if ($f == 'templates') {