From d81cff6f6c24573c96173e8267a49d4d0c25a2e3 Mon Sep 17 00:00:00 2001 From: Kamil Date: Thu, 4 Apr 2024 10:08:32 +0200 Subject: [PATCH 01/14] #1 - added simple setup of package with tests, codestyle and environment stuff --- .github/workflows/check-pr-title.yml | 12 +++++++ .github/workflows/check.yml | 43 ++++++++++++++++++++++ .github/workflows/dependabot.yml | 35 ++++++++++++++++++ .gitignore | 6 ++++ Makefile | 25 +++++++++++++ codestyle.php | 14 ++++++++ composer.json | 33 +++++++++++++++++ docker-compose.yml | 9 +++++ environment/php/Dockerfile | 18 ++++++++++ src/VersionHelper.php | 40 +++++++++++++++++++++ src/scripts/check.sh | 14 ++++++++ src/scripts/version.sh | 53 ++++++++++++++++++++++++++++ tests/GenerateVersionTest.php | 27 ++++++++++++++ 13 files changed, 329 insertions(+) create mode 100644 .github/workflows/check-pr-title.yml create mode 100644 .github/workflows/check.yml create mode 100644 .github/workflows/dependabot.yml create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 codestyle.php create mode 100644 composer.json create mode 100644 docker-compose.yml create mode 100644 environment/php/Dockerfile create mode 100644 src/VersionHelper.php create mode 100755 src/scripts/check.sh create mode 100755 src/scripts/version.sh create mode 100644 tests/GenerateVersionTest.php diff --git a/.github/workflows/check-pr-title.yml b/.github/workflows/check-pr-title.yml new file mode 100644 index 0000000..3f36097 --- /dev/null +++ b/.github/workflows/check-pr-title.yml @@ -0,0 +1,12 @@ +name: Check PR Title +on: + pull_request: + branches: [ "main" ] + types: [opened, edited, synchronize, ready_for_review, reopened] + +jobs: + check-pr-title: + name: Check PR title + runs-on: ubuntu-22.04 + steps: + - uses: blumilksoftware/action-pr-title@v1.2.0 diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml new file mode 100644 index 0000000..739498e --- /dev/null +++ b/.github/workflows/check.yml @@ -0,0 +1,43 @@ +name: Test & lint PHP codebase + +on: + pull_request: + branches: [ "main" ] + types: [opened, synchronize, reopened, ready_for_review] + paths: + - '**.php' + - 'composer.json' + - 'composer.lock' + - 'phpunit.xml' + +jobs: + test-and-lint-php: + name: Test & lint PHP codebase + timeout-minutes: 10 + if: github.event.pull_request.draft == false + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4.1.1 # https://github.com/actions/checkout + + - name: Cache dependencies + uses: actions/cache@v4.0.2 # https://github.com/actions/cache + with: + path: vendor + key: ${{ runner.os }}-composer-dependencies-${{ hashFiles('composer.lock') }} + restore-keys: ${{ runner.os }}-composer-dependencies + + - name: Setup PHP + uses: shivammathur/setup-php@2.30.0 # https://github.com/shivammathur/setup-php + with: + php-version: 8.3 + extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, pdo_pgsql, intl + coverage: none + + - name: Install Composer dependencies + working-directory: src + run: composer install --prefer-dist --no-interaction + + - name: Run test + working-directory: src + run: composer test diff --git a/.github/workflows/dependabot.yml b/.github/workflows/dependabot.yml new file mode 100644 index 0000000..ae834ac --- /dev/null +++ b/.github/workflows/dependabot.yml @@ -0,0 +1,35 @@ +version: 2 + +updates: + - package-ecosystem: composer + directory: "/" + schedule: + interval: monthly + time: "06:30" + timezone: "Europe/Warsaw" + commit-message: + prefix: "- (php) " + target-branch: main + open-pull-requests-limit: 1 + + - package-ecosystem: github-actions + directory: "/" + schedule: + interval: monthly + time: "06:30" + timezone: "Europe/Warsaw" + commit-message: + prefix: "- (github actions) " + target-branch: main + open-pull-requests-limit: 1 + + - package-ecosystem: docker + directory: "/" + schedule: + interval: monthly + time: "06:30" + timezone: "Europe/Warsaw" + commit-message: + prefix: "- (docker dev) " + target-branch: main + open-pull-requests-limit: 1 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7224874 --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +/.composer +/.idea +/vendor + +composer.lock +.env diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..a0ca765 --- /dev/null +++ b/Makefile @@ -0,0 +1,25 @@ +DOCKER_COMPOSE_FILENAME=docker-compose.yml +PHP_FPM_SERVICE_NAME=php + +.PHONY: shell +shell: + docker compose -f ${DOCKER_COMPOSE_FILENAME} exec ${PHP_FPM_SERVICE_NAME} ash + +.PHONY: test +test: + docker compose -f ${DOCKER_COMPOSE_FILENAME} exec ${PHP_FPM_SERVICE_NAME} composer test + +.PHONY: csf +csf: + docker compose -f ${DOCKER_COMPOSE_FILENAME} exec ${PHP_FPM_SERVICE_NAME} composer csf + +.PHONY: run +run: + docker compose -f ${DOCKER_COMPOSE_FILENAME} up -d + +.PHONY: stop +stop: + docker compose -f ${DOCKER_COMPOSE_FILENAME} stop + +.PHONY: restart +restart: stop run diff --git a/codestyle.php b/codestyle.php new file mode 100644 index 0000000..fc6ee0d --- /dev/null +++ b/codestyle.php @@ -0,0 +1,14 @@ +config(); diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..4d66ae6 --- /dev/null +++ b/composer.json @@ -0,0 +1,33 @@ +{ + "name": "blumilksoftware/version", + "description": "Blumilk versioning package", + "license": "MIT", + "type": "library", + "require": { + "php": "^8.1" + }, + "require-dev": { + "blumilksoftware/codestyle": "^2.8", + "phpunit/phpunit": "^11.0.9" + }, + "authors": [ + { + "name": "Kamil Piech", + "email": "kamil.piech@blumilk.pl" + } + ], + "config": { + "optimize-autoloader": true, + "sort-packages": true + }, + "autoload": { + "psr-4": { + "Blumilk\\Version\\": "src/" + } + }, + "scripts": { + "cs": "./vendor/bin/php-cs-fixer fix --dry-run --diff --config codestyle.php", + "csf": "./vendor/bin/php-cs-fixer fix --diff --config codestyle.php", + "test": "./vendor/bin/phpunit tests --colors=always" + } +} diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..b90db7c --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,9 @@ +services: + php: + container_name: blumilk-version-php + build: + context: environment/php + working_dir: /application + user: ${CURRENT_UID:-1000} + volumes: + - .:/application diff --git a/environment/php/Dockerfile b/environment/php/Dockerfile new file mode 100644 index 0000000..0639be6 --- /dev/null +++ b/environment/php/Dockerfile @@ -0,0 +1,18 @@ +ARG PHP_VERSION=8.3.1-fpm-alpine +ARG COMPOSER_TAG_VERSION=2.6.6-bin + +FROM composer/composer:${COMPOSER_TAG_VERSION} AS composer_binary + +FROM php:${PHP_VERSION} + +COPY --from=composer_binary /composer /usr/bin/composer + +ENV COMPOSER_HOME=/application/.composer +ENV COMPOSER_MEMORY_LIMIT=-1 + +RUN apk update && apk upgrade \ + && apk add --no-cache icu-dev git \ + && docker-php-ext-install \ + mysqli \ + pdo \ + pdo_mysql diff --git a/src/VersionHelper.php b/src/VersionHelper.php new file mode 100644 index 0000000..0b918ee --- /dev/null +++ b/src/VersionHelper.php @@ -0,0 +1,40 @@ +long = $long; + } + + public function generate(): string + { + return shell_exec(self::SCRIPTS_DIRECTORY . "check.sh") + ? $this->getVersionBasedOnGit() + : $this->getVersionBasedOnXYZ(); + } + + private function getVersionBasedOnGit(): string + { + if ($this->long) { + return shell_exec(self::SCRIPTS_DIRECTORY . "version.sh --long"); + } + + return shell_exec(self::SCRIPTS_DIRECTORY . "version.sh"); + } + + private function getVersionBasedOnXYZ(): string + { + return (string)time(); + } +} diff --git a/src/scripts/check.sh b/src/scripts/check.sh new file mode 100755 index 0000000..b58ef97 --- /dev/null +++ b/src/scripts/check.sh @@ -0,0 +1,14 @@ +#!/usr/bin/env sh + +isGitRepo() { + git rev-parse --is-inside-work-tree >/dev/null 2>&1 +} + +isGitRepo +if [ $? -gt 0 ]; then + echo "Not a git repository." + return 0 +fi + +echo "Git repository detected." +return 1 diff --git a/src/scripts/version.sh b/src/scripts/version.sh new file mode 100755 index 0000000..3fd17ab --- /dev/null +++ b/src/scripts/version.sh @@ -0,0 +1,53 @@ +#!/usr/bin/env sh + +# Calculates project version from git state. +# Usage: +# ./version.sh [-l | --long] +# +# Short format: {latest tag} +# Long format: {latest tag}|{latest commit}-{commit count}({branch name}{dirty indicator}) +# Examples: +# - v1.3.2 - short format with tag +# - dev - short format with no tag +# - v1.3.2|a6072b3-27 - clean repo on main branch (or master, or detached head) +# - dev|a6072b3-27 - clean repo on main branch if no tags found +# - v1.3.2|a6072b3-27(*) - dirty repo on main branch +# - v1.3.2|a6072b3-27(#33-payments-refactor) - clean repo on non-main branch +# - v1.3.2|a6072b3-27(#33-payments-refactor*) - dirty repo on non-main branch + +getLastTag() { + TAG=$(git describe --tags --abbrev=0 2>/dev/null) + + if [ $? -eq 0 ]; then + echo "$TAG" + else + echo "dev" + fi +} + +gitBranch() { + BRANCH=$(git rev-parse --abbrev-ref HEAD) + + if [ "$BRANCH" != "main" ] && [ "$BRANCH" != "master" ] && [ "$BRANCH" != "HEAD" ]; then + echo "$BRANCH" + fi +} + +COMMIT_COUNT=$(git rev-list HEAD --count) +COMMIT_HASH=$(git log --format="%h" -n 1) +LAST_TAG=$(getLastTag) + +DIRTY_REPO=$([ -n "$(git status -s)" ] && echo '*') +BRANCH_NAME=$(gitBranch) +BRANCH_INFO=$([ -n "$DIRTY_REPO" ] || [ -n "$BRANCH_NAME" ] && echo "($BRANCH_NAME$DIRTY_REPO)") + +if [ $# -eq 1 ]; then + if [ "$1" = "--long" ] || [ "$1" = "-l" ]; then + echo "$LAST_TAG|$COMMIT_HASH-$COMMIT_COUNT$BRANCH_INFO" + else + echo "Unrecognized flag passed: $1" + exit 1 + fi +else + echo "$LAST_TAG" +fi diff --git a/tests/GenerateVersionTest.php b/tests/GenerateVersionTest.php new file mode 100644 index 0000000..9cdcc7e --- /dev/null +++ b/tests/GenerateVersionTest.php @@ -0,0 +1,27 @@ +generate(); + + $this->assertIsString($version); + $this->assertStringNotContainsString("|", $version); + } + + public function testGeneratingLongVersion(): void + { + $version = (new VersionHelper(true))->generate(); + $versionHash = trim(shell_exec("git log --format='%h' -n 1")); + + $this->assertIsString($version); + $this->assertStringContainsString("|", $version); + $this->assertStringContainsString($versionHash, $version); + } +} From 07e3b3cea1c5b0b9b11d33ff678c4a793a238e35 Mon Sep 17 00:00:00 2001 From: Kamil Date: Thu, 4 Apr 2024 11:44:05 +0200 Subject: [PATCH 02/14] #1 - wip: moved shell commands to php methods --- src/VersionHelper.php | 92 +++++++++++++++++++++++++++++------ src/scripts/check.sh | 14 ------ src/scripts/version.sh | 10 ++++ tests/GenerateVersionTest.php | 6 +-- 4 files changed, 89 insertions(+), 33 deletions(-) delete mode 100755 src/scripts/check.sh diff --git a/src/VersionHelper.php b/src/VersionHelper.php index 0b918ee..35bbd6c 100644 --- a/src/VersionHelper.php +++ b/src/VersionHelper.php @@ -6,35 +6,95 @@ class VersionHelper { - public const string SCRIPTS_DIRECTORY = "src/scripts/"; + public function generate(bool $long = false): string + { + if (!$this->isGitInstalled() && !$this->isGitRepository()) { + return $this->generateVersionBasedOnTimestamp(); + } + + if ($long) { + return $this->generateVersionWithLongFormat(); + } + + return $this->generateVersionWithShortFormat(); + } + + private function isGitInstalled(): bool + { + $output = shell_exec("git --version"); + + return !empty(trim($output)); + } + + private function isGitRepository(): bool + { + $output = shell_exec("git rev-parse --is-inside-work-tree >/dev/null 2>&1"); + + return empty(trim($output)); + } + + private function generateVersionBasedOnTimestamp(): string + { + return (string)time(); + } - public function __construct( - public bool $long = false, - ) {} + private function generateVersionWithLongFormat(): string + { + $lastTag = $this->getLastTag(); + $commitCount = $this->getCommitCount(); + $commitHash = $this->getCommitHash(); + $branchInfo = $this->getBranchInfo(); + + return "$lastTag|$commitHash-$commitCount$branchInfo"; + } + + private function generateVersionWithShortFormat(): string + { + return $this->getLastTag(); + } + + private function getLastTag(): string + { + $tag = shell_exec("git describe --tags --abbrev=0 2>/dev/null"); + + return $tag ? trim($tag) : "dev"; + } - public function setLong(bool $long): void + private function getCommitCount(): string { - $this->long = $long; + return trim(shell_exec("git rev-list HEAD --count")); } - public function generate(): string + private function getCommitHash(): string { - return shell_exec(self::SCRIPTS_DIRECTORY . "check.sh") - ? $this->getVersionBasedOnGit() - : $this->getVersionBasedOnXYZ(); + return trim(shell_exec("git log --format=\"%h\" -n 1")); } - private function getVersionBasedOnGit(): string + private function getBranchInfo(): string { - if ($this->long) { - return shell_exec(self::SCRIPTS_DIRECTORY . "version.sh --long"); + $branchName = $this->getGitBranch(); + $dirtyRepo = $this->isDirtyRepo(); + + if ($dirtyRepo || $branchName) { + return "($branchName$dirtyRepo)"; } - return shell_exec(self::SCRIPTS_DIRECTORY . "version.sh"); + return ""; } - private function getVersionBasedOnXYZ(): string + private function getGitBranch(): string { - return (string)time(); + $branch = shell_exec("git rev-parse --abbrev-ref HEAD"); + + if ($branch !== "main" && $branch !== "master" && $branch !== "HEAD") { + return trim($branch); + } + + return ""; + } + + private function isDirtyRepo(): string + { + return trim(shell_exec("[ -n \"$(git status -s)\" ] && echo '*'")); } } diff --git a/src/scripts/check.sh b/src/scripts/check.sh deleted file mode 100755 index b58ef97..0000000 --- a/src/scripts/check.sh +++ /dev/null @@ -1,14 +0,0 @@ -#!/usr/bin/env sh - -isGitRepo() { - git rev-parse --is-inside-work-tree >/dev/null 2>&1 -} - -isGitRepo -if [ $? -gt 0 ]; then - echo "Not a git repository." - return 0 -fi - -echo "Git repository detected." -return 1 diff --git a/src/scripts/version.sh b/src/scripts/version.sh index 3fd17ab..ee626d4 100755 --- a/src/scripts/version.sh +++ b/src/scripts/version.sh @@ -25,6 +25,16 @@ getLastTag() { fi } +isGitRepo() { + git rev-parse --is-inside-work-tree >/dev/null 2>&1 +} + +isGitRepo +if [ $? -gt 0 ]; then + echo "Not a git repository" + exit 1 +fi + gitBranch() { BRANCH=$(git rev-parse --abbrev-ref HEAD) diff --git a/tests/GenerateVersionTest.php b/tests/GenerateVersionTest.php index 9cdcc7e..dc3911b 100644 --- a/tests/GenerateVersionTest.php +++ b/tests/GenerateVersionTest.php @@ -7,7 +7,7 @@ class GenerateVersionTest extends TestCase { - public function testGeneratingVersion(): void + public function testGeneratingVersionBasedOnGit(): void { $version = (new VersionHelper())->generate(); @@ -15,9 +15,9 @@ public function testGeneratingVersion(): void $this->assertStringNotContainsString("|", $version); } - public function testGeneratingLongVersion(): void + public function testGeneratingLongVersionBasedOnGit(): void { - $version = (new VersionHelper(true))->generate(); + $version = (new VersionHelper())->generate(true); $versionHash = trim(shell_exec("git log --format='%h' -n 1")); $this->assertIsString($version); From 4bf09bf08e48c9f65defe608745191a4106ee81a Mon Sep 17 00:00:00 2001 From: Kamil Date: Thu, 4 Apr 2024 12:44:01 +0200 Subject: [PATCH 03/14] #1 - rollback to option with bash script, added readme --- readme.md | 70 ++++++++++++++++++++++++ src/Version.php | 40 ++++++++++++++ src/VersionHelper.php | 100 ---------------------------------- src/scripts/check.sh | 14 +++++ tests/GenerateVersionTest.php | 27 ++++++++- 5 files changed, 148 insertions(+), 103 deletions(-) create mode 100644 src/Version.php delete mode 100644 src/VersionHelper.php create mode 100755 src/scripts/check.sh diff --git a/readme.md b/readme.md index bf5619a..f7e318a 100644 --- a/readme.md +++ b/readme.md @@ -1 +1,71 @@ +[![Packagist PHP Version Support](https://img.shields.io/packagist/php-v/blumilksoftware/version?style=for-the-badge)](https://packagist.org/packages/blumilksoftware/version) +[![Packagist Version](https://img.shields.io/packagist/v/blumilksoftware/version?style=for-the-badge)](https://packagist.org/packages/blumilksoftware/version) +[![Packagist Downloads](https://img.shields.io/packagist/dt/blumilksoftware/version?style=for-the-badge)](https://packagist.org/packages/blumilksoftware/version/stats) + ## blumilksoftware/version +A versioning based on git for all Blumilk projects. If git is not available, it falls back to timestamp. + +### Usage +Add package to our project: +```shell +composer require blumilksoftware/version +``` + +Then use the `Version` class to generate version strings based on Git or timestamp: +```php +generate(); +``` + +#### Configuration +You can configure the `Version` class to generate long version strings: +```php +generate(); +``` + +### Contributing +In cloned or forked repository, run: +```shell +composer install +``` + +There are scripts available for package codestyle checking and testing: + +| Command | Description | +|-----------------|--------------------------------------------------------------| +| `composer cs` | Runs codestyle against the package itself | +| `composer csf` | Runs codestyle with fixer enabled against the package itself | +| `composer test` | Runs all test cases | + +There is also the Docker Compose configuration available: +```shell +docker compose up -d +docker compose exec php php -v +docker compose exec php composer -v +``` + +There are also Makefile commands available: +```shell +make run +make shell +make test +make csf +make stop +``` + +Please maintain our project guidelines: +* keep issues well described, labeled and in English, +* add issue number to all your commits, +* add issue number to your branch name, +* squash your commits into one commit with standardized name. diff --git a/src/Version.php b/src/Version.php new file mode 100644 index 0000000..ba3fdf6 --- /dev/null +++ b/src/Version.php @@ -0,0 +1,40 @@ +long = $long; + } + + public function generate(): string + { + return shell_exec(self::SCRIPTS_DIRECTORY . "check.sh") + ? $this->getVersionBasedOnGit() + : $this->getVersionBasedOnTimestamp(); + } + + private function getVersionBasedOnGit(): string + { + if ($this->long) { + return shell_exec(self::SCRIPTS_DIRECTORY . "version.sh --long"); + } + + return shell_exec(self::SCRIPTS_DIRECTORY . "version.sh"); + } + + private function getVersionBasedOnTimestamp(): string + { + return (string)time(); + } +} diff --git a/src/VersionHelper.php b/src/VersionHelper.php deleted file mode 100644 index 35bbd6c..0000000 --- a/src/VersionHelper.php +++ /dev/null @@ -1,100 +0,0 @@ -isGitInstalled() && !$this->isGitRepository()) { - return $this->generateVersionBasedOnTimestamp(); - } - - if ($long) { - return $this->generateVersionWithLongFormat(); - } - - return $this->generateVersionWithShortFormat(); - } - - private function isGitInstalled(): bool - { - $output = shell_exec("git --version"); - - return !empty(trim($output)); - } - - private function isGitRepository(): bool - { - $output = shell_exec("git rev-parse --is-inside-work-tree >/dev/null 2>&1"); - - return empty(trim($output)); - } - - private function generateVersionBasedOnTimestamp(): string - { - return (string)time(); - } - - private function generateVersionWithLongFormat(): string - { - $lastTag = $this->getLastTag(); - $commitCount = $this->getCommitCount(); - $commitHash = $this->getCommitHash(); - $branchInfo = $this->getBranchInfo(); - - return "$lastTag|$commitHash-$commitCount$branchInfo"; - } - - private function generateVersionWithShortFormat(): string - { - return $this->getLastTag(); - } - - private function getLastTag(): string - { - $tag = shell_exec("git describe --tags --abbrev=0 2>/dev/null"); - - return $tag ? trim($tag) : "dev"; - } - - private function getCommitCount(): string - { - return trim(shell_exec("git rev-list HEAD --count")); - } - - private function getCommitHash(): string - { - return trim(shell_exec("git log --format=\"%h\" -n 1")); - } - - private function getBranchInfo(): string - { - $branchName = $this->getGitBranch(); - $dirtyRepo = $this->isDirtyRepo(); - - if ($dirtyRepo || $branchName) { - return "($branchName$dirtyRepo)"; - } - - return ""; - } - - private function getGitBranch(): string - { - $branch = shell_exec("git rev-parse --abbrev-ref HEAD"); - - if ($branch !== "main" && $branch !== "master" && $branch !== "HEAD") { - return trim($branch); - } - - return ""; - } - - private function isDirtyRepo(): string - { - return trim(shell_exec("[ -n \"$(git status -s)\" ] && echo '*'")); - } -} diff --git a/src/scripts/check.sh b/src/scripts/check.sh new file mode 100755 index 0000000..b58ef97 --- /dev/null +++ b/src/scripts/check.sh @@ -0,0 +1,14 @@ +#!/usr/bin/env sh + +isGitRepo() { + git rev-parse --is-inside-work-tree >/dev/null 2>&1 +} + +isGitRepo +if [ $? -gt 0 ]; then + echo "Not a git repository." + return 0 +fi + +echo "Git repository detected." +return 1 diff --git a/tests/GenerateVersionTest.php b/tests/GenerateVersionTest.php index dc3911b..3d5e059 100644 --- a/tests/GenerateVersionTest.php +++ b/tests/GenerateVersionTest.php @@ -2,14 +2,14 @@ declare(strict_types=1); -use Blumilk\Version\VersionHelper; +use Blumilk\Version\Version; use PHPUnit\Framework\TestCase; class GenerateVersionTest extends TestCase { public function testGeneratingVersionBasedOnGit(): void { - $version = (new VersionHelper())->generate(); + $version = (new Version())->generate(); $this->assertIsString($version); $this->assertStringNotContainsString("|", $version); @@ -17,11 +17,32 @@ public function testGeneratingVersionBasedOnGit(): void public function testGeneratingLongVersionBasedOnGit(): void { - $version = (new VersionHelper())->generate(true); + $version = (new Version(true))->generate(); $versionHash = trim(shell_exec("git log --format='%h' -n 1")); $this->assertIsString($version); $this->assertStringContainsString("|", $version); $this->assertStringContainsString($versionHash, $version); } + + public function testGeneratingVersionNotBasedOnGit(): void + { + $versionHash = trim(shell_exec("git log --format='%h' -n 1")); + $now = date("Y-m-d"); + + $versionHelperMock = $this->getMockBuilder(Version::class) + ->onlyMethods(["generate"]) + ->getMock(); + + $versionHelperMock->expects($this->once()) + ->method("generate") + ->willReturn((string)time()); + + $version = $versionHelperMock->generate(); + + $this->assertIsString($version); + $this->assertStringNotContainsString("|", $version); + $this->assertStringNotContainsString($versionHash, $version); + $this->assertSame($now, date("Y-m-d", (int)$version)); + } } From 31c032713518171d8782b4e97c166048b537f21a Mon Sep 17 00:00:00 2001 From: Kamil Date: Thu, 4 Apr 2024 12:53:45 +0200 Subject: [PATCH 04/14] #1 - wip: added helper class, updated tests and readme --- readme.md | 11 ++++++++ src/VersionHelper.php | 18 +++++++++++++ tests/VersionHelperTest.php | 27 +++++++++++++++++++ ...enerateVersionTest.php => VersionTest.php} | 2 +- 4 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 src/VersionHelper.php create mode 100644 tests/VersionHelperTest.php rename tests/{GenerateVersionTest.php => VersionTest.php} (97%) diff --git a/readme.md b/readme.md index f7e318a..051db35 100644 --- a/readme.md +++ b/readme.md @@ -33,7 +33,18 @@ use Blumilk\Version\Version; $version = (new Version(true))->generate(); ``` +#### Helper class +You can use also the `VersionHelper` class to generate version strings: +```php +generate(); + } + + public static function generateLongVersion(): string + { + return (new Version(long: true))->generate(); + } +} diff --git a/tests/VersionHelperTest.php b/tests/VersionHelperTest.php new file mode 100644 index 0000000..6815017 --- /dev/null +++ b/tests/VersionHelperTest.php @@ -0,0 +1,27 @@ +assertIsString($version); + $this->assertStringNotContainsString("|", $version); + } + + public function testGeneratingLongVersionBasedOnGit(): void + { + $version = VersionHelper::generateLongVersion(); + $versionHash = trim(shell_exec("git log --format='%h' -n 1")); + + $this->assertIsString($version); + $this->assertStringContainsString("|", $version); + $this->assertStringContainsString($versionHash, $version); + } +} diff --git a/tests/GenerateVersionTest.php b/tests/VersionTest.php similarity index 97% rename from tests/GenerateVersionTest.php rename to tests/VersionTest.php index 3d5e059..571e2ba 100644 --- a/tests/GenerateVersionTest.php +++ b/tests/VersionTest.php @@ -5,7 +5,7 @@ use Blumilk\Version\Version; use PHPUnit\Framework\TestCase; -class GenerateVersionTest extends TestCase +class VersionTest extends TestCase { public function testGeneratingVersionBasedOnGit(): void { From 9f542a44ee6e6ef13016cb43c117423c197ef9e9 Mon Sep 17 00:00:00 2001 From: Kamil Date: Thu, 4 Apr 2024 13:01:24 +0200 Subject: [PATCH 05/14] #1 - ci fix --- .github/workflows/check.yml | 2 +- environment/php/Dockerfile | 6 +----- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml index 739498e..557e6e8 100644 --- a/.github/workflows/check.yml +++ b/.github/workflows/check.yml @@ -31,7 +31,7 @@ jobs: uses: shivammathur/setup-php@2.30.0 # https://github.com/shivammathur/setup-php with: php-version: 8.3 - extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, pdo_pgsql, intl + extensions: dom, curl, libxml, mbstring, zip, pcntl, intl, git coverage: none - name: Install Composer dependencies diff --git a/environment/php/Dockerfile b/environment/php/Dockerfile index 0639be6..4cabb85 100644 --- a/environment/php/Dockerfile +++ b/environment/php/Dockerfile @@ -11,8 +11,4 @@ ENV COMPOSER_HOME=/application/.composer ENV COMPOSER_MEMORY_LIMIT=-1 RUN apk update && apk upgrade \ - && apk add --no-cache icu-dev git \ - && docker-php-ext-install \ - mysqli \ - pdo \ - pdo_mysql + && apk add --no-cache icu-dev git From 94ef50f900f202b97fa878df974a1a70980660f3 Mon Sep 17 00:00:00 2001 From: Kamil Date: Thu, 4 Apr 2024 13:04:23 +0200 Subject: [PATCH 06/14] #1 - ci fix --- .github/workflows/check.yml | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml index 557e6e8..1f091db 100644 --- a/.github/workflows/check.yml +++ b/.github/workflows/check.yml @@ -34,10 +34,25 @@ jobs: extensions: dom, curl, libxml, mbstring, zip, pcntl, intl, git coverage: none + - name: Cache composer dependencies + uses: actions/cache@v2 + with: + path: vendor + key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }} + restore-keys: | + ${{ runner.os }}-composer- + + - name: Validate composer.json + run: composer validate + - name: Install Composer dependencies working-directory: src run: composer install --prefer-dist --no-interaction + - name: Run linter + working-directory: src + run: composer cs + - name: Run test working-directory: src run: composer test From 40bcb76127e543e18c963adc86aa0b6a91621f62 Mon Sep 17 00:00:00 2001 From: Kamil Date: Thu, 4 Apr 2024 13:07:09 +0200 Subject: [PATCH 07/14] #1 - ci fix --- .github/workflows/check.yml | 29 +++++++++++------------------ 1 file changed, 11 insertions(+), 18 deletions(-) diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml index 1f091db..9687aee 100644 --- a/.github/workflows/check.yml +++ b/.github/workflows/check.yml @@ -17,21 +17,17 @@ jobs: if: github.event.pull_request.draft == false runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v4.1.1 # https://github.com/actions/checkout + strategy: + matrix: + php: ["8.1", "8.2"] - - name: Cache dependencies - uses: actions/cache@v4.0.2 # https://github.com/actions/cache - with: - path: vendor - key: ${{ runner.os }}-composer-dependencies-${{ hashFiles('composer.lock') }} - restore-keys: ${{ runner.os }}-composer-dependencies + steps: + - uses: actions/checkout@v2 - name: Setup PHP - uses: shivammathur/setup-php@2.30.0 # https://github.com/shivammathur/setup-php + uses: shivammathur/setup-php@v2 with: - php-version: 8.3 - extensions: dom, curl, libxml, mbstring, zip, pcntl, intl, git + php-version: ${{ matrix.php }} coverage: none - name: Cache composer dependencies @@ -45,14 +41,11 @@ jobs: - name: Validate composer.json run: composer validate - - name: Install Composer dependencies - working-directory: src - run: composer install --prefer-dist --no-interaction + - name: Install dependencies + run: composer install --prefer-dist --no-progress - - name: Run linter - working-directory: src + - name: Run code style checker run: composer cs - - name: Run test - working-directory: src + - name: Run tests run: composer test From fab0f1587236b4d2324f437021622d3251fb95c1 Mon Sep 17 00:00:00 2001 From: Kamil Date: Thu, 4 Apr 2024 13:08:34 +0200 Subject: [PATCH 08/14] #1 - composer php update --- .github/workflows/check.yml | 2 +- composer.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml index 9687aee..4744f79 100644 --- a/.github/workflows/check.yml +++ b/.github/workflows/check.yml @@ -19,7 +19,7 @@ jobs: strategy: matrix: - php: ["8.1", "8.2"] + php: ["8.2", "8.3"] steps: - uses: actions/checkout@v2 diff --git a/composer.json b/composer.json index 4d66ae6..839c800 100644 --- a/composer.json +++ b/composer.json @@ -4,7 +4,7 @@ "license": "MIT", "type": "library", "require": { - "php": "^8.1" + "php": "^8.2" }, "require-dev": { "blumilksoftware/codestyle": "^2.8", From 1ac56fac1d0c8ecb8eb73c508669a6645226b306 Mon Sep 17 00:00:00 2001 From: Kamil Date: Thu, 4 Apr 2024 13:11:07 +0200 Subject: [PATCH 09/14] #1 - ci update --- .github/workflows/check.yml | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml index 4744f79..b0250e8 100644 --- a/.github/workflows/check.yml +++ b/.github/workflows/check.yml @@ -22,24 +22,24 @@ jobs: php: ["8.2", "8.3"] steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v4.1.1 - - name: Setup PHP - uses: shivammathur/setup-php@v2 - with: - php-version: ${{ matrix.php }} - coverage: none + - name: Validate composer.json and composer.lock + run: composer validate - - name: Cache composer dependencies - uses: actions/cache@v2 + - name: Cache dependencies + uses: actions/cache@v4.0.1 with: path: vendor - key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }} - restore-keys: | - ${{ runner.os }}-composer- + key: ${{ runner.os }}-composer-dependencies-${{ hashFiles('composer.lock') }} + restore-keys: ${{ runner.os }}-composer-dependencies - - name: Validate composer.json - run: composer validate + - name: Setup PHP + uses: shivammathur/setup-php@2.30.0 + with: + php-version: 8.3 + extensions: dom, curl, libxml, mbstring, zip, pcntl, intl + coverage: none - name: Install dependencies run: composer install --prefer-dist --no-progress From 625ca703f3e0ec87f256ab04d5bceabe8e84b2da Mon Sep 17 00:00:00 2001 From: Kamil Date: Thu, 4 Apr 2024 13:11:43 +0200 Subject: [PATCH 10/14] #1 - ci update --- .github/workflows/check.yml | 4 ---- 1 file changed, 4 deletions(-) diff --git a/.github/workflows/check.yml b/.github/workflows/check.yml index b0250e8..da92833 100644 --- a/.github/workflows/check.yml +++ b/.github/workflows/check.yml @@ -17,10 +17,6 @@ jobs: if: github.event.pull_request.draft == false runs-on: ubuntu-latest - strategy: - matrix: - php: ["8.2", "8.3"] - steps: - uses: actions/checkout@v4.1.1 From 9f4a6ff535d0cf599bd94384b9f5b72e3f2e9a0c Mon Sep 17 00:00:00 2001 From: Kamil Piech Date: Fri, 5 Apr 2024 07:19:38 +0200 Subject: [PATCH 11/14] Apply suggestions from code review Co-authored-by: Ewelina Skrzypacz <56546832+EwelinaSkrzypacz@users.noreply.github.com> --- readme.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/readme.md b/readme.md index 051db35..27dca7d 100644 --- a/readme.md +++ b/readme.md @@ -3,10 +3,10 @@ [![Packagist Downloads](https://img.shields.io/packagist/dt/blumilksoftware/version?style=for-the-badge)](https://packagist.org/packages/blumilksoftware/version/stats) ## blumilksoftware/version -A versioning based on git for all Blumilk projects. If git is not available, it falls back to timestamp. +A versioning based on git for all Blumilk projects. If Git is not available, it falls back to the timestamp. ### Usage -Add package to our project: +Add package to the project: ```shell composer require blumilksoftware/version ``` @@ -46,7 +46,7 @@ $shortVersion = VersionHelper::generateShortVersion(); $longVersion = VersionHelper::generateLongVersion(); ``` ### Contributing -In cloned or forked repository, run: +In a cloned or forked repository, run: ```shell composer install ``` @@ -77,6 +77,6 @@ make stop Please maintain our project guidelines: * keep issues well described, labeled and in English, -* add issue number to all your commits, -* add issue number to your branch name, -* squash your commits into one commit with standardized name. +* add the issue number to all your commits, +* add the issue number to your branch name, +* squash your commits into one commit with a standardized name. From a7ac4057dc6fb3e26d93d21765c603a0c90982c9 Mon Sep 17 00:00:00 2001 From: Kamil Date: Fri, 26 Apr 2024 14:56:05 +0200 Subject: [PATCH 12/14] #1 - cr fixes --- Makefile | 6 +++++- composer.json | 2 +- docker-compose.yml | 3 ++- environment/php/Dockerfile | 12 +++++------- 4 files changed, 13 insertions(+), 10 deletions(-) diff --git a/Makefile b/Makefile index a0ca765..5a613cf 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ PHP_FPM_SERVICE_NAME=php .PHONY: shell shell: - docker compose -f ${DOCKER_COMPOSE_FILENAME} exec ${PHP_FPM_SERVICE_NAME} ash + docker compose -f ${DOCKER_COMPOSE_FILENAME} exec ${PHP_FPM_SERVICE_NAME} bash .PHONY: test test: @@ -17,6 +17,10 @@ csf: run: docker compose -f ${DOCKER_COMPOSE_FILENAME} up -d +.PHONY: build +build: + docker compose -f ${DOCKER_COMPOSE_FILENAME} build + .PHONY: stop stop: docker compose -f ${DOCKER_COMPOSE_FILENAME} stop diff --git a/composer.json b/composer.json index 839c800..7a1792d 100644 --- a/composer.json +++ b/composer.json @@ -4,7 +4,7 @@ "license": "MIT", "type": "library", "require": { - "php": "^8.2" + "php": "^8.3" }, "require-dev": { "blumilksoftware/codestyle": "^2.8", diff --git a/docker-compose.yml b/docker-compose.yml index b90db7c..c1ab2da 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,8 +1,9 @@ services: php: - container_name: blumilk-version-php + container_name: blumilk-version build: context: environment/php + tty: true working_dir: /application user: ${CURRENT_UID:-1000} volumes: diff --git a/environment/php/Dockerfile b/environment/php/Dockerfile index 4cabb85..7c9f1da 100644 --- a/environment/php/Dockerfile +++ b/environment/php/Dockerfile @@ -1,5 +1,5 @@ -ARG PHP_VERSION=8.3.1-fpm-alpine -ARG COMPOSER_TAG_VERSION=2.6.6-bin +ARG PHP_VERSION=8.3.6-cli-bookworm +ARG COMPOSER_TAG_VERSION=2.7.4-bin FROM composer/composer:${COMPOSER_TAG_VERSION} AS composer_binary @@ -7,8 +7,6 @@ FROM php:${PHP_VERSION} COPY --from=composer_binary /composer /usr/bin/composer -ENV COMPOSER_HOME=/application/.composer -ENV COMPOSER_MEMORY_LIMIT=-1 - -RUN apk update && apk upgrade \ - && apk add --no-cache icu-dev git +RUN apt-get update \ + && apt-get upgrade \ + && apt-get install -y git From 584d44c77e93749d4799a99c7fa6d5703302d6e1 Mon Sep 17 00:00:00 2001 From: Kamil Date: Mon, 6 May 2024 08:04:19 +0200 Subject: [PATCH 13/14] #1 - cr fixes --- Makefile | 15 ++++++--------- composer.json | 5 +++-- environment/php/Dockerfile | 7 ++++--- src/Version.php | 14 +++++++++++--- 4 files changed, 24 insertions(+), 17 deletions(-) diff --git a/Makefile b/Makefile index 5a613cf..c8727d9 100644 --- a/Makefile +++ b/Makefile @@ -1,29 +1,26 @@ -DOCKER_COMPOSE_FILENAME=docker-compose.yml -PHP_FPM_SERVICE_NAME=php - .PHONY: shell shell: - docker compose -f ${DOCKER_COMPOSE_FILENAME} exec ${PHP_FPM_SERVICE_NAME} bash + docker compose exec php bash .PHONY: test test: - docker compose -f ${DOCKER_COMPOSE_FILENAME} exec ${PHP_FPM_SERVICE_NAME} composer test + docker compose exec php composer test .PHONY: csf csf: - docker compose -f ${DOCKER_COMPOSE_FILENAME} exec ${PHP_FPM_SERVICE_NAME} composer csf + docker compose exec php composer csf .PHONY: run run: - docker compose -f ${DOCKER_COMPOSE_FILENAME} up -d + docker compose up -d .PHONY: build build: - docker compose -f ${DOCKER_COMPOSE_FILENAME} build + docker compose build .PHONY: stop stop: - docker compose -f ${DOCKER_COMPOSE_FILENAME} stop + docker compose stop .PHONY: restart restart: stop run diff --git a/composer.json b/composer.json index 7a1792d..3d38d93 100644 --- a/composer.json +++ b/composer.json @@ -4,10 +4,11 @@ "license": "MIT", "type": "library", "require": { - "php": "^8.3" + "php": "^8.3", + "symfony/process": "v7.0.7" }, "require-dev": { - "blumilksoftware/codestyle": "^2.8", + "blumilksoftware/codestyle": "^v3.0.0", "phpunit/phpunit": "^11.0.9" }, "authors": [ diff --git a/environment/php/Dockerfile b/environment/php/Dockerfile index 7c9f1da..375a244 100644 --- a/environment/php/Dockerfile +++ b/environment/php/Dockerfile @@ -7,6 +7,7 @@ FROM php:${PHP_VERSION} COPY --from=composer_binary /composer /usr/bin/composer -RUN apt-get update \ - && apt-get upgrade \ - && apt-get install -y git +RUN apt-get update && apt-get install -y git zip \ + && apt-get install -y libzip-dev \ + && docker-php-ext-install zip \ + && docker-php-ext-enable zip diff --git a/src/Version.php b/src/Version.php index ba3fdf6..30afb8e 100644 --- a/src/Version.php +++ b/src/Version.php @@ -4,6 +4,8 @@ namespace Blumilk\Version; +use Symfony\Component\Process\Process; + class Version { public const string SCRIPTS_DIRECTORY = "src/scripts/"; @@ -19,18 +21,24 @@ public function setLong(bool $long): void public function generate(): string { - return shell_exec(self::SCRIPTS_DIRECTORY . "check.sh") + return (new Process(["./check.sh"], self::SCRIPTS_DIRECTORY))->run() ? $this->getVersionBasedOnGit() : $this->getVersionBasedOnTimestamp(); } private function getVersionBasedOnGit(): string { + $process = new Process(["./version.sh"], self::SCRIPTS_DIRECTORY); + if ($this->long) { - return shell_exec(self::SCRIPTS_DIRECTORY . "version.sh --long"); + $process = new Process(["./version.sh", "--long"], self::SCRIPTS_DIRECTORY); } - return shell_exec(self::SCRIPTS_DIRECTORY . "version.sh"); + $process->mustRun(); + + return $process->isSuccessful() + ? $process->getOutput() + : $this->getVersionBasedOnTimestamp(); } private function getVersionBasedOnTimestamp(): string From 8a3456773252f7d3af9cf05954ca4106939629b0 Mon Sep 17 00:00:00 2001 From: Kamil Date: Mon, 6 May 2024 09:15:29 +0200 Subject: [PATCH 14/14] #1 - cr fix --- environment/php/Dockerfile | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/environment/php/Dockerfile b/environment/php/Dockerfile index 375a244..61bcbd3 100644 --- a/environment/php/Dockerfile +++ b/environment/php/Dockerfile @@ -9,5 +9,4 @@ COPY --from=composer_binary /composer /usr/bin/composer RUN apt-get update && apt-get install -y git zip \ && apt-get install -y libzip-dev \ - && docker-php-ext-install zip \ - && docker-php-ext-enable zip + && docker-php-ext-install zip