1. 安裝dartfmt
官方推薦的dart代碼規(guī)范檢查包没卸,dartfmt地址
dart pub global activate dart_style //全局安裝dartfmt
mark:
* 若出現(xiàn) # [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办悟,若沒創(chuàng)建git尘奏,請先給項目創(chuàng)建git
2. touch pre-commit //創(chuàng)建pre-commit文件
3. vim pre-commit //編輯pre-commit
3.1 我們在 pre-commit里面添加腳本
(此處我要求git commit的時候,自動格式化flutter/lib下的代碼病蛉。 目錄可按照自己項目按需修改)
-w參數(shù)會格式化代碼后覆蓋之前的代碼炫加。
詳細的dartfmt參數(shù)請參考dartfmt(https://github.com/dart-lang/dart_style)
#!/bin/sh
#format dart code
exec dartfmt -w flutter/lib/
4. 給腳本添加執(zhí)行權(quán)限
chmod +x pre-commit
3. 嘗試修改項目,并commit代碼
git commit -m 'xxxx' //我們發(fā)現(xiàn)執(zhí)行這句會觸發(fā)pre-commit腳本铺然,完成對代碼的格式調(diào)整