Skip to content

Commit

Permalink
Merge branch '47341-outdated' into 'master'
Browse files Browse the repository at this point in the history
Added outdated command (fixes #47341)

See merge request iserv/cotor!3
  • Loading branch information
althaus committed May 13, 2022
2 parents 7be5cb7 + 811d700 commit 8dc24d4
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 7 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ All notable changes to this project will be documented in this file, in reverse

## [Unreleased]

### Added

- Added `outdated` command. #47341

### Fixed

- Fixed `update-all` command not finding any tools.

## 1.4 - 2022-04-12

### Changed
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ For each tool a standalone folder named by the package without the vendor will b
* **install $name**: Installs a new tool. `$name` must be a tool's composer or registered shortcut name.
* **update $name**: Updates an installed tool. `$name` must be a tool's name without vendor or registered shortcut name.
* **update-all**: Updates all installed tools.
* **outdated**: Lists all tools and checks if they are up-to-date.
* **extend $name $extension**: Installs a tool extension. `$name` must be a tool's composer or registered shortcut name. `$extension` must be the composer name of the extension.

## How to build cotor
Expand Down
1 change: 1 addition & 0 deletions bin/composertoolsinstaller
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ $application->add(new Command\InstallCommand($filesystem));
$application->add(new Command\ExtendCommand($filesystem));
$application->add(new Command\UpdateCommand());
$application->add(new Command\UpdateAllCommand());
$application->add(new Command\OutdatedCommand());
$application->run();
12 changes: 9 additions & 3 deletions src/Command/AbstractToolCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ protected function getPackage(string $name): Package
/**
* @throws ProcessFailedException
*/
protected function runComposerWithPackage(string $command, string $targetDir, Package $package, bool $useVersion): void
protected function runComposerWithPackage(string $command, string $targetDir, Package $package, bool $useVersion): string
{
$name = $package->getComposerName();
if ($useVersion) {
Expand All @@ -42,23 +42,29 @@ protected function runComposerWithPackage(string $command, string $targetDir, Pa

$process = new Process(['composer', $command, sprintf('--working-dir=%s', $targetDir), $name]);
$process->mustRun();

return $process->getOutput();
}

/**
* @throws ProcessFailedException
*/
protected function runComposer(string $command, string $targetDir): void
protected function runComposer(string $command, string $targetDir): string
{
$process = new Process(['composer', $command, sprintf('--working-dir=%s', $targetDir)]);
$process->mustRun();

return $process->getOutput();
}

/**
* @throws ProcessFailedException
*/
protected function runComposerWithArguments(string $command, string $targetDir, string ...$arguments): void
protected function runComposerWithArguments(string $command, string $targetDir, string ...$arguments): string
{
$process = new Process(array_merge(['composer', $command, sprintf('--working-dir=%s', $targetDir)], $arguments));
$process->mustRun();

return $process->getOutput();
}
}
59 changes: 59 additions & 0 deletions src/Command/OutdatedCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);

namespace IServ\ComposerToolsInstaller\Command;

use IServ\ComposerToolsInstaller\Tools\ToolPath;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\Process\Exception\ProcessFailedException;

final class OutdatedCommand extends AbstractToolCommand
{
protected static $defaultName = 'outdated';

protected function configure(): void
{
$this
->setDescription('Check if tools are outdated or not')
->setHelp('This command allows you to check if tools are outdated or not')
;
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);

$toolsDir = getcwd() . '/tools';
if (!is_dir($toolsDir)) {
$io->error('There is no tools directory! Did you miss to install something first?');

return Command::INVALID;
}

$targetDirs = ToolPath::glob($toolsDir);

if ([] === $targetDirs) {
$io->warning('Could not find any tools! Did you miss to run `install`?');
}

foreach ($targetDirs as $targetDir) {
try {
if ('' === $out = $this->runComposerWithArguments('outdated', $targetDir, '--direct')) {
$io->writeln(sprintf('<info>✓</info> %s is up-to-date.', ToolPath::path2name($targetDir)));
} elseif (preg_match('#^1(?:.+?)/(?:.+?)\s(?P<current>.+?)\s.\s(?P<new>.+?)\s#', $out, $matches)) {
$io->writeln(sprintf('<comment>⚠</comment> %s is outdated: %s => %s', ToolPath::path2name($targetDir), $matches['current'], $matches['new']));
} else {
$io->writeln(sprintf('<comment>⚠</comment> %s is outdated: %s', ToolPath::path2name($targetDir), trim($out)));
}
} catch (ProcessFailedException) {
$io->writeln(sprintf('<error>✗</error> Failed to check if %s is outdated.', ToolPath::path2name($targetDir)));
}
}

return Command::SUCCESS;
}
}
15 changes: 11 additions & 4 deletions src/Command/UpdateAllCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace IServ\ComposerToolsInstaller\Command;

use IServ\ComposerToolsInstaller\Tools\ToolPath;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
Expand Down Expand Up @@ -33,12 +34,18 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return Command::INVALID;
}

foreach (glob($toolsDir . '/*', GLOB_ONLYDIR) as $targetDir) {
$targetDirs = ToolPath::glob($toolsDir);

if ([] === $targetDirs) {
$io->warning('Could not find any tools! Did you miss to run `install`?');
}

foreach ($targetDirs as $targetDir) {
try {
$this->runComposer('update', $targetDir);
$io->writeln(sprintf('<info>✓</info> %s updated successfully.', substr(strrchr($targetDir, '/'), 1)));
} catch (ProcessFailedException $e) {
$io->writeln(sprintf('<error>✗</error> Failed to update %s.', substr(strrchr($targetDir, '/'), 1)));
$io->writeln(sprintf('<info>✓</info> %s updated successfully.', ToolPath::path2name($targetDir)));
} catch (ProcessFailedException) {
$io->writeln(sprintf('<error>✗</error> Failed to update %s.', ToolPath::path2name($targetDir)));
}
}

Expand Down
13 changes: 13 additions & 0 deletions src/Tools/ToolPath.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,17 @@ public static function createExecutable(string $toolsDir, string $name): string
{
return sprintf('%s/%s', $toolsDir, $name);
}

/** @return array<int, string> */
public static function glob(string $toolsDir): array
{
$targetDirs = glob($toolsDir . '/.*', GLOB_ONLYDIR);

return array_filter($targetDirs, static fn (string $dir): bool => !str_ends_with($dir, '/.') && !str_ends_with($dir, '/..'));
}

public static function path2name(string $tooldir): string
{
return substr(strrchr($tooldir, '/'), 2); // 2 = Remove slash and dot
}
}

0 comments on commit 8dc24d4

Please sign in to comment.