Skip to content

Commit ce91e0c

Browse files
committed
Status improvments (#750)
1 parent e8c5047 commit ce91e0c

5 files changed

Lines changed: 76 additions & 39 deletions

File tree

src/Console/Commands/DeployCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class DeployCommand extends Command
2727

2828
protected string $packagesPath;
2929

30-
protected string $errorMessage;
30+
private string $errorMessage = '';
3131

3232
public function __construct()
3333
{

src/Console/Commands/StatusCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class StatusCommand extends Command
2323

2424
protected string $packagesPath;
2525

26-
protected string $errorMessage;
26+
protected string $errorMessage = '';
2727

2828
public function __construct()
2929
{

src/Console/Traits/Check.php

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ private function check(): array
1515
$status = 'linked';
1616
$message = 'Devlink is linked, happy coding!';
1717
} elseif (! $hasDevlink && $hasDeploy) {
18-
$status = 'unlinked';
18+
$status = 'deploy';
1919
$message = 'Devlink is unlinked, ready for deployment!';
2020
}
2121

@@ -38,18 +38,15 @@ private function check(): array
3838
$isLocal = ($package['type'] ?? '') === 'local';
3939
$isPrivate = ($package['type'] ?? '') === 'private';
4040

41-
// Check if package is linked
4241
$isLinked = false;
4342
if ($isLocal) {
44-
// Local packages: check for path entry in composer.json
4543
foreach ($repositories as $repo) {
4644
if (($repo['type'] ?? '') === 'path' && ($repo['url'] ?? '') === $packagePath) {
4745
$isLinked = true;
4846
break;
4947
}
5048
}
5149
} else {
52-
// Public and Private packages: check for symlink
5350
$isLinked = is_link($packagePath);
5451
}
5552

@@ -104,7 +101,7 @@ private function check(): array
104101
}
105102

106103
if (file_exists(base_path('composer.json-deploy'))) {
107-
$status = 'unlinked';
104+
$status = 'deploy';
108105
$message = 'Devlink is unlinked and ready for deployment';
109106
}
110107

@@ -144,9 +141,4 @@ private function checkUpdated(): bool
144141

145142
return true;
146143
}
147-
148-
private function resolvePath(string $path): string
149-
{
150-
return str_starts_with($path, '~/') ? str_replace('~', getenv('HOME'), $path) : rtrim(realpath($path) ?: $path, '/');
151-
}
152144
}

