home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / phing / tasks / ext / JslLintTask.php < prev    next >
Encoding:
PHP Script  |  2007-10-07  |  4.4 KB  |  142 lines

  1. <?php
  2. /*
  3.  *  $Id$
  4.  *
  5.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6.  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7.  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8.  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9.  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11.  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13.  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15.  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16.  *
  17.  * This software consists of voluntary contributions made by many individuals
  18.  * and is licensed under the LGPL. For more information please see
  19.  * <http://phing.info>.
  20.  */
  21.   
  22. require_once 'phing/Task.php';
  23.  
  24.   /**
  25.   * A Javascript lint task. Checks syntax of Javascript files.
  26.   * Javascript lint (http://www.javascriptlint.com) must be in the system path.
  27.   * This class is based on Knut Urdalen's PhpLintTask.
  28.   *
  29.   * @author Stefan Priebsch <stefan.priebsch@e-novative.de>
  30.   */
  31.   class JslLintTask extends Task
  32.   {
  33.     protected $file;  // the source file (from xml attribute)
  34.     protected $filesets = array(); // all fileset objects assigned to this task
  35.  
  36.     protected $haltOnFailure = false;
  37.     protected $hasErrors = false;
  38.     private $badFiles = array();
  39.  
  40.     /**
  41.      * The haltonfailure property
  42.      * @param boolean $aValue
  43.      */
  44.     public function setHaltOnFailure($aValue) {
  45.       $this->haltOnFailure = $aValue;
  46.     }
  47.   
  48.     /**
  49.      * File to be performed syntax check on
  50.      * @param PhingFile $file
  51.      */
  52.     public function setFile(PhingFile $file) {
  53.       $this->file = $file;
  54.     }
  55.     
  56.     /**
  57.      * Nested creator, creates a FileSet for this task
  58.      *
  59.      * @return FileSet The created fileset object
  60.      */
  61.     function createFileSet() {
  62.       $num = array_push($this->filesets, new FileSet());
  63.       return $this->filesets[$num-1];
  64.     }
  65.   
  66.     /**
  67.      * Execute lint check against PhingFile or a FileSet
  68.      */
  69.     public function main() {
  70.       if(!isset($this->file) and count($this->filesets) == 0) {
  71.         throw new BuildException("Missing either a nested fileset or attribute 'file' set");
  72.       }
  73.   
  74.       if($this->file instanceof PhingFile) {
  75.         $this->lint($this->file->getPath());
  76.       } else { // process filesets
  77.         $project = $this->getProject();
  78.         foreach($this->filesets as $fs) {
  79.           $ds = $fs->getDirectoryScanner($project);
  80.           $files = $ds->getIncludedFiles();
  81.           $dir = $fs->getDir($this->project)->getPath();
  82.           foreach($files as $file) {
  83.             $this->lint($dir.DIRECTORY_SEPARATOR.$file);
  84.           }
  85.         }
  86.       }
  87.   
  88.       if ($this->haltOnFailure && $this->hasErrors) throw new BuildException('Syntax error(s) in JS files:' .implode(', ',$this->badFiles));
  89.     }
  90.   
  91.     /**
  92.      * Performs the actual syntax check
  93.      *
  94.      * @param string $file
  95.      * @return void
  96.      */
  97.     protected function lint($file)
  98.     {
  99.       exec('jsl', $output);
  100.       if (!preg_match('/JavaScript\sLint/', implode('', $output))) throw new BuildException('Javascript Lint not found');
  101.     
  102.       $command = 'jsl -process ';
  103.  
  104.       if(file_exists($file))
  105.       {
  106.         if(is_readable($file))
  107.         {
  108.           $message = array();
  109.           exec($command.'"'.$file.'"', $message);
  110.  
  111.           $summary = $message[sizeof($message) - 1];
  112.  
  113.           preg_match('/^(.*)\serror/', $summary, $matches);
  114.           $errors = $matches[0];
  115.           
  116.           preg_match('/^(.*)\swarning/', $summary, $matches);
  117.           $warnings = $matches[0];
  118.  
  119.           if(0 != $warnings)
  120.           {
  121.             $this->log($file . ': ' . $warnings . ' warnings detected', Project::MSG_INFO);
  122.           }
  123.             
  124.           if(0 != $errors)
  125.           {
  126.             $this->log($file . ': ' . $errors . ' errors detected', Project::MSG_ERR);
  127.             $this->badFiles[] = $file;
  128.             $this->hasErrors = true;
  129.           } else {
  130.             $this->log($file . ': No syntax errors detected', Project::MSG_INFO);
  131.           }
  132.         } else {
  133.           throw new BuildException('Permission denied: '.$file);
  134.         }
  135.       } else {
  136.         throw new BuildException('File not found: '.$file);
  137.       }
  138.     }
  139.   }
  140.  
  141. ?>
  142.