Skip to content

Commit

Permalink
Fetch application version from vendor/composer/installed.php
Browse files Browse the repository at this point in the history
  • Loading branch information
guvra committed Feb 8, 2024
1 parent 55ea19e commit ea038b0
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 26 deletions.
4 changes: 0 additions & 4 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,6 @@ jobs:
id: 'get-version'
run: 'echo "version=${GITHUB_REF#refs/tags/}" >> $GITHUB_OUTPUT'

# Cannot use ${{ github.ref }}, it isn't the tag name, but "/refs/tags/tag_name"
- name: 'Replace application version'
run: sed -i "s/VERSION = 'dev'/VERSION = '${{ steps.get-version.outputs.version }}'/" src/Console/Application.php

- name: 'Create phar'
run: 'bin/compile'

Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"php": "^8.1",
"ext-json": "*",
"ext-pdo": "*",
"composer-runtime-api": "^2.0",
"doctrine/dbal": "^3.1",
"druidfi/mysqldump-php": "^1.0",
"fakerphp/faker": "^1.17",
Expand Down
3 changes: 3 additions & 0 deletions docker/php-cli/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ RUN docker-php-ext-install pdo_mysql
RUN ln -s $PHP_INI_DIR/php.ini-development $PHP_INI_DIR/php.ini
COPY ./config/php.ini $PHP_INI_DIR/conf.d/gdpr-dump.ini

# Install git (for application version detection during compilation)
RUN apk add --no-cache git

# Install composer
ENV COMPOSER_ALLOW_SUPERUSER 1
COPY --from=composer:2 /usr/bin/composer /usr/local/bin/composer
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,14 +4,36 @@

namespace Smile\GdprDump\Console;

use Composer\InstalledVersions;
use Symfony\Component\Console\Application as BaseApplication;

class Application extends BaseApplication
{
public const VERSION = 'dev';
private const PACKAGE_NAME = 'smile/gdpr-dump';

public function __construct()
{
parent::__construct('GdprDump', self::VERSION);
parent::__construct('GdprDump', $this->getPackageVersion());
}

/**
* Get the application version.
*/
private function getPackageVersion(): string
{
$prettyVersion = (string) InstalledVersions::getPrettyVersion(self::PACKAGE_NAME);
$reference = (string) InstalledVersions::getReference(self::PACKAGE_NAME);

if ($prettyVersion === '' || $reference === '') {
return 'Unknown version';
}

if (preg_match('/[^v\d.]/', $prettyVersion) === 0) {
// Tags
return $prettyVersion;
}

// Branches (with ref)
return $prettyVersion . '@' . substr($reference, 0, 7);
}
}
20 changes: 0 additions & 20 deletions tests/unit/Console/ApplicationTest.php

This file was deleted.

0 comments on commit ea038b0

Please sign in to comment.