diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index 5686de80..d0c3bab6 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -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' diff --git a/composer.json b/composer.json index 90984bcb..32d13466 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/docker/php-cli/Dockerfile b/docker/php-cli/Dockerfile index a166c409..c28ace80 100644 --- a/docker/php-cli/Dockerfile +++ b/docker/php-cli/Dockerfile @@ -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 diff --git a/src/Console/Application.php b/src/Console/Application.php index 9f1fac42..a41e57f7 100644 --- a/src/Console/Application.php +++ b/src/Console/Application.php @@ -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); } } diff --git a/tests/unit/Console/ApplicationTest.php b/tests/unit/Console/ApplicationTest.php deleted file mode 100644 index f0260196..00000000 --- a/tests/unit/Console/ApplicationTest.php +++ /dev/null @@ -1,20 +0,0 @@ -assertSame(Application::VERSION, $application->getVersion()); - } -}