Coder Social home page Coder Social logo

jonasblunck / ccm Goto Github PK

View Code? Open in Web Editor NEW
52.0 7.0 13.0 1.32 MB

Compute code complexity for c, c++, c#, javascript, typescript and PowerShell.

License: MIT License

C# 89.88% C++ 6.91% TypeScript 0.19% C 1.81% JavaScript 0.49% PowerShell 0.71%

ccm's Introduction

CCM

Overview

CCM is a tool that analyzes c, c++, c#, javascript, TypeScript and PowerShell code and reports back with cyclomatic complexity metric.

Command line usage

To use CCM.exe, simply use one of the two modes for invocation:

  • CCM.exe <path-to-config-file>
  • CCM.exe <folder-path-to-analyze> [/xml] [/ignorecases] [/threshold=5] [/nummetrics=10]

Arguments

   <path-to-config-file>    Path to configuration file. See structure of file below. 
                            This option gives the best control over how ccm behaves.
   <folder-path-to-analyze> Path to directory which ccm will analyze.
   /xml                     Output results as XML (this parameter only valid if a config file is not passed in).
   /ignorecases             Don't count each case in switch block as a branching point (only valid if config file is not passed in).
   /threshold=5             Ignore units with metrics lower than assigned value.
   /nummetrics=10           Only report top 10 metrics (see numMetrics element in configuration file)

Configuration file

Below is an example of a configuration file.

 <ccm>
    <exclude>
     <file>myfile.cpp</file>
     <folder>myfolder</folder>
    </exclude>
    <analyze>
      <folder>..\..\code</folder>
   </analyze>
   <recursive>yes</recursive>
   <outputter>XML</outputter>
   <suppressMethodSignatures>yes</suppressMethodSignatures>
   <switchStatementBehavior>IgnoreCases</switchStatementBehavior>
   <numMetrics>30</numMetrics>
   <threshold>6</threshold>
   <fileExtensions>
     <fileExtension>.cxx</fileExtension>
   </fileExtensions>
 </ccm>
  • <exclude> element can be used to exclude files and/or folders from analysis.
  • <analyze> element specified which folders to analyze. All paths in the <folder> element is relative to the location of the configuration file.
  • <recursive> element tells CCM to traverse folders or not.
  • <outputter> element tells CCM how to output the data. Valid values are 'XML', 'Tabbed', 'Text', 'CSV'
  • <suppressMethodSignatures> set to 'yes' and CCM will only print the name of the method and not the full signature.
  • <switchStatementBehavior> set to 'IgnoreCases' and CCM will not count each case statement in switch blocks as a branching point.
  • <numMetrics> tells CCM how many metrics that should be reported. Only the top x functions will be reported.
  • <threshold> tells CCM to ignore units with a complexity less than configured value.
  • <fileExtensions> can be used to add additional file extensions for analysis. Per default, these are included: .h, .cpp, .c, .hpp, .cs, .js, .ts, .psm1 and .ps1

Example output

Below is example output from the Text outputter (can be contolled in the <outputter> element in the config file).

Driver::HandleDirectory(string basePath,string path) : 7 - simple, without much risk (\Driver.cs@line 141)
Driver::IsValidFile(string filename) : 6 - simple, without much risk (\Driver.cs@line 84)
Program::CreateConfigurationFromArgs(string [ ] args) : 6 - simple, without much risk (\Program.cs@line 71)
Driver::PathShouldBeExcluded(string path) : 5 - simple, without much risk (\Driver.cs@line 109)
XmlOutputter::Output(List<ccMetric>metrics,List<ErrorInfo>errors,bool verbose) : 5 - simple, without much risk (\XmlOutputter.cs@line 17)
ConsoleOutputter::Output(List<ccMetric>metrics,List<ErrorInfo>errors,bool verbose) : 4 - simple, without much risk (\ConsoleOutputter.cs@line 12)
Driver::AnalyzeFilestream(object context) : 4 - simple, without much risk (\Driver.cs@line 47)
TabbedOutputter::Output(List<ccMetric>metrics,List<ErrorInfo>errors,bool verbose) : 4 - simple, without much risk (\TabbedOutputter.cs@line 12)
Program::OutputterFactory(string outputType) : 3 - simple, without much risk (\Program.cs@line 60)
Program::ValidateArgs(string [ ] args) : 2 - simple, without much risk (\Program.cs@line 15)
Program::LoadConfiguration(string [ ] args) : 2 - simple, without much risk (\Program.cs@line 97)
Program::Main(string [ ] args) : 2 - simple, without much risk (\Program.cs@line 122)

