-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathecs
More file actions
executable file
·94 lines (81 loc) · 3.06 KB
/
Copy pathecs
File metadata and controls
executable file
·94 lines (81 loc) · 3.06 KB
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
#!/usr/bin/env php
<?php declare(strict_types=1);
// the entry point of version 3: its command line is translated and handed to DressCode, a version preset
// maps to the standard itself because the PHP version now comes from the project
foreach ([__DIR__ . '/vendor/autoload.php', __DIR__ . '/../../autoload.php'] as $file) {
if (is_file($file)) {
require $file;
break;
}
}
if (!class_exists(DressCode\Console\Application::class)) {
fwrite(STDERR, "Install dependencies using `composer install`.\n");
exit(2);
}
if (($argv[1] ?? null) === 'migrate') { // version 3 configuration (ncs.php, ncs.xml) to dresscode.neon
$cwd = (string) getcwd();
if (is_file("$cwd/dresscode.neon")) {
fwrite(STDERR, "Error: dresscode.neon already exists here.\n");
exit(2);
}
$migration = new Nette\CodingStandard\ConfigMigration;
$config = $migration->migrate($cwd);
if ($config === null) {
fwrite(STDERR, "Error: nothing to migrate, neither ncs.php nor ncs.xml is here.\n");
exit(2);
}
file_put_contents("$cwd/dresscode.neon", $config);
echo "dresscode.neon written; delete ncs.php and ncs.xml once it works.\n";
foreach ($migration->unknown as $name) {
echo "Warning: no rule for $name, left out.\n";
}
foreach ($migration->notes as $note) {
echo "Note: $note\n";
}
exit(0);
}
$command = null;
$forceFix = false;
$paths = [];
$options = [];
$args = array_slice($argv, 1);
for ($i = 0; $i < count($args); $i++) {
$arg = $args[$i];
if ($arg === '-h' || $arg === '--help') {
$options[] = '--help';
} elseif ($arg === '-V' || $arg === '--version') {
$options[] = '--version';
} elseif ($arg === '--fix') {
$forceFix = true;
} elseif ($arg === '--no-progress') {
// nothing to disable
} elseif (preg_match('~^--(preset|config-file)(?:=(.*))?$~', $arg, $m)) {
$value = $m[2] ?? $args[++$i] ?? '';
if ($m[1] === 'config-file') {
fwrite(STDERR, "Option --config-file is no longer supported: configure the project with dresscode.neon or dresscode:ignore comments.\n");
exit(2);
}
$presets = [
'optimize-fn' => Nette\CodingStandard\Presets\OptimizeFn::class,
'clean-code' => Nette\CodingStandard\Presets\CleanCode::class,
'types' => Nette\CodingStandard\Presets\Types::class,
];
if (!preg_match('~^php\d*$~', $value)) {
array_push($options, '--preset', $presets[$value] ?? $value);
}
} elseif ($command === null && $paths === [] && ($arg === 'check' || $arg === 'fix')) {
$command = $arg;
} else {
$paths[] = $arg;
}
}
if (!array_intersect($options, ['--help', '--version'])) {
$command = $forceFix ? 'fix' : ($command ?? 'check'); // the paths are left to the configuration, which knows whether it decides them
}
// what a project without a configuration file gets; version 3 checked src and tests by itself
$default = DressCode\Config::create()
->extension(Nette\CodingStandard\Extension::class)
->preset('dresscode/nette')
->paths(array_filter(['src', 'tests'], 'is_dir') ?: ['.']);
$argv = array_merge([$argv[0]], $command === null ? [] : [$command], $paths, $options);
exit((new DressCode\Console\Application(defaultConfig: $default))->run($argv));