123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243 |
- <?php
- /*
- * $Id: JslLintTask.php 905 2010-10-05 16:28:03Z mrook $
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- * This software consists of voluntary contributions made by many individuals
- * and is licensed under the LGPL. For more information please see
- * <http://phing.info>.
- */
-
- require_once 'phing/Task.php';
- require_once 'phing/util/DataStore.php';
- /**
- * A Javascript lint task. Checks syntax of Javascript files.
- * Javascript lint (http://www.javascriptlint.com) must be in the system path.
- * This class is based on Knut Urdalen's PhpLintTask.
- *
- * @author Stefan Priebsch <stefan.priebsch@e-novative.de>
- * @version $Id: JslLintTask.php 905 2010-10-05 16:28:03Z mrook $
- * @package phing.tasks.ext
- */
- class JslLintTask extends Task
- {
- protected $file; // the source file (from xml attribute)
- protected $filesets = array(); // all fileset objects assigned to this task
- protected $showWarnings = true;
- protected $haltOnFailure = false;
- protected $hasErrors = false;
- private $badFiles = array();
-
- private $cache = null;
- private $conf = null;
-
- private $executable = "jsl";
- /**
- * Sets the flag if warnings should be shown
- * @param boolean $show
- */
- public function setShowWarnings($show) {
- $this->showWarnings = StringHelper::booleanValue($show);
- }
- /**
- * The haltonfailure property
- * @param boolean $aValue
- */
- public function setHaltOnFailure($aValue) {
- $this->haltOnFailure = $aValue;
- }
-
- /**
- * File to be performed syntax check on
- * @param PhingFile $file
- */
- public function setFile(PhingFile $file) {
- $this->file = $file;
- }
-
- /**
- * Whether to store last-modified times in cache
- *
- * @param PhingFile $file
- */
- public function setCacheFile(PhingFile $file)
- {
- $this->cache = new DataStore($file);
- }
- /**
- * jsl config file
- *
- * @param PhingFile $file
- */
- public function setConfFile(PhingFile $file)
- {
- $this->conf = $file;
- }
-
- public function setExecutable($path){
- $this->executable = $path;
- }
-
- public function getExecutable(){
- return $this->executable;
- }
- /**
- * Nested creator, creates a FileSet for this task
- *
- * @return FileSet The created fileset object
- */
- function createFileSet() {
- $num = array_push($this->filesets, new FileSet());
- return $this->filesets[$num-1];
- }
-
- /**
- * Execute lint check against PhingFile or a FileSet
- */
- public function main() {
- if(!isset($this->file) and count($this->filesets) == 0) {
- throw new BuildException("Missing either a nested fileset or attribute 'file' set");
- }
-
- exec($this->executable, $output);
- if (!preg_match('/JavaScript\sLint/', implode('', $output))) throw new BuildException('Javascript Lint not found');
-
- if($this->file instanceof PhingFile) {
- $this->lint($this->file->getPath());
- } else { // process filesets
- $project = $this->getProject();
- foreach($this->filesets as $fs) {
- $ds = $fs->getDirectoryScanner($project);
- $files = $ds->getIncludedFiles();
- $dir = $fs->getDir($this->project)->getPath();
- foreach($files as $file) {
- $this->lint($dir.DIRECTORY_SEPARATOR.$file);
- }
- }
- }
-
- if ($this->haltOnFailure && $this->hasErrors) throw new BuildException('Syntax error(s) in JS files:' .implode(', ',$this->badFiles));
- }
-
- /**
- * Performs the actual syntax check
- *
- * @param string $file
- * @return void
- */
- protected function lint($file)
- {
- $command = $this->executable . ' -output-format ' . escapeshellarg('file:__FILE__;line:__LINE__;message:__ERROR__') . ' ';
- if (isset($this->conf)) {
- $command .= '-conf ' . $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);
- $summary = $messages[sizeof($messages) - 1];
- preg_match('/(\d+)\serror/', $summary, $matches);
- $errorCount = $matches[1];
-
- preg_match('/(\d+)\swarning/', $summary, $matches);
- $warningCount = $matches[1];
- $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);
- }
- }
-
- if($errorCount > 0)
- {
- $this->log($file . ': ' . $errorCount . ' errors detected', Project::MSG_ERR);
- foreach ($errors as $error) {
- $this->log('- line ' . $error['line'] . (isset($error['column']) ? ' column ' . $error['column'] : '') . ': ' . $error['message'], Project::MSG_ERR);
- }
- $this->badFiles[] = $file;
- $this->hasErrors = true;
- } else if (!$this->showWarnings || $warningCount == 0) {
- $this->log($file . ': No syntax errors detected', Project::MSG_INFO);
- }
- if ($this->cache)
- {
- $this->cache->put($file, filemtime($file));
- }
- } else {
- throw new BuildException('Permission denied: '.$file);
- }
- } else {
- throw new BuildException('File not found: '.$file);
- }
- }
- }
|