Platforms

CCM is built in C#, targetting .NET Core 3.1 and as such runs on Windows, Mac and other platform supported by .NET Core.

Building the code

  1. Open ccm.sln in Visual Studio 2022
  2. Build
  3. Run unit tests (optional)

Running the tool on Mac OS X

Different options

  1. ./bin/Release/net6.0/ccm (this will display the help)
  2. ./bin/Release/net6.0/ccm ./bin/ccm.config (will run based on the ccm.config file)
  3. ./bin/Release/net6.0/ccm ./source (run the analyzer for the source folder)

Running the tool on Windows

Different options

  1. .\bin\Release\net6.0\CCM.exe (this will display the help)
  2. .\bin\Release\net6.0\CCM.exe .\bin\ccm.config (will run based on the ccm.config file)
  3. .\bin\Release\net6.0\CCM.exe .\source (run the analyzer for the source folder)

Integration into build pipelines

CCM can easily be integrated into build pipelines by using the xml output option. Example:

$output = ./bin/Release/netcoreapp3.1/ccm <path-to-analyze> /xml /threshold=11
$xml = [xml]($output)
$xml.ccm.metric | %{  
   Write-Error "$($_.unit) has complexity $($_.complexity) and is classified as '$($_.classification)'"
}

ccm's People

Contributors

jonasblunck avatar mydeveloperday avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

ccm's Issues

make numMetrics available outside configuration file

I want to see all metrics, not only the worst 30 items.
With a configuration file one could use a high enough number for numMetrics (how about "*"). But outside the configuration file, this cannot be changed. Please make it available outside the configuration file.

file exclusions in ccm.cofig does not take effect

Using the following ccm.config file:

<ccm>
  <exclude>
    <file>site.min.js</file>
    <folder>lib</folder>
  </exclude>
  <fileExtensions>
    <fileExtension>.ts</fileExtension>
  </fileExtensions>
  <analyze>
    <folder>..\Web\wwwroot</folder>
  </analyze>
  <recursive>yes</recursive>
  <outputter>XML</outputter>
  <suppressMethodSignatures>yes</suppressMethodSignatures>
  <switchStatementBehavior>TraditionalInclude</switchStatementBehavior>
  <numMetrics>1000</numMetrics>
  <threshold>1</threshold>
</ccm>

I got the following report:

