From 27b403c3f81016480820c0d4d62a905640a5a3d9 Mon Sep 17 00:00:00 2001 From: SebSept Date: Mon, 24 May 2021 00:17:38 +0200 Subject: [PATCH 1/4] start of `header-stamp` command implementation. --- .../PrestashopDevToolsHeaderStamp.php | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 src/Command/PrestashopDevTools/PrestashopDevToolsHeaderStamp.php diff --git a/src/Command/PrestashopDevTools/PrestashopDevToolsHeaderStamp.php b/src/Command/PrestashopDevTools/PrestashopDevToolsHeaderStamp.php new file mode 100644 index 0000000..f4cd730 --- /dev/null +++ b/src/Command/PrestashopDevTools/PrestashopDevToolsHeaderStamp.php @@ -0,0 +1,71 @@ + + * @copyright since 2021 Sébastien Monterisi + * @license https://opensource.org/licenses/MIT MIT License + */ + +declare(strict_types=1); + +namespace SebSept\PsDevToolsPlugin\Command\PrestashopDevTools; + +final class PrestashopDevToolsHeaderStamp extends PrestashopDevTools +{ + private const HEADERSTAMP_FILE = '.header_stamp.txt'; + private const HEADERSTAMP_UNCHANGED_MARKER = 'This small piece of text marks the fact this file havn\'t been customized'; + + public function getComposerScriptName(): string + { + return 'header-stamp'; + } + + public function isToolConfigured(): bool + { + // @todo add output + return $this->isComposerScriptDefined() + && $this->headerStampFileExists() + && $this->isHeaderStampFileCustomized(); + } + + public function configureTool(): void + { + /* + * Some refactoring is needed before implementing this, probably. + * + * - The header-stamp package is a requirement of prestashop/php-dev-tools. + * - We can rely on the fact that prestashop/php-dev-tools will still include it. :/ + * - Or write a command that doesn't extends \...\Command\PrestashopDevTools\PrestashopDevTools + * - but in this case we should check if \...\ComposerPackageCommand::getInstalledDevRequires + * returns the correct response if headerstamp is installed by prestashop/php-dev-tools + * - or implements something that can check for multiple composer pachages, not only one. + * - By the way, getInstalledDevRequires may better check for package provided for normal use And dev use (?) + */ + throw new \Exception('Implement me'); + } + + private function headerStampFileExists(): bool + { + return file_exists(self::HEADERSTAMP_FILE); + } + + private function isHeaderStampFileCustomized(): bool + { + $headerStampContents = file_get_contents(self::HEADERSTAMP_FILE); + if (false === $headerStampContents) { + throw new \Exception(sprintf('Failed to read headerstamp contents on file %s', self::HEADERSTAMP_FILE)); + } + + return false !== strpos($headerStampContents, self::HEADERSTAMP_UNCHANGED_MARKER); + } +} From 523033b057d86ccf52117b0bad81a6aaed460ced Mon Sep 17 00:00:00 2001 From: SebSept Date: Mon, 24 May 2021 13:44:05 +0200 Subject: [PATCH 2/4] start of `header-stamp` command implementation. - added to CommandProvider --- .../PrestashopDevToolsHeaderStamp.php | 24 +++++++++++++++++-- src/Composer/PsDevToolsCommandProvider.php | 2 ++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/Command/PrestashopDevTools/PrestashopDevToolsHeaderStamp.php b/src/Command/PrestashopDevTools/PrestashopDevToolsHeaderStamp.php index f4cd730..fe6b785 100644 --- a/src/Command/PrestashopDevTools/PrestashopDevToolsHeaderStamp.php +++ b/src/Command/PrestashopDevTools/PrestashopDevToolsHeaderStamp.php @@ -20,7 +20,9 @@ namespace SebSept\PsDevToolsPlugin\Command\PrestashopDevTools; -final class PrestashopDevToolsHeaderStamp extends PrestashopDevTools +use SebSept\PsDevToolsPlugin\Command\ComposerPackageCommand; + +final class PrestashopDevToolsHeaderStamp extends ComposerPackageCommand { private const HEADERSTAMP_FILE = '.header_stamp.txt'; private const HEADERSTAMP_UNCHANGED_MARKER = 'This small piece of text marks the fact this file havn\'t been customized'; @@ -30,10 +32,28 @@ public function getComposerScriptName(): string return 'header-stamp'; } + public function getPackageName(): ?string + { + return 'prestashop/header-stamp'; + } + + public function getPackageVersionConstraint(): ?string + { + return '^2.0'; + } + + protected function configure(): void + { + $this->setName($this->getComposerScriptName()); + parent::configure(); + } + public function isToolConfigured(): bool { + var_dump('inst : ' . $this->isPackageInstalled()); // @todo add output - return $this->isComposerScriptDefined() + return $this->isPackageInstalled() + && $this->isComposerScriptDefined() && $this->headerStampFileExists() && $this->isHeaderStampFileCustomized(); } diff --git a/src/Composer/PsDevToolsCommandProvider.php b/src/Composer/PsDevToolsCommandProvider.php index 02e7da0..f3bd73d 100644 --- a/src/Composer/PsDevToolsCommandProvider.php +++ b/src/Composer/PsDevToolsCommandProvider.php @@ -22,6 +22,7 @@ use Composer\Plugin\Capability\CommandProvider; use SebSept\PsDevToolsPlugin\Command\PrestashopDevTools\PrestashopDevToolsCsFixer; +use SebSept\PsDevToolsPlugin\Command\PrestashopDevTools\PrestashopDevToolsHeaderStamp; use SebSept\PsDevToolsPlugin\Command\PrestashopDevTools\PrestashopDevToolsPhpStan; use SebSept\PsDevToolsPlugin\Command\SebSept\HelloCommand; use SebSept\PsDevToolsPlugin\Command\SebSept\IndexPhpFiller; @@ -37,6 +38,7 @@ public function getCommands(): array new PrestashopDevToolsCsFixer(), new IndexPhpFiller(), new PrecommitHook(), + new PrestashopDevToolsHeaderStamp(), ]; } } From ae10866629ad08ad532ef34be213203904e1a875 Mon Sep 17 00:00:00 2001 From: SebSept Date: Mon, 24 May 2021 14:09:42 +0200 Subject: [PATCH 3/4] test requirement - squash me --- .../PrestashopDevTools/PrestashopDevToolsHeaderStamp.php | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/Command/PrestashopDevTools/PrestashopDevToolsHeaderStamp.php b/src/Command/PrestashopDevTools/PrestashopDevToolsHeaderStamp.php index fe6b785..2057fe2 100644 --- a/src/Command/PrestashopDevTools/PrestashopDevToolsHeaderStamp.php +++ b/src/Command/PrestashopDevTools/PrestashopDevToolsHeaderStamp.php @@ -39,7 +39,7 @@ public function getPackageName(): ?string public function getPackageVersionConstraint(): ?string { - return '^2.0'; + return '* || ^2.0'; // 2.0 is ok but in case prestashop/php-dev-tools changes it's requirements... } protected function configure(): void @@ -50,10 +50,7 @@ protected function configure(): void public function isToolConfigured(): bool { - var_dump('inst : ' . $this->isPackageInstalled()); - // @todo add output - return $this->isPackageInstalled() - && $this->isComposerScriptDefined() + return $this->isComposerScriptDefined() && $this->headerStampFileExists() && $this->isHeaderStampFileCustomized(); } From 6feff59aa46dc882e1a5f91d6cb3f390c75bc106 Mon Sep 17 00:00:00 2001 From: SebSept Date: Mon, 24 May 2021 15:47:14 +0200 Subject: [PATCH 4/4] ... headerStamp Implementation ... --- resources/.header_stamp.txt | 14 ++++++++++ src/Command/ComposerPackageCommand.php | 4 +++ .../PrestashopDevToolsHeaderStamp.php | 26 +++++++++---------- 3 files changed, 31 insertions(+), 13 deletions(-) create mode 100644 resources/.header_stamp.txt diff --git a/resources/.header_stamp.txt b/resources/.header_stamp.txt new file mode 100644 index 0000000..5978e0d --- /dev/null +++ b/resources/.header_stamp.txt @@ -0,0 +1,14 @@ +{{vendor}} {{packageName}} - {{packageDescription}}. + +Copyright (c) {{year}} {{authorName}} + +NOTICE OF LICENSE + +This source file is subject to the {{openSourcLicenseName}} License +that is bundled with this package in the file LICENSE.txt. +It is also available through the world-wide-web at this URL: +https://opensource.org/licenses/{{openSourcLicenseName}} + +@author {{authorName}} <{{authorEmail}}> +@copyright since {{year}} {{authorName}} +@license https://opensource.org/licenses/{{openSourcLicenseName}} {{openSourcLicenseName}} License diff --git a/src/Command/ComposerPackageCommand.php b/src/Command/ComposerPackageCommand.php index 0a9f582..2f8cd46 100644 --- a/src/Command/ComposerPackageCommand.php +++ b/src/Command/ComposerPackageCommand.php @@ -44,6 +44,10 @@ abstract public function getPackageVersionConstraint(): ?string; abstract public function isToolConfigured(): bool; + /** + * it configures the tool. + * It doesn't do checks, only perform addComposerScript(), file copy, etc. + */ abstract public function configureTool(): void; final protected function execute(InputInterface $input, OutputInterface $output): int diff --git a/src/Command/PrestashopDevTools/PrestashopDevToolsHeaderStamp.php b/src/Command/PrestashopDevTools/PrestashopDevToolsHeaderStamp.php index 2057fe2..6ea3568 100644 --- a/src/Command/PrestashopDevTools/PrestashopDevToolsHeaderStamp.php +++ b/src/Command/PrestashopDevTools/PrestashopDevToolsHeaderStamp.php @@ -21,9 +21,11 @@ namespace SebSept\PsDevToolsPlugin\Command\PrestashopDevTools; use SebSept\PsDevToolsPlugin\Command\ComposerPackageCommand; +use Symfony\Component\Filesystem\Filesystem; final class PrestashopDevToolsHeaderStamp extends ComposerPackageCommand { + private const SOURCE_HEADERSTAMP_FILE = __DIR__ . '/../../../resources/.header_stamp.txt'; private const HEADERSTAMP_FILE = '.header_stamp.txt'; private const HEADERSTAMP_UNCHANGED_MARKER = 'This small piece of text marks the fact this file havn\'t been customized'; @@ -57,18 +59,16 @@ public function isToolConfigured(): bool public function configureTool(): void { - /* - * Some refactoring is needed before implementing this, probably. - * - * - The header-stamp package is a requirement of prestashop/php-dev-tools. - * - We can rely on the fact that prestashop/php-dev-tools will still include it. :/ - * - Or write a command that doesn't extends \...\Command\PrestashopDevTools\PrestashopDevTools - * - but in this case we should check if \...\ComposerPackageCommand::getInstalledDevRequires - * returns the correct response if headerstamp is installed by prestashop/php-dev-tools - * - or implements something that can check for multiple composer pachages, not only one. - * - By the way, getInstalledDevRequires may better check for package provided for normal use And dev use (?) - */ - throw new \Exception('Implement me'); + // copyHeaderStampFile and add a marker to know if file was customized. + $this->getIO()->write('Preparing hearder stamp file ... ', false); + $fs = new Filesystem(); + $fs->copy(self::SOURCE_HEADERSTAMP_FILE, self::HEADERSTAMP_FILE, true); + file_put_contents(self::HEADERSTAMP_FILE, self::HEADERSTAMP_UNCHANGED_MARKER, FILE_APPEND); + $this->getIO()->write('OK'); + + $this->addComposerScript([sprintf("header-stamp --exclude=vendor,node_modules --license='%s'", self::HEADERSTAMP_FILE)]); + + throw new \RuntimeException(sprintf('You must now edit %s file.', self::HEADERSTAMP_FILE)); } private function headerStampFileExists(): bool @@ -83,6 +83,6 @@ private function isHeaderStampFileCustomized(): bool throw new \Exception(sprintf('Failed to read headerstamp contents on file %s', self::HEADERSTAMP_FILE)); } - return false !== strpos($headerStampContents, self::HEADERSTAMP_UNCHANGED_MARKER); + return false == strpos($headerStampContents, self::HEADERSTAMP_UNCHANGED_MARKER); } }