Skip to content

Commit 41c6a19

Browse files
committed
feat: add flag to show only redundant files and to delete the redundant files
1 parent d16aa36 commit 41c6a19

1 file changed

Lines changed: 52 additions & 3 deletions

File tree

‎src/Commands/DiffViewsCommand.php‎

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,16 @@
99
class DiffViewsCommand extends Command
1010
{
1111
protected $signature = 'forms:diff-views
12-
{--only-redundant : Show only published views that are identical to the package views}';
12+
{--only-redundant : Show only published views that are identical to the package views}
13+
{--delete-redundant : Delete redundant published views after listing them}';
1314

1415
protected $description = 'Find redundant or modified published form views in your application.';
1516

1617
public function handle(): int
1718
{
1819
$filesystem = new Filesystem();
1920

20-
$packagePath = __DIR__ . '/../../resources/views';
21+
$packagePath = __DIR__ . '/../../resources/views';
2122
$publishedPath = resource_path('views/vendor/forms');
2223

2324
if (! $filesystem->isDirectory($packagePath)) {
@@ -41,11 +42,14 @@ public function handle(): int
4142
'modified' => 0,
4243
];
4344

45+
// keep track of redundant files (published) for optional deletion
46+
$redundantFiles = [];
47+
4448
// ONLY check files in your app, ignore anything not published.
4549
foreach ($publishedFiles as $relativePath => $publishedRealPath) {
4650

4751
if (! isset($packageFiles[$relativePath])) {
48-
// orphan — ignore as per your requirements
52+
// orphans ignored
4953
continue;
5054
}
5155

@@ -58,11 +62,16 @@ public function handle(): int
5862
} else {
5963
$rows[] = ['<fg=gray>REDUNDANT</>', $relativePath];
6064
}
65+
6166
$counts['redundant']++;
67+
68+
// collect for potential deletion
69+
$redundantFiles[$relativePath] = $publishedRealPath;
6270
} else {
6371
if (! $onlyRedundant) {
6472
$rows[] = ['<fg=yellow>MODIFIED</>', $relativePath];
6573
}
74+
6675
$counts['modified']++;
6776
}
6877
}
@@ -74,6 +83,12 @@ public function handle(): int
7483
);
7584

7685
$this->printSummary($counts);
86+
87+
// deletion hook (even if nothing to delete, this is a no-op)
88+
if ($this->option('delete-redundant') && ! empty($redundantFiles)) {
89+
$this->deleteRedundantViews($redundantFiles, $filesystem);
90+
}
91+
7792
return 0;
7893
}
7994

@@ -84,6 +99,11 @@ public function handle(): int
8499

85100
$this->printSummary($counts);
86101

102+
// deletion hook after showing table + summary
103+
if ($this->option('delete-redundant') && ! empty($redundantFiles)) {
104+
$this->deleteRedundantViews($redundantFiles, $filesystem);
105+
}
106+
87107
return 0;
88108
}
89109

@@ -115,4 +135,33 @@ protected function printSummary(array $counts): void
115135
$this->line(' <fg=gray>Redundant (can be deleted):</> '.$counts['redundant']);
116136
$this->line(' <fg=yellow>Modified (customized override):</> '.$counts['modified']);
117137
}
138+
139+
/**
140+
* Prompt and delete redundant published views.
141+
*/
142+
protected function deleteRedundantViews(array $redundantFiles, Filesystem $filesystem): void
143+
{
144+
$this->newLine();
145+
146+
$count = count($redundantFiles);
147+
148+
if ($count === 0) {
149+
$this->info('No redundant views to delete.');
150+
return;
151+
}
152+
153+
if (! $this->confirm("Delete " . $count . " redundant view file(s) from your application?", false)) {
154+
$this->info('No files were deleted.');
155+
return;
156+
}
157+
158+
foreach ($redundantFiles as $relativePath => $realPath) {
159+
try {
160+
$filesystem->delete($realPath);
161+
$this->line(" <fg=red>DELETED</> {$relativePath}");
162+
} catch (\Exception $e) {
163+
$this->line(" <fg=red>FAILED</> {$relativePath}: {$e->getMessage()}");
164+
}
165+
}
166+
}
118167
}

0 commit comments

Comments
 (0)