Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check for latest version #308

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions src/NewCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,35 @@ protected function interact(InputInterface $input, OutputInterface $output)
| |___| (_| | | | (_| |\ V / __/ |
|______\__,_|_| \__,_| \_/ \___|_|</>'.PHP_EOL.PHP_EOL);

if ($this->existsNewerInstallerVersion()) {
$output->writeln(PHP_EOL.' <bg=green;fg=white> NEW VERSION </> A newer version of Laravel Installer is available.'.PHP_EOL);

$shouldUpdate = select(
label: 'Do you want to update your Laravel Installer before continuing?',
options: [
'yes' => 'Yes, please update the Laravel Installer',
'no' => 'No, continue with the current version',
],
default: 'yes',
);

if ($shouldUpdate === 'yes') {
$this->composer = new Composer(new Filesystem());

$output->writeln(' <bg=green;fg=white> NEW VERSION </> Updating Laravel Installer...'.PHP_EOL);

$this->runCommands([
$this->findComposer().' global update laravel/installer',
], $input, $output);

$output->writeln(PHP_EOL.' <bg=green;fg=white> NEW VERSION </> The Laravel Installer was sucessfully updated.'.PHP_EOL);

$output->writeln(' <fg=gray>➜</> Please run <options=bold>laravel new</> again.'.PHP_EOL);

exit(0);
}
}

if (! $input->getArgument('name')) {
$input->setArgument('name', text(
label: 'What is the name of your project?',
Expand Down Expand Up @@ -894,4 +923,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);
}
}
Loading