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 new file mode 100644 index 0000000..6ea3568 --- /dev/null +++ b/src/Command/PrestashopDevTools/PrestashopDevToolsHeaderStamp.php @@ -0,0 +1,88 @@ + + * @copyright since 2021 Sébastien Monterisi + * @license https://opensource.org/licenses/MIT MIT License + */ + +declare(strict_types=1); + +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'; + + public function getComposerScriptName(): string + { + return 'header-stamp'; + } + + public function getPackageName(): ?string + { + return 'prestashop/header-stamp'; + } + + public function getPackageVersionConstraint(): ?string + { + return '* || ^2.0'; // 2.0 is ok but in case prestashop/php-dev-tools changes it's requirements... + } + + protected function configure(): void + { + $this->setName($this->getComposerScriptName()); + parent::configure(); + } + + public function isToolConfigured(): bool + { + return $this->isComposerScriptDefined() + && $this->headerStampFileExists() + && $this->isHeaderStampFileCustomized(); + } + + public function configureTool(): void + { + // 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 + { + 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); + } +} 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(), ]; } }