Skip to content

Commit 1a31cb4

Browse files
authored
Add version for output (#94)
Enhanced the software to include a versioning feature that is reflected in console output during CSV validation as well as in the CliApplication class itself. The feature fetches the version from a new ".version" file, unless it's operating in a PHPunit environment, in which case it outputs a generic 'unknown version' message.
1 parent 58f53f2 commit 1a31cb4

18 files changed

+167
-22
lines changed

.github/workflows/main.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,9 @@ jobs:
167167
steps:
168168
- name: Checkout code
169169
uses: actions/checkout@v4
170+
with:
171+
fetch-depth: 0
172+
ref: ${{ github.event.pull_request.head.ref }}
170173

171174
- name: Setup PHP
172175
uses: shivammathur/setup-php@v2
@@ -197,6 +200,9 @@ jobs:
197200
steps:
198201
- name: Checkout code
199202
uses: actions/checkout@v4
203+
with:
204+
fetch-depth: 0
205+
ref: ${{ github.event.pull_request.head.ref }}
200206

201207
- name: Setup PHP
202208
uses: shivammathur/setup-php@v2
@@ -232,6 +238,12 @@ jobs:
232238
steps:
233239
- name: Checkout code
234240
uses: actions/checkout@v4
241+
with:
242+
fetch-depth: 0
243+
ref: ${{ github.event.pull_request.head.ref }}
244+
245+
- name: Save the current version
246+
run: make build-version --no-print-directory
235247

236248
- name: Login to Docker Hub
237249
uses: docker/login-action@v3
@@ -242,6 +254,7 @@ jobs:
242254
- name: 🐳 Building Docker Image
243255
uses: docker/build-push-action@v5
244256
with:
257+
context: .
245258
push: false
246259
tags: jbzoo/csv-blueprint:local
247260

@@ -262,6 +275,9 @@ jobs:
262275
steps:
263276
- name: Checkout code
264277
uses: actions/checkout@v4
278+
with:
279+
fetch-depth: 0
280+
ref: ${{ github.event.pull_request.head.ref }}
265281

266282
- name: 👍 Valid CSV files
267283
uses: ./

.github/workflows/release-docker.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ jobs:
2323
steps:
2424
- name: Checkout code
2525
uses: actions/checkout@v4
26+
with:
27+
fetch-depth: 0
28+
ref: ${{ github.ref_name }}
29+
30+
- name: Save the current version
31+
run: make build-version --no-print-directory
2632

2733
- name: Set up QEMU
2834
uses: docker/setup-qemu-action@v3

.github/workflows/release-phar.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,14 @@ jobs:
2323
steps:
2424
- name: Checkout code
2525
uses: actions/checkout@v4
26+
with:
27+
fetch-depth: 0
28+
ref: ${{ github.ref_name }}
2629

2730
- name: Setup PHP
2831
uses: shivammathur/setup-php@v2
2932
with:
30-
php-version: 8.1
33+
php-version: 8.3
3134
tools: composer
3235

3336
- name: Build project in production mode

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ phpunit.xml
1818
/docker/preload.php
1919
*.cache
2020
*.phar
21+
.version

Dockerfile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,22 @@ FROM php:8.3-cli-alpine
1616
ADD --chmod=0755 https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/install-php-extensions /usr/local/bin/
1717
RUN install-php-extensions opcache @composer
1818

19+
1920
# Install application
21+
# run `make build-version` before!
2022
ENV COMPOSER_ALLOW_SUPERUSER=1
2123
COPY . /app
24+
COPY ./.version /app/
2225
RUN cd /app \
2326
&& composer install --no-dev \
2427
--classmap-authoritative \
2528
--no-progress \
29+
--no-suggest \
30+
--optimize-autoloader \
2631
&& composer clear-cache \
2732
&& chmod +x /app/csv-blueprint
2833

34+
2935
RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini"
3036
COPY ./docker/php.ini /usr/local/etc/php/conf.d/docker-z99-php.ini
3137

Makefile

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,29 @@ INVALID_SCHEMA ?= --schema='./tests/schemas/demo_invalid.yml'
3131
build: ##@Project Build project in development mode
3232
@composer install --optimize-autoloader
3333
@rm -f `pwd`/ci-report-converter
34+
@make build-version
3435

3536
build-prod: ##@Project Build project in production mode
36-
@composer install --no-dev --classmap-authoritative
37+
@composer install --no-dev --classmap-authoritative --no-progress --no-suggest --optimize-autoloader
3738
@rm -f `pwd`/ci-report-converter
39+
@make build-version
3840

3941
build-phar-file: ##@Project Build PHAR file
4042
curl -L "https://github.com/box-project/box/releases/download/4.5.1/box.phar" -o ./build/box.phar
43+
@make build-version
4144
@php ./build/box.phar --version
4245
@php ./build/box.phar compile -vv
4346
@ls -lh ./build/csv-blueprint.phar
4447

48+
build-version: ##@Project Save version info
49+
$(eval TAG := $(shell git describe --tags --abbrev=0))
50+
$(eval BRANCH := $(shell git rev-parse --abbrev-ref HEAD))
51+
$(eval LAST_COMMIT_DATE := $(shell git log -1 --format=%cI))
52+
$(eval SHORT_COMMIT_HASH := $(shell git rev-parse --short HEAD))
53+
$(eval STABLE_FLAG := $(shell git diff --quiet $(TAG) HEAD -- && echo "true" || echo "false"))
54+
@echo "$(TAG)\n$(STABLE_FLAG)\n$(BRANCH)\n$(LAST_COMMIT_DATE)\n$(SHORT_COMMIT_HASH)" > `pwd`/.version
55+
@echo "Version info saved to `pwd`/.version"
56+
4557
update: ##@Project Update dependencies
4658
@echo "Composer flags: $(JBZOO_COMPOSER_UPDATE_FLAGS)"
4759
@composer update $(JBZOO_COMPOSER_UPDATE_FLAGS)
@@ -62,6 +74,7 @@ demo-github: ##@Demo Run demo invalid CSV for GitHub Actions
6274
# Docker ###############################################################################################################
6375
docker-build: ##@Docker (Re-)build Docker image
6476
$(call title,"Building Docker Image")
77+
@make build-version
6578
@docker build -t $(DOCKER_IMAGE) .
6679

6780
docker-demo: ##@Docker Run demo via Docker

README.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -792,6 +792,7 @@ Default report format is `table`:
792792
./csv-blueprint validate:csv --csv='./tests/fixtures/demo.csv' --schema='./tests/schemas/demo_invalid.yml'
793793
794794
795+
CSV Blueprint: vX.Y.Z
795796
Found Schemas : 1
796797
Found CSV files : 1
797798
Pairs by pattern: 1
@@ -877,10 +878,6 @@ It's random ideas and plans. No orderings and deadlines. <u>But batch processing
877878
* Extending with custom rules and custom report formats. Plugins?
878879
* Input encoding detection + `BOM` (right now it's experimental). It works but not so accurate... UTF-8/16/32 is the best choice for now.
879880

880-
* **Release workflow**
881-
* Auto insert tool version into the Docker image and phar file. It's important to know the version of the tool you are using.
882-
* Show version as part of output.
883-
884881
* **Performance and optimization**
885882
* Benchmarks as part of the CI(?) and Readme. It's important to know how much time the validation process takes.
886883
* Parallel validation of really-really large files (1GB+ ?). I know you have them and not so much memory.

box.json.dist

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@
1818
"schema-examples",
1919
"src"
2020
],
21-
"files" : ["csv-blueprint.php"],
21+
"files" : [
22+
"csv-blueprint.php",
23+
".version"
24+
],
2225
"git-version" : "git-version",
2326

2427
"finder" : [{"in" : "vendor"}]

csv-blueprint.php

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
namespace JBZoo\CsvBlueprint;
1818

19-
use JBZoo\Cli\CliApplication;
20-
2119
\define('PATH_ROOT', __DIR__);
2220

2321
$vendorPaths = [
@@ -41,18 +39,9 @@
4139
throw new \ErrorException($message, 0, $severity, $file, $line);
4240
});
4341

