-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKcodeComposerGenerator.php
More file actions
99 lines (85 loc) · 3.2 KB
/
KcodeComposerGenerator.php
File metadata and controls
99 lines (85 loc) · 3.2 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
<?php
declare(strict_types=1);
namespace KaririCode\Devkit\Configuration;
use KaririCode\Devkit\Contract\ConfigGenerator;
use KaririCode\Devkit\Core\ProjectContext;
/**
* Generates `.kcode/composer.json` — the self-contained dev-toolchain manifest.
*
* When `kcode init` runs, this file is written to `.kcode/` and then
* `composer install --working-dir=.kcode/ --no-interaction` is executed
* by the InitCommand. Tools are installed into `.kcode/vendor/bin/`,
* keeping the target project's own composer.json free of dev-tool deps.
*
* Version constraints come from `devkit.php` → `tools` key (optional).
* Falls back to KaririCode-certified defaults when not specified.
*
* @since 1.0.0
*/
final class KcodeComposerGenerator implements ConfigGenerator
{
private const array DEFAULT_TOOL_VERSIONS = [
'phpunit/phpunit' => '^12.5',
'phpstan/phpstan' => '^2.0',
'friendsofphp/php-cs-fixer' => '^3.64',
'rector/rector' => '^2.0',
'vimeo/psalm' => '^6.0',
];
/** @var array<string, string> Maps devkit.php tool short-names → Composer package names */
private const array TOOL_SHORT_NAME_MAP = [
'phpunit' => 'phpunit/phpunit',
'phpstan' => 'phpstan/phpstan',
'php-cs-fixer' => 'friendsofphp/php-cs-fixer',
'rector' => 'rector/rector',
'psalm' => 'vimeo/psalm',
];
#[\Override]
public function toolName(): string
{
return 'kcode-composer';
}
#[\Override]
public function outputPath(): string
{
return 'composer.json';
}
#[\Override]
public function generate(ProjectContext $context): string
{
$require = $this->resolveVersions($context->toolVersions);
$manifest = [
'name' => 'kariricode/devkit-tools',
'description' => 'Dev toolchain managed by kcode — do not edit manually.',
'require' => $require,
'config' => [
'bin-compat' => 'full',
'optimize-autoloader' => true,
'sort-packages' => true,
'preferred-install' => 'dist',
'allow-plugins' => [
'infection/extension-installer' => false,
],
],
'minimum-stability' => 'stable',
'prefer-stable' => true,
];
return json_encode($manifest, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES) . \PHP_EOL;
}
/**
* Merge user-supplied version constraints with defaults.
* User constraints win on conflict; short-names are resolved to package names.
*
* @param array<string, string> $userVersions From devkit.php → tools
* @return array<string, string>
*/
private function resolveVersions(array $userVersions): array
{
$resolved = self::DEFAULT_TOOL_VERSIONS;
foreach ($userVersions as $shortName => $constraint) {
$package = self::TOOL_SHORT_NAME_MAP[$shortName] ?? $shortName;
$resolved[$package] = $constraint;
}
ksort($resolved);
return $resolved;
}
}