forked from instaclick/gherkincs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcuke
executable file
·184 lines (146 loc) · 4.71 KB
/
cuke
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/usr/bin/env php -d memory_limit=-1
<?php
/**
* Cuke - Coding Standard Checker and Semantic Analyzer for Gherkin
*
* @copyright 2013 Instaclick Inc.
*
* @author Juti Noppornpitak <[email protected]>
*/
require_once 'vendor/autoload.php';
$basePath = dirname(__FILE__);
use IC\Gherkinics\AnalyzerManager;
use IC\Gherkinics\Analyzer;
use IC\Gherkinics\Core;
use IC\Gherkinics\Lexer;
use IC\Gherkinics\Util\Output;
use IC\Gherkinics\Printer;
/**
* Check if the flag is given
*
* @param string $flagName the flag name
* @param array $argumentList the list of arguments
*
* @return boolean
*/
function hasFlag($flagName, array $argumentList)
{
return in_array('--' . $flagName, $argumentList);
}
/**
* Retrieve the flag index
*
* @param string $flagName the flag name
* @param array $argumentList the list of arguments
*
* @return string|null
*/
function getFlagIndex($flagName, array $argumentList)
{
return hasFlag($flagName, $argumentList) ? array_search('--' . $flagName, $argumentList) : null;
}
/**
* Retrieve the flag value
*
* @param string $flagName the flag name
* @param array $argumentList the list of arguments
* @param integer $flagIndex the flag index
*
* @return boolean
*/
function getFlagFilePathValue($flagName, array $argumentList, $flagIndex)
{
$value = $argumentList[$flagIndex + 1];
// Detect the absolute path (UNIX only).
if ( ! preg_match('/^\//', $value)) {
$value = getcwd() . '/' . $value;
}
return $value;
}
/**
* Main
*
* @param array $argumentList the argument list
*/
function main($argumentList)
{
global $basePath;
static $validOutputOptionList = array(
'jcs', // Jenkins Checkstyle
'html', // HTML
);
$startingTime = microtime(true);
$output = new Output();
$manager = new AnalyzerManager();
$cuke = new Core();
$argumentLength = count($argumentList);
if ($argumentLength < 2) {
$output->writeln('USAGE: cuke [--jcs /path/for/report] [--html /path/to/directory/for/report] /path/to/config_file /path/pattern/to/scan');
exit(1);
}
$configPath = $argumentList[$argumentLength - 2];
$targetPath = $argumentList[$argumentLength - 1];
$optionMap = array();
// Parse the arguments
foreach ($validOutputOptionList as $name) {
if ( ! hasFlag($name, $argumentList)) {
continue;
}
$index = getFlagIndex($name, $argumentList);
$value = getFlagFilePathValue($name, $argumentList, $index);
$optionMap[$name] = $value;
}
// Set up the analyzer manager.
$manager->setLexer(new Lexer());
$config = simplexml_load_file($configPath);
if ( ! isset($config->analyzers)) {
$output->writeln('Notice: the configuration file is invalid.');
exit(1);
}
if ( ! isset($config->analyzers->analyzer)) {
$output->writeln('Terminated due to that no analyzers are found.');
exit(1);
}
foreach ($config->analyzers->analyzer as $analyzer) {
$analyzerClass = '\\'.$analyzer['class'];
$output->write(' Registering analyzer: ' . $analyzerClass);
$manager->registerAnalyzer(new $analyzerClass());
$output->writeln("\r[DONE]");
}
// Set up the core object.
$cuke->setBasePath($targetPath);
$cuke->setAnalyzerManager($manager);
$output->writeln(PHP_EOL . 'Analyzing feature files...');
$pathToFeedbackMap = $cuke->scan(is_dir($targetPath) ? $targetPath . '/*' : $targetPath);
$output->writeln('');
// Prepare the printer.
$printer = null;
switch (true) {
case isset($optionMap['html']):
$printer = new Printer\HtmlPrinter(
$basePath . '/view', // template pool
$basePath . '/static', // static pool
$optionMap['html'],
$targetPath
);
break;
case isset($optionMap['jcs']):
$printer = new Printer\JenkinsReportPrinter(
$basePath . '/view', // template pool
$optionMap['jcs'],
$targetPath
);
break;
default:
$printer = new Printer\TerminalPrinter($output, $basePath);
}
$printer->doPrint($pathToFeedbackMap);
$output->writeln('Analysis complete.');
$output->writeln(PHP_EOL . 'Please note that this tool only detects classic errors.' . PHP_EOL);
$output->writeln(sprintf('Peak memory usage in MB: %.2f', memory_get_peak_usage(true) / 1024 / 1024));
$output->writeln(sprintf('Time elapsed in seconds: %.2f ', microtime(true) - $startingTime));
if (count($pathToFeedbackMap) > 0) {
exit(1);
}
}
main(array_slice($argv, 1));