Exemplo n.º 1
0
 /**
  * Return the list of files to parse
  *
  * @return string[] list of absolute files to parse
  */
 protected function getFilesToParse()
 {
     $filesToParse = array();
     if ($this->file instanceof PhingFile) {
         $filesToParse[] = $this->file->getPath();
     } else {
         // append any files in filesets
         foreach ($this->filesets as $fs) {
             $dir = $fs->getDir($this->project)->getAbsolutePath();
             foreach ($fs->getDirectoryScanner($this->project)->getIncludedFiles() as $filename) {
                 $fileAbsolutePath = $dir . DIRECTORY_SEPARATOR . $filename;
                 if ($this->cache) {
                     $lastMTime = $this->cache->get($fileAbsolutePath);
                     $currentMTime = filemtime($fileAbsolutePath);
                     if ($lastMTime >= $currentMTime) {
                         continue;
                     } else {
                         $this->cache->put($fileAbsolutePath, $currentMTime);
                     }
                 }
                 $filesToParse[] = $fileAbsolutePath;
             }
         }
     }
     return $filesToParse;
 }
 /**
  * Performs the actual syntax check
  *
  * @param  string $file
  *
  * @throws BuildException
  *
  * @return bool|void
  */
 protected function lint($file)
 {
     $command = $this->executable . ' -output-format ' . escapeshellarg('file:__FILE__;line:__LINE__;message:__ERROR__') . ' ';
     if (isset($this->conf)) {
         $command .= '-conf ' . escapeshellarg($this->conf->getPath()) . ' ';
     }
     $command .= '-process ';
     if (file_exists($file)) {
         if (is_readable($file)) {
             if ($this->cache) {
                 $lastmtime = $this->cache->get($file);
                 if ($lastmtime >= filemtime($file)) {
                     $this->log("Not linting '" . $file . "' due to cache", Project::MSG_DEBUG);
                     return false;
                 }
             }
             $messages = array();
             exec($command . '"' . $file . '"', $messages, $return);
             if ($return > 100) {
                 throw new BuildException("Could not execute Javascript Lint executable '{$this->executable}'");
             }
             $summary = $messages[sizeof($messages) - 1];
             preg_match('/(\\d+)\\serror/', $summary, $matches);
             $errorCount = count($matches) > 1 ? $matches[1] : 0;
             preg_match('/(\\d+)\\swarning/', $summary, $matches);
             $warningCount = count($matches) > 1 ? $matches[1] : 0;
             $errors = array();
             $warnings = array();
             if ($errorCount > 0 || $warningCount > 0) {
                 $last = false;
                 foreach ($messages as $message) {
                     $matches = array();
                     if (preg_match('/^(\\.*)\\^$/', $message)) {
                         $column = strlen($message);
                         if ($last == 'error') {
                             $errors[count($errors) - 1]['column'] = $column;
                         } else {
                             if ($last == 'warning') {
                                 $warnings[count($warnings) - 1]['column'] = $column;
                             }
                         }
                         $last = false;
                     }
                     if (!preg_match('/^file:(.+);line:(\\d+);message:(.+)$/', $message, $matches)) {
                         continue;
                     }
                     $msg = $matches[3];
                     $data = array('filename' => $matches[1], 'line' => $matches[2], 'message' => $msg);
                     if (preg_match('/^.*error:.+$/i', $msg)) {
                         $errors[] = $data;
                         $last = 'error';
                     } else {
                         if (preg_match('/^.*warning:.+$/i', $msg)) {
                             $warnings[] = $data;
                             $last = 'warning';
                         }
                     }
                 }
             }
             if ($this->showWarnings && $warningCount > 0) {
                 $this->log($file . ': ' . $warningCount . ' warnings detected', Project::MSG_WARN);
                 foreach ($warnings as $warning) {
                     $this->log('- line ' . $warning['line'] . (isset($warning['column']) ? ' column ' . $warning['column'] : '') . ': ' . $warning['message'], Project::MSG_WARN);
                 }
                 $this->hasWarnings = true;
             }
             if ($errorCount > 0) {
                 $this->log($file . ': ' . $errorCount . ' errors detected', Project::MSG_ERR);
                 if (!isset($this->badFiles[$file])) {
                     $this->badFiles[$file] = array();
                 }
                 foreach ($errors as $error) {
                     $message = 'line ' . $error['line'] . (isset($error['column']) ? ' column ' . $error['column'] : '') . ': ' . $error['message'];
                     $this->log('- ' . $message, Project::MSG_ERR);
                     array_push($this->badFiles[$file], $message);
                 }
                 $this->hasErrors = true;
             } else {
                 if (!$this->showWarnings || $warningCount == 0) {
                     $this->log($file . ': No syntax errors detected', Project::MSG_VERBOSE);
                     if ($this->cache) {
                         $this->cache->put($file, filemtime($file));
                     }
                 }
             }
         } else {
             throw new BuildException('Permission denied: ' . $file);
         }
     } else {
         throw new BuildException('File not found: ' . $file);
     }
 }