Skip to content
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

Get application version from composer.lock #176

Merged
merged 1 commit into from
May 22, 2024
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p

## [v3.5.0]
- Support `phpunit/php-timer` 7.0
- Add `composer-runtime-api:"^2.0"` dependency to be able to get application version automatically

## [v3.4.1]
- Add test to verify that `Application::VERSION` match the latest git tag
Expand Down
10 changes: 7 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@
},
"require": {
"php": "^7.4 || ^8.0",
"symfony/console": "^4.4 || ^5.0 || ^6.0 || ^7.0",
"symfony/finder": "^4.4 || ^5.0 || ^6.0 || ^7.0",
"composer-runtime-api": "^2.0",
"nikic/php-parser": "^4.18 || ^5.0",
"php-parallel-lint/php-console-highlighter": "^1.0",
"phpunit/php-timer": "^2.0 || ^3.0 || ^4.0 || ^5.0 || ^6.0 || ^7.0"
"phpunit/php-timer": "^2.0 || ^3.0 || ^4.0 || ^5.0 || ^6.0 || ^7.0",
"symfony/console": "^4.4 || ^5.0 || ^6.0 || ^7.0",
"symfony/finder": "^4.4 || ^5.0 || ^6.0 || ^7.0"
},
"require-dev": {
"phpunit/phpunit": "^9.6",
Expand All @@ -34,6 +35,9 @@
"Povils\\PHPMND\\Tests\\": "tests/"
}
},
"config": {
"sort-packages": true
},
"bin": [
"bin/phpmnd"
],
Expand Down
26 changes: 24 additions & 2 deletions src/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Povils\PHPMND\Console;

use Composer\InstalledVersions;
use OutOfBoundsException;
use Povils\PHPMND\Command\RunCommand;
use Povils\PHPMND\Container;
use Symfony\Component\Console\Application as BaseApplication;
Expand All @@ -14,14 +16,15 @@

class Application extends BaseApplication
{
public const VERSION = '3.5.0';
public const PACKAGE_NAME = 'povils/phpmnd';

private const NAME = 'phpmnd';

private Container $container;

public function __construct(Container $container)
{
parent::__construct(self::NAME, self::VERSION);
parent::__construct(self::NAME, self::getPrettyVersion());

$this->setDefaultCommand('run', true);

Expand Down Expand Up @@ -62,4 +65,23 @@ protected function getDefaultCommands(): array
{
return [new HelpCommand(), new RunCommand()];
}

public static function getPrettyVersion(): string
{
// Pre 2.0 Composer runtime didn't have this class.
if (!class_exists(InstalledVersions::class)) {
return 'unknown';
}

try {
return (string) InstalledVersions::getPrettyVersion(self::PACKAGE_NAME);
} catch (OutOfBoundsException $e) {
if (preg_match('#package .*' . preg_quote(self::PACKAGE_NAME, '#') . '.* not installed#i', $e->getMessage()) === 0) {
throw $e;
}

// We have a bogus exception: how can PHPMND be not installed if we're here?
return 'not-installed';
}
}
}
13 changes: 3 additions & 10 deletions src/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,8 @@ private function __construct(array $values)
public static function create(): self
{
return new self([
Parser::class => static function (self $container): Parser {
return (new ParserFactory())->createForHostVersion();
},
FileParser::class => static function (self $container): FileParser {
return new FileParser($container->getParser());
},
Parser::class => static fn (): Parser => (new ParserFactory())->createForHostVersion(),
FileParser::class => static fn (self $container): FileParser => new FileParser($container->getParser()),
]);
}

Expand All @@ -57,10 +53,7 @@ private function offsetSet(string $id, Closure $value): void
unset($this->values[$id]);
}

/**
* @return object
*/
private function get(string $id)
private function get(string $id): object
{
if (!isset($this->keys[$id])) {
throw new InvalidArgumentException(sprintf('Unknown service "%s"', $id));
Expand Down
2 changes: 1 addition & 1 deletion src/Printer/Xml.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function printData(OutputInterface $output, HintList $hintList, array $de
$output->writeln('Generate XML output...');
$dom = new DOMDocument();
$rootNode = $dom->createElement('phpmnd');
$rootNode->setAttribute('version', Application::VERSION);
$rootNode->setAttribute('version', Application::getPrettyVersion());
$rootNode->setAttribute('fileCount', (string) count($groupedList));

$filesNode = $dom->createElement('files');
Expand Down
2 changes: 1 addition & 1 deletion tests/Printer/XmlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public function testPrintData() : void

private function assertXml(string $expected, string $actualFile) : void
{
$expectedXml = str_replace('%%PHPMND_VERSION%%', Application::VERSION, $expected);
$expectedXml = str_replace('%%PHPMND_VERSION%%', Application::getPrettyVersion(), $expected);
$this->assertXmlStringEqualsXmlString($expectedXml, file_get_contents($actualFile));
}
}
Loading