Skip to content

Commit

Permalink
Merge pull request #40 from jakzal/feature/phar-update
Browse files Browse the repository at this point in the history
Add a command to automatically update phar versions
  • Loading branch information
jakzal authored Mar 15, 2018
2 parents 4336c37 + b3d2104 commit d58022d
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ script:

after_script:
- docker images
- docker run -it --rm jakzal/phpqa:$flavour sh -c 'php /usr/local/bin/tools.php update-phars && diff $TOOLS_JSON.bak $TOOLS_JSON || echo -e "\033[0;91mSome tools might have new phar releases. Update tools.json.\033[0m"'
68 changes: 68 additions & 0 deletions tools.php
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,52 @@ function UpdateReadme(PleaseTry $tools, string $filePath)
}
}

namespace ToolsUpdate {

use Model\Command;
use Model\MultiStepCommand;
use Model\ShCommand;

function FindLatestPharsCommand(string $jsonPath): Command
{
$command = <<<'CMD'
grep -e 'github\.com.*releases.*\.phar"' %TOOLS_JSON% |
sed -e 's@.*github.com/\(.*\)/releases.*@\1@' |
xargs -I"{}" sh -c "curl -s -XGET 'https://api.github.com/repos/{}/releases' -H 'Accept:application/json' | grep browser_download_url | head -n 1" |
sed -e 's/^[^:]*: "\([^"]*\)"/\1/'
CMD;
$command = strtr($command, ['%TOOLS_JSON%' => $jsonPath]);

return new ShCommand($command);
}

function FindLatestPhars(string $jsonPath): array
{
$phars = [];

exec((string) FindLatestPharsCommand($jsonPath), $phars);

return $phars;
}

function UpdatePharsCommand(string $jsonPath, array $phars): Command
{
$replacements = implode(' ', array_map(
function (string $phar) {
$project = preg_replace('@https://[^/]*/([^/]*/[^/]*).*@', '$1', $phar);

return strtr(
'-e "s@\"phar\": \"\([^\"]*%PROJECT%[^\"]*\)\"@\"phar\": \"%PHAR%\"@"',
['%PROJECT%' => $project, '%PHAR%' => $phar]
);
},
$phars
));

return new ShCommand(sprintf('sed -i.bak %s %s', $replacements, $jsonPath));
}
}

namespace Runner {

use Model\Command;
Expand All @@ -600,6 +646,8 @@ function Run(Command $command): int {
use function DocUpdate\UpdateReadme;
use function Runner\Run;
use function Test\TestCommand;
use function ToolsUpdate\FindLatestPhars;
use function ToolsUpdate\UpdatePharsCommand;

$jsonPath = !empty(getenv('TOOLS_JSON')) ? getenv('TOOLS_JSON') : __DIR__ . '/tools.json';
$action = $argv[1] ?? 'list';
Expand All @@ -619,6 +667,26 @@ function Run(Command $command): int {
UpdateReadme($tools, $filePath);
printf('%s was updated.', $filePath);

break;
case 'update-phars':
$phars = FindLatestPhars($jsonPath);

if (empty($phars)) {
print('Could not find any phars to update.');

exit(1);
}

print('Found phars:'.PHP_EOL);

foreach ($phars as $phar) {
printf('* %s'.PHP_EOL, $phar);
}

printf('Updated "%s".'.PHP_EOL, $jsonPath);

exit(Run(UpdatePharsCommand($jsonPath, $phars)));

break;
case 'list':
print('Available tools:' . PHP_EOL);
Expand Down

0 comments on commit d58022d

Please sign in to comment.