Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.
Draft
Show file tree
Hide file tree
Changes from all 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
14 changes: 14 additions & 0 deletions resources/.header_stamp.txt
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions src/Command/ComposerPackageCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
88 changes: 88 additions & 0 deletions src/Command/PrestashopDevTools/PrestashopDevToolsHeaderStamp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php
/**
* SebSept Ps_dev_base - Tools for quality Prestashop Module development.
*
* Copyright (c) 2021 Sébastien Monterisi
*
* NOTICE OF LICENSE
*
* This source file is subject to the MIT 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/MIT
*
* @author Sébastien Monterisi <contact@seb7.fr>
* @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('<info>OK</info>');

$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);
}
}
2 changes: 2 additions & 0 deletions src/Composer/PsDevToolsCommandProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -37,6 +38,7 @@ public function getCommands(): array
new PrestashopDevToolsCsFixer(),
new IndexPhpFiller(),
new PrecommitHook(),
new PrestashopDevToolsHeaderStamp(),
];
}
}