Skip to content

Commit c664f49

Browse files
committed
wip
1 parent 60d3c1b commit c664f49

File tree

7 files changed

+130
-28
lines changed

7 files changed

+130
-28
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Backpack\CRUD\app\Console\Commands\Upgrade\Concerns;
4+
5+
trait ExtractsFirstInteger
6+
{
7+
/**
8+
* Extract the first integer occurrence from the given string.
9+
*/
10+
protected function extractFirstInteger(string $value): ?int
11+
{
12+
if (preg_match('/(\d+)/', $value, $matches)) {
13+
return (int) $matches[1];
14+
}
15+
16+
return null;
17+
}
18+
}

src/app/Console/Commands/Upgrade/Step.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22

33
namespace Backpack\CRUD\app\Console\Commands\Upgrade;
44

5+
use Backpack\CRUD\app\Console\Commands\Upgrade\Concerns\ExtractsFirstInteger;
6+
57
abstract class Step
68
{
9+
use ExtractsFirstInteger;
10+
711
public function __construct(protected UpgradeContext $context)
812
{
913
}

src/app/Console/Commands/Upgrade/UpgradeCommand.php

Lines changed: 60 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
namespace Backpack\CRUD\app\Console\Commands\Upgrade;
44

55
use Backpack\CRUD\app\Console\Commands\Traits\PrettyCommandOutput;
6+
use Backpack\CRUD\app\Console\Commands\Upgrade\Concerns\ExtractsFirstInteger;
67
use Illuminate\Console\Command;
78
use RuntimeException;
89

910
class UpgradeCommand extends Command
1011
{
11-
use PrettyCommandOutput;
12+
use PrettyCommandOutput, ExtractsFirstInteger;
1213

1314
protected $signature = 'backpack:upgrade
1415
{version=7 : Target Backpack version to prepare for.}
@@ -114,21 +115,30 @@ public function handle(): int
114115
}
115116
}
116117

117-
return $this->outputSummary($majorVersion, $results);
118+
$expectedVersionInstalled = $this->hasExpectedBackpackVersion($context, $config);
119+
120+
return $this->outputSummary($majorVersion, $results, $expectedVersionInstalled, $config);
118121
}
119122

