Skip to content

Commit

Permalink
Add update check and initiate artisan command
Browse files Browse the repository at this point in the history
  • Loading branch information
anisAronno committed Sep 26, 2024
1 parent f32f949 commit f5af460
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 1 deletion.
59 changes: 59 additions & 0 deletions app/Console/Commands/CheckUpdateCommand.php
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';
}
}
65 changes: 65 additions & 0 deletions app/Console/Commands/UpdateInitiateCommand.php
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;
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "anisaronno/laravel-starter",
"type": "project",
"version": "0.3.2",
"version": "0.3.3",
"description": "A perfect laravel starter project for any kind of project.",
"keywords": [
"laravel",
Expand Down

0 comments on commit f5af460

Please sign in to comment.