PHP7 fix
[Pman.Core] / JsCompile.php
index 87099b9..a5b9159 100644 (file)
@@ -5,6 +5,8 @@
 * 
 *  -- we will use this later to compile on the fly...
 *
+*  -- updated to use roojspacker https://github.com/roojs/roojspacker
+*
 *
 * For general usage:
 *  $x = new Pman_Core_JsCompile();
@@ -17,7 +19,10 @@ require_once 'Pman.php';
 class Pman_Core_JsCompile  extends Pman
 {
     
-    static $cli_desc = "Wrapper around Javascript compression tools";
+    static $cli_desc = "Wrapper around Javascript compression tools
+                        Runs the javascript compiler - merging all the JS files so the load faster.
+                        Note: cfg option Pman_Builder['jspacker'] must be set to location of jstoolkit code 
+";
     
     var $cli = false;
     function getAuth()
@@ -32,7 +37,7 @@ class Pman_Core_JsCompile  extends Pman
     }
     
     
-    function get($proj, $args)
+    function get($proj, $args=array())
     {
         if (empty($args)) {
             die("missing action : eg. build or install");
@@ -78,6 +83,16 @@ class Pman_Core_JsCompile  extends Pman
      *
      */
     
+    static function jsSort($a,$b)
+    {
+        $a = substr($a, 0, -3);
+        $b=  substr($b, 0, -3);
+        if ($a == $b) {
+            return 0;
+        }
+        return ($a > $b) ? +1 : -1;
+    }
+    
     
     function packScript($basedir, $files,  $output_url, $compile=true)
     {
@@ -86,6 +101,8 @@ class Pman_Core_JsCompile  extends Pman
         // or the compressed version.
         // first expand files..
         
+        echo "<!-- compiling   $basedir  -->\n";
+        
         $arfiles = array();
         $ofiles = array();
         foreach($files as $f) {
@@ -95,14 +112,21 @@ class Pman_Core_JsCompile  extends Pman
             if (!is_dir($basedir .'/' .$f)) {
                 
                 $arfiles[$basedir .'/' .$f] = filemtime($basedir .'/' .$f);
-                 $ofiles[] = $f;
+                $ofiles[] = $f;
                 continue;
             }
+            
             foreach(glob($basedir .'/' .$f.'/*.js') as $fx) {
+                
                 $arfiles[$fx] = filemtime($fx);
                 $ofiles [] = $f . '/'. basename($fx);
             }
         }
+        $tf = 
+        // sort exc. the .js
+        usort($ofiles,function($a,$b) { return Pman_Core_JsCompile::jsSort($a,$b); });
+        
+        //print_R($ofiles);
         
         $output = md5(serialize($arfiles)) .'.js';
         
@@ -119,9 +143,11 @@ class Pman_Core_JsCompile  extends Pman
             echo '<script type="text/javascript" src="'.$output_url.'/'.$f.'"></script>'."\n";
             
         }
-         
-        
+          
     }
+    
+    // this is depricated... - we can use the pear CSS library for this..
+    
     function packCss($basedir, $files,   $output_url)
     {
         // this outputs <script tags..>
@@ -181,9 +207,12 @@ class Pman_Core_JsCompile  extends Pman
             return false;
         }
         require_once 'System.php';
+        
         $seed= System::which('seed');
-        if (!$seed) {
-            echo '<!-- seed not installed -->';
+        $gjs = System::which('gjs');
+        
+        if (!$seed && !$gjs) {
+            echo '<!-- seed or gjs are  not installed -->';
             return false;
             
         }
@@ -201,7 +230,9 @@ class Pman_Core_JsCompile  extends Pman
             mkdir(dirname($output), 0755, true);
         }
         $eoutput = escapeshellarg($output);
-        $cmd = "$seed {$o['cssminify']}  $eoutput " . implode($ofiles, ' ');
+        $cmd = $seed ?
+            ("$seed {$o['cssminify']}  $eoutput " . implode($ofiles, ' ')) :
+            ("$gjs {$o['cssminify']} -- -- $eoutput " . implode($ofiles, ' ')); 
         //echo "<PRE>$cmd\n"; echo `$cmd`; exit;
         `$cmd`;
         
@@ -214,37 +245,133 @@ class Pman_Core_JsCompile  extends Pman
         
     }
     /**
-     * wrapper arroudn packer...
+     * wrapper arround packer...
+     * uses the translation module & puts index in __tra
+     * 
      * @param {Array} map of $files => filemtime the files to pack
      * @param {String} $output name fo file to output
      *
      */