120-
protected function outputSummary(string $majorVersion, array $results): int
123+
protected function outputSummary(
124+
string $majorVersion,
125+
array $results,
126+
bool $expectedVersionInstalled = false,
127+
?UpgradeConfigInterface $config = null
128+
): int
121129
{
122130
$format = $this->outputFormat();
123131

124-
$hasFailure = collect($results)->contains(function ($entry) {
132+
$resultsCollection = collect($results);
133+
134+
$hasFailure = $resultsCollection->contains(function ($entry) {
125135
/** @var StepResult $result */
126136
$result = $entry['result'];
127137

128138
return $result->status->isFailure();
129139
});
130140

131-
$warnings = collect($results)->filter(function ($entry) {
141+
$warnings = $resultsCollection->filter(function ($entry) {
132142
/** @var StepResult $result */
133143
$result = $entry['result'];
134144

@@ -174,6 +184,20 @@ protected function outputSummary(string $majorVersion, array $results): int
174184
$this->note('All checks passed, you are ready to continue with the manual steps from the upgrade guide.', 'green', 'green');
175185
}
176186

187+
$postUpgradeCommands = [];
188+
189+
if ($config !== null) {
190+
$postUpgradeCommands = ($config)::postUpgradeCommands();
191+
}
192+
193+
if ($expectedVersionInstalled && ! $hasFailure && ! empty($postUpgradeCommands)) {
194+
$this->note("Now that you have v{$majorVersion} installed, don't forget to run the following commands:", 'green', 'green');
195+
196+
foreach ($postUpgradeCommands as $command) {
197+
$this->note($command);
198+
}
199+
}
200+
177201
$this->newLine();
178202

179203
return $hasFailure ? Command::FAILURE : Command::SUCCESS;
@@ -267,4 +291,35 @@ protected function extractMajorVersion(string $version): string
267291

268292
return $version;
269293
}
294+
295+
protected function hasExpectedBackpackVersion(UpgradeContext $context, UpgradeConfigInterface $config): bool
296+
{
297+
$targetConstraint = $config::backpackCrudRequirement();
298+
$targetMajor = $this->extractFirstInteger($targetConstraint);
299+
300+
$composerConstraint = $context->composerRequirement('backpack/crud');
301+
302+
if ($composerConstraint === null) {
303+
return false;
304+
}
305+
306+
$composerMajor = $this->extractFirstInteger($composerConstraint);
307+
308+
if ($targetMajor !== null && ($composerMajor === null || $composerMajor < $targetMajor)) {
309+
return false;
310+
}
311+
312+
$installedMajor = $context->packageMajorVersion('backpack/crud');
313+
314+
if ($installedMajor === null) {
315+
return false;
316+
}
317+
318+
if ($targetMajor !== null && $installedMajor < $targetMajor) {
319+
return false;
320+
}
321+
322+
return true;
323+
}
324+
270325
}

src/app/Console/Commands/Upgrade/UpgradeConfigInterface.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,9 @@ public function steps(): array;
1515
public function addons(): array;
1616

1717
public static function backpackCrudRequirement(): string;
18+
19+
/**
20+
* @return array<int, string>
21+
*/
22+
public static function postUpgradeCommands(): array;
1823
}

src/app/Console/Commands/Upgrade/v7/Steps/EnsureBackpackCrudRequirementStep.php

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,17 @@ public function run(): StepResult
3333
}
3434

3535
$requiredMajor = $this->extractFirstInteger($constraint);
36-
3736
$targetConstraint = $this->targetConstraint();
37+
$targetMajor = $this->targetMajor();
3838

39-
if ($requiredMajor === null || $requiredMajor < 7) {
39+
if ($requiredMajor === null) {
40+
return StepResult::failure(
41+
sprintf('Update composer.json to require backpack/crud:%s (or newer).', $targetConstraint),
42+
["Current constraint: {$constraint}"]
43+
);
44+
}
45+
46+
if ($targetMajor !== null && $requiredMajor < $targetMajor) {
4047
return StepResult::failure(
4148
sprintf('Update composer.json to require backpack/crud:%s (or newer).', $targetConstraint),
4249
["Current constraint: {$constraint}"]
@@ -46,25 +53,21 @@ public function run(): StepResult
4653
$installedMajor = $this->context()->packageMajorVersion('backpack/crud');
4754
$installedPretty = $this->context()->installedPackagePrettyVersion('backpack/crud');
4855

49-
if ($installedMajor !== null && $installedMajor < 7) {
56+
$comparisonMajor = $targetMajor ?? $requiredMajor;
57+
58+
if ($comparisonMajor !== null && $installedMajor !== null && $installedMajor < $comparisonMajor) {
5059
return StepResult::warning(
51-
'Composer requirement is updated, but Backpack v7 is not installed yet. Run composer update.',
60+
sprintf(
61+
'Composer requirement is updated, but a Backpack version satisfying %s is not installed yet. Run composer update.',
62+
$targetConstraint
63+
),
5264
["Installed version: {$installedPretty}"]
5365
);
5466
}
5567

5668
return StepResult::success("Composer.json requires backpack/crud {$constraint}.");
5769
}
5870

59-
protected function extractFirstInteger(string $value): ?int
60-
{
61-
if (preg_match('/(\d+)/', $value, $matches)) {
62-
return (int) $matches[1];
63-
}
64-
65-
return null;
66-
}
67-
6871
public function canFix(StepResult $result): bool
6972
{
7073
if ($result->status !== StepStatus::Failed) {
@@ -80,8 +83,13 @@ public function canFix(StepResult $result): bool
8083
}
8184

8285
$requiredMajor = $this->extractFirstInteger($this->currentConstraint);
86+
$targetMajor = $this->targetMajor();
8387

84-
return $requiredMajor === null || $requiredMajor < 7;
88+
if ($targetMajor === null) {
89+
return $requiredMajor === null;
90+
}
91+
92+
return $requiredMajor === null || $requiredMajor < $targetMajor;
8593
}
8694

8795
public function fixMessage(StepResult $result): string
@@ -114,4 +122,15 @@ private function targetConstraint(): string
114122
{
115123
return UpgradeCommandConfig::backpackCrudRequirement();
116124
}
125+
126+
private function targetMajor(): ?int
127+
{
128+
$constraintMajor = $this->extractFirstInteger($this->targetConstraint());
129+
130+
if ($constraintMajor !== null) {
131+
return $constraintMajor;
132+
}
133+
134+
return $this->extractFirstInteger($this->context()->targetVersion());
135+
}
117136
}

src/app/Console/Commands/Upgrade/v7/Steps/EnsureFirstPartyAddonsAreCompatibleStep.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,6 @@ protected function matchesExpectedConstraint(string $constraint, string $expecte
6767
return trim($constraint) === trim($expected);
6868
}
6969

70-
protected function extractFirstInteger(string $value): ?int
71-
{
72-
if (preg_match('/(\d+)/', $value, $matches)) {
73-
return (int) $matches[1];
74-
}
75-
76-
return null;
77-
}
78-
7970
public function canFix(StepResult $result): bool
8071
{
8172
return $result->status === StepStatus::Warning && ! empty($this->mismatched);

src/app/Console/Commands/Upgrade/v7/UpgradeCommandConfig.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,14 @@ public static function backpackCrudRequirement(): string
6060
{
6161
return '^7.0.0-beta';
6262
}
63+
64+
public static function postUpgradeCommands(): array
65+
{
66+
return [
67+
'php artisan basset:clear',
68+
'php artisan config:clear',
69+
'php artisan cache:clear',
70+
'php artisan view:clear',
71+
];
72+
}
6373
}

0 commit comments

Comments
 (0)