diff --git a/app/Commands/Command.php b/app/Commands/Command.php index e75235c..401974b 100644 --- a/app/Commands/Command.php +++ b/app/Commands/Command.php @@ -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. @@ -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. * diff --git a/app/Commands/Concerns/InteractsWithIO.php b/app/Commands/Concerns/InteractsWithIO.php index c28fb34..ae7ede5 100644 --- a/app/Commands/Concerns/InteractsWithIO.php +++ b/app/Commands/Concerns/InteractsWithIO.php @@ -123,6 +123,19 @@ public function successfulStep($text) $this->line('==> '.$text.''); } + /** + * Display a warn "step" message. + * + * @param string|array $text + * @return void + */ + public function warnStep($text) + { + $text = $this->formatStepText($text); + + $this->line('==> '.$text.''); + } + /** * Display a ask "step" message. * diff --git a/app/Commands/Concerns/InteractsWithVersions.php b/app/Commands/Concerns/InteractsWithVersions.php new file mode 100644 index 0000000..235b3d1 --- /dev/null +++ b/app/Commands/Concerns/InteractsWithVersions.php @@ -0,0 +1,52 @@ +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'); + } +}