<ccm>
  <metric>
    <complexity>1</complexity>
    <unit>Math::add(a,b)</unit>
    <classification>simple, without much risk</classification>
    <file>..\Web\wwwroot\js\math.ts</file>
    <startLineNumber>3</startLineNumber>
    <endLineNumber>5</endLineNumber>
    <SLOC>2</SLOC>
  </metric>
  <metric>
    <complexity>1</complexity>
    <unit>Math()</unit>
    <classification>simple, without much risk</classification>
    <file>..\Web\wwwroot\js\math.js</file>
    <startLineNumber>8</startLineNumber>
    <endLineNumber>9</endLineNumber>
    <SLOC>1</SLOC>
  </metric>
  <metric>
    <complexity>1</complexity>
    <unit>it(&quot;for two positive numbers the result is a positive number&quot;,function()</unit>
    <classification>simple, without much risk</classification>
    <file>..\Web\wwwroot\tests\math.spec.js</file>
    <startLineNumber>2</startLineNumber>
    <endLineNumber>6</endLineNumber>
    <SLOC>4</SLOC>
  </metric>
  <metric>
    <complexity>1</complexity>
    <unit>describe(&quot;Use of Add function&quot;,function()</unit>
    <classification>simple, without much risk</classification>
    <file>..\Web\wwwroot\tests\math.spec.js</file>
    <startLineNumber>1</startLineNumber>
    <endLineNumber>7</endLineNumber>
    <SLOC>6</SLOC>
  </metric>
  <metric>
    <complexity>1</complexity>
    <unit>Math.prototype.add(a,b)</unit>
    <classification>simple, without much risk</classification>
    <file>..\Web\wwwroot\js\math.js</file>
    <startLineNumber>10</startLineNumber>
    <endLineNumber>12</endLineNumber>
    <SLOC>2</SLOC>
  </metric>
  <metric>
    <complexity>1</complexity>
    <unit>Math::substract(a,b)</unit>
    <classification>simple, without much risk</classification>
    <file>..\Web\wwwroot\js\math.ts</file>
    <startLineNumber>7</startLineNumber>
    <endLineNumber>9</endLineNumber>
    <SLOC>2</SLOC>
  </metric>
  <metric>
    <complexity>1</complexity>
    <unit>Math.prototype.substract(a,b)</unit>
    <classification>simple, without much risk</classification>
    <file>..\Web\wwwroot\js\math.js</file>
    <startLineNumber>13</startLineNumber>
    <endLineNumber>15</endLineNumber>
    <SLOC>2</SLOC>
  </metric>
  <metric>
    <complexity>1</complexity>
    <unit>=(function()</unit>
    <classification>simple, without much risk</classification>
    <file>..\Web\wwwroot\js\math.js</file>
    <startLineNumber>7</startLineNumber>
    <endLineNumber>17</endLineNumber>
    <SLOC>10</SLOC>
  </metric>
  <metric>
    <complexity>1</complexity>
    <unit>;(function(Web)</unit>
    <classification>simple, without much risk</classification>
    <file>..\Web\wwwroot\js\math.js</file>
    <startLineNumber>6</startLineNumber>
    <endLineNumber>19</endLineNumber>
    <SLOC>13</SLOC>
  </metric>
  <metric>
    <complexity>1</complexity>
    <unit>;(function(ProductConfiguration)</unit>
    <classification>simple, without much risk</classification>
    <file>..\Web\wwwroot\js\math.js</file>
    <startLineNumber>4</startLineNumber>
    <endLineNumber>20</endLineNumber>
    <SLOC>16</SLOC>
  </metric>
  <metric>
    <complexity>1</complexity>
    <unit>;(function(Project)</unit>
    <classification>simple, without much risk</classification>
    <file>..\Web\wwwroot\js\math.js</file>
    <startLineNumber>2</startLineNumber>
    <endLineNumber>21</endLineNumber>
    <SLOC>19</SLOC>
  </metric>
</ccm>

I want to have the complexity only of typescript files.

Absolute path of file is more preffered, to make it working under jenkins/CI

As far as I could see file path that is saved inside of file is relative for analyzed folder.
This creates problems if analyzis started for non-root folder.
For example we have:
trunc
-ProjectFolder
-Tests
-Backups
we started ccm for For C:\jenkins\trunk-job\trunc\ProjectFolder (using xml config)
found files will be reported based in C:\jenkins\trunk-job\trunc\ProjectFolder.
For example \scripts\MainApp.js.

This creates problems for Jenkins plugins (like https://wiki.jenkins-ci.org/display/JENKINS/CCM+Plugin)
which could not display fragment of code that was affected.

Of course it could be added an option for this plugin as root folder (which is now missing) but for me it feels natural to have absolute path. Or at least option to control what path we need.

As a workaround for now I run msbuild post task that replace on value from cmm.config.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.