Skip to content

Commit

Permalink
Merge 1ded933 into 58f53f2
Browse files Browse the repository at this point in the history
  • Loading branch information
SmetDenis committed Mar 26, 2024
2 parents 58f53f2 + 1ded933 commit 1000ad4
Show file tree
Hide file tree
Showing 12 changed files with 126 additions and 19 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ phpunit.xml
/docker/preload.php
*.cache
*.phar
.version
18 changes: 18 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,44 @@ VALID_SCHEMA ?= --schema='./tests/schemas/demo_valid.yml'
INVALID_CSV ?= --csv='./tests/fixtures/batch/*.csv'
INVALID_SCHEMA ?= --schema='./tests/schemas/demo_invalid.yml'

VERSION_FILE ?= ./.version

# Build/install ########################################################################################################
build: ##@Project Build project in development mode
@composer install --optimize-autoloader
@rm -f `pwd`/ci-report-converter
@make build-version

build-prod: ##@Project Build project in production mode
@composer install --no-dev --classmap-authoritative
@rm -f `pwd`/ci-report-converter
@make build-version

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

build-version: ##@Project Save version info
$(eval TAG := $(shell git describe --tags --abbrev=0))
$(eval BRANCH := $(shell git rev-parse --abbrev-ref HEAD))
$(eval LAST_COMMIT_DATE := $(shell git log -1 --format=%cI))
$(eval SHORT_COMMIT_HASH := $(shell git rev-parse --short HEAD))
$(eval STABLE_FLAG := $(shell git diff --quiet $(TAG) HEAD -- && echo "true" || echo "false"))
@echo "$(TAG)\n$(STABLE_FLAG)\n$(BRANCH)\n$(LAST_COMMIT_DATE)\n$(SHORT_COMMIT_HASH)" > $(VERSION_FILE)

update: ##@Project Update dependencies
@echo "Composer flags: $(JBZOO_COMPOSER_UPDATE_FLAGS)"
@composer update $(JBZOO_COMPOSER_UPDATE_FLAGS)


print-version: ##@Project Print version info
@./csv-blueprint
@echo "------------------------------"
@./csv-blueprint --version

# Demo #################################################################################################################
demo: ##@Demo Run demo via PHP binary
$(call title,"Demo - Valid CSV \(PHP binary\)")
Expand Down
5 changes: 1 addition & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -792,6 +792,7 @@ Default report format is `table`:
./csv-blueprint validate:csv --csv='./tests/fixtures/demo.csv' --schema='./tests/schemas/demo_invalid.yml'
CSV Blueprint: unknown version (phpunit)
Found Schemas : 1
Found CSV files : 1
Pairs by pattern: 1
Expand Down Expand Up @@ -877,10 +878,6 @@ It's random ideas and plans. No orderings and deadlines. <u>But batch processing
* Extending with custom rules and custom report formats. Plugins?
* 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.

* **Release workflow**
* Auto insert tool version into the Docker image and phar file. It's important to know the version of the tool you are using.
* Show version as part of output.

* **Performance and optimization**
* Benchmarks as part of the CI(?) and Readme. It's important to know how much time the validation process takes.
* Parallel validation of really-really large files (1GB+ ?). I know you have them and not so much memory.
Expand Down
19 changes: 4 additions & 15 deletions csv-blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

namespace JBZoo\CsvBlueprint;

use JBZoo\Cli\CliApplication;

\define('PATH_ROOT', __DIR__);

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

(new CliApplication('CSV Blueprint', '@git-version@'))
$cliApp = (new CliApplication('CSV Blueprint', Utils::getVersion(true)));
$cliApp->setVersion(Utils::getVersion(false));

$cliApp
->registerCommandsByPath(PATH_ROOT . '/src/Commands', __NAMESPACE__)
->setLogo(
<<<'EOF'
_____ ______ _ _ _
/ __ \ | ___ \ | (_) | |
| / \/_____ __ | |_/ / |_ _ ___ _ __ _ __ _ _ __ | |_
| | / __\ \ / / | ___ \ | | | |/ _ \ '_ \| '__| | '_ \| __|
| \__/\__ \\ V / | |_/ / | |_| | __/ |_) | | | | | | | |_
\____/___/ \_/ \____/|_|\__,_|\___| .__/|_| |_|_| |_|\__|
| |
|_|
EOF,
)
->run();
35 changes: 35 additions & 0 deletions src/CliApplication.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

