-
Notifications
You must be signed in to change notification settings - Fork 5
/
pretty
executable file
·130 lines (110 loc) · 3.72 KB
/
pretty
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
#!/usr/bin/env php
<?php
ini_set('display_errors', '1');
error_reporting(E_ALL);
putenv(join("", array("PATH=vendor/bin", PATH_SEPARATOR, getenv("PATH"))));
$commands = [
/**
* The default command will not change any file, it's just an analysis.
*/
'default' => [
'php-cs-fixer' => 'php-cs-fixer fix --dry-run --allow-risky=yes --diff',
'phpcs' => 'phpcs',
'default' => 'phpcs --standard=PSR2 --extensions=php --ignore="vendor/*" .',
],
/**
* The `fix` command will modify the files to try to fix the errors.
*/
'fix' => [
'php-cs-fixer' => 'php-cs-fixer fix --allow-risky=yes',
'phpcs' => 'phpcbf',
'default' => 'phpcbf --standard=PSR2 --extensions=php --ignore="vendor/*" .',
],
/**
* In CI we disable the cache.
*/
'ci' => [
'php-cs-fixer' => 'php-cs-fixer fix --dry-run --allow-risky=yes --diff --using-cache=no',
'phpcs' => 'phpcs --no-cache --no-colors',
'default' => 'phpcs --standard=PSR2 --extensions=php --ignore="vendor/*" --no-cache --no-colors .',
],
];
$task = isset($argv[1]) ? $argv[1] : 'default';
if ($task === 'help') {
help();
exit(0);
}
if (!isset($commands[$task])) {
echo "Unrecognized command '$task'\n";
help();
exit(1);
}
$commands = $commands[$task];
$command = '';
// Detect which tool to run
if (file_exists('.phpcs.xml') || file_exists('phpcs.xml') || file_exists('.phpcs.xml.dist') || file_exists('phpcs.xml.dist')) {
echo "PHP CodeSniffer configuration file found, running CodeSniffer with version\n";
$command = $commands['phpcs'];
$program = explode(' ', $command, 2)[0];
assertPhpCodeSnifferIsInstalled($program);
passthru($program." --version");
echo "\n";
} elseif (file_exists('.php_cs') || file_exists('.php_cs.dist')) {
echo "PHP-CS-Fixer configuration file found, running PHP-CS-Fixer with version\n";
assertPhpCsFixerIsInstalled();
$command = $commands['php-cs-fixer'];
passthru("php-cs-fixer --version");
echo "\n";
} else {
echo "No configuration file found, running PHP CodeSniffer with PSR-2 with version\n";
$command = $commands['default'];
$program = explode(' ', $command, 2)[0];
assertPhpCodeSnifferIsInstalled($program);
passthru($program." --version");
echo "\n";
}
// Run the analysis
passthru($command, $exitCode);
if ($exitCode !== 0) {
echo "Errors were found, run 'pretty fix' to fix them.\n";
}
exit($exitCode);
function commandExists($command)
{
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
$return = shell_exec('where ' . escapeshellarg($command));
} else {
$return = shell_exec('which ' . escapeshellarg($command));
}
return !empty($return);
}
function assertPhpCodeSnifferIsInstalled($command)
{
if (!commandExists($command)) {
echo <<<INSTRUCTIONS
ERROR: PHP CodeSniffer does not seem to be installed because the '$command' program cannot be found.
You can install it by following the instructions here: https://github.com/squizlabs/PHP_CodeSniffer#installation
INSTRUCTIONS;
exit(1);
}
}
function assertPhpCsFixerIsInstalled()
{
if (!commandExists('php-cs-fixer')) {
echo <<<'INSTRUCTIONS'
ERROR: PHP-CS-Fixer does not seem to be installed because the 'php-cs-fixer' program cannot be found.
You can install it by following the instructions here: https://github.com/FriendsOfPHP/PHP-CS-Fixer#installation
INSTRUCTIONS;
exit(1);
}
}
function help()
{
echo <<<'HELP'
Available commands:
- pretty: runs an analysis (will not fix the code)
- pretty fix: fix as many errors as possible in the code
- pretty ci: runs an analysis in a continuous integration environment
- pretty help: displays this help
HELP;
}