44-
(new CliApplication('CSV Blueprint', '@git-version@'))
42+
$cliApp = (new CliApplication('CSV Blueprint', Utils::getVersion(true)));
43+
$cliApp->setVersion(Utils::getVersion(false));
44+
45+
$cliApp
4546
->registerCommandsByPath(PATH_ROOT . '/src/Commands', __NAMESPACE__)
46-
->setLogo(
47-
<<<'EOF'
48-
_____ ______ _ _ _
49-
/ __ \ | ___ \ | (_) | |
50-
| / \/_____ __ | |_/ / |_ _ ___ _ __ _ __ _ _ __ | |_
51-
| | / __\ \ / / | ___ \ | | | |/ _ \ '_ \| '__| | '_ \| __|
52-
| \__/\__ \\ V / | |_/ / | |_| | __/ |_) | | | | | | | |_
53-
\____/___/ \_/ \____/|_|\__,_|\___| .__/|_| |_|_| |_|\__|
54-
| |
55-
|_|
56-
EOF,
57-
)
5847
->run();

src/CliApplication.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
/**
4+
* JBZoo Toolbox - Csv-Blueprint.
5+
*
6+
* This file is part of the JBZoo Toolbox project.
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*
10+
* @license MIT
11+
* @copyright Copyright (C) JBZoo.com, All rights reserved.
12+
* @see https://github.com/JBZoo/Csv-Blueprint
13+
*/
14+
15+
declare(strict_types=1);
16+
17+
namespace JBZoo\CsvBlueprint;
18+
19+
class CliApplication extends \JBZoo\Cli\CliApplication
20+
{
21+
private array $appLogo = [
22+
' __________ __ ___ __ _ __ ',
23+
' / ___/ __/ | / / / _ )/ /_ _____ ___ ____(_)__ / /_',
24+
'/ /___\ \ | |/ / / _ / / // / -_) _ \/ __/ / _ \/ __/',
25+
'\___/___/ |___/ /____/_/\_,_/\__/ .__/_/ /_/_//_/\__/ ',
26+
' /_/ ',
27+
];
28+
29+
public function getLongVersion(): string
30+
{
31+
$logo = '<info>' . \implode("</info>\n<info>", $this->appLogo) . '</info>';
32+
33+
return "{$logo}</info>\n" . Utils::getVersion(false);
34+
}
35+
}

0 commit comments

Comments
 (0)