iseed838/githooks

yii2 的 Git hooks 包装器

0.1.2 2020-04-28 06:46 UTC

This package is auto-updated.

Last update: 2024-09-30 01:26:59 UTC


README

yii2 的 Git hooks 处理器

Git 版本控制系统提供了许多项目管理功能,包括基于事件的模式。Git 事件模型提供了在事件发生后调用脚本的能力,例如切换到另一个分支、在本地分支上加载项目等。此包提供了注册 Git 事件并根据发生的事件进行设置的能力。

要安装 composer 包,运行以下命令

composer require iseed838/githooks ^0.1

  1. 接下来,您需要将控制台模块连接到配置文件 @console\config\main.php
    'modules' => [
        'githooks' => githooks\Module:: class
    ]
  1. 插件将为控制台添加 2 个命令
  • githooks/hooks/register 注册事件设置
  • githooks/hooks/handle 捕获事件
  1. 接下来,您应该取消事件配置的订阅。配置是通过 yii2 依赖注入容器执行的。整个配置在项目容器部分取消订阅
    'container' => [
        'singletons' => [
            ....
        ]
    ]
  1. 以下模型参与配置
  • GitHookRule - 钩子规则
  • BaseEvent 实现 EventInterface 是事件的基础类
  • BaseEventHandler 实现 EventHandlerInterface - 事件处理器的基础类
  1. 默认情况下,钩子设置有预置值。您可以覆盖它们并指定设置的链接
    'githook.yii.parameters' => function () {
        return GitHooksParameters::make();
    },
  1. 接下来,您需要配置规则:每个规则包含
  • Git 钩子名称
  • 事件模型
  • 处理器模型
  • 可选的 GitHookParameters 参数集
    'githook.post_merge.migrate' => function () {
        return new GitHookRule(GitHooksParameters::HOOK_POST_MERGE, AlwaysEvent::make(),
    MigrateEventHandler::make(), Yii::$container->get('githook.yii.parameters'));
    },
    'githook.post_merge.cache_flush' => function () {
        return new GitHookRule(GitHooksParameters::HOOK_POST_MERGE, AlwaysEvent::make(),
    CacheFlushEventHandler::make(), Yii::$container->get('githook.yii.parameters'));
    },
  1. 配置 Git 事件后,您需要使用 githooks/hooks/register 控制台命令注册钩子。此命令将在 {ROOT}./.githooks/ 目录中创建文件夹并将 Git hooks 文件上传到其中

基本配置中提供了以下事件

  • AlwaysEvent - 总是返回 true
  • ComposerChangeEvent - composer.json 文件是否已更改
  • NodeChangeEvent - package.json 文件是否已更改
  • WebFileUpdateEvent - css、js、sass、less 文件是否已更改

以及事件处理器

  • ComposerUpdateEventHandler - composer install 命令的处理器
  • CacheFlushEventHandler - yii cache/flush-all 命令的处理器
  • GulpUpdateEventHandler - gulp build 命令的处理器
  • MigrateEventHandler - yii migrate 命令的处理器
  • NodeUpdateEventHandler - npm install 命令的处理器
  • RbacUpdateEventHandler - yii rbac/init 命令的处理器
  • RedisDropEventHandler - yii redis/drop 命令的处理器

您可以通过继承 BaseEvent 类或应用 EventInterface 接口来创建自己的事件。处理器可以通过继承 BaseEventHandler 类或应用 EventHandlerInterface 接口来创建

  1. 接下来,将规则添加到钩子参数中。
    GitHooksParameters :: class => [
        'rules' => [
            Instance :: of ('githook.post_merge.migrate'),
            Instance :: of ('githook.post_merge.cache_flush'),
        ]
    ]

这就完成了。

以下是一个完整的配置示例

        'githook.yii.parameters'              => function () {
            return GitHooksParameters::make();
        },
        'githook.yii_test.parameters'         => function () {
            $parameters = GitHooksParameters::make();
        
            return $parameters->setYiiPath('yii_test');
        },
        'githook.post_merge.migrate'          => function () {
            return new GitHookRule(GitHooksParameters::HOOK_POST_MERGE, AlwaysEvent::make(),
                MigrateEventHandler::make(), Yii::$container->get('githook.yii.parameters'));
        },
        'githook.post_merge.cache_flush'      => function () {
            return new GitHookRule(GitHooksParameters::HOOK_POST_MERGE, AlwaysEvent::make(),
                CacheFlushEventHandler::make(), Yii::$container->get('githook.yii.parameters'));
        },
        'githook.post_merge.lang_drop'        => function () {
            return new GitHookRule(GitHooksParameters::HOOK_POST_MERGE, AlwaysEvent::make(),
                RedisDropEventHandler::make(), Yii::$container->get('githook.yii.parameters'));
        },
        'githook.post_merge.composer_install' => function () {
            return new GitHookRule(GitHooksParameters::HOOK_POST_MERGE, ComposerChangeEvent::make(),
                ComposerUpdateEventHandler::make(), Yii::$container->get('githook.yii.parameters'));
        },
        'githook.post_merge.npm_install'      => function () {
            return new GitHookRule(GitHooksParameters::HOOK_POST_MERGE, NodeChangeEvent::make(),
                NodeUpdateEventHandler::make(), Yii::$container->get('githook.yii.parameters'));
        },
        'githook.post_merge.test_migrate'     => function () {
            return new GitHookRule(GitHooksParameters::HOOK_POST_MERGE, AlwaysEvent::make(),
                MigrateEventHandler::make(), Yii::$container->get('githook.yii_test.parameters'));
        },
        'githook.post_merge.test_cache_flush' => function () {
            return new GitHookRule(GitHooksParameters::HOOK_POST_MERGE, AlwaysEvent::make(),
                CacheFlushEventHandler::make(), Yii::$container->get('githook.yii_test.parameters'));
        },
        GitHooksParameters::class             => [
            'rules' => [
                Instance::of('githook.post_merge.migrate'),
                Instance::of('githook.post_merge.cache_flush'),
                Instance::of('githook.post_merge.lang_drop'),
                Instance::of('githook.post_merge.composer_install'),
                Instance::of('githook.post_merge.npm_install'),
                Instance::of('githook.post_merge.test_migrate'),
                Instance::of('githook.post_merge.test_cache_flush'),
            ]
        ]