/**
* JBZoo Toolbox - Csv-Blueprint.
*
* This file is part of the JBZoo Toolbox project.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT
* @copyright Copyright (C) JBZoo.com, All rights reserved.
* @see https://github.com/JBZoo/Csv-Blueprint
*/

declare(strict_types=1);

namespace JBZoo\CsvBlueprint;

class CliApplication extends \JBZoo\Cli\CliApplication
{
private array $appLogo = [
' __________ __ ___ __ _ __ ',
' / ___/ __/ | / / / _ )/ /_ _____ ___ ____(_)__ / /_',
'/ /___\ \ | |/ / / _ / / // / -_) _ \/ __/ / _ \/ __/',
'\___/___/ |___/ /____/_/\_,_/\__/ .__/_/ /_/_//_/\__/ ',
' /_/ ',
];

public function getLongVersion(): string
{
$logo = \implode("\n", $this->appLogo);

return "<info>{$logo}</info>\n" . Utils::getVersion(false);
}
}
4 changes: 4 additions & 0 deletions src/Commands/ValidateCsv.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ protected function configure(): void

protected function executeAction(): int
{
if ($this->isHumanReadableMode()) {
$this->_('CSV Blueprint: ' . Utils::getVersion(false, true));
}

if ($this->getOptBool('profile')) {
\define('PROFILE_MODE', true);
}
Expand Down
44 changes: 44 additions & 0 deletions src/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Symfony\Component\Finder\SplFileInfo;

use function JBZoo\Cli\cli;
use function JBZoo\Utils\bool;

final class Utils
{
Expand Down Expand Up @@ -291,6 +292,44 @@ public static function printFile(string $fullpath): string
return "{$directory}<blue>{$basename}</blue>";
}

public static function getVersion(bool $tagOnly = false, bool $oneLine = false): string
{
if (self::isPhpUnit()) {
return 'unknown version (phpunit)';
}

$versionFile = __DIR__ . '/../.version';
if (!\file_exists($versionFile)) {
return 'unknown version';
}

$parts = \array_filter(\explode("\n", (string)\file_get_contents($versionFile)));
$expectedParts = 5;
if (\count($parts) < $expectedParts) {
return 'unknown version';
}

[$tag, $isStable, $branch, $date, $hash] = $parts;

$dateStr = (new \DateTimeImmutable($date))->format('d M Y H:i');
$tag = 'v' . \trim($tag, 'v');

if ($tagOnly) {
return $tag;
}

$version = ["<info>{$tag}</info>", $dateStr];

if (!bool($isStable)) {
$version[] = '<comment>Experimental!</comment>';
$version[] = "\nbranch: {$branch} ({$hash})";
}

$result = \implode(' ', $version);

return \trim($oneLine ? \str_replace("\n", ' ', $result) : $result);
}

/**
* @param SplFileInfo[] $files
*/
Expand All @@ -309,4 +348,9 @@ private static function filterNotUsedFiles(array $files): array
{
return \array_keys(\array_filter($files, static fn ($value) => $value === false));
}

private static function isPhpUnit(): bool
{
return \defined('PHPUNIT_COMPOSER_INSTALL') || \defined('__PHPUNIT_PHAR__');
}
}
6 changes: 6 additions & 0 deletions tests/Commands/ValidateCsvBasicTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public function testValidateOneCsvPositive(): void
]);

$expected = $expected = <<<'TXT'
CSV Blueprint: unknown version (phpunit)
Found Schemas : 1
Found CSV files : 1
Pairs by pattern: 1
Expand Down Expand Up @@ -66,6 +67,7 @@ public function testValidateOneCsvNegative(): void
]);

$expected = <<<'TXT'
CSV Blueprint: unknown version (phpunit)
Found Schemas : 1
Found CSV files : 1
Pairs by pattern: 1
Expand Down Expand Up @@ -105,6 +107,7 @@ public function testValidateOneCsvWithInvalidSchemaNegative(): void
]);

$expected = <<<'TXT'
CSV Blueprint: unknown version (phpunit)
Found Schemas : 1
Found CSV files : 1
Pairs by pattern: 1
Expand Down Expand Up @@ -167,6 +170,7 @@ public function testInvalidSchemaNotMatched(): void
[$actual, $exitCode] = Tools::virtualExecution('validate:csv', $options);

