Skip to content
This repository was archived by the owner on Mar 20, 2025. It is now read-only.

Commit 5ea7a3a

Browse files
committed
Auto-invalidate manifest per build
1 parent 293c112 commit 5ea7a3a

File tree

2 files changed

+72
-7
lines changed

2 files changed

+72
-7
lines changed

src/Support/TaskManifest.php

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

33
namespace Shift\Cli\Support;
44

5+
use Illuminate\Support\Str;
6+
57
class TaskManifest
68
{
7-
private array $manifest = [];
9+
private array $tasks = [];
810

911
private string $manifestPath;
1012

@@ -21,16 +23,24 @@ public function __construct(string $vendorPath, array $defaultTasks)
2123

2224
public function list(): array
2325
{
24-
if (! empty($this->manifest)) {
25-
return $this->manifest;
26+
if (! empty($this->tasks)) {
27+
return $this->tasks;
2628
}
2729

2830
if (! is_file($this->manifestPath)) {
2931
$this->build();
3032
}
3133

32-
return $this->manifest = is_file($this->manifestPath) ?
33-
require $this->manifestPath : [];
34+
$manifest = require $this->manifestPath;
35+
if (! $this->isStale($manifest)) {
36+
return $this->tasks = $manifest['tasks'];
37+
}
38+
39+
$this->build();
40+
$manifest = require $this->manifestPath;
41+
$this->tasks = $manifest['tasks'];
42+
43+
return $this->tasks;
3444
}
3545

3646
public function build(): void
@@ -53,12 +63,27 @@ public function build(): void
5363
->all());
5464
}
5565

56-
protected function write(array $manifest): void
66+
private static function currentNamespace(): string
67+
{
68+
return Str::before(__NAMESPACE__, '\\');
69+
}
70+
71+
private function isStale(array $manifest): bool
72+
{
73+
return $manifest['namespace'] !== self::currentNamespace();
74+
}
75+
76+
protected function write(array $tasks): void
5777
{
5878
if (! is_writable($dirname = dirname($this->manifestPath))) {
5979
throw new \Exception("The {$dirname} directory must be present and writable.");
6080
}
6181

82+
$manifest = [
83+
'namespace' => self::currentNamespace(),
84+
'tasks' => $tasks,
85+
];
86+
6287
file_put_contents(
6388
$this->manifestPath, '<?php return ' . var_export($manifest, true) . ';'
6489
);

tests/Feature/Support/TaskManifestTest.php

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class TaskManifestTest extends TestCase
1515
public function list_returns_existing_manifest()
1616
{
1717
$this->fakeProject([
18-
'shift-tasks.php' => '<?php return ["task-name" => "fqcn"];',
18+
'shift-tasks.php' => '<?php return ["namespace" => "Shift", "tasks" => ["task-name" => "fqcn"]];',
1919
]);
2020

2121
$taskManifest = new TaskManifest($this->currentSnapshotPath(), []);
@@ -32,6 +32,42 @@ public function list_returns_default_tasks_for_no_manifest()
3232

3333
$this->assertSame(['task-name' => 'Fully\\Qualified\\Class\\Name'], $taskManifest->list());
3434
$this->assertFileExists($this->currentSnapshotPath() . DIRECTORY_SEPARATOR . 'shift-tasks.php');
35+
36+
$manifest = require $this->currentSnapshotPath() . DIRECTORY_SEPARATOR . 'shift-tasks.php';
37+
$this->assertSame('Shift', $manifest['namespace']);
38+
$this->assertArrayHasKey('tasks', $manifest);
39+
}
40+
41+
#[Test]
42+
public function list_rebuilds_stale_manifest()
43+
{
44+
$this->fakeProject([
45+
'shift-tasks.php' => '<?php return ["namespace" => "Stale", "tasks" => ["task-name" => "foo"]];',
46+
'composer/installed.json' => json_encode([
47+
'packages' => [
48+
[
49+
'extra' => [
50+
'shift' => [
51+
'tasks' => ['task1' => '\\Package\\Task1', 'task2' => '\\Package\\Task2'],
52+
],
53+
],
54+
],
55+
],
56+
]),
57+
]);
58+
59+
$taskManifest = new TaskManifest($this->currentSnapshotPath(), ['task-name' => 'Fully\\Qualified\\Class\\Name']);
60+
61+
$this->assertEqualsCanonicalizing(
62+
['task-name' => 'Fully\\Qualified\\Class\\Name', 'task1' => '\\Package\\Task1', 'task2' => '\\Package\\Task2'],
63+
$taskManifest->list()
64+
);
65+
66+
$this->assertFileExists($this->currentSnapshotPath() . DIRECTORY_SEPARATOR . 'shift-tasks.php');
67+
68+
$manifest = require $this->currentSnapshotPath() . DIRECTORY_SEPARATOR . 'shift-tasks.php';
69+
$this->assertSame('Shift', $manifest['namespace']);
70+
$this->assertArrayHasKey('tasks', $manifest);
3571
}
3672

3773
#[Test]
@@ -66,5 +102,9 @@ public function build_returns_merged_tasks_from_packages()
66102
);
67103

68104
$this->assertFileExists($this->currentSnapshotPath() . DIRECTORY_SEPARATOR . 'shift-tasks.php');
105+
106+
$manifest = require $this->currentSnapshotPath() . DIRECTORY_SEPARATOR . 'shift-tasks.php';
107+
$this->assertSame('Shift', $manifest['namespace']);
108+
$this->assertArrayHasKey('tasks', $manifest);
69109
}
70110
}

0 commit comments

Comments
 (0)