src/Console/Traits/Deploy.php

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,37 +9,34 @@ trait Deploy
99
{
1010
private function deploy(): void
1111
{
12-
$this->unlink();
13-
$this->cleanup();
12+
$this->removeSymlinks();
13+
$this->cleanupDirectory();
1414
$this->moveComposerFiles();
1515
}
1616

17-
/**
18-
* Remove all symlinks in the packages directory.
19-
*/
20-
private function unlink(): void
17+
private function removeSymlinks(): void
2118
{
2219
if (is_dir($this->packagesPath)) {
2320
$i = 0;
2421
foreach (scandir($this->packagesPath) as $item) {
2522
if ($item !== '.' && $item !== '..' && is_link("$this->packagesPath/$item")) {
2623
unlink("$this->packagesPath/$item");
2724
$i++;
25+
info("Removed $item");
2826
}
2927
}
3028

3129
info("Removed $i symlinks");
3230
}
3331
}
3432

35-
/**
36-
* Remove the packages directory if it is empty.
37-
*/
38-
private function cleanup(): void
33+
private function cleanupDirectory(): void
3934
{
4035
if (is_dir($this->packagesPath) && count(scandir($this->packagesPath)) === 2) {
4136
info('Removing packages directory...');
4237
rmdir($this->packagesPath);
38+
} else {
39+
info('Packages directory is not empty, skipping cleanup.');
4340
}
4441
}
4542

@@ -48,6 +45,12 @@ private function moveComposerFiles(): void
4845
$linked = $this->composerJsonPath.'-linked';
4946
$deploy = $this->composerJsonPath.'-deploy';
5047

48+
if (! file_exists($linked) && file_exists($deploy)) {
49+
info('Project is already in deployment state.');
50+
51+
return;
52+
}
53+
5154
if (! file_exists($linked)) {
5255
$this->errorMessage = 'composer.json-linked not found!';
5356
error($this->errorMessage);

src/Console/Traits/Show.php

Lines changed: 59 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ trait Show
1414
private function show(): void
1515
{
1616
$fullStatus = $this->check();
17+
$isInSync = $this->arePackagesInSync($fullStatus['packages']);
1718

1819
$headers = ['Package', 'Type', 'Enabled', 'Valid', 'Active', 'Version', 'Path'];
1920
$rows = array_map(function ($row) {
@@ -47,26 +48,22 @@ private function show(): void
4748
$badge = '<fg=black;bg=red;options=bold> ';
4849
}
4950

50-
if ($fullStatus['status'] === 'unlinked') {
51-
$badge = '<fg=black;bg=gray;options=bold> ';
51+
if ($fullStatus['status'] === 'deploy') {
52+
$badge = '<fg=black;bg=blue;options=bold> ';
5253
}
5354

5455
if ($fullStatus['status'] === 'linked') {
5556
$badge = '<fg=black;bg=green;options=bold> ';
5657
}
5758

58-
if ($fullStatus['status'] === 'deployed') {
59-
$badge = '<fg=black;bg=green;options=bold> ';
60-
}
61-
62-
if ($fullStatus['updated']) {
59+
if ($isInSync) {
6360
$updateBadge = '<fg=black;bg=green;options=bold> ';
6461
} else {
6562
$updateBadge = '<fg=black;bg=red;options=bold> ';
6663
}
6764

6865
info(' '.$badge.strtoupper($fullStatus['status']).' </> '.$fullStatus['message']);
69-
info(' '.$updateBadge.' UPDATE </> '.($fullStatus['updated'] ? 'All packages are in sync with composer.json' : 'You need to run `php artisan devlink:link` to update the packages'));
66+
info(' '.$updateBadge.' UPDATE </> '.($isInSync ? 'All packages are in sync with composer.json' : 'You need to run `php artisan devlink:link` to update the packages'));
7067
info(' ');
7168
}
7269

@@ -77,6 +74,16 @@ private function getInstalledVersion(string $name, array $package): ?string
7774
return null;
7875
}
7976

77+
$path = $package['path'] ?? '';
78+
if ($path && ! str_contains($path, 'disabled/')) {
79+
$composerJson = realpath(base_path($path)).'/composer.json';
80+
if (file_exists($composerJson)) {
81+
$composerData = json_decode(file_get_contents($composerJson), true);
82+
83+
return $composerData['version'] ?? 'dev-main';
84+
}
85+
}
86+
8087
$composerLock = base_path('composer.lock');
8188
if (! file_exists($composerLock)) {
8289
return null;
@@ -97,15 +104,6 @@ private function getInstalledVersion(string $name, array $package): ?string
97104
}
98105
}
99106

100-
// If not found in lock file but composer.json exists, assume dev-main
101-
$path = $package['path'] ?? '';
102-
if ($path && ! str_contains($path, 'disabled/')) {
103-
$composerJson = realpath(base_path($path)).'/composer.json';
104-
if (file_exists($composerJson)) {
105-
return 'dev-main';
106-
}
107-
}
108-
109107
return null;
110108
}
111109

@@ -159,4 +157,48 @@ private function getPackageName(string $name, array $package): ?string
159157

160158
return $data['name'] ?? null;
161159
}
160+
161+
private function arePackagesInSync(array $packages): bool
162+
{
163+
$composerJson = base_path('composer.json');
164+
if (! file_exists($composerJson)) {
165+
return false;
166+
}
167+
168+
$composerData = json_decode(file_get_contents($composerJson), true);
169+
if (! $composerData) {
170+
return false;
171+
}
172+
173+
$composerRequire = array_merge(
174+
$composerData['require'] ?? [],
175+
$composerData['require-dev'] ?? []
176+
);
177+
178+
foreach ($packages as $package) {
179+
$packageName = $this->getPackageName($package['name'], $package['config']);
180+
if (! $packageName) {
181+
continue;
182+
}
183+
184+
$expectedPath = $package['config']['path'] ?? '';
185+
if (empty($expectedPath)) {
186+
continue;
187+
}
188+
189+
if (! isset($composerRequire[$packageName])) {
190+
return false;
191+
}
192+
193+
$composerPath = $composerRequire[$packageName];
194+
if (str_contains($composerPath, 'path:')) {
195+
$composerPath = trim(str_replace('path:', '', $composerPath));
196+
if ($composerPath !== $expectedPath) {
197+
return false;
198+
}
199+
}
200+
}
201+
202+
return true;
203+
}
162204
}

0 commit comments

Comments
 (0)