Skip to content

Commit

Permalink
Warns about the latest version
Browse files Browse the repository at this point in the history
  • Loading branch information
nunomaduro committed Aug 2, 2021
1 parent b1ca9ce commit 83f89bf
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 1 deletion.
19 changes: 18 additions & 1 deletion app/Commands/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@
use Laravel\Forge\Forge;
use Laravel\Forge\Resources\Server;
use LaravelZero\Framework\Commands\Command as BaseCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

abstract class Command extends BaseCommand
{
use Concerns\InteractsWithIO;
use Concerns\InteractsWithIO,
Concerns\InteractsWithVersions;

/**
* The aliases of the command.
Expand Down Expand Up @@ -88,6 +91,20 @@ public function __construct(
$this->setAliases($this->aliases);
}

/**
* Execute the console command.
*
* @param \Symfony\Component\Console\Input\InputInterface $input
* @param \Symfony\Component\Console\Output\OutputInterface $output
* @return int
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
return tap(parent::execute($input, $output), function () {
$this->ensureLatestVersion();
});
}

/**
* Gets the current server.
*
Expand Down
13 changes: 13 additions & 0 deletions app/Commands/Concerns/InteractsWithIO.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,19 @@ public function successfulStep($text)
$this->line('<fg=green>==></> <options=bold>'.$text.'</>');
}

/**
* Display a warn "step" message.
*
* @param string|array $text
* @return void
*/
public function warnStep($text)
{
$text = $this->formatStepText($text);

$this->line('<fg=yellow>==></> <options=bold>'.$text.'</>');
}

/**
* Display a ask "step" message.
*
Expand Down
52 changes: 52 additions & 0 deletions app/Commands/Concerns/InteractsWithVersions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace App\Commands\Concerns;

trait InteractsWithVersions
{
/**
* Warns the user about the latest version of Forge CLI.
*
* @return void
*/
protected function ensureLatestVersion()
{
$current = 'v' . config('app.version');

if (version_compare($remote = $this->getLatestVersion(), $current) > 0) {
$this->warnStep(['You are using an outdated version %s of Forge CLI. Please update to %s.', $current, $remote]);
}
}

/**
* Returns the latest version.
*
* @return string
*/
protected function getLatestVersion()
{
$resolver = function () {
$package = json_decode(file_get_contents(
'https://packagist.org/p2/laravel/forge-cli.json'
), true);

return collect($package['packages']['laravel/forge-cli'])
->first()['version'];
};

if (is_null($this->config->get('latest_version_verified_at'))) {
$this->config->set('latest_version_verified_at', now()->timestamp);
}

if (is_null($this->config->get('latest_version'))) {
$this->config->set('latest_version', call_user_func($resolver));
}

if ($this->config->get('latest_version_verified_at') < now()->subDays(1)->timestamp) {
$this->config->set('latest_version', call_user_func($resolver));
$this->config->set('latest_version_verified_at', now()->timestamp);
}

return $this->config->get('latest_version');
}
}

0 comments on commit 83f89bf

Please sign in to comment.