-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInitCommand.php
More file actions
158 lines (128 loc) · 6.04 KB
/
InitCommand.php
File metadata and controls
158 lines (128 loc) · 6.04 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
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
<?php
declare(strict_types=1);
namespace KaririCode\Devkit\Command;
use KaririCode\Devkit\Core\Devkit;
use KaririCode\Devkit\Core\MigrationDetector;
/**
* Generates all config files inside `.kcode/`.
*
* With `--config`, scaffolds a `devkit.php` override file in the project root.
*
* @since 1.0.0
*/
final class InitCommand extends AbstractCommand
{
#[\Override]
public function name(): string
{
return 'init';
}
#[\Override]
public function description(): string
{
return 'Generate .kcode/ configs (--config to scaffold devkit.php)';
}
#[\Override]
public function execute(Devkit $devkit, array $arguments): int
{
$this->banner('KaririCode Devkit — Init');
$context = $devkit->context();
$this->info("Project: {$context->projectName}");
$this->info("Namespace: {$context->namespace}");
$this->info("PHP: {$context->phpVersion}");
$count = $devkit->init();
$this->line();
$this->info("Generated {$count} config file(s) in .kcode/");
$this->info(".kcode/ added to .gitignore (regenerate with kcode init)");
// Scaffold devkit.php if requested
if ($this->hasFlag($arguments, '--config')) {
$this->scaffoldDevkitConfig($context->projectRoot);
}
// Hint: detect redundant root-level configs and dev dependencies
$detector = new MigrationDetector();
$migration = $detector->detect($context->projectRoot);
if ($migration->hasRedundancies) {
$this->line();
$this->warning(\sprintf(
'Found %d redundant item(s) that kcode replaces.',
$migration->totalItems,
));
$this->line(' Run \033[1mkcode migrate\033[0m to review and clean up.');
}
return 0;
}
private function scaffoldDevkitConfig(string $projectRoot): void
{
$configPath = $projectRoot . \DIRECTORY_SEPARATOR . 'devkit.php';
if (is_file($configPath)) {
$this->warning('devkit.php already exists — skipping scaffold.');
return;
}
$content = <<<'PHP_WRAP'
<?php
declare(strict_types=1);
/**
* KaririCode Devkit — Project Overrides
*
* This file customizes the devkit behavior for this project.
* Only uncomment the keys you need to change — unset keys use
* auto-detected values from composer.json + KaririCode defaults.
*
* Merge semantics:
* - cs_fixer_rules → MERGED with KaririCode defaults (your rules win on conflict)
* - rector_sets → REPLACES KaririCode defaults entirely
* - All others → REPLACES the auto-detected value
*
* After editing, run: kcode init
*
* @see https://github.com/kariricode/devkit
*/
return [
// ── Project Identity ──────────────────────────────────────
// 'project_name' => 'kariricode/my-component',
// 'namespace' => 'KaririCode\\MyComponent',
// ── PHP Version ───────────────────────────────────────────
// 'php_version' => '8.4',
// ── Static Analysis ───────────────────────────────────────
// 'phpstan_level' => 9, // 0–9 (default: 9)
// 'psalm_level' => 3, // 1–9 (default: 3)
// ── Directories ───────────────────────────────────────────
// 'source_dirs' => ['src'],
// 'test_dirs' => ['tests'],
// 'exclude_dirs' => ['src/Contract'], // excluded from static analysis
// ── Test Suites ───────────────────────────────────────────
// 'test_suites' => [
// 'Unit' => 'tests/Unit',
// 'Integration' => 'tests/Integration',
// ],
// ── Coverage ──────────────────────────────────────────────
// 'coverage_exclude' => ['src/Exception'],
// ── Code Style (MERGED with KaririCode defaults) ──────────
// 'cs_fixer_rules' => [
// 'concat_space' => ['spacing' => 'one'],
// 'yoda_style' => false,
// ],
// ── Rector (REPLACES KaririCode defaults) ─────────────────
// 'rector_sets' => [
// 'LevelSetList::UP_TO_PHP_84',
// 'SetList::CODE_QUALITY',
// 'SetList::DEAD_CODE',
// 'SetList::EARLY_RETURN',
// 'SetList::TYPE_DECLARATION',
// ],
// ── Tool Versions (informational) ─────────────────────────
// 'tools' => [
// 'phpunit' => '^11.0',
// 'phpstan' => '^2.0',
// 'php-cs-fixer' => '^3.64',
// 'rector' => '^2.0',
// 'psalm' => '^6.0',
// ],
];
PHP_WRAP;
file_put_contents($configPath, $content . \PHP_EOL);
$this->line();
$this->info('Scaffolded devkit.php in project root.');
$this->line(' Edit it, then run \033[1mkcode init\033[0m to regenerate configs.');
}
}