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";
25 // command line only ATM
26 $this->cli = HTML_FlexyFramework::get()->cli;
27 // var_dump($this->cli);
35 function get($proj, $args)
38 die("missing action : eg. build or install");
41 if (empty($args[1])) {
43 $ar = $this->gatherProjects();
44 echo "SELECT Component to build\n";
48 $ar = array($args[1]);
61 case 'install' : // needed for install on remote sites..
73 * @param {String} basedir absolute path to files
74 * @param {Array} list of files (ontop of basedir)
75 * @param {String} output url (path to basedir basically), or false
82 function packScript($basedir, $files, $output_url, $compile=true)
84 // this outputs <script tags..>
85 // either for just the original files,
86 // or the compressed version.
87 // first expand files..
89 echo "<!-- compiling $basedir -->\n";
93 foreach($files as $f) {
94 if (!file_exists($basedir .'/' .$f)) {
97 if (!is_dir($basedir .'/' .$f)) {
99 $arfiles[$basedir .'/' .$f] = filemtime($basedir .'/' .$f);
103 foreach(glob($basedir .'/' .$f.'/*.js') as $fx) {
104 $arfiles[$fx] = filemtime($fx);
105 $ofiles [] = $f . '/'. basename($fx);
109 $output = md5(serialize($arfiles)) .'.js';
111 if ( $compile && !file_exists($basedir.'/_cache_/'.$output)) {
112 $this->pack($arfiles,$basedir.'/_cache_/'.$output);
115 if ($compile && file_exists($basedir.'/_cache_/'.$output)) {
117 echo '<script type="text/javascript" src="'.$output_url.'/_cache_/'. $output.'"></script>';
120 foreach($ofiles as $f) {
121 echo '<script type="text/javascript" src="'.$output_url.'/'.$f.'"></script>'."\n";
127 function packCss($basedir, $files, $output_url)
129 // this outputs <script tags..>
130 // either for just the original files,
131 // or the compressed version.
132 // first expand files..
137 foreach($files as $f) {
138 if (!file_exists($basedir .'/' .$f)) {
141 if (!is_dir($basedir .'/' .$f)) {
142 $arfiles[$basedir .'/' .$f] = filemtime($basedir .'/' .$f);
146 foreach(glob($basedir .'/' .$f.'/*.css') as $fx) {
147 $arfiles[$fx] = filemtime($fx);
148 $ofiles [] = $f . '/'. basename($fx);
152 $output = md5(serialize($arfiles)) .'.css';
154 if (!file_exists($basedir.'/_cache_/'.$output)) {
155 $this->packCssCore($arfiles,$basedir.'/_cache_/'.$output);
157 //var_dump()$basedir. '/_cache_/'.$output);
158 if (file_exists($basedir. '/_cache_/'.$output)) {
159 echo '<link type="text/css" rel="stylesheet" media="screen" href="'.$output_url. '/_cache_/'. $output.'" />';
162 foreach($ofiles as $f ) {
163 echo '<link type="text/css" rel="stylesheet" media="screen" href="'.$output_url.'/'.$f.'" />'."\n";
170 * wrapper arroudn packer...
171 * @param {Array} map of $files => filemtime the files to pack
172 * @param {String} $output name fo file to output
175 function packCssCore($files, $output)
179 $o = HTML_FlexyFramework::get()->Pman_Core;
181 if (empty($o['cssminify']) || !file_exists($o['cssminify'])) {
182 echo '<!-- cssminify not set -->';
185 require_once 'System.php';
186 $seed= System::which('seed');
188 echo '<!-- seed not installed -->';
192 $targetm = file_exists($output) ? filemtime($output) : 0;
195 foreach($files as $f => $mt) {
196 $max = max($max,$mt);
197 $ofiles[] = escapeshellarg($f);
199 if ($max < $targetm) {
202 if (!file_exists(dirname($output))) {
203 mkdir(dirname($output), 0755, true);
205 $eoutput = escapeshellarg($output);
206 $cmd = "$seed {$o['cssminify']} $eoutput " . implode($ofiles, ' ');
207 //echo "<PRE>$cmd\n"; echo `$cmd`; exit;
211 // we should do more checking.. return val etc..
212 if (file_exists($output) && ($max < filemtime($output) ) ) {
219 * wrapper arround packer...
220 * uses the translation module & puts index in __tra
222 * @param {Array} map of $files => filemtime the files to pack
223 * @param {String} $output name fo file to output
226 function pack($files, $output, $translation_base=false)
230 $o = HTML_FlexyFramework::get()->Pman_Core;
232 if (empty($o['jspacker']) || !file_exists($o['jspacker'].'/pack.js')) {
233 echo '<!-- JS COMPILE ERROR: option: Pman_Core[jspacker] not set to directory -->';
237 require_once 'System.php';
238 $seed= System::which('seed');
240 echo '<!-- JS COMPILE ERROR: seed not installed -->';
244 $targetm = file_exists($output) && filesize($output) ? filemtime($output) : 0;
247 foreach($files as $f => $mt) {
248 $max = max($max,$mt);
249 $ofiles[] = escapeshellarg($f);
251 if ($max < $targetm) {
252 echo '<!-- use cached compile. -->';
256 if (!file_exists(dirname($output))) {
257 mkdir(dirname($output), 0755, true);
259 $lsort = create_function('$a,$b','return strlen($a) > strlen($b) ? 1 : -1;');
260 usort($ofiles, $lsort);
262 $eoutput = " -o " . escapeshellarg($output) ;
265 if ( $translation_base) {
266 $toutput = "-t ". escapeshellarg(preg_replace('/\.js$/', '.__translation__.js', $output)) .
267 " -p " . escapeshellarg($translation_base) ;//." -k "; // this kills the compression.
272 $cmd = "$seed {$o['jspacker']}/pack.js $eoutput $toutput " . implode($ofiles, ' ') . ' 2>&1';
273 //echo "<PRE>$cmd\n";
276 echo "<!-- Compile javascript
278 " . htmlspecialchars($cmd) . "
286 file_put_contents($output.'.log', $cmd."\n\n". $res);
287 // since this only appears when we change.. it's ok to dump it out..
288 echo "<!-- Compiled javascript
289 " . htmlspecialchars($res) . "
292 // we should do more checking.. return val etc..
293 if (file_exists($output) && ($max < filemtime($output) ) ) {
297 echo "<!-- JS COMPILE ERROR: packed file did not exist -->";
306 * @param {String} $proj name of Pman component to build
307 * runs pack.js -m {proj} -a $src/*.js
312 function build($proj)
314 echo "Building $proj\n";
317 $this->err = "no project";
318 if ($this->cli) echo $this->err;
321 // first item in path is always the app start directory..
322 $src= array_shift(explode(PATH_SEPARATOR, ini_get('include_path'))) .'/Pman/'. $proj;
326 //$tmp = ini_get('session.save_path')."/{$proj}_". posix_getuid(). '_'.md5($src);
329 require_once 'System.php';
330 $seed= System::which('seed');
332 $this->err ="no seed installed";
333 if ($this->cli) echo $this->err;
337 $o = HTML_FlexyFramework::get()->Pman_Core;
339 if (empty($o['jspacker']) || !file_exists($o['jspacker'].'/pack.js')) {
340 $this->err ="no jstoolkit path set [Pman_Core][jspacker] to the
341 introspection documentation directory where pack.js is located.";
342 if ($this->cli) echo $this->err;
346 // should we be more specirfic!??!?!?
348 $cmd = "$seed {$o['jspacker']}/pack.js -m $proj -a $src/*.js";
351 // technically we should trash old compiled files..
352 // or we move towards a 'cache in session directory model..'
357 $ret = $tmp . '/'. $proj . '.js';
359 echo "BUILT: $ret \n";
366 // link {PROJECT}/compiled/{PROJECT}.js to _compiled_ folder to make it live.
368 function install($proj)
371 $base = dirname(realpath($_SERVER["SCRIPT_FILENAME"]));
375 var_dump($base .'/Pman/'. $proj.'/compiled/'.$proj .'.js');
376 $src = realpath($base .'/Pman/'. $proj.'/compiled/'.$proj .'.js');
378 echo "SKIP : no js file $proj\n";
381 if (!file_exists("$base/_compiled_")) {
382 mkdir ("$base/_compiled_", 0755, true);
384 $target = "$base/_compiled_/".$proj .'.js';
385 print_R(array($src,$target));
386 if (file_exists($target)) {
387 return; // already installed.
390 symlink($src, $target);
395 function gatherProjects() {
396 $src= array_shift(explode(PATH_SEPARATOR, ini_get('include_path'))) .'/Pman';
399 foreach(scandir($src) as $f) {
400 if (!strlen($f) || $f[0] == '.') {
408 if ($f == 'templates') {