Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.

Commit 8cf94b6

Browse files
authored
Fixes #857: Adding YAML linting target. (#904)
* Removing deprecated phingcludes. * Fixes #857: Adding YAML linting target. * Updating yaml-cli. * Replacing other console yaml command implementation. * Updating yaml-cli. * Halting on YAML validation error.
1 parent 03010ba commit 8cf94b6

10 files changed

+207
-126
lines changed

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"doctrine/common": "^2.5",
3030
"vierbergenlars/php-semver": "^3.0",
3131
"grasmash/drupal-security-warning": "^1.0.0",
32-
"grasmash/yaml-cli": "^0.2.2"
32+
"grasmash/yaml-cli": "^0.3"
3333
},
3434
"autoload": {
3535
"psr-4": {

composer.lock

+6-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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/BltLogger.php

Whitespace-only changes.

phing/phingcludes/FileInFilesetCondition.php

-113
This file was deleted.

phing/phingcludes/YamlLintTask.php

+169
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
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+
}

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/simplesamlphp.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
<!-- Sets a value in project.yml to let other targets know simplesamlphp is installed -->
1515
<echo>Updating ${blt.config-files.project}...</echo>
16-
<exec dir="${repo.root}" command="${composer.bin}/drupal yaml:update:value ${blt.config-files.project} simplesamlphp true" logoutput="true" checkreturn="true" passthru="true" level="${blt.exec_level}"/>
16+
<exec dir="${repo.root}" command="${composer.bin}/yaml-cli update:value ${blt.config-files.project} simplesamlphp true" logoutput="true" checkreturn="true" passthru="true" level="${blt.exec_level}"/>
1717

1818
<!-- Creates a symlink from the docroot to the web accessible library dir. -->
1919
<echo>Creating a symbolic link from ${docroot}/simplesaml to web accessible directory in the simplesamlphp library</echo>

0 commit comments

Comments
 (0)