Skip to content

Commit 1d1e0f9

Browse files
committed
Adding option to warn if a file is unexpectedly missing
Plus fixes, phpcs, etc
1 parent cd380c0 commit 1d1e0f9

File tree

5 files changed

+57
-44
lines changed

5 files changed

+57
-44
lines changed

src/Configurator/AbstractConfigurator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,15 @@ abstract public function unconfigure(Recipe $recipe, $config, Lock $lock);
4343

4444
abstract public function update(RecipeUpdate $recipeUpdate, array $originalConfig, array $newConfig): void;
4545

46-
protected function write($messages)
46+
protected function write($messages, $verbosity = IOInterface::VERBOSE)
4747
{
4848
if (!\is_array($messages)) {
4949
$messages = [$messages];
5050
}
5151
foreach ($messages as $i => $message) {
5252
$messages[$i] = ' '.$message;
5353
}
54-
$this->io->writeError($messages, true, IOInterface::VERBOSE);
54+
$this->io->writeError($messages, true, $verbosity);
5555
}
5656

5757
protected function isFileMarked(Recipe $recipe, string $file): bool

src/Configurator/AddLinesConfigurator.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Symfony\Flex\Configurator;
44

5+
use Composer\IO\IOInterface;
56
use Symfony\Flex\Lock;
67
use Symfony\Flex\Recipe;
78
use Symfony\Flex\Update\RecipeUpdate;
@@ -35,7 +36,11 @@ public function configure(Recipe $recipe, $config, Lock $lock, array $options =
3536

3637
$file = $this->path->concatenate([$this->options->get('root-dir'), $patch['file']]);
3738
if (!is_file($file)) {
38-
$this->write(sprintf('Skipping patch on file "%s": it does not exist.', $patch['file']));
39+
$this->write(
40+
sprintf('Skipping update of file "%s": it does not exist.', $patch['file']),
41+
isset($patch['warn_if_missing']) && $patch['warn_if_missing'] ? IOInterface::NORMAL : IOInterface::VERBOSE
42+
);
43+
3944
continue;
4045
}
4146

@@ -50,7 +55,7 @@ public function configure(Recipe $recipe, $config, Lock $lock, array $options =
5055
throw new \InvalidArgumentException(sprintf('The "position" key is required for the "add-lines" configurator for recipe "%s".', $recipe->getName()));
5156
}
5257
$position = $patch['position'];
53-
if (!in_array($position, self::VALID_POSITIONS, true)) {
58+
if (!\in_array($position, self::VALID_POSITIONS, true)) {
5459
throw new \InvalidArgumentException(sprintf('The "position" key must be one of "%s" for the "add-lines" configurator for recipe "%s".', implode('", "', self::VALID_POSITIONS), $recipe->getName()));
5560
}
5661

@@ -96,7 +101,7 @@ public function update(RecipeUpdate $recipeUpdate, array $originalConfig, array
96101
return !isset($item['requires']) || $this->isPackageInstalled($item['requires']);
97102
});
98103

99-
$filterDuplicates = function(array $sourceConfig, array $comparisonConfig) {
104+
$filterDuplicates = function (array $sourceConfig, array $comparisonConfig) {
100105
$filtered = [];
101106
foreach ($sourceConfig as $sourceItem) {
102107
$found = false;
@@ -177,7 +182,7 @@ private function unPatchFile(string $file, $value)
177182
}
178183

179184
$position = strpos($fileContents, $value);
180-
$fileContents = substr_replace($fileContents, '', $position, strlen($value));
185+
$fileContents = substr_replace($fileContents, '', $position, \strlen($value));
181186

182187
file_put_contents($file, $fileContents);
183188
}

src/PackageJsonSynchronizer.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ private function resolvePackageJsonDependencies($phpPackage): array
126126
}
127127

128128
if ($packageJson->read()['symfony']['needsPackageAsADependency'] ?? true) {
129-
$dependencies['@' . $phpPackage['name']] = 'file:' . substr($packageJson->getPath(), 1 + \strlen($this->rootDir), -13);
129+
$dependencies['@'.$phpPackage['name']] = 'file:'.substr($packageJson->getPath(), 1 + \strlen($this->rootDir), -13);
130130
}
131131

132132
foreach ($packageJson->read()['peerDependencies'] ?? [] as $peerDependency => $constraint) {
@@ -147,7 +147,7 @@ private function resolveImportMapPackages($phpPackage): array
147147
foreach ($packageJson->read()['symfony']['importmap'] ?? [] as $dependency => $constraint) {
148148
if (0 === strpos($constraint, 'path:')) {
149149
$dependencies[$dependency] = [
150-
'path' => dirname($packageJson->getPath()).'/'.substr($constraint, 5)
150+
'path' => \dirname($packageJson->getPath()).'/'.substr($constraint, 5),
151151
];
152152

153153
continue;
@@ -226,7 +226,7 @@ private function updateImportMap(array $importMapEntries): void
226226
return;
227227
}
228228

229-
$importMapData = include($this->rootDir.'/importmap.php');
229+
$importMapData = include $this->rootDir.'/importmap.php';
230230

231231
$remotePackageNameAndConstraints = [];
232232
foreach ($importMapEntries as $name => $importMapEntry) {
@@ -250,7 +250,11 @@ private function updateImportMap(array $importMapEntries): void
250250
continue;
251251
}
252252

253-
throw new \InvalidArgumentException(sprintf('Invalid importmap entry: %s', var_export($importMapEntry, true)));
253+
throw new \InvalidArgumentException(sprintf('Invalid importmap entry: "%s".', var_export($importMapEntry, true)));
254+
}
255+
256+
if (!$remotePackageNameAndConstraints) {
257+
return;
254258
}
255259

256260
$this->scriptExecutor->execute(

src/ScriptExecutor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ private function expandPhpScript(string $cmd, array $scriptArguments): string
117117

118118
$arguments = $phpFinder->findArguments();
119119

120-
if ($env = (string) (getenv('COMPOSER_ORIGINAL_INIS'))) {
120+
if ($env = (string) getenv('COMPOSER_ORIGINAL_INIS')) {
121121
$paths = explode(\PATH_SEPARATOR, $env);
122122
$ini = array_shift($paths);
123123
} else {

tests/Configurator/AddLinesConfiguratorTest.php

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,10 @@ protected function setUp(): void
3232
$filesystem->remove(FLEX_TEST_DIR);
3333
}
3434

35-
3635
public function testFileDoesNotExistSkipped()
3736
{
3837
$this->runConfigure([
39-
['file' => 'non-existent.php']
38+
['file' => 'non-existent.php'],
4039
]);
4140
$this->assertFileDoesNotExist(FLEX_TEST_DIR.'/non-existent.php');
4241
}
@@ -54,7 +53,7 @@ public function testLinesAddedToTopOfFile()
5453
[
5554
'file' => 'assets/app.js',
5655
'position' => 'top',
57-
'content' => "import './bootstrap';"
56+
'content' => "import './bootstrap';",
5857
],
5958
]);
6059
$actualContents = $this->readFile('assets/app.js');
@@ -63,7 +62,8 @@ public function testLinesAddedToTopOfFile()
6362
import * as Turbo from '@hotwired/turbo';
6463
6564
console.log(Turbo);
66-
EOF,
65+
EOF
66+
,
6767
$actualContents);
6868
}
6969

