Skip to content

Commit 596603b

Browse files
committed
[TASK] Ask for permission to modify if composer property already exists.
1 parent ce30c46 commit 596603b

File tree

1 file changed

+65
-33
lines changed

1 file changed

+65
-33
lines changed

Classes/Command/AcceptanceTestsCommand.php

Lines changed: 65 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ protected function configure(): void
5353
protected function execute(InputInterface $input, OutputInterface $output): int
5454
{
5555
$this->io = new SymfonyStyle($input, $output);
56-
$packages = $this->getPackageResolver()->getPackageManager()->getActivePackages();
5756

57+
$packages = $this->getPackageResolver()->getPackageManager()->getActivePackages();
5858
$choices = array_reduce($packages, function ($result, PackageInterface $package) {
5959
if ($package->getPackageMetaData()->getPackageType() === 'typo3-cms-extension') {
6060
$packageKey = $package->getPackageKey();
@@ -65,26 +65,32 @@ protected function execute(InputInterface $input, OutputInterface $output): int
6565

6666
$selectedPackageName = $this->io->choice('Select a package to create acceptance tests for', $choices);
6767
$this->package = $this->getPackageResolver()->resolvePackage($selectedPackageName);
68-
6968
$packageKey = $this->package->getPackageKey();
69+
$targetPackagePath = $this->package->getPackagePath();
70+
71+
if ($this->updateComposerFile($targetPackagePath)) {
72+
$this->io->writeln('<info>Updated composer.json for EXT:' . $packageKey . '</info>');
73+
} else {
74+
$this->io->writeln('<comment>Failed to update composer.json for EXT:' . $packageKey . '</comment>');
75+
}
76+
7077
$this->io->writeln('Selected package: ' . $packageKey);
7178
$finder = GeneralUtility::makeInstance(Finder::class);
7279

73-
$targetPackage = $this->package->getPackagePath();
7480
$codeTemplatePath = '/Resources/Private/CodeTemplates/AcceptanceTests';
7581
$templatePath = $this->getPackageResolver()->resolvePackage('b13/make')->getPackagePath() . $codeTemplatePath;
7682

7783
$this->filesystem->mkdir([
78-
$targetPackage . '/Tests/Acceptance',
79-
$targetPackage . '/Tests/Acceptance/Fixtures',
80-
$targetPackage . '/Tests/Acceptance/Application',
81-
$targetPackage . '/Tests/Acceptance/Support/Extension'
84+
$targetPackagePath . '/Tests/Acceptance/Fixtures',
85+
$targetPackagePath . '/Tests/Acceptance/Application',
86+
$targetPackagePath . '/Tests/Acceptance/Support/Extension'
8287
]);
8388

8489
// Create public folder which is required for e.g. acceptance tests to work
85-
$publicFolderPath = $targetPackage . '/Resources/Public';
90+
$publicFolderPath = $targetPackagePath . '/Resources/Public';
8691
if (!is_dir($publicFolderPath)) {
87-
$createPublic = $this->io->confirm('Resource/Public is necessary e.g. for acceptance tests. Do you want to create it now?', true);
92+
$createPublic = $this->io->confirm('Resource/Public is necessary e.g. for acceptance tests. Do you want to create it now?',
93+
true);
8894
if ($createPublic) {
8995
$this->filesystem->mkdir([$publicFolderPath]);
9096
// Ensure the folder will be detected by git and committed
@@ -95,7 +101,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
95101
$files = $finder->in($templatePath)->files();
96102

97103
foreach ($files as $file) {
98-
$target = $targetPackage . 'Tests' . explode('AcceptanceTests', $file->getRealPath())[1];
104+
$target = $targetPackagePath . 'Tests' . explode('AcceptanceTests', $file->getRealPath())[1];
99105

100106
if (!is_file($target)) {
101107
$content = $file->getContents();
@@ -109,12 +115,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int
109115
}
110116
}
111117

112-
if ($this->updateComposerFile($targetPackage)) {
113-
$this->io->writeln('<info>Updated composer.json for EXT:' . $packageKey . '</info>');
114-
} else {
115-
$this->io->writeln('<comment>Failed to update composer.json for EXT:' . $packageKey . '</comment>');
116-
}
117-
118118
return 0;
119119
}
120120

@@ -129,23 +129,39 @@ protected function updateComposerFile(string $packagePath): bool
129129
$composerFile = $packagePath . '/composer.json';
130130
$composerJson = file_get_contents($composerFile);
131131
$composer = json_decode($composerJson, true);
132-
$namespace = rtrim($this->getNamespace(), '\\');
133-
134-
// @todo: if a value already exists ask for permission to change it?!
135-
$composer['require-dev']['codeception/codeception'] = '^4.1';
136-
$composer['require-dev']['codeception/module-asserts'] = '^1.2';
137-
$composer['require-dev']['codeception/module-webdriver'] = '^1.1';
138-
$composer['require-dev']['typo3/testing-framework'] = '^6.16.2';
139-
140-
$composer['autoload-dev']['psr-4'][$namespace . '\\Tests\\'] = 'Tests/';
141-
142-
$composer['config']['vendor-dir'] = '.Build/vendor';
143-
$composer['config']['bin-dir'] = '.Build/bin';
132+
if (json_last_error() !== JSON_ERROR_NONE) {
133+
throw new \JsonException('Could not parse ' . $composerFile);
134+
}
144135

145-
$composer['extra']['typo3/cms']['app-dir'] = '.Build';
146-
$composer['extra']['typo3/cms']['web-dir'] = '.Build/Web';
136+
$namespace = rtrim($this->getNamespace(), '\\');
147137

148-
return GeneralUtility::writeFile($composerFile, json_encode($composer, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), true);
138+
$addToComposerFile = [
139+
'require-dev' => [
140+
'codeception/codeception' => '^4.1',
141+
'codeception/module-asserts' => '^1.2',
142+
'codeception/module-webdriver' => '^1.1',
143+
'typo3/testing-framework' => '^6.16.2'
144+
],
145+
'autoload-dev' => [
146+
'psr-4' => [
147+
$namespace . '\\Tests\\' => 'Tests/'
148+
]
149+
],
150+
'config' => [
151+
'vendor-dir' => '.Build/vendor',
152+
'bin-dir' => '.Build/bin',
153+
],
154+
'extra' => [
155+
'typo3/cms' => [
156+
'app-dir' => '.Build',
157+
'web-dir' => '.Build/Web',
158+
]
159+
]
160+
];
161+
162+
$enhancedComposer = $this->enhanceComposerFile($composer, $addToComposerFile);
163+
164+
return GeneralUtility::writeFile($composerFile, json_encode($enhancedComposer, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), true);
149165
}
150166

151167
/**
@@ -157,8 +173,10 @@ protected function updateComposerFile(string $packagePath): bool
157173
protected function substituteMarkersAndSave(string $content, string $target): void
158174
{
159175
$markerService = GeneralUtility::makeInstance(MarkerBasedTemplateService::class);
160-
$templateContent = $markerService->substituteMarker($content, '{{NAMESPACE}}', rtrim($this->getNamespace(), '\\'));
161-
$templateContent = $markerService->substituteMarker($templateContent, '{{EXTENSION_KEY}}', $this->package->getPackageKey());
176+
$templateContent = $markerService->substituteMarker($content, '{{NAMESPACE}}',
177+
rtrim($this->getNamespace(), '\\'));
178+
$templateContent = $markerService->substituteMarker($templateContent, '{{EXTENSION_KEY}}',
179+
$this->package->getPackageKey());
162180

163181
try {
164182
$this->filesystem->dumpFile($target, $templateContent);
@@ -176,4 +194,18 @@ protected function getNamespace(): string
176194
{
177195
return (string)key((array)($this->package->getValueFromComposerManifest('autoload')->{'psr-4'} ?? []));
178196
}
197+
198+
private function enhanceComposerFile(array &$composer, array &$addToComposerFile): array
199+
{
200+
foreach ($addToComposerFile as $key => $value)
201+
{
202+
if (is_array($value) && isset($composer[$key])) {
203+
$this->enhanceComposerFile($composer[$key], $value);
204+
} else {
205+
$composer[$key] = $value;
206+
}
207+
}
208+
209+
return $composer;
210+
}
179211
}

0 commit comments

Comments
 (0)