美文网首页
TortoiseGit hook scripts 实现代码格式化

TortoiseGit hook scripts 实现代码格式化

作者: 爱吃秋葵的莫冲 | 来源:发表于2018-07-27 12:07 被阅读173次

TortoiseGit hook scripts 实现代码格式化说明

我们有时候代码要放在开发服务器上,多人合作完成,这种情况下项目的代码需要结合 TortoiseGit 实现代码格式化。

1. 設置curl環境變量

添加CURL系统变量路径

gitbin目录加入系统环境变量路径,例如C:\Program Files (x86)\Git\bin;

打开cmd面板,执行curl,若执行后输出curl: try 'curl --help' or 'curl --manual' for more information,说明添加成功

2. 設置HOOK

2.1 设置 hook scripts

  • 右键打开TortoiseGit->Hook Scripts,点击add按纽打开新增面板
  • 勾选左上角的Enable(默认)
  • 右上角下拉框选择Start Commit hook
  • Run when working tree path is under中输入项目根目录地址,例如\\192.168.2.249\home\laravel5\toutiao
  • Commond line to execute的输入框中输入bat文件地址,例如:
    \\192.168.2.249\home\laravel5\toutiao\public\format.bat
  • 选中Wait for the script to finish
  • 选中Hide the script while running

2.2 添加 php文件

  • 在项目域名目录中添加format.php
<?php

chdir(dirname(dirname(__FILE__)));
shell_exec('sh format.sh');
exit();
  • format.php加入.gitignore

2.3 添加 bat文件

  • 在项目域名目录中添加format.bat
curl  http://yoursystemdomain/format.php
  • format.bat加入.gitignore

2.4 添加格式化配置文件

在项目根目录下新建format.php_cs.dist文件,输入以下内容,$finder请根据实际项目目录设置需要格式化的目录和文件:

<?php

$finder = PhpCsFixer\Finder::create()
    ->files()
    ->in(__DIR__)
    ->exclude("vendor")
    ->exclude("ThinkPHP")
    ->exclude("SmartAdmin")
    ->exclude('google-api-php-client-master')
    ->exclude('Download')
    ->exclude('Deploy')
    ->exclude('Upload')
    ->exclude('Public')
    ->exclude('Runtime')
    ->exclude("PHPExcel")
    ->exclude("Facebook")
    ->notPath(__DIR__  . "/Application/Portal/ORG/PHPExcel.class.php")
    ->name('*.php')
    ;

return PhpCsFixer\Config::create()
    ->setRules([
        '@PHP56Migration' => true,
        '@Symfony'                            => true,
        'array_syntax'                        => ['syntax' => 'short'],
        'combine_consecutive_unsets' => true,
        // one should use PHPUnit methods to set up expected exception instead of annotations
        'general_phpdoc_annotation_remove'    => ['expectedException', 'expectedExceptionMessage', 'expectedExceptionMessageRegExp'],
        'heredoc_to_nowdoc' => true,
        'no_extra_consecutive_blank_lines'    => ['break', 'continue', 'extra', 'return', 'throw', 'use', 'parenthesis_brace_block', 'square_brace_block', 'curly_brace_block'],
        'no_short_echo_tag' => true,
        'no_unreachable_default_argument_value' => true,
        'no_useless_else' => true,
        'no_useless_return' => true,
        'ordered_class_elements' => true,
        'ordered_imports' => true,
        'phpdoc_add_missing_param_annotation' => true,
        'no_trailing_comma_in_list_call'=>true,
        'phpdoc_order' => true,
        'semicolon_after_instruction' => true,
        'psr4'=>true,
        'strict_comparison' => false,
        'strict_param' => false,
        'binary_operator_spaces'=>['align_double_arrow' => true, 'align_equals' => true],
        'concat_space'=> ["spacing"=> "one"]
    ])
    ->setFinder($finder);

2.5 添加shell文件

在项目根目录下新建format.sh文件,内容如下:

#!/bin/bash
filepath=$(cd "$(dirname "$0")"; pwd)
cd $filepath

php-cs-fixer fix --config=format.php_cs.dist --allow-risky yes -vvv

2.6 测试

  • 在项目根目录右键执行Git Commit,检测代码格式化是否有执行,若执行成功,会在项目根目录生成.php_cs.cache文件,请将该文件加入.gitignore
  • 检查代码是否格式化成功

2.7 说明

如果你的项目已经添加了上述格式化部署,你只需要执行#1 #2.1 两个步骤就可以实现代码自动格式化的功能。

参考资料

相关文章

网友评论

      本文标题:TortoiseGit hook scripts 实现代码格式化

      本文链接:https://www.haomeiwen.com/subject/iygivxtx.html