8000 Add self-update check to command runs by PeteBishwhip · Pull Request #10 · NativeCLI/NativeCLI · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add self-update check to command runs #10

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

Merged
merged 5 commits into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
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
44 changes: 44 additions & 0 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
use NativeCLI\Command\NewCommand;
use NativeCLI\Command\SelfUpdateCommand;
use NativeCLI\Command\UpdateNativePHPCommand;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Console\Output\OutputInterface;
use Throwable;

final class Application extends \Symfony\Component\Console\Application
{
Expand All @@ -22,6 +28,44 @@ public static function create(): Application
return new Application();
}

/**
* @throws \Exception
*/
public function run(?InputInterface $input = null, ?OutputInterface $output = null): int
{
try {
$input ??= new ArgvInput();
$output ??= new ConsoleOutput();

if ($input->getFirstArgument() != 'self-update') {
$tempInput = new ArgvInput([
'self-update',
'--check',
'--format=json',
]);

$tempOutput = new BufferedOutput();
$this->find('self-update')->run($tempInput, $tempOutput);

$jsonOutput = json_decode($tempOutput->fetch(), true);

if (
json_last_error() === JSON_ERROR_NONE
&& isset($jsonOutput['update_available'])
&& $jsonOutput['update_available'] === true
) {
$output->writeln(
'<info>There is a new version of NativePHP available. Run `nativecli self-update` to update.</info>'
);
}
}
} catch (Throwable) {
// Continue silently. Allow the command requested to run.
}

return parent::run($input, $output);
}

public function getCommands(): array
{
return [
Expand Down
76 changes: 67 additions & 9 deletions src/Command/SelfUpdateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ConfirmationQuestion;
use Symfony\Component\Process\Process;
Expand All @@ -30,18 +31,37 @@ protected function configure(): void
'The version to update to',
'latest',
);

$this->addOption(
'check',
null,
InputOption::VALUE_NONE,
'Check for updates only',
)->addOption(
'format',
null,
InputOption::VALUE_OPTIONAL,
'The format to output the update information in',
'text',
);
}

/**
* @throws Exception
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$format = $input->getOption('format');

// Get users home directory
$home = $_SERVER['HOME'] ?? $_SERVER['USERPROFILE'] ?? null;

if ($home === null) {
$output->writeln('<error>Failed to determine home directory</error>');
$output->writeln(
$format === 'json'
? json_encode(['error' => 'Failed to determine home directory'])
: '<error>Failed to determine home directory</error>'
);

return Command::FAILURE;
}
Expand All @@ -52,15 +72,19 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$version = Version::getLatestVersion();

if ($version === null) {
$output->writeln('<error>Failed to retrieve latest version</error>');
$output->writeln($format === 'json'
? json_encode(['error' => 'Failed to retrieve latest version'])
: '<error>Failed to retrieve latest version</error>');

return Command::FAILURE;
}
} else {
$availableVersions = Version::getAvailableVersions();

if (!$availableVersions->contains($version)) {
$output->writeln('<error>Version ' . $version . ' is not available</error>');
$output->writeln($format === 'json'
? json_encode(['error' => 'Version ' . $version . ' is not available'])
: '<error>Version ' . $version . ' is not available</error>');

return Command::FAILURE;
}
Expand All @@ -69,14 +93,32 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$version = $availableVersions->first(fn (SemanticVersion $v) => $v->isEqual(SemanticVersion::parse($version)));

if ($version === null) {
$output->writeln('<error>Failed to retrieve version ' . $version . '</error>');
$output->writeln(
$format === 'json'
? json_encode(['error' => 'Failed to retrieve version ' . $version])
: '<error>Failed to retrieve version ' . $version . '</error>'
);

return Command::FAILURE;
}
}

if (Version::isCurrentVersion($version)) {
$output->writeln('<info>Already up to date</info>');
$output->writeln(
$format === 'json'
? json_encode(['update_available' => false])
: '<info>Already up to date</info>'
);

return Command::SUCCESS;
}

if ($input->getOption('check')) {
$output->writeln(
$format === 'json'
? json_encode(['update_available' => true, 'version' => (string) $version])
: '<info>Update available: ' . $version . '</info>'
);

return Command::SUCCESS;
}
Expand All @@ -89,12 +131,20 @@ protected function execute(InputInterface $input, OutputInterface $output): int
);

if (!$questionHelper->ask($input, $output, $question)) {
$output->writeln('<error>Update cancelled by user</error>');
$output->writeln(
$format === 'json'
? json_encode(['error' => 'Update cancelled by user'])
: '<error>Update cancelled by user</error>'
);

return Command::FAILURE;
}

$output->writeln('<info>Updating to version ' . $version . '</info>');
$output->writeln(
$format === 'json'
? json_encode(['update_available' => true, 'version' => (string) $version])
: '<info>Updating to version ' . $version . '</info>'
);

$composer = new Composer(new Filesystem(), getcwd());
$process = new Process([...$composer->findComposer(), 'global', 'require', 'nativecli/nativecli:' . $version]);
Expand All @@ -103,12 +153,20 @@ protected function execute(InputInterface $input, OutputInterface $output): int
});

if ($status !== Command::SUCCESS) {
$output->writeln('<error>Failed to update to version ' . $version . '</error>');
$output->writeln(
$format === 'json'
? json_encode(['error' => 'Failed to update to version ' . $version])
: '<error>Failed to update to version ' . $version . '</error>'
);

return $status;
}

$output->writeln('<info>Successfully updated to version ' . $version . '</info>');
$output->writeln(
$format === 'json'
? json_encode(['success' => 'Successfully updated to version ' . $version])
: '<info>Successfully updated to version ' . $version . '</info>'
);

return Command::SUCCESS;
}
Expand Down
0