Skip to content

Commit a9f83f6

Browse files
committed
refactor(command): apply DIP to InitCommand — inject MigrationDetector
InitCommand now receives MigrationDetector via constructor injection, matching the existing MigrateCommand pattern (DIP — ARFA 1.3 S5). Also extends the init flow with a Phase 2: installs dev tools via Devkit::installTools() after config generation. Use --skip-install to generate configs only (useful in CI / offline environments). bin/kcode wiring updated to pass new MigrationDetector() to InitCommand.
1 parent 6d33367 commit a9f83f6

2 files changed

Lines changed: 37 additions & 7 deletions

File tree

bin/kcode

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ declare(strict_types=1);
4343
$devkit = new \KaririCode\Devkit\Core\Devkit($detector);
4444

4545
// Register config generators
46+
$devkit->addGenerator(new \KaririCode\Devkit\Configuration\KcodeComposerGenerator());
4647
$devkit->addGenerator(new \KaririCode\Devkit\Configuration\PhpUnitConfigGenerator());
4748
$devkit->addGenerator(new \KaririCode\Devkit\Configuration\PhpStanConfigGenerator());
4849
$devkit->addGenerator(new \KaririCode\Devkit\Configuration\CsFixerConfigGenerator());
@@ -75,7 +76,9 @@ declare(strict_types=1);
7576

7677
$app = new \KaririCode\Devkit\Command\Application($devkit);
7778

78-
$app->register(new \KaririCode\Devkit\Command\InitCommand());
79+
$app->register(new \KaririCode\Devkit\Command\InitCommand(
80+
new \KaririCode\Devkit\Core\MigrationDetector(),
81+
));
7982
$app->register(new \KaririCode\Devkit\Command\MigrateCommand(
8083
new \KaririCode\Devkit\Core\MigrationDetector(),
8184
));

src/Command/InitCommand.php

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,24 @@
88
use KaririCode\Devkit\Core\MigrationDetector;
99

1010
/**
11-
* Generates all config files inside `.kcode/`.
11+
* Generates all config files inside `.kcode/` and installs dev tools.
1212
*
13-
* With `--config`, scaffolds a `devkit.php` override file in the project root.
13+
* On `kcode init`, writes all tool configs (phpunit.xml.dist, phpstan.neon, etc.)
14+
* to `.kcode/` via the registered generators, then runs `composer install
15+
* --working-dir=.kcode/` to install the tool binaries into `.kcode/vendor/bin/`.
16+
*
17+
* Flags:
18+
* --config Scaffold a `devkit.php` override file in the project root
19+
* --skip-install Generate configs only (skip composer install step)
1420
*
1521
* @since 1.0.0
1622
*/
1723
final class InitCommand extends AbstractCommand
1824
{
25+
public function __construct(
26+
private readonly MigrationDetector $detector,
27+
) {
28+
}
1929
#[\Override]
2030
public function name(): string
2131
{
@@ -38,20 +48,37 @@ public function execute(Devkit $devkit, array $arguments): int
3848
$this->info("Namespace: {$context->namespace}");
3949
$this->info("PHP: {$context->phpVersion}");
4050

51+
// ── Phase 1: Generate config files into .kcode/ ─────────────────
4152
$count = $devkit->init();
4253

4354
$this->line();
4455
$this->info("Generated {$count} config file(s) in .kcode/");
4556
$this->info(".kcode/ added to .gitignore (regenerate with kcode init)");
4657

47-
// Scaffold devkit.php if requested
58+
// ── Phase 2: Install dev tools into .kcode/vendor/ ──────────────
59+
if (! $this->hasFlag($arguments, '--skip-install')) {
60+
$this->line();
61+
$this->info("Installing dev tools into .kcode/vendor/ ...");
62+
63+
$exitCode = $devkit->installTools($context->projectRoot);
64+
65+
if (0 !== $exitCode) {
66+
$this->warning("composer install failed (exit {$exitCode}). Run manually:");
67+
$this->line(" composer install --working-dir={$context->devkitDir} --no-interaction");
68+
69+
return $exitCode;
70+
}
71+
72+
$this->info("Dev tools installed in .kcode/vendor/bin/");
73+
}
74+
75+
// ── Phase 3: Scaffold devkit.php if requested ────────────────────
4876
if ($this->hasFlag($arguments, '--config')) {
4977
$this->scaffoldDevkitConfig($context->projectRoot);
5078
}
5179

52-
// Hint: detect redundant root-level configs and dev dependencies
53-
$detector = new MigrationDetector();
54-
$migration = $detector->detect($context->projectRoot);
80+
// ── Phase 4: Hint about redundant legacy configs ──────────────────
81+
$migration = $this->detector->detect($context->projectRoot);
5582

5683
if ($migration->hasRedundancies) {
5784
$this->line();

0 commit comments

Comments
 (0)