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

#1 - initialize project #2

Merged
merged 15 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from 14 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
12 changes: 12 additions & 0 deletions .github/workflows/check-pr-title.yml
Original file line number Diff line number Diff line change
@@ -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
47 changes: 47 additions & 0 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
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

- name: Validate composer.json and composer.lock
run: composer validate

- name: Cache dependencies
uses: actions/cache@v4.0.1
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
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

- name: Run code style checker
run: composer cs

- name: Run tests
run: composer test
35 changes: 35 additions & 0 deletions .github/workflows/dependabot.yml
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/.composer
/.idea
/vendor

composer.lock
.env
26 changes: 26 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
.PHONY: shell
shell:
docker compose exec php bash

.PHONY: test
test:
docker compose exec php composer test

.PHONY: csf
csf:
docker compose exec php composer csf

.PHONY: run
run:
docker compose up -d

.PHONY: build
build:
docker compose build

.PHONY: stop
stop:
docker compose stop

.PHONY: restart
restart: stop run
14 changes: 14 additions & 0 deletions codestyle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

use Blumilk\Codestyle\Config;
use Blumilk\Codestyle\Configuration\Defaults\Paths;

$paths = new Paths("src", "tests");

$config = new Config(
paths: $paths,
);

return $config->config();
34 changes: 34 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"name": "blumilksoftware/version",
"description": "Blumilk versioning package",
"license": "MIT",
"type": "library",
"require": {
"php": "^8.3",
"symfony/process": "v7.0.7"
},
"require-dev": {
"blumilksoftware/codestyle": "^v3.0.0",
"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"
}
}
10 changes: 10 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
services:
php:
container_name: blumilk-version
build:
context: environment/php
tty: true
working_dir: /application
user: ${CURRENT_UID:-1000}
volumes:
- .:/application
13 changes: 13 additions & 0 deletions environment/php/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
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

FROM php:${PHP_VERSION}

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
mtracz marked this conversation as resolved.
Show resolved Hide resolved
81 changes: 81 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1 +1,82 @@
[![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 the timestamp.

### Usage
Add package to the project:
```shell
composer require blumilksoftware/version
```

Then use the `Version` class to generate version strings based on Git or timestamp:
```php
<?php

declare(strict_types=1);

use Blumilk\Version\Version;

$version = (new Version())->generate();
```

#### Configuration
You can configure the `Version` class to generate long version strings:
```php
<?php

declare(strict_types=1);

use Blumilk\Version\Version;

$version = (new Version(true))->generate();
```
#### Helper class
You can use also the `VersionHelper` class to generate version strings:
```php
<?php

declare(strict_types=1);

use Blumilk\Version\VersionHelper;

$shortVersion = VersionHelper::generateShortVersion();
$longVersion = VersionHelper::generateLongVersion();
```
### Contributing
In a 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 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.
48 changes: 48 additions & 0 deletions src/Version.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

namespace Blumilk\Version;

use Symfony\Component\Process\Process;

class Version
{
public const string SCRIPTS_DIRECTORY = "src/scripts/";

public function __construct(
public bool $long = false,
) {}

public function setLong(bool $long): void
{
$this->long = $long;
}

public function generate(): string
{
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) {
$process = new Process(["./version.sh", "--long"], self::SCRIPTS_DIRECTORY);
}

$process->mustRun();

return $process->isSuccessful()
? $process->getOutput()
: $this->getVersionBasedOnTimestamp();
}

private function getVersionBasedOnTimestamp(): string
{
return (string)time();
mtracz marked this conversation as resolved.
Show resolved Hide resolved
}
}
18 changes: 18 additions & 0 deletions src/VersionHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Blumilk\Version;

class VersionHelper
{
public static function generateShortVersion(): string
{
return (new Version())->generate();
}

public static function generateLongVersion(): string
{
return (new Version(long: true))->generate();
}
}
14 changes: 14 additions & 0 deletions src/scripts/check.sh
Original file line number Diff line number Diff line change
@@ -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
Loading
Loading