Page Comparison - Introducing dupFinder (v.23 vs v.24)

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Redirect
locationhttp://www.jetbrains.com/resharperplatform/help?Keyword=dupFinder

Table of Contents

dupFinder is a free command line tool that finds duplicates in C# and Visual Basic .NET code - no more, no less. But being a JetBrains tool, dupFinder does it in a smart way. By default, it considers code fragments as duplicates not only if they are identical, but also if they are structurally similar, even if they contain different variables, fields, methods, types or literals. Of course, you can configure the allowed similarity as well as the minimum relative size of duplicated fragments.

Running Duplicate Analysis

Note
iconfalse

Wiki Markup
{div:class=text quote}...get it running with your custom CI, version control, quality control or any other server...{div}

dupFinder is not exactly a new kid on the block. For quite a while, JetBrains TeamCity has included it out of the box, and this is probably the easiest and the most efficient way to make use of dupFinder. However, from now on you can get it running with your custom CI, version control, quality control or any other server and here is how: download and unzip the command line tools package, and then run

Code Block

dupFinder [OPTIONS] source

...

Practical Use

Note
iconfalse

Wiki Markup
{div:class=text quote}...see how to start duplicate analysis using an MSBuild target...{div}

We are now ready to have some practice with dupFinder. In the steps described below we’ll take a solution, e.g. SolutionWithDuplicates.sln and see how to start duplicate analysis using an MSBuild target with a simple HTML report based on the dupFinder output.

...

Now let’s think ahead to processing the dupFinder output. If we leverage the /show-text option, we’ll be able to build an HTML report by applying an XSL transformation to the dupFinder XML output; something like this will do:

Code Block

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="html" indent="yes" />
    <xsl:template match="/">
        <html>
            <body>
                <h1>Statistics</h1>
                <p>Total codebase size: <xsl:value-of select="//CodebaseCost"/></p>
                <p>Code to analyze: <xsl:value-of select="//TotalDuplicatesCost"/></p>
                <p>Total size of duplicated fragments: <xsl:value-of select="//CodebaseCost" /></p>
                <h1>Detected Duplicates</h1>
                <xsl:for-each select="//Duplicates/Duplicate">
                    <h2>Duplicated Code. Size: <xsl:value-of  select="@Cost"/></h2>
                    <h3>Duplicated Fragments:</h3>
                    <xsl:for-each select="Fragment">
                        <xsl:variable name="i" select="position()"/>
                        <p>Fragment <xsl:value-of select="$i"/>  in file <xsl:value-of select="FileName"/></p>
                        <pre><xsl:value-of select="Text"/></pre>
                    </xsl:for-each>
                </xsl:for-each>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

...

The simplest way to run duplicate analysis and the following transformation is specify a new MSBuild target. We are now in the solution directory, so we go into one of its project subdirectories and open the project file (*.csproj) with a text editor, then add the following element into the root <Project> node:

Code Block

<Target Name="AfterBuild">
  <Exec
    WorkingDirectory=".."
    Command="C:\programs\CLT\dupfinder.exe /output=&quot;dupReport.xml&quot; /show-text &quot;SolutionWithDuplicates.sln&quot;"/>
  <XslTransformation XslInputPath="C:\programs\CLT\dupFinder.xsl" XmlInputPaths="..\dupReport.xml" OutputPaths="..\dupReport.html"/>
</Target>

...