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:

CleanupCode.exe YouTrackSharp.sln

The tool will scan all files in our solution and apply code formatting rules specified in the Full Cleanup profile:

Code cleanup command line - No parameters

Tip: after running CleanupCode.exe, a 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 settingsCleanupCode.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.

Custom code cleanup profile in ReSharper

Once saved, we can run CleanupCode.exe --profile=YouTrackSharp YouTrackSharp.sln, telling it to use our custom code cleanup profile instead.

Use a custom code cleanup profile

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 .DotSettings other than the solution-shared one.
  • --profile – Run a specific code cleanup profile.
  • --include/--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 CleanupCode.exe --help.

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!

This entry was posted in How-To's and tagged , , , , , . Bookmark the permalink.

13 Responses to Code Cleanup with the ReSharper Command Line Tools

  1. Robin says:

    I absolutely love that you are enhancing the command line tools for Reshaper! But please please make them Mac OS or Linux compatible! We are literally running a Windows slave only for running Reshaper inspections.. Rider uses the Reshaper backend on Mac. Or add a command line interface to Rider while working on a standalone solution.

    Thank you for your great products!

    • Thanks for the great feedback!

    • Thomas says:

      I can only second the request for Linux and Mac OS support. Linux being my personal focus 😀
      I hear from my colleagues thar has talked to jetbrains developers at a conference that you have an internal version that can actually do this. Care to elaborate 😀

  2. Urs Meili says:

    Great – I’ve waited for that feature for years :-)

  3. David De Sloovere says:

    What other download options are there for these tools? Manual download is not the best option when integrating in an automated process.
    I’ve found packages on the Chocolatey repository, I hope these are official and get updated with every new release.

  4. Michael Chadwick says:

    Does this tool work on C++ projects?

    • This only works on C# and Visual Basic .NET code for now.

      • Tim Finer says:

        Why does the page for Resharper C++ say that it does then?

        https://www.jetbrains.com/resharper-cpp/whatsnew/#v2018-1-clang-tidy-integration-updates

        Command-line code cleanup
        ReSharper Command Line Tools, a free standalone collection of tools that can be used from the command line or as a part of the continuous integration process, now lets you perform code cleanup in C++ projects. Run cleanupcode.x86.exe on your solution, and it will automatically reformat your code, remove common redundancies, apply Clang-Tidy fix-its and common code style fixes. This instantly eliminates code style violations in a project or a solution, and ensures a uniform code base.

        In related news, bogus errors which were output by the InspectCode tool on x64 projects were fixed in this release. In addition, TeamCity 2018.1 is able to run InspectCode on C++ projects (see TW-48978 for details).

        To learn more, see Code Cleanup with the ReSharper Command Line Tools.

  5. Igor says:

    For some reason CodeCleanup does not add explicit type modifiers, does not apply var style and don’t use auto-property. I tried default Full Cleanup profile and custom profile. Am I doing something wrong or CodeCleanup tool not intended for this?

  6. Pingback: What’s New in ReSharper C++ 2018.1 - ReSharper C++ BlogReSharper C++ Blog

Leave a Reply

Your email address will not be published. Required fields are marked *