$expected = <<<'TXT'
CSV Blueprint: unknown version (phpunit)
Found Schemas : 1
Found CSV files : 1
Pairs by pattern: 0
Expand Down Expand Up @@ -216,6 +220,7 @@ public function testInvalidSchemaAndNotFoundCSV(): void
[$actual, $exitCode] = Tools::virtualExecution('validate:csv', $options);

$expected = <<<'TXT'
CSV Blueprint: unknown version (phpunit)
Found Schemas : 1
Found CSV files : 0
Pairs by pattern: 0
Expand Down Expand Up @@ -261,6 +266,7 @@ public function testValidateOneCsvNoHeaderNegative(): void
]);

$expected = $expected = <<<'TXT'
CSV Blueprint: unknown version (phpunit)
Found Schemas : 1
Found CSV files : 1
Pairs by pattern: 1
Expand Down
6 changes: 6 additions & 0 deletions tests/Commands/ValidateCsvBatchCsvTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public function testValidateManyCsvPositive(): void
[$actual, $exitCode] = Tools::virtualExecution('validate:csv', $optionsAsString);

$expected = $expected = <<<'TXT'
CSV Blueprint: unknown version (phpunit)
Found Schemas : 1
Found CSV files : 4
Pairs by pattern: 4
Expand Down Expand Up @@ -83,6 +84,7 @@ public function testValidateManyCsvNegative(): void
[$actual, $exitCode] = Tools::virtualExecution('validate:csv', $options);

$expected = <<<'TXT'
CSV Blueprint: unknown version (phpunit)
Found Schemas : 1
Found CSV files : 3
Pairs by pattern: 3
Expand Down Expand Up @@ -173,6 +175,10 @@ public function testMultipleCsvOptions(): void
'',
);

// Remove version
$actual = \preg_replace('/^.+\n/', '', $actual);
$expected = \preg_replace('/^.+\n/', '', $expected);

isNotEmpty($expected);
isNotEmpty($actual);
isSame($expectedCode, 1);
Expand Down
2 changes: 2 additions & 0 deletions tests/Commands/ValidateCsvBatchSchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public function testMultiSchemaDiscovery(): void
[$actual, $exitCode] = Tools::virtualExecution('validate:csv', $optionsAsString);

$expected = <<<'TXT'
CSV Blueprint: unknown version (phpunit)
Found Schemas : 3
Found CSV files : 1
Pairs by pattern: 2
Expand Down Expand Up @@ -121,6 +122,7 @@ public function testNoPattern(): void
[$actual, $exitCode] = Tools::virtualExecution('validate:csv', $optionsAsString);

$expected = <<<'TXT'
CSV Blueprint: unknown version (phpunit)
Found Schemas : 2
Found CSV files : 1
Pairs by pattern: 2
Expand Down
2 changes: 2 additions & 0 deletions tests/Commands/ValidateCsvQuickTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ final class ValidateCsvQuickTest extends TestCase
public function testEnabled(): void
{
$expectedFull = <<<'TXT'
CSV Blueprint: unknown version (phpunit)
Found Schemas : 1
Found CSV files : 3
Pairs by pattern: 3
Expand Down Expand Up @@ -67,6 +68,7 @@ public function testEnabled(): void
public function testDisabled(): void
{
$expectedFull = <<<'TXT'
CSV Blueprint: unknown version (phpunit)
Found Schemas : 1
Found CSV files : 3
Pairs by pattern: 3
Expand Down
3 changes: 3 additions & 0 deletions tests/Commands/ValidateCsvReportsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ final class ValidateCsvReportsTest extends TestCase
public function testDefault(): void
{
$expected = <<<'TXT'
CSV Blueprint: unknown version (phpunit)
Found Schemas : 1
Found CSV files : 1
Pairs by pattern: 1
Expand Down Expand Up @@ -79,6 +80,7 @@ public function testDefault(): void
public function testText(): void
{
$expected = <<<'TXT'
CSV Blueprint: unknown version (phpunit)
Found Schemas : 1
Found CSV files : 1
Pairs by pattern: 1
Expand Down Expand Up @@ -120,6 +122,7 @@ public function testText(): void
public function testGithub(): void
{
$expected = <<<'TXT'
CSV Blueprint: unknown version (phpunit)
Found Schemas : 1
Found CSV files : 1
Pairs by pattern: 1
Expand Down

0 comments on commit 1000ad4

Please sign in to comment.