Skip to content

Commit

Permalink
Check for latest version
Browse files Browse the repository at this point in the history
  • Loading branch information
Jubeki committed Jan 24, 2024
1 parent d374eb6 commit 92187b1
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions src/NewCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -843,6 +843,10 @@ protected function runCommands($commands, InputInterface $input, OutputInterface
$output->write(' '.$line);
});

if($this->existsNewerInstallerVersion()) {
$output->writeln(' <bg=green;fg=white> NEW VERSION </> A newer version of Laravel Installer is available. To update simply run <options=bold>`composer global update`</>'.PHP_EOL);
}

return $process;
}

Expand Down Expand Up @@ -894,4 +898,54 @@ protected function pregReplaceInFile(string $pattern, string $replace, string $f
preg_replace($pattern, $replace, file_get_contents($file))
);
}

/**
* Check if there is a newer version of Laravel Installer.
*/
protected function existsNewerInstallerVersion(): bool
{
$appVersion = $this->getApplication()->getVersion();
$latestVersion = $this->getLatestVersion();

return version_compare($appVersion, $latestVersion, '<');
}

/**
* Get latest version without leading "v"
*/
protected function getLatestVersion(): string
{
$latestRelease = $this->getLatestRelease();

$tag = $latestRelease['tag_name'];

if(str_starts_with($tag, 'v')) {
$tag = substr($tag, 1);
}

return $tag;
}

/**
* Get latest release information
*/
protected function getLatestRelease(): array
{
// Required for GitHub API, otherwise the request will be 403 Forbidden
$context = stream_context_create([
'http' => [
'header' => [
'User-Agent: Laravel Installer',
],
],
]);

$json = file_get_contents(
'https://api.github.com/repos/laravel/installer/releases/latest',
false,
$context
);

return json_decode($json, true);
}
}

0 comments on commit 92187b1

Please sign in to comment.