Skip to content

Commit 48b517e

Browse files
committed
Fixes acquia#857: Adding YAML linting target.
1 parent 37fcc50 commit 48b517e

File tree

4 files changed

+196
-3
lines changed

4 files changed

+196
-3
lines changed

phing/build.xml

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<taskdef name="filterFileListByFileSet" classname="phingcludes.FilterFileListByFileSetTask"/>
1010
<taskdef name="verbosityTask" classname="phingcludes.VerbosityTask"/>
1111
<taskdef name="phpVariable" classname="phingcludes.PhpVariableTask"/>
12+
<taskdef name="yamlLint" classname="phingcludes.YamlLintTask"/>
1213

1314
<if>
1415
<available property="xdebug.enabled" extension="xdebug"/>

phing/phingcludes/YamlLintTask.php

+168
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
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+
// Throw an exception if Behat fails.
111+
if ($this->haltonerror && $return != 0) {
112+
throw new BuildException("yaml-cli exited with code $return");
113+
}
114+
}
115+
}
116+
117+
$this->log("All scanned YAML files are valid.");
118+
119+
if (!empty($this->return_property)) {
120+
$this->getProject()
121+
->setProperty($this->return_property, $return);
122+
}
123+
124+
return $return != 0;
125+
}
126+
127+
/**
128+
* Checks if the yaml-cli executable exists.
129+
*
130+
* @param string $executable The path to Behat
131+
*
132+
* @return bool
133+
*/
134+
protected function yamlCliExists($executable)
135+
{
136+
// First check if the executable path is a file.
137+
if (is_file($executable)) {
138+
return true;
139+
}
140+
// Now check to see if the executable has a path.
141+
$return = shell_exec('type '.escapeshellarg($executable));
142+
143+
return (empty($return) ? false : true);
144+
}
145+
146+
147+
/**
148+
* Return the list of files to parse
149+
*
150+
* @see PhpCodeSnifferTask
151+
*
152+
* @return string[] list of absolute files to parse
153+
*/
154+
protected function getFilesetFiles()
155+
{
156+
$files = array();
157+
158+
foreach ($this->filesets as $fs) {
159+
$dir = $fs->getDir($this->project)->getAbsolutePath();
160+
foreach ($fs->getDirectoryScanner($this->project)->getIncludedFiles() as $filename) {
161+
$file_path = $dir . DIRECTORY_SEPARATOR . $filename;
162+
$files[] = $file_path;
163+
}
164+
}
165+
166+
return $files;
167+
}
168+
}

phing/tasks/filesets.xml

+18-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,13 @@
2020
<exclude name="**/vendor/**/*"/>
2121
</patternset>
2222

23+
<patternset id="files.yaml">
24+
<include name="**/*.yaml"/>
25+
<include name="**/*.yml"/>
26+
</patternset>
27+
2328
<!--
24-
PHP Files
29+
PHP files
2530
-->
2631
<fileset dir="${repo.root}" id="files.php.all">
2732
<patternset refid="files.php"/>
@@ -40,11 +45,22 @@
4045
</fileset>
4146

4247
<!--
43-
Frontend Files
48+
Frontend files
4449
-->
4550
<fileset dir="${docroot}/themes/custom" id="files.frontend.custom.themes" expandsymboliclinks="true">
4651
<patternset refid="files.frontend"/>
4752
</fileset>
4853

54+
<!--
55+
Other files
56+
-->
57+
<fileset dir="${repo.root}/config" id="files.yaml.cm">
58+
<patternset refid="files.yaml"/>
59+
</fileset>
60+
61+
<fileset dir="${docroot}/modules/custom" id="files.yaml.custom">
62+
<patternset refid="files.yaml"/>
63+
</fileset>
64+
4965

5066
</project>

phing/tasks/validate.xml

+9-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
<!-- Run all validation targets. -->
77
<target name="validate:all" description="Runs all code validation targets."
8-
depends="validate:composer, validate:lint, validate:phpcs, validate:twig"
8+
depends="validate:composer, validate:lint, validate:phpcs, validate:twig, validate:yaml"
99
hidden="true" />
1010

1111
<!-- Verify that composer json and lock files are in sync. -->
@@ -32,6 +32,14 @@
3232
</phplint>
3333
</target>
3434

35+
<!-- Run linter against all yaml files in repository. -->
36+
<target name="validate:yaml" description="Runs a PHP Lint against all code.">
37+
<yamlLint executable="${composer.bin}/yaml-cli">
38+
<fileset refid="files.yaml.cm"/>
39+
<fileset refid="files.yaml.custom"/>
40+
</yamlLint>
41+
</target>
42+
3543
<!-- Run code sniffer against specific files. -->
3644
<target name="validate:phpcs:files" description="" hidden="true">
3745
<fail unless="files" message="Missing files parameter."/>

0 commit comments

Comments
 (0)