JslLintTask.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. /*
  3. * $Id: JslLintTask.php 905 2010-10-05 16:28:03Z mrook $
  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. require_once 'phing/Task.php';
  22. require_once 'phing/util/DataStore.php';
  23. /**
  24. * A Javascript lint task. Checks syntax of Javascript files.
  25. * Javascript lint (http://www.javascriptlint.com) must be in the system path.
  26. * This class is based on Knut Urdalen's PhpLintTask.
  27. *
  28. * @author Stefan Priebsch <stefan.priebsch@e-novative.de>
  29. * @version $Id: JslLintTask.php 905 2010-10-05 16:28:03Z mrook $
  30. * @package phing.tasks.ext
  31. */
  32. class JslLintTask extends Task
  33. {
  34. protected $file; // the source file (from xml attribute)
  35. protected $filesets = array(); // all fileset objects assigned to this task
  36. protected $showWarnings = true;
  37. protected $haltOnFailure = false;
  38. protected $hasErrors = false;
  39. private $badFiles = array();
  40. private $cache = null;
  41. private $conf = null;
  42. private $executable = "jsl";
  43. /**
  44. * Sets the flag if warnings should be shown
  45. * @param boolean $show
  46. */
  47. public function setShowWarnings($show) {
  48. $this->showWarnings = StringHelper::booleanValue($show);
  49. }
  50. /**
  51. * The haltonfailure property
  52. * @param boolean $aValue
  53. */
  54. public function setHaltOnFailure($aValue) {
  55. $this->haltOnFailure = $aValue;
  56. }
  57. /**
  58. * File to be performed syntax check on
  59. * @param PhingFile $file
  60. */
  61. public function setFile(PhingFile $file) {
  62. $this->file = $file;
  63. }
  64. /**
  65. * Whether to store last-modified times in cache
  66. *
  67. * @param PhingFile $file
  68. */
  69. public function setCacheFile(PhingFile $file)
  70. {
  71. $this->cache = new DataStore($file);
  72. }
  73. /**
  74. * jsl config file
  75. *
  76. * @param PhingFile $file
  77. */
  78. public function setConfFile(PhingFile $file)
  79. {
  80. $this->conf = $file;
  81. }
  82. public function setExecutable($path){
  83. $this->executable = $path;
  84. }
  85. public function getExecutable(){
  86. return $this->executable;
  87. }
  88. /**
  89. * Nested creator, creates a FileSet for this task
  90. *
  91. * @return FileSet The created fileset object
  92. */
  93. function createFileSet() {
  94. $num = array_push($this->filesets, new FileSet());
  95. return $this->filesets[$num-1];
  96. }
  97. /**
  98. * Execute lint check against PhingFile or a FileSet
  99. */
  100. public function main() {
  101. if(!isset($this->file) and count($this->filesets) == 0) {
  102. throw new BuildException("Missing either a nested fileset or attribute 'file' set");
  103. }
  104. exec($this->executable, $output);
  105. if (!preg_match('/JavaScript\sLint/', implode('', $output))) throw new BuildException('Javascript Lint not found');
  106. if($this->file instanceof PhingFile) {
  107. $this->lint($this->file->getPath());
  108. } else { // process filesets
  109. $project = $this->getProject();
  110. foreach($this->filesets as $fs) {
  111. $ds = $fs->getDirectoryScanner($project);
  112. $files = $ds->getIncludedFiles();
  113. $dir = $fs->getDir($this->project)->getPath();
  114. foreach($files as $file) {
  115. $this->lint($dir.DIRECTORY_SEPARATOR.$file);
  116. }
  117. }
  118. }
  119. if ($this->haltOnFailure && $this->hasErrors) throw new BuildException('Syntax error(s) in JS files:' .implode(', ',$this->badFiles));
  120. }
  121. /**
  122. * Performs the actual syntax check
  123. *
  124. * @param string $file
  125. * @return void
  126. */
  127. protected function lint($file)
  128. {
  129. $command = $this->executable . ' -output-format ' . escapeshellarg('file:__FILE__;line:__LINE__;message:__ERROR__') . ' ';
  130. if (isset($this->conf)) {
  131. $command .= '-conf ' . $this->conf->getPath() . ' ';
  132. }
  133. $command .= '-process ';
  134. if(file_exists($file))
  135. {
  136. if(is_readable($file))
  137. {
  138. if ($this->cache)
  139. {
  140. $lastmtime = $this->cache->get($file);
  141. if ($lastmtime >= filemtime($file))
  142. {
  143. $this->log("Not linting '" . $file . "' due to cache", Project::MSG_DEBUG);
  144. return false;
  145. }
  146. }
  147. $messages = array();
  148. exec($command.'"'.$file.'"', $messages);
  149. $summary = $messages[sizeof($messages) - 1];
  150. preg_match('/(\d+)\serror/', $summary, $matches);
  151. $errorCount = $matches[1];
  152. preg_match('/(\d+)\swarning/', $summary, $matches);
  153. $warningCount = $matches[1];
  154. $errors = array();
  155. $warnings = array();
  156. if ($errorCount > 0 || $warningCount > 0) {
  157. $last = false;
  158. foreach ($messages as $message) {
  159. $matches = array();
  160. if (preg_match('/^(\.*)\^$/', $message)) {
  161. $column = strlen($message);
  162. if ($last == 'error') {
  163. $errors[count($errors) - 1]['column'] = $column;
  164. } else if ($last == 'warning') {
  165. $warnings[count($warnings) - 1]['column'] = $column;
  166. }
  167. $last = false;
  168. }
  169. if (!preg_match('/^file:(.+);line:(\d+);message:(.+)$/', $message, $matches)) continue;
  170. $msg = $matches[3];
  171. $data = array('filename' => $matches[1], 'line' => $matches[2], 'message' => $msg);
  172. if (preg_match('/^.*error:.+$/i', $msg)) {
  173. $errors[] = $data;
  174. $last = 'error';
  175. } else if (preg_match('/^.*warning:.+$/i', $msg)) {
  176. $warnings[] = $data;
  177. $last = 'warning';
  178. }
  179. }
  180. }
  181. if($this->showWarnings && $warningCount > 0)
  182. {
  183. $this->log($file . ': ' . $warningCount . ' warnings detected', Project::MSG_WARN);
  184. foreach ($warnings as $warning) {
  185. $this->log('- line ' . $warning['line'] . (isset($warning['column']) ? ' column ' . $warning['column'] : '') . ': ' . $warning['message'], Project::MSG_WARN);
  186. }
  187. }
  188. if($errorCount > 0)
  189. {
  190. $this->log($file . ': ' . $errorCount . ' errors detected', Project::MSG_ERR);
  191. foreach ($errors as $error) {
  192. $this->log('- line ' . $error['line'] . (isset($error['column']) ? ' column ' . $error['column'] : '') . ': ' . $error['message'], Project::MSG_ERR);
  193. }
  194. $this->badFiles[] = $file;
  195. $this->hasErrors = true;
  196. } else if (!$this->showWarnings || $warningCount == 0) {
  197. $this->log($file . ': No syntax errors detected', Project::MSG_INFO);
  198. }
  199. if ($this->cache)
  200. {
  201. $this->cache->put($file, filemtime($file));
  202. }
  203. } else {
  204. throw new BuildException('Permission denied: '.$file);
  205. }
  206. } else {
  207. throw new BuildException('File not found: '.$file);
  208. }
  209. }
  210. }