|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * |
| 5 | + * |
| 6 | + * Example usage: |
| 7 | + * |
| 8 | + * |
| 9 | + */ |
| 10 | +require_once 'phing/Task.php'; |
| 11 | + |
| 12 | +class YamlLintTask extends Task { |
| 13 | + |
| 14 | + protected $filesets = array(); // all fileset objects assigned to this task |
| 15 | + |
| 16 | + /** |
| 17 | + * Path the the yaml-cli executable. |
| 18 | + * |
| 19 | + * @var string |
| 20 | + */ |
| 21 | + protected $executable = 'yaml-cli'; |
| 22 | + |
| 23 | + /** |
| 24 | + * Stop processing on first failed scenario. |
| 25 | + * |
| 26 | + * @var bool |
| 27 | + */ |
| 28 | + protected $haltonerror = false; |
| 29 | + |
| 30 | + /** |
| 31 | + * The return value. |
| 32 | + * |
| 33 | + * @var null |
| 34 | + */ |
| 35 | + protected $return_property = null; |
| 36 | + |
| 37 | + /** |
| 38 | + * Nested adder, adds a set of files (nested fileset attribute). |
| 39 | + * |
| 40 | + * @param FileSet $fs |
| 41 | + * @return void |
| 42 | + */ |
| 43 | + public function addFileSet(FileSet $fs) |
| 44 | + { |
| 45 | + $this->filesets[] = $fs; |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Set the path to the yaml-cli executable. |
| 50 | + * |
| 51 | + * @param string $str The executable |
| 52 | + * |
| 53 | + * @return void |
| 54 | + */ |
| 55 | + public function setExecutable($str) |
| 56 | + { |
| 57 | + $this->executable = $str; |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Sets the flag if test execution should stop in the event of a failure. |
| 62 | + * |
| 63 | + * @param bool $stop If all tests should stop on failure. |
| 64 | + * |
| 65 | + * @return void |
| 66 | + */ |
| 67 | + public function setHaltonerror($stop) |
| 68 | + { |
| 69 | + $this->haltonerror = StringHelper::booleanValue($stop); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * The Phing property the return code should be assigned to. |
| 74 | + * |
| 75 | + * @param string $str The Phing property. |
| 76 | + * |
| 77 | + * @return void |
| 78 | + */ |
| 79 | + public function setReturnProperty($str) |
| 80 | + { |
| 81 | + $this->return_property = $str; |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * The main entry point method. |
| 86 | + * |
| 87 | + * @throws BuildException |
| 88 | + * @return bool $return |
| 89 | + */ |
| 90 | + public function main() { |
| 91 | + |
| 92 | + if (count($this->filesets) == 0) { |
| 93 | + throw new BuildException("You must define a fileset."); |
| 94 | + } |
| 95 | + |
| 96 | + if (!$this->yamlCliExists($this->executable)) { |
| 97 | + throw new BuildException( |
| 98 | + 'ERROR: the yaml-cli executable "'.$this->executable.'" does not exist.', |
| 99 | + $this->getLocation() |
| 100 | + ); |
| 101 | + } |
| 102 | + |
| 103 | + foreach ($this->getFilesetFiles() as $file) { |
| 104 | + $this->log("Linting $file", Project::MSG_VERBOSE); |
| 105 | + $command = "{$this->executable} lint $file"; |
| 106 | + $last_line = system($command, $return); |
| 107 | + |
| 108 | + if ($return) { |
| 109 | + // If this is non-zero, there was a failure. |
| 110 | + $this->log("The file $file does not contain valid YAML.", Project::MSG_ERR); |
| 111 | + // Throw an exception if Behat fails. |
| 112 | + if ($this->haltonerror && $return != 0) { |
| 113 | + throw new BuildException("yaml-cli exited with code $return"); |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + $this->log("All scanned YAML files are valid."); |
| 119 | + |
| 120 | + if (!empty($this->return_property)) { |
| 121 | + $this->getProject() |
| 122 | + ->setProperty($this->return_property, $return); |
| 123 | + } |
| 124 | + |
| 125 | + return $return != 0; |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Checks if the yaml-cli executable exists. |
| 130 | + * |
| 131 | + * @param string $executable The path to Behat |
| 132 | + * |
| 133 | + * @return bool |
| 134 | + */ |
| 135 | + protected function yamlCliExists($executable) |
| 136 | + { |
| 137 | + // First check if the executable path is a file. |
| 138 | + if (is_file($executable)) { |
| 139 | + return true; |
| 140 | + } |
| 141 | + // Now check to see if the executable has a path. |
| 142 | + $return = shell_exec('type '.escapeshellarg($executable)); |
| 143 | + |
| 144 | + return (empty($return) ? false : true); |
| 145 | + } |
| 146 | + |
| 147 | + |
| 148 | + /** |
| 149 | + * Return the list of files to parse |
| 150 | + * |
| 151 | + * @see PhpCodeSnifferTask |
| 152 | + * |
| 153 | + * @return string[] list of absolute files to parse |
| 154 | + */ |
| 155 | + protected function getFilesetFiles() |
| 156 | + { |
| 157 | + $files = array(); |
| 158 | + |
| 159 | + foreach ($this->filesets as $fs) { |
| 160 | + $dir = $fs->getDir($this->project)->getAbsolutePath(); |
| 161 | + foreach ($fs->getDirectoryScanner($this->project)->getIncludedFiles() as $filename) { |
| 162 | + $file_path = $dir . DIRECTORY_SEPARATOR . $filename; |
| 163 | + $files[] = $file_path; |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + return $files; |
| 168 | + } |
| 169 | +} |
0 commit comments