推广自己的网站需要怎么做,网上建设银行网站首页,网站建设刂搜金手指下拉贰伍,wordpress外贸网店主题是不是大家也会觉得代码审查里面审查代码格式化问题是无意义的#xff0c;但是不审查又觉得过不去#xff1f;是否有个专门的工具人#xff0c;用来协助修复代码格式化的问题#xff1f;本文来安利大家一个特别好用的方法#xff0c;使用 dotnet 完全开源的专业格式化工具… 是不是大家也会觉得代码审查里面审查代码格式化问题是无意义的但是不审查又觉得过不去是否有个专门的工具人用来协助修复代码格式化的问题本文来安利大家一个特别好用的方法使用 dotnet 完全开源的专业格式化工具 dotnet format 配合 GitHub 的自动构建 Action 做的自动代码格式化机器人这个机器人可以被指定到特定时机如每天晚上或者每次代码合并等进行代码格式化格式化完成之后可以选择直接推送或者提代码审查这个方法将需要用到 dotnet 完全开源的专业格式化工具 dotnet format 工具请看 https://github.com/dotnet/format用法十分简单可以复制本文最后的 GitHub 的自动构建 Action 的脚本放在仓库的 .github\workflows 文件夹里面。现在请让我告诉大家这个构建脚本的细节在 .github\workflows 文件夹里面创建的所有 yml 文件都会当成构建脚本每个脚本就应该给定一个名字如下面代码name: Daily code format check
然后设置构建脚本的触发时机如下面代码设置了在推送了 master 分支时触发构建脚本on: push:branches: - master
其他触发时机等还请大家去阅读官方文档下一步是指定运行在什么设备上如下面代码jobs:dotnet-format:runs-on: windows-latest
接下来就是将代码拉下来了可以通过如下代码将当前分支的最新代码拉下来 steps:- name: Checkout repouses: actions/checkoutv2with:ref: $
本文的格式化方法是使用 dotnet format 工具格式化的在使用这个工具之前需要先安装请使用如下代码进行安装 - name: Install dotnet-formatrun: dotnet tool install -g dotnet-format
原本可以使用一句命令 dotnet format 就进行格式化但是当前遇到的问题是如果代码格式化没有任何文件更改那么此时就不应该做创建新的分支和开启代码审查了因此就需要用到 jfversluis 大佬的 dotnet-format 脚本。这个脚本可以输出参数用于在后续步骤判断如果没有文件更改也就是没有代码需要格式化就不需要开启代码审查了 - name: Run dotnet formatid: formatuses: jfversluis/dotnet-formatv1.0.5with:repo-token: $action: fixonly-changed-files: true # only works for PRs# workspace: Xamarin.Forms.sln 默认根路径只有一个 sln 文件可以忽略这一行
如果自己的仓库里面的根路径也就是放在和 .git 文件夹所在的相同的文件夹存在了一个 sln 文件那么可以忽略 workspace 参数调用了上面代码脚本之后将会输出可以使用如下代码判断是否有文件更改if: steps.format.outputs.has-changes true
接下来是 commit 代码如果代码文件有更改的话 - name: Commit filesif: steps.format.outputs.has-changes true # 如果有格式化才继续# 下面将使用机器人的账号你可以替换为你自己的账号run: |git config --local user.name github-actions-dotnet-formatter[bot]git config --local user.email 41898282github-actions[bot]users.noreply.github.comgit commit -a -m Automated dotnet-format update
上面代码的邮件等是 GitHub 机器人的账号你可以替换为你的账号最后一步是开启代码审查然后指定代码审查者 - name: Create Pull Requestif: steps.format.outputs.has-changes true # 如果有格式化才继续uses: peter-evans/create-pull-requestv3with:title: [Bot] Automated PR to fix formatting errorsbody: |Automated PR to fix formatting errorscommitter: GitHub noreplygithub.comauthor: github-actions[bot] 41898282github-actions[bot]users.noreply.github.com# 以下是给定代码审查者需要设置仓库有权限的开发者assignees: lindexi,walterlvreviewers: lindexi,walterlv# 对应的上传分支branch: t/bot/fix-codeformatting
这样就能完成了在开发者将代码合并或推送到主分支的时候自动尝试代码格式化如果代码格式化有文件更改了那么开启一个代码审查如下图感谢 jfversluis 大佬的 dotnet-format 脚本和 Peter Evans 的创建代码审查的 create-pull-request 脚本我比较推荐使用这个方法尽管 dotnet format 工具是专业的代码格式化工具不会让格式化前后的代码的 IL 有变更。但是我依然推荐进行一次代码审查其实不使用 jfversluis 大佬的脚本也可以因为 Peter Evans 的创建代码审查的 create-pull-request 脚本会自动判断如果没有 commit 就不创建代码审查因此只需要跳过 commit 的失败就可以了如下面代码 - name: Install dotnet-formatrun: dotnet tool install -g dotnet-format- name: Run dotnet formatrun: dotnet format- name: Commit files# 下面将使用机器人的账号你可以替换为你自己的账号run: |git config --local user.name github-actions-dotnet-formatter[bot]git config --local user.email 41898282github-actions[bot]users.noreply.github.comgit commit -a -m Automated dotnet-format updatecontinue-on-error: true
可以看到代码十分简洁而另外的方法是在每个开发者开启代码审查的时候尝试格式化他的代码这样可以让代码审查者也许会更开森代码十分简单请看下面name: Format check on pull request
on: pull_request
jobs:dotnet-format:runs-on: windows-lateststeps:- name: Install dotnet-formatrun: dotnet tool install -g dotnet-format- name: Checkout repouses: actions/checkoutv2with:ref: $- name: Run dotnet formatid: formatuses: jfversluis/dotnet-formatv1.0.5with:repo-token: $action: fixonly-changed-files: true- name: Commit filesif: steps.format.outputs.has-changes truerun: |git config --local user.name github-actions[bot]git config --local user.email 41898282github-actions[bot]users.noreply.github.comgit commit -a -m Automated dotnet-format updateCo-authored-by: $ $$users.noreply.github.com- name: Push changesif: steps.format.outputs.has-changes trueuses: ad-m/github-push-actionv0.5.0with:github_token: $branch: $
但是这个方法也许会让开发者不开森因为他下一次上传代码的时候需要先拉代码也许因为格式化给他了额外的改动。另外的如 Xamarin 仓库的注释其实代码推送无法用在 fork 的仓库上也就是说如果这个代码审查是另一个开发者在他 fork 的仓库里面发起的此时的这个方法将会失效我现在在 dotnetCampus.Ipc 就接入这个自动代码格式化机器人用起来还不错构建脚本的全部代码请看下面name: Code format checkon: push:branches: - master
jobs:dotnet-format:runs-on: windows-lateststeps:- name: Checkout repouses: actions/checkoutv2with:ref: $- name: Install dotnet-formatrun: dotnet tool install -g dotnet-format- name: Run dotnet formatrun: dotnet format- name: Commit files# 下面将使用机器人的账号你可以替换为你自己的账号run: |git config --local user.name github-actions-dotnet-formatter[bot]git config --local user.email 41898282github-actions[bot]users.noreply.github.comgit commit -a -m Automated dotnet-format updatecontinue-on-error: true- name: Create Pull Request# if: steps.format.outputs.has-changes true # 如果有格式化才继续uses: peter-evans/create-pull-requestv3with:title: [Bot] Automated PR to fix formatting errorsbody: |Automated PR to fix formatting errorscommitter: GitHub noreplygithub.comauthor: github-actions[bot] 41898282github-actions[bot]users.noreply.github.com# 以下是给定代码审查者需要设置仓库有权限的开发者assignees: lindexi,walterlvreviewers: lindexi,walterlv# 对应的上传分支branch: t/bot/fix-codeformatting