Skip to content

Commit 55e2ed8

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

File tree

1 file changed

+73
-33
lines changed

1 file changed

+73
-33
lines changed

Classes/Command/AcceptanceTestsCommand.php

Lines changed: 73 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,35 @@ 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(
93+
'Resource/Public is necessary e.g. for acceptance tests. Do you want to create it now?',
94+
true
95+
);
96+
8897
if ($createPublic) {
8998
$this->filesystem->mkdir([$publicFolderPath]);
9099
// Ensure the folder will be detected by git and committed
@@ -95,7 +104,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
95104
$files = $finder->in($templatePath)->files();
96105

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

100109
if (!is_file($target)) {
101110
$content = $file->getContents();
@@ -109,12 +118,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int
109118
}
110119
}
111120

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-
118121
return 0;
119122
}
120123

@@ -129,23 +132,39 @@ protected function updateComposerFile(string $packagePath): bool
129132
$composerFile = $packagePath . '/composer.json';
130133
$composerJson = file_get_contents($composerFile);
131134
$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';
135+
if (json_last_error() !== JSON_ERROR_NONE) {
136+
throw new \JsonException('Could not parse ' . $composerFile);
137+
}
144138

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

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

151170
/**
@@ -157,8 +176,16 @@ protected function updateComposerFile(string $packagePath): bool
157176
protected function substituteMarkersAndSave(string $content, string $target): void
158177
{
159178
$markerService = GeneralUtility::makeInstance(MarkerBasedTemplateService::class);
160-
$templateContent = $markerService->substituteMarker($content, '{{NAMESPACE}}', rtrim($this->getNamespace(), '\\'));
161-
$templateContent = $markerService->substituteMarker($templateContent, '{{EXTENSION_KEY}}', $this->package->getPackageKey());
179+
$templateContent = $markerService->substituteMarker(
180+
$content,
181+
'{{NAMESPACE}}',
182+
rtrim($this->getNamespace(), '\\')
183+
);
184+
$templateContent = $markerService->substituteMarker(
185+
$templateContent,
186+
'{{EXTENSION_KEY}}',
187+
$this->package->getPackageKey()
188+
);
162189

163190
try {
164191
$this->filesystem->dumpFile($target, $templateContent);
@@ -176,4 +203,17 @@ protected function getNamespace(): string
176203
{
177204
return (string)key((array)($this->package->getValueFromComposerManifest('autoload')->{'psr-4'} ?? []));
178205
}
206+
207+
private function enhanceComposerFile(array &$composer, array &$addToComposerFile): array
208+
{
209+
foreach ($addToComposerFile as $key => $value) {
210+
if (is_array($value) && isset($composer[$key])) {
211+
$this->enhanceComposerFile($composer[$key], $value);
212+
} else {
213+
$composer[$key] = $value;
214+
}
215+
}
216+
217+
return $composer;
218+
}
179219
}

0 commit comments

Comments
 (0)