Code Cleanup with the ReSharper Command Line Tools
Along with the latest releases of ReSharper, we have made Code Cleanup part of the ReSharper Command Line Tools. These tools allow us to run several ReSharper features from the command line or as part of continuous integration. Two tools have been there for a while — InspectCode (which can run hundreds of ReSharper code inspections outside of Visual Studio) and dupFinder (which helps finding duplicate code in C# and VB.NET code).
The new Code Cleanup command line will apply formatting code style preferences and other improvements such as file layout, removing redundancies, optimizing using, … in bulk mode. This instantly eliminates code style violations in a project or solution, and ensures a uniform code base.
Getting started with the Code Cleanup command line tool
First, we will have to download the ReSharper Command Line Tools that contain Code Cleanup. Note that these are free and do not require us to have the full ReSharper installed (except for creating custom code cleanup profiles).
After downloading and unzipping the command line tools, either on a local machine or a CI server, we can run
CleanupCode.exe to automatically apply code formatting and code style preferences.
Since the tool can target a project or solution, let’s dive in at once and run it against the latest YouTrackSharp solution:
The tool will scan all files in our solution and apply code formatting rules specified in the Full Cleanup profile:
Tip: after running
git diff will reveal any changes that were made by the tool.
Using a custom code cleanup profile
Out of the box, ReSharper ships with two default code cleanup profiles: Reformat Code (only applies code formatting rules) and Full Cleanup (which applies all available cleanup tasks (except adding a file header).
When no additional settings are specified or no custom cleanup profile is found in the solution shared settings,
CleanupCode.exe will make use of ReSharper’s Full Cleanup profile.
To run a custom set of tasks, we’ll have to create a custom code cleanup profile from the ReSharper | Options menu, under Code Editing | Code Cleanup and specify the set of desired tasks there. We can start with a Duplicate of an existing profile, and then start customizing. To share this custom code cleanup profile with members of our team (and the
CleanupCode.exe tool), we will have to save the settings to the team-shared settings. This will generate a
<SolutionName>.sln.DotSettings file for us that can be added to source control.
Once saved, we can run
CleanupCode.exe --profile=YouTrackSharp YouTrackSharp.sln, telling it to use our custom code cleanup profile instead.
Once completed, the Code Cleanup tool will have updated code in our solution based on our custom preferences!
There are a number of other command line options we can specify. Here are a few that might be of use:
--settings– Use a
.DotSettingsother than the solution-shared one.
--profile– Run a specific code cleanup profile.
--exclude– Relative path(s) that define the files to include/exclude during clean up.
--disable-settings-layers– Don’t use other settings layers from ReSharper.
- Several options related to MSBuild, cache folders, … Details are available from
It’s possible to generate a configuration file in which these options are specified. Using the
CleanupCode.exe --config-create=config.xml, an empty configuration file is generated. After customizing it, we can use it with
CleanupCode.exe --config=config.xml instead of having to pass these options on the command line.
Where to use Code Cleanup?
We’ve seen we can use the new Code Cleanup command line tool to apply formatting and other code style preferences to our code base. They are especially handy when working in mixed teams.
Imagine some developers are using ReSharper or Rider to enforce these settings, and others are not. Or an open source project where different contributors have different environments. The command line tools can be run as part of a continuous integration pipeline, or perhaps as a git precommit hook, to ensure every team member commits code that adheres to code style preferences.
We could use
CleanupCode.exe on the build server to reformat code from a pull request before merging it into the master branch. Or perhaps run the tool and check if any changes were made – failing the build if there is a diff (and thus code style settings have not been followed).
Give the ReSharper Command Line Tools a try!