-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add update check and initiate artisan command
- Loading branch information
1 parent
f32f949
commit f5af460
Showing
3 changed files
with
125 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
namespace App\Console\Commands; | ||
|
||
use Illuminate\Console\Command; | ||
use Illuminate\Support\Facades\File; | ||
|
||
class CheckUpdateCommand extends Command | ||
{ | ||
protected $signature = 'update:check'; | ||
protected $description = 'Check for available updates for the project'; | ||
|
||
public function handle() | ||
{ | ||
$currentVersion = $this->getCurrentVersion(); | ||
$latestRelease = $this->fetchReleaseData(); | ||
|
||
if (is_null($latestRelease)) { | ||
$this->error('Could not fetch data from GitHub'); | ||
return Command::FAILURE; | ||
} | ||
|
||
$latestVersion = ltrim($latestRelease['tag_name'], 'v'); | ||
$changelog = $latestRelease['body'] ?? 'No changelog available'; | ||
|
||
// Output information | ||
$this->info('Current Version: ' . $currentVersion); | ||
$this->info('Latest Version: ' . $latestVersion); | ||
$this->info('Changelog: ' . $changelog); | ||
|
||
if (version_compare($latestVersion, $currentVersion, '>')) { | ||
$this->alert('Update Available!'); | ||
} else { | ||
$this->alert('Your project is already up to date.'); | ||
} | ||
|
||
return Command::SUCCESS; | ||
} | ||
|
||
private function fetchReleaseData() | ||
{ | ||
$repoUrl = 'https://api.github.com/repos/anisAronno/laravel-starter/releases/latest'; | ||
$context = stream_context_create([ | ||
'http' => ['header' => ['User-Agent: PHP']] | ||
]); | ||
$response = @file_get_contents($repoUrl, false, $context); | ||
return $response ? json_decode($response, true) : null; | ||
} | ||
|
||
private function getCurrentVersion(): string | ||
{ | ||
$composerFile = base_path('composer.json'); | ||
if (File::exists($composerFile)) { | ||
$composerContent = json_decode(File::get($composerFile), true); | ||
return $composerContent['version'] ?? '0.0.0'; | ||
} | ||
return '0.0.0'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
namespace App\Console\Commands; | ||
|
||
use App\Jobs\UpdateProjectJob; | ||
use Illuminate\Console\Command; | ||
use Illuminate\Support\Facades\Log; | ||
|
||
class UpdateInitiateCommand extends Command | ||
{ | ||
protected $signature = 'update:initiate {version?}'; | ||
|
||
protected $description = 'Initiate project update to the latest version or a specific version.'; | ||
|
||
public function handle() | ||
{ | ||
$version = $this->argument('version'); | ||
|
||
if ($version) { | ||
$this->info("Initiating update for version: $version"); | ||
} else { | ||
$this->info('Initiating update for the latest version.'); | ||
} | ||
|
||
// Check for updates using the UpdaterController's logic | ||
$releaseData = $this->fetchReleaseData($version); | ||
|
||
if (is_null($releaseData) || empty($releaseData['zipball_url'])) { | ||
$this->error('No update available.'); | ||
|
||
return Command::FAILURE; | ||
} | ||
|
||
// Dispatch the update job | ||
UpdateProjectJob::dispatch($releaseData); | ||
|
||
$this->info('Update process has been started.'); | ||
|
||
return Command::SUCCESS; | ||
} | ||
|
||
private function fetchReleaseData(?string $version) | ||
{ | ||
// Base URL for GitHub releases | ||
$baseRepoUrl = 'https://api.github.com/repos/anisAronno/laravel-starter/releases'; | ||
|
||
// Determine the URL based on whether a specific version is provided | ||
$repoUrl = $version | ||
? "{$baseRepoUrl}/tags/v{$version}" // Fetch specific version if provided | ||
: "{$baseRepoUrl}/latest"; // Otherwise, fetch the latest release | ||
|
||
// Make the API request with User-Agent | ||
$context = stream_context_create([ | ||
'http' => [ | ||
'header' => [ | ||
'User-Agent: PHP', | ||
], | ||
], | ||
]); | ||
|
||
$response = @file_get_contents($repoUrl, false, $context); | ||
|
||
return $response !== false ? json_decode($response, true) : null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters