ReSharper Code Cleanup from the Command Line

TL;DR; We can run ReSharper's Code Cleanup and Code Inspection from the command line. Here is how.

  1. Download the ReSharper Command Line Tools.
  2. Extract the tools.
  3. Enter the unzipped directory.
  4. View the list of executable files.

Here are the above steps in PowerShell:

PS> Expand-Archive JetBrains.ReSharper.CommandLineTools.2017.3.1.zip  
PS> cd JetBrains.ReSharper.CommandLineTools.2017.3.1  
PS> dir *exe -Name  
clang-tidy.exe  
cleanupcode.exe  
cleanupcode.x86.exe  
CsLex.exe  
dupfinder.exe  
ErrorsGen.exe  
inspectcode.exe  
inspectcode.x86.exe  
JetBrains.Platform.Satellite.exe  
JetLauncher32.exe  

The names are self-documenting. Before we use them, it's convenient to move all the command line tools to C:/resharper and to add that directory to the PATH.

PS> move * C:/resharperclt  
PS> [Environment]::SetEnvironmentVariable("Path", $env:Path + ";C:\resharperclt", [System.EnvironmentVariableTarget]::Machine)  

Now lets take a look at using cleanupcode.exe. We can see the help with cleanupcode --help. We can run it most simply against an entire project file or solution; that said, using the include and exclude options lets us be more specific.

Here are a few examples:

Clean up all the *.cs files that are below the Controllers directory; exclude some of the auto-generated code such as designer files.

PS> cleanupcode MyApp.csproj --include="Controllers/**/*.cs" --exclude="**/*.designer.*"  

Cleanup a single file in a solution.

PS> cleanupcode MyApp.sln --include="MyLibrary/Program.cs"  

We can add that command to our build or to a version control hook (i.e. a Git hook) to automate certain style rules and thereby to speed up our development velocity.

One of the best parts is that the ReSharper command line tools are free. Suggestions? Comments? Find me on Twitter @shaunluttin.