-    function pack($files, $output)
+    
+    function pack($files, $output, $translation_base=false)
+    {
+        
+        if (empty($files)) {
+            return false;
+        }
+        
+        $o = HTML_FlexyFramework::get()->Pman_Core;
+        if (isset($o['packseed'])) {
+            return $this->packSeed($files,$output,$translation_base);
+        }
+        
+        
+        require_once 'System.php';
+        $packer = System::which('roojspacker');
+        
+        
+        if (!$packer) {
+            echo '<!-- roojspacker is not installed -->';
+            return false;
+            
+        }
+        $targetm = file_exists($output) && filesize($output) ? filemtime($output) : 0;
+        $max = 0;
+        $ofiles = array();
+        foreach($files as $f => $mt) {
+            $max = max($max,$mt);
+            $ofiles[] = escapeshellarg($f);
+        }
+        if ($max < $targetm) {
+            echo '<!--  use cached compile. -->';
+            return true;
+        }
+        
+        if (file_exists($output)) {
+            unlink($output);
+        }
+        
+         
+        if (!file_exists(dirname($output))) {
+            mkdir(dirname($output), 0755, true);
+        }
+        
+        usort($ofiles, function($a,$b)  { return strlen($a) > strlen($b) ? 1 : -1; });
+        
+        //$eoutput = " -k  -o " . escapeshellarg($output) ; // with whitespace..
+        $eoutput = "  -t " . escapeshellarg($output) ;
+          
+        // no support for translation any more?         
+        //if (  $translation_base) {
+        //    $toutput = " -t ". escapeshellarg(preg_replace('/\.js$/', '.__translation__.js', $output)) .
+        //            " -p " . escapeshellarg($translation_base) ;//." -k "; // this kills the compression.
+        //            
+        //}
+        
+    
+        $cmd = "$packer  $eoutput  -f " . implode(' -f ', $ofiles) . ' 2>&1';
+        //echo "<PRE>$cmd\n";
+        //echo `$cmd`;
+        
+         echo "<!-- Compile javascript
+          
+            " . htmlspecialchars($cmd) . "
+            
+            -->";
+            
+       // return false;
+        
+        $res = `$cmd`;
+        //exit;
+        file_put_contents($output.'.log', $cmd."\n\n". $res);
+        // since this only appears when we change.. it's ok to dump it out..
+        echo "<!-- Compiled javascript
+            " . htmlspecialchars($res) . "
+            -->";
+        clearstatcache();
+        // we should do more checking.. return val etc..
+        if (file_exists($output) && filesize($output) && ($max < filemtime($output) ) ) {
+            echo "<!-- file looks like its been generated -->\n";
+            return true;
+        }
+        echo '<script type="text/javascript"> alert('. json_encode("Error: Javascript Compile failed\n" . $res) .');</script>';
+     
+        
+        echo "<!-- JS COMPILE ERROR: packed file did not exist  -->";
+        return false;
+        
+    }
+    
+    // depricated verison using seed.
+    function packSeed($files, $output, $translation_base=false)
     {
         
          
         $o = HTML_FlexyFramework::get()->Pman_Core;
         
-        if (empty($o['jspacker']) || !file_exists($o['jspacker'].'/pack.js')) {
+        if (empty($o['packseed']) || !file_exists($o['jspacker'].'/pack.js')) {
             echo '<!-- JS COMPILE ERROR: option: Pman_Core[jspacker] not set to directory -->';
             return false;
             
         }
         require_once 'System.php';
         $seed= System::which('seed');
-        if (!$seed) {
-            echo '<!-- JS COMPILE ERROR: seed not installed -->';
+        $gjs = System::which('gjs');
+        
+        if (!$seed && !$gjs) {
+            echo '<!-- seed or gjs are  not installed -->';
             return false;
             
         }
-        $targetm = file_exists($output) ? filemtime($output) : 0;
+        $targetm = file_exists($output) && filesize($output) ? filemtime($output) : 0;
         $max = 0;
         $ofiles = array();
         foreach($files as $f => $mt) {
             $max = max($max,$mt);
             $ofiles[] = escapeshellarg($f);
         }
-        if ($max < $targetm)  {
+        if ($max < $targetm) {
+            echo '<!--  use cached compile. -->';
             return true;
         }
         //var_dump($output);
@@ -254,8 +381,21 @@ class Pman_Core_JsCompile  extends Pman
         $lsort = create_function('$a,$b','return strlen($a) > strlen($b) ? 1 : -1;');
         usort($ofiles, $lsort);
         
-        $eoutput = escapeshellarg($output);
-        $cmd = "$seed {$o['jspacker']}/pack.js  -o $eoutput " . implode($ofiles, ' ') . ' 2>&1';
+        //$eoutput = " -k  -o " . escapeshellarg($output) ; // with whitespace..
+        $eoutput = "  -o " . escapeshellarg($output) ;
+                   
+        if (  $translation_base) {
+            $toutput = " -t ". escapeshellarg(preg_replace('/\.js$/', '.__translation__.js', $output)) .
+                    " -p " . escapeshellarg($translation_base) ;//." -k "; // this kills the compression.
+                    
+        }
+        
+        
+        $cmd = ($seed ?
+             "$seed {$o['packseed']}/pack.js " :
+             "$gjs -I {$o['packseed']} -I {$o['packseed']}/JSDOC  {$o['packseed']}/pack.js -- -- " 
+              
+             ) . " $eoutput  $toutput " . implode($ofiles, ' ') . ' 2>&1';
         //echo "<PRE>$cmd\n";
         //echo `$cmd`;
         
@@ -265,13 +405,13 @@ class Pman_Core_JsCompile  extends Pman
             
             -->";
             
-        return false;
+       // return false;
         
         $res = `$cmd`;
         //exit;
-        file_put_contents($output.'.log'. $cmd."\n\n". $res);
+        file_put_contents($output.'.log', $cmd."\n\n". $res);
         // since this only appears when we change.. it's ok to dump it out..
-          echo "<!-- Compiled javascript
+        echo "<!-- Compiled javascript
             " . htmlspecialchars($res) . "
             -->";
             
@@ -280,7 +420,9 @@ class Pman_Core_JsCompile  extends Pman
             
             return true;
         }
-        echo "<!-- JS COMPILE ERROR: packed file did not exist  -->";
+        
+         
+        echo "\n<!-- JS COMPILE ERROR: packed file did not exist  -->\n";
         return false;
         
     }
@@ -322,8 +464,8 @@ class Pman_Core_JsCompile  extends Pman
         
         $o = HTML_FlexyFramework::get()->Pman_Core;
         
-        if (empty($o['jspacker']) || !file_exists($o['jspacker'].'/pack.js')) {
-            $this->err ="no jstoolkit path set [Pman_Core][jspacker] to the
+        if (empty($o['packseed']) || !file_exists($o['packseed'].'/pack.js')) {
+            $this->err ="no jstoolkit path set [Pman_Core][packseed] to the
                     introspection documentation directory where pack.js is located.";
             if ($this->cli) echo $this->err;
             return false;
@@ -331,7 +473,7 @@ class Pman_Core_JsCompile  extends Pman
         
         // should we be more specirfic!??!?!?
          
-        $cmd = "$seed {$o['jspacker']}/pack.js -m $proj  -a  $src/*.js";
+        $cmd = "$seed {$o['packseed']}/pack.js -m $proj  -a  $src/*.js";
         echo "$cmd\n";
         passthru($cmd);
         // technically we should trash old compiled files..