美文网首页
Flutter(Dart)配置git hook,检查dart语言

Flutter(Dart)配置git hook,检查dart语言

作者: lee_zhou_iOS开发者 | 来源:发表于2021-03-12 19:24 被阅读0次

1. 安装dartfmt

官方推荐的dart代码规范检查包,dartfmt地址

dart pub global activate dart_style  //全局安装dartfmt

mark: 
* 若出现 # [pub global activate command - $HOME/.pub-cache/bin not on path](https://stackoverflow.com/questions/52794266/pub-global-activate-command-home-pub-cache-bin-not-on-path)
错误,
* 请按照提示修改 ~/.bash_profile文件
* 修改后请使用 source ~/.bash_profile指令,让修改生效

2. 配置git hook

1.  cd .git/hook/     //进入项目的git目录下的hook,若没创建git,请先给项目创建git

2. touch pre-commit  //创建pre-commit文件

3. vim pre-commit  //编辑pre-commit
  3.1 我们在 pre-commit里面添加脚本
   (此处我要求git commit的时候,自动格式化flutter/lib下的代码。 目录可按照自己项目按需修改)
    -w参数会格式化代码后覆盖之前的代码。
    详细的dartfmt参数请参考dartfmt(https://github.com/dart-lang/dart_style)
    #!/bin/sh
    #format dart code
    exec dartfmt -w flutter/lib/ 

4. 给脚本添加执行权限
chmod +x pre-commit

3. 尝试修改项目,并commit代码

git commit -m 'xxxx'   //我们发现执行这句会触发pre-commit脚本,完成对代码的格式调整

相关文章

网友评论

      本文标题:Flutter(Dart)配置git hook,检查dart语言

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