Files
2026-09-21 10:14:57 +01:00

101 lines
2.5 KiB
PHP
Executable File

<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Process;
use ZipArchive;
class ExportSource extends Command
{
protected $signature = 'source:export';
protected $description = 'Export project source to ~/Downloads';
public function handle(): int
{
$project = basename(base_path());
$timestamp = now()->format('Ymd-His');
$downloads = rtrim((string) getenv('HOME'), '/') . '/Downloads';
$archive = "{$downloads}/{$project}-source-{$timestamp}.zip";
$this->info("Exporting {$project}...");
/*
* Fast export of all Git-tracked source.
*/
$result = Process::path(base_path())
->timeout(60)
->run([
'git',
'archive',
'--format=zip',
"--output={$archive}",
'HEAD',
]);
if ($result->failed()) {
$this->error($result->errorOutput());
return self::FAILURE;
}
$zip = new ZipArchive;
if ($zip->open($archive) !== true) {
$this->error('Could not open ZIP.');
return self::FAILURE;
}
/*
* Remove ONLY:
*
* node_modules
* vendor
* Provision
* .scannerwork
* contents of storage/framework
* contents of storage/logs
*/
$removeDirectories = [
'node_modules/',
'vendor/',
'Provision/',
'provision/',
'.scannerwork/',
'storage/framework/',
'storage/logs/',
];
for ($i = $zip->numFiles - 1; $i >= 0; $i--) {
$name = $zip->getNameIndex($i);
foreach ($removeDirectories as $directory) {
if (str_starts_with($name, $directory)) {
$zip->deleteIndex($i);
break;
}
}
}
/*
* Restore the storage directory structure EMPTY.
*/
$zip->addEmptyDir('storage/framework');
$zip->addEmptyDir('storage/framework/cache');
$zip->addEmptyDir('storage/framework/cache/data');
$zip->addEmptyDir('storage/framework/sessions');
$zip->addEmptyDir('storage/framework/testing');
$zip->addEmptyDir('storage/framework/views');
$zip->addEmptyDir('storage/logs');
$zip->close();
$this->info('Done.');
$this->line($archive);
return self::SUCCESS;
}
}