@@ -80,7 +80,7 @@ public function testLinesAddedToBottomOfFile()
8080
[
8181
'file' => 'assets/app.js',
8282
'position' => 'bottom',
83-
'content' => "import './bootstrap';"
83+
'content' => "import './bootstrap';",
8484
],
8585
]);
8686
$actualContents = $this->readFile('assets/app.js');
@@ -89,7 +89,8 @@ public function testLinesAddedToBottomOfFile()
8989
9090
console.log(Turbo);
9191
import './bootstrap';
92-
EOF,
92+
EOF
93+
,
9394
$actualContents);
9495
}
9596

@@ -122,7 +123,6 @@ public function testLinesAddedAfterTarget()
122123
// enables the Symfony UX Stimulus bridge (used in assets/bootstrap.js)
123124
.enableStimulusBridge('./assets/controllers.json')
124125
EOF
125-
126126
],
127127
]);
128128

@@ -144,7 +144,8 @@ public function testLinesAddedAfterTarget()
144144
;
145145
146146
module.exports = Encore.getWebpackConfig();
147-
EOF,
147+
EOF
148+
,
148149
$actualContents);
149150
}
150151

@@ -171,7 +172,6 @@ public function testSkippedIfTargetCannotBeFound()
171172
172173
// some new line
173174
EOF
174-
175175
],
176176
]);
177177

