uuf6429 / elder-brother
为您的基于PHP的项目设置和管理贡献策略。
Requires
- php: ^5.5.0 || ^7.0
- psr/log: ^1.0
- symfony/console: ^3.1
Requires (Dev)
- friendsofphp/php-cs-fixer: ^1.11
- phpunit/phpunit: ^4.8
Suggests
- friendsofphp/php-cs-fixer: Needed for PhpCsFixer action
This package is auto-updated.
Last update: 2020-01-22 21:38:13 UTC
README
Elder Brother自动调节项目前的贡献。
使用如Git-hooks等事件,可以在多个方面简化开发,例如
- 在提交更改前确保代码风格一致
- 运行输出应包含在提交中的工具
- 在检出旧提交时执行回滚
- 在检出较新提交时编译源代码或执行迁移
这些工具使得设置这些任务以及将这些策略置于版本控制下变得简单。
目录
安装
-
使用Composer将库添加到您的项目中
$ composer require neronmoon/scriptsdev $ composer require uuf6429/elder-brother "~1.0" --dev
注意
- 这种方式下,Elder Brother 只会在开发期间安装(Composer 应在生产中使用
--no-dev
运行)。 scriptsdev
包将确保在开发期间安装,在生产期间不会破坏。- 您可能还需要安装一些额外的包(以下详细说明)以使用某些特定操作。
- 这种方式下,Elder Brother 只会在开发期间安装(Composer 应在生产中使用
-
将以下条目添加到您的
composer.json
{ "scripts-dev": { "post-install-cmd": "vendor/bin/elder-brother install", "post-update-cmd": "vendor/bin/elder-brother install" } }
-
创建一个
.brother.php
配置文件(如下所述)并将.brother.local.php
添加到您的.gitignore
文件中(这允许用户级别配置)。
用法
Elder Brother默认从两个文件读取配置,.brother.php
和 .brother.local.php
(这些文件应由您的VCS忽略)。
典型的配置文件结构如下
<?php use uuf6429\ElderBrother\Action; use uuf6429\ElderBrother\Event; use uuf6429\ElderBrother\Change\GitChangeSet; return [ Event\Git::PRE_COMMIT => [ 1 => new Action\PhpLinter( GitChangeSet::getAddedCopiedModified() ->name('/\\.php$/') ), 2 => new Action\PhpCsFixer( GitChangeSet::getAddedCopiedModified() ->name('/\\.php$/') ), ] ];
基本上,配置是由按事件分组的行为数组。在上面的示例中,PhpLinter
和 PhpCsFixer
行为将在事件发生之前检查提交中的所有文件(GitChangeSet::getAddedCopiedModified()
)(Event\Git::PRE_COMMIT
)。建议您为每个行为分配一个定义的数字索引,以便它们可以被用户配置轻松覆盖。请注意,无论项目在数组中看起来如何,行为执行都是从最小的索引开始的。
可用操作
执行自定义代码(ExecuteCode)
new ExecuteCode( string $description, // Description of the intention of the callback callable $callback // The callback to execute. It will receive $config, $input and $output as parameters )
执行传入的回调、函数或静态方法。
执行外部程序(ExecuteProgram)
new ExecuteProgram( string $description, // Description of the intention of the program string $command, // Program command line (with parameters) bool $breakOnFailure, // (Optional, default is true) Stop execution if program returns non-0 exit code array|null $environment, // (Optional, default is null / current vars) Environment variables to pass to program string|null $currentDir, // The current directory to use for program int $timeout // (Optional, default is 60) The time to wait for program to finish (in seconds) )
执行外部程序。
禁止文件(ForbiddenFiles)
new ForbiddenFiles( \FileList $files, string $reason )
如果$files
不为空,将根据$reason
中指定的原因停止进程。
PHP 代码风格修复器(PhpCsFixer)
new PhpCsFixer( \FileList $files, // The files to check int|null $level, // (Optional, defaults to NONE_LEVEL) Fixer level to use string[] $fixers, // (Optional, default is empty) Set the fixers to use bool $addAutomatically // (Optional, default is true) Whether to add modified files to commit or not )
将所有提供的文件通过 PHP-CS-Fixer 运行,修复任何代码风格问题。
PHP 代码风格修复器(PhpCsFixerOld)
new PhpCsFixerOld( \FileList $files, // The files to check string|null $binFile, // (Optional, default is from vendor) File path to PHP-CS-Fixer binary string|null $configFile, // (Optional, default is project root) File path to PHP-CS-Fixer config bool $addAutomatically // (Optional, default is true) Whether to add modified files to commit or not )
已弃用:请使用 PhpCsFixer 类。
将所有提供的文件通过 PHP-CS-Fixer 运行,修复任何代码风格问题。
PHP 语法检查(PhpLinter)
new PhpLinter( \FileList $files // The files to check )
确保所有提供的文件都是有效的 PHP 文件,如果无效,则使用错误和非零退出代码终止进程。
为文件显示警告(RiskyFiles)
new RiskyFiles( \FileList $files, string $reason )
如果$files
不为空,将根据$reason
中指定的原因显示警告。
常见问题解答(FAQ)
这个工具背后的主要动机是什么?
有相当多合作者的项目通常会制定关于接受哪种补丁的政策。这通常通过代码审查(可能非常耗时)、自定义工具(设置困难)或预提交钩子中的 shell 脚本来执行。
这里的核心理念是在源代码到达主存储库之前,在客户端建立一个框架来强制执行项目贡献政策 - 节省开发人员时间和维护成本。
为什么选择 PHP?
为什么不呢?有两种替代方案:shell 脚本和其他语言。shell 脚本在这个用途中有许多缺点 - 它们不是跨平台兼容的,也不容易理解。其他语言可能适合,但这意味着需要您的合作者了解它们。我们基本上使用生态系统中的现有工具。