Zaoszczędź swój czas, niech kod wyczyści się sam – ReSharper CLI CleanupCode GitHub Action
Action in Marketplace: ReSharper CLI CleanupCode
Action in Repository: ArturWincenciak/ReSharper_CleanupCode@v2.0
- Fork this repo
- Create a Pull Request
- Go to
Actions
and observe the action in action - Check out history of your repo and see newly created commit
That is a project uses a GitHub Action that allows you to run ReSharper's CleanupCode Command-Line Tool in order to automatically apply code style rules and reformat code in a project. The action is triggered on a push event, and it cleans up the code using the specified profile, and commits the changes with a specified commit message.
Note that this code includes an example class DemoClass
that breaks some style rules and a unit test class DemoUnitTests
,
which also breaks rules but is used to demonstrate how the action can be configured to exclude certain types of code from being
cleaned up.
Usage cleanup_code.yml
name: ReSharper CLI CleanupCode
on: [ push ]
jobs:
cleanup:
runs-on: ubuntu-latest
name: Cleanup Code
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: 7.0.x
- name: Restore Dependencies
run: dotnet restore ReSharperCleanupCodeDemo.sln
- name: Cleanup Code
id: cleanup
uses: ArturWincenciak/ReSharper_CleanupCode@v4.14
with:
solution: 'ReSharperCleanupCodeDemo.sln'
fail_on_reformat_needed: 'no'
jb_cleanup_code_arg: '--verbosity=INFO --profile=Built-in: Full Cleanup --exclude=**UnitTests/**.*'
Checkout
: download the source code from the current repository where the Action was initiatedSetup .NET
: install the specified version of .NET on the virtual machine where the Action is runRestore Dependencies
: restore all project dependencies, such as NuGet librariesCleanup Code
: clean up the code action
The last step, named Cleanup Code
, is the one that is most relevant to us here.
Using action input jb_cleanup_code_arg
for instance like down below
you can configure CleanupCode with command-line parameters as it is described
here in clear and concise specification.
jb_cleanup_code_arg: '--verbosity=INFO --profile=Almost Full Cleanup --exclude=**UnitTests/**.*'
Here has been used --exclude
parameter to exclude unit test code from being cleaned up and --profile
to choose
on of my custom configuration stored in team shared ReSharperCleanupCodeDemo.sln.DotSettings
settings.
steps:
- name: Cleanup Code
uses: ArturWincenciak/ReSharper_CleanupCode@v2.0
with:
solution: 'ReSharperCleanupCodeDemo.sln'
To learn more, please visit specification in the marketplace.
All your settings should be saved in Solution Team-Shared Layer you can find this project here in that file ReSharperCleanupCodeDemo.sln.DotSettings.
Read more here:
I suggest exporting all settings, including default settings, to a .editorconfig file. It is important to explicitly save all default settings to avoid potential issues in the future caused by changes in default settings in newer versions of the command-line tool software. It is important to explicitly save all default settings in a transparent manner.
Read more here:
As you can see in the cleanup_code.yaml file:
jb_cleanup_code_arg: '--verbosity=INFO --profile=Almost Full Cleanup --exclude=**UnitTests/**.*'
one of the settings is the --profile
flag. I have set my profile to be named Almost Full Cleanup
.
I have configured my profile to exclude *.md
files and the .editorconfig
file from the cleaning up.
Read more here:
My current favorite setting ReSharperCleanupCodeDemo.sln.DotSettings looks like that:
In this repository I have prepared a ready-made script local-dev-cleanup-code.sh
that you can run locally.
That script will perform clean up code and create a commit with the changes in your local repository.
This is an additional feature that allows you to push the commit to the remote repository by your self and avoiding the
need to pull
automatically created commit on the remote repository. This can save you the hassle of resolving conflicts
in case you have made changes to the same file locally in the meantime.
This will save your time from having to enter a commit message each time. The commit with the changes will be created automatically.
Feel free to use the script and perform clean up your code locally with a fully automated commit and save your time.
This script can be attached to the git hooks, however, attaching this script to the
pre-commit
git hook is not advisable as it may slow down our work considerably due to the lengthy clean up code process. It may be more beneficial to add this script to thepre-push
git hook instead.
First of all remember to add the required manifest file and then run that commands:
dotnet tool restore
dotnet tool update --global JetBrains.ReSharper.GlobalTools
source .bashrc
cc
cc -a no
There are situations where Cleanup Code does not do the entire job for us, but we can still greatly help ourselves and speed up ours Code Review process by adding an additional automatic step that performs an inspection of the code and, adds a comments to the submitted Pull Request on our behalf.
Down below, I demonstrate how to combine ReSharper CLI CleanupCode
and ReSharper CLI InspectCode using
GitHub Action
that contains two jobs: cleanup
and inspection
.
- Marketplace: ReSharper CLI CleanupCode
- that one I've created by my self
- Marketplace: ReSharper CLI InspectCode
- that one I've found in the Marketplace and used it here, it was an inspiration for me to create my own action up above
name: ReSharper CLI CleanupCode
on: [ push ]
jobs:
cleanup:
runs-on: ubuntu-latest
name: Cleanup Code
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: 7.0.x
- name: Restore Dependencies
run: dotnet restore ReSharperCleanupCodeDemo.sln
- name: Cleanup Code
id: cleanup
uses: ArturWincenciak/ReSharper_CleanupCode@v2.0
with:
solution: 'ReSharperCleanupCodeDemo.sln'
fail_on_reformat_needed: 'no'
jb_cleanup_code_arg: '--verbosity=INFO --profile=Almost Full Cleanup --exclude=**UnitTests/**.*'
inspection:
runs-on: ubuntu-latest
name: Inspect Code
needs: cleanup
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: 7.0.x
- name: Restore Dependencies
run: dotnet restore ReSharperCleanupCodeDemo.sln
- name: Inspect code
uses: muno92/resharper_inspectcode@1.6.5
with:
solutionPath: ./ReSharperCleanupCodeDemo.sln
failOnIssue: 1
minimumSeverity: notice
solutionWideAnalysis: true
Let's look at the following screenshots to see how the jobs have been configured and utilized in this project.