@@ -193,7 +193,7 @@ public function testPatchIgnoredIfValueAlreadyExists()
193193
[
194194
'file' => 'assets/app.js',
195195
'position' => 'top',
196-
'content' => "import './bootstrap';"
196+
'content' => "import './bootstrap';",
197197
],
198198
]);
199199
$actualContents = $this->readFile('assets/app.js');
@@ -212,30 +212,31 @@ public function testLinesAddedToMultipleFiles()
212212
EOF
213213
);
214214

215-
216215
$this->runConfigure([
217216
[
218217
'file' => 'assets/app.js',
219218
'position' => 'top',
220-
'content' => "import './bootstrap';"
219+
'content' => "import './bootstrap';",
221220
],
222221
[
223222
'file' => 'assets/bootstrap.js',
224223
'position' => 'bottom',
225-
'content' => "console.log('on the bottom');"
224+
'content' => "console.log('on the bottom');",
226225
],
227226
]);
228227

229228
$this->assertSame(<<<EOF
230229
import './bootstrap';
231230
import * as Turbo from '@hotwired/turbo';
232-
EOF,
231+
EOF
232+
,
233233
$this->readFile('assets/app.js'));
234234

235235
$this->assertSame(<<<EOF
236236
console.log('bootstrap.js');
237237
console.log('on the bottom');
238-
EOF,
238+
EOF
239+
,
239240
$this->readFile('assets/bootstrap.js'));
240241
}
241242

@@ -248,20 +249,22 @@ public function testLineSkippedIfRequiredPackageMissing()
248249
EOF
249250
);
250251

252+
$composer = $this->createComposerMockWithPackagesInstalled([]);
251253
$this->runConfigure([
252254
[
253255
'file' => 'assets/app.js',
254256
'position' => 'top',
255257
'content' => "import './bootstrap';",
256258
'requires' => 'symfony/invented-package',
257259
],
258-
]);
260+
], $composer);
259261
$actualContents = $this->readFile('assets/app.js');
260262
$this->assertSame(<<<EOF
261263
import * as Turbo from '@hotwired/turbo';
262264
263265
console.log(Turbo);
264-
EOF,
266+
EOF
267+
,
265268
$actualContents);
266269
}
267270

@@ -293,7 +296,8 @@ public function testLineProcessedIfRequiredPackageIsPresent()
293296
import * as Turbo from '@hotwired/turbo';
294297
295298
console.log(Turbo);
296-
EOF,
299+
EOF
300+
,
297301
$actualContents);
298302
}
299303

@@ -356,7 +360,7 @@ public function getUnconfigureTests()
356360
console.log(Turbo);
357361
EOF
358362
,
359-
"console.log(Turbo);",
363+
'console.log(Turbo);',
360364
<<<EOF
361365
import * as Turbo from '@hotwired/turbo';
362366
import './bootstrap';
@@ -390,7 +394,7 @@ public function getUnconfigureTests()
390394
console.log(Turbo);
391395
EOF
392396
,
393-
"console.log(Turbo);",
397+
'console.log(Turbo);',
394398
<<<EOF
395399
import * as Turbo from '@hotwired/turbo';
396400
import './bootstrap';
@@ -439,14 +443,13 @@ public function getUpdateTests()
439443
console.log('on the bottom');
440444
EOF;
441445

442-
443446
yield 'recipe_changes_patch_contents' => [
444447
['assets/app.js' => $appJs],
445448
[
446-
['file' => 'assets/app.js', 'position' => 'top', 'content' => "import './bootstrap';"]
449+
['file' => 'assets/app.js', 'position' => 'top', 'content' => "import './bootstrap';"],
447450
],
448451
[
449-
['file' => 'assets/app.js', 'position' => 'top', 'content' => "import './stimulus_bootstrap';"]
452+
['file' => 'assets/app.js', 'position' => 'top', 'content' => "import './stimulus_bootstrap';"],
450453
],
451454
['assets/app.js' => <<<EOF
452455
import './stimulus_bootstrap';
@@ -460,28 +463,29 @@ public function getUpdateTests()
460463
yield 'recipe_file_and_value_same_before_and_after' => [
461464
['assets/app.js' => $appJs],
462465
[
463-
['file' => 'assets/app.js', 'position' => 'top', 'content' => "import * as Turbo from '@hotwired/turbo';"]
466+
['file' => 'assets/app.js', 'position' => 'top', 'content' => "import * as Turbo from '@hotwired/turbo';"],
464467
],
465468
[
466-
['file' => 'assets/app.js', 'position' => 'top', 'content' => "import * as Turbo from '@hotwired/turbo';"]
469+
['file' => 'assets/app.js', 'position' => 'top', 'content' => "import * as Turbo from '@hotwired/turbo';"],
467470
],
468471
['assets/app.js' => $appJs],
469472
];
470473

471474
yield 'different_files_unconfigures_old_and_configures_new' => [
472475
['assets/app.js' => $appJs, 'assets/bootstrap.js' => $bootstrapJs],
473476
[
474-
['file' => 'assets/app.js', 'position' => 'top', 'content' => "import * as Turbo from '@hotwired/turbo';"]
477+
['file' => 'assets/app.js', 'position' => 'top', 'content' => "import * as Turbo from '@hotwired/turbo';"],
475478
],
476479
[
477-
['file' => 'assets/bootstrap.js', 'position' => 'top', 'content' => "import * as Turbo from '@hotwired/turbo';"]
480+
['file' => 'assets/bootstrap.js', 'position' => 'top', 'content' => "import * as Turbo from '@hotwired/turbo';"],
478481
],
479482
[
480483
'assets/app.js' => <<<EOF
481484
import './bootstrap';
482485
483486
console.log(Turbo);
484-
EOF,
487+
EOF
488+
,
485489
'assets/bootstrap.js' => <<<EOF
486490
import * as Turbo from '@hotwired/turbo';
487491
console.log('bootstrap.js');
@@ -494,21 +498,21 @@ public function getUpdateTests()
494498
yield 'recipe_changes_but_ignored_because_package_not_installed' => [
495499
['assets/app.js' => $appJs],
496500
[
497-
['file' => 'assets/app.js', 'position' => 'top', 'content' => "import './bootstrap';", 'requires' => 'symfony/not-installed']
501+
['file' => 'assets/app.js', 'position' => 'top', 'content' => "import './bootstrap';", 'requires' => 'symfony/not-installed'],
498502
],
499503
[
500-
['file' => 'assets/app.js', 'position' => 'top', 'content' => "import './stimulus_bootstrap';", 'requires' => 'symfony/not-installed']
504+
['file' => 'assets/app.js', 'position' => 'top', 'content' => "import './stimulus_bootstrap';", 'requires' => 'symfony/not-installed'],
501505
],
502506
['assets/app.js' => $appJs],
503507
];
504508

505509
yield 'recipe_changes_are_applied_if_required_package_installed' => [
506510
['assets/app.js' => $appJs],
507511
[
508-
['file' => 'assets/app.js', 'position' => 'top', 'content' => "import './bootstrap';", 'requires' => 'symfony/installed-package']
512+
['file' => 'assets/app.js', 'position' => 'top', 'content' => "import './bootstrap';", 'requires' => 'symfony/installed-package'],
509513
],
510514
[
511-
['file' => 'assets/app.js', 'position' => 'top', 'content' => "import './stimulus_bootstrap';", 'requires' => 'symfony/installed-package']
515+
['file' => 'assets/app.js', 'position' => 'top', 'content' => "import './stimulus_bootstrap';", 'requires' => 'symfony/installed-package'],
512516
],
513517
['assets/app.js' => <<<EOF
514518
import './stimulus_bootstrap';
@@ -541,7 +545,7 @@ private function runUnconfigure(array $config)
541545
private function createConfigurator(Composer $composer = null)
542546
{
543547
return new AddLinesConfigurator(
544-
$composer ? $composer : $this->getMockBuilder(Composer::class)->getMock(),
548+
$composer ?: $this->getMockBuilder(Composer::class)->getMock(),
545549
$this->getMockBuilder(IOInterface::class)->getMock(),
546550
new Options(['config-dir' => 'config', 'root-dir' => FLEX_TEST_DIR])
547551
);

0 commit comments

Comments
 (0)