Skip to content
This repository has been archived by the owner on Dec 5, 2024. It is now read-only.

Commit

Permalink
Merge pull request #28 from SebSept/missing_indexes_check_on_precommit
Browse files Browse the repository at this point in the history
index filler can be added in pre-commit script (using --ckeck-only option)
  • Loading branch information
SebSept authored May 22, 2021
2 parents ca50635 + bbc4674 commit 34f5a88
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 11 deletions.
74 changes: 74 additions & 0 deletions notes_bu
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# todo


# todo secondaire

- implementer l'installation d'un hook pre-commit
- implemeter la commande :hello
- integrer worfklow phpstan maison
- intégration workflow github - https://github.com/PrestaShopCorp/ pas mal d'actions, voir aussi celle de presta (sonst les mêmes ?) (comme https://github.com/PrestaShop/github-webhook-parser)
- ugrade d'un package
- ajouter ref à la page https://devdocs.prestashop.com/1.7/modules/testing/ci-cd/

# todo pour plus tard

- comment installer phpstan ? phpstan est requi par prestashop-dev-tools mais pas inclu dedans.
pour le moment, il est inclu dans le composer du ce packages, c'est plus simple.
mais il pourrait être dans le configureTool de PrestashopDevToolsPhpStan.
- a l'install : note sur le niveau de phpstan

## Todo vraiment plus tard
intégration phpunit
yaml lint : https://yamllint.readthedocs.io/en/v1.2.1/quickstart.html#installing-yamllint
task runner pour le lintage hors ide des ressources
intégration lint js
intégration lint html/css/scss
intégrer validation de module avec validateur prestashop ?

## Todo en attente
modification header-stamp pour traiter les header sans 'prestashop' dedans.
vendor/prestashop/header-stamp/src/Command/UpdateLicensesCommand.php:336

## Mémoire

pour install sans symlink : COMPOSER_MIRROR_PATH_REPOS=1 composer install

# ressources

lint js : .stylelintrc
- https://github.com/PrestaShop/stylelint-config
- https://github.com/PrestaShop/stylelint-browser-compatibility
eslint : https://github.com/PrestaShop/eslint-config


voir https://github.com/PrestaShop/example-modules

-----

# v2

## Install

s'install en local avec :
COMPOSER_MIRROR_PATH_REPOS=1 composer require --dev sebsept/ps_dev_base:2.x-dev
ou
COMPOSER_MIRROR_PATH_REPOS=1 composer require --dev sebsept/ps_dev_base:dev-pre-commit-hook
COMPOSER_MIRROR_PATH_REPOS=1 composer require --dev sebsept/ps_dev_base:dev-auto_config_phpstan
en ayant ajouté ça au composer :

"repositories": [
{
"type": "path",
"url": "/home/http/perso/prestashop/examplemodule",
"package": {
"name": "sebsept/ps_dev_base"
}
}
]



prestashop/php-dev-tools
Commande init de php-dev-tools copie dossier /templates/phpstan/ dans tests/phpstan
Contient le fichier de config phpstan.neon, qui inclue lui-même un autre fichier de config du package

68 changes: 57 additions & 11 deletions src/Command/SebSept/IndexPhpFiller.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,17 @@

namespace SebSept\PsDevToolsPlugin\Command\SebSept;

use Composer\IO\IOInterface;
use Exception;
use SebSept\PsDevToolsPlugin\Command\Contract\PreCommitRegistrableCommand;
use SebSept\PsDevToolsPlugin\Command\ScriptCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;

final class IndexPhpFiller extends ScriptCommand
final class IndexPhpFiller extends ScriptCommand implements PreCommitRegistrableCommand
{
const SOURCE_INDEX_FILE = '/../../../resources/index.php';

Expand All @@ -41,11 +44,15 @@ protected function configure(): void
$this->setHelp($this->getDescription() . <<<'HELP'
This is a security requirement of Prestashop to avoid the contents to be listed.
The command adds all the missing <comment>index.php</comment> files unless you add the <info>--check-only</info> option.
With <info>--check-only</info> option the command returns 1 if some files are missing. 0 if all required files are present.
More informations on the official documentation.
https://devdocs.prestashop.com/1.7/modules/sell/techvalidation-checklist/#a-file-indexphp-exists-in-each-folder
HELP
);
)
->addOption('check-only', null, InputOption::VALUE_NONE, 'Checks if index.php are missing');
parent::configure();
}

Expand All @@ -56,11 +63,17 @@ public function getComposerScriptName(): string

protected function execute(InputInterface $input, OutputInterface $output): int
{
$this->isComposerScriptDefined() ?: $this->addComposerScript(['composer psdt:fill-indexes']);

$this->fs = new Filesystem();
if ($input->getOption('check-only')) {
return $this->checkMissingIndexes();
}

$this->getIO()->write('Adding missing index.php to all directories.'
. PHP_EOL . '<comment>Existing index.php are not replaced.</comment>');
try {
$this->recursivelyAddIndexes();
$this->addMissingIndexFiles();
$this->getIO()->write('<info>Done</info>');

return 0;
Expand All @@ -71,15 +84,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}
}

private function recursivelyAddIndexes(): void
private function addMissingIndexFiles(): void
{
$directoryIterator = (new Finder())
->in($this->getcwd())
->directories()
->exclude('vendor')
->getIterator();

foreach ($directoryIterator as $fileInfo) {
foreach ($this->getDirectoryIterator() as $fileInfo) {
$this->addIndex($fileInfo);
}
}
Expand All @@ -92,6 +99,28 @@ private function addIndex(\SplFileInfo $splFileInfo): void
$this->fs->copy($this->getSourceIndexPath(), $target);
}

private function checkMissingIndexes(): int
{
$missingIndexFiles = [];

foreach ($this->getDirectoryIterator() as $fileInfo) {
$indexPhpPath = $fileInfo->getPathname() . '/index.php';
$this->fs->exists($indexPhpPath) ?: array_push($missingIndexFiles, $indexPhpPath);
}

if (empty($missingIndexFiles)) {
$this->getIO()->write('Good : no missing index files.');

return 0;
}

$this->getIO()->write(':( Missing index.php files.');
$this->getIO()->write($missingIndexFiles, true, IOInterface::VERBOSE);
$this->getIO()->write(['Use <info>-v</info> option to list missing files', 'Remove <info>--check-only</info> option to add missing files.']);

return 1;
}

private function getSourceIndexPath(): string
{
return __DIR__ . self::SOURCE_INDEX_FILE;
Expand All @@ -106,4 +135,21 @@ private function getcwd(): string

return $cwd;
}

/**
* @return \AppendIterator|\Iterator|\RecursiveIteratorIterator|\Symfony\Component\Finder\Iterator\CustomFilterIterator|\Symfony\Component\Finder\Iterator\DateRangeFilterIterator|\Symfony\Component\Finder\Iterator\DepthRangeFilterIterator|\Symfony\Component\Finder\Iterator\FilecontentFilterIterator|\Symfony\Component\Finder\Iterator\FilenameFilterIterator|\Symfony\Component\Finder\Iterator\FileTypeFilterIterator|\Symfony\Component\Finder\Iterator\PathFilterIterator|\Symfony\Component\Finder\Iterator\SizeRangeFilterIterator|\Symfony\Component\Finder\SplFileInfo[]
*/
private function getDirectoryIterator()
{
return (new Finder())
->in($this->getcwd())
->directories()
->exclude('vendor')
->getIterator();
}

public function getComposerPrecommitScriptContent(): ?string
{
return 'composer psdt:fill-indexes --check-only';
}
}

0 comments on commit 34f5a88

Please sign in to comment.