Skip to content
Pavel Bansky edited this page Jan 8, 2018 · 14 revisions

DevSkim functionality can be used or extended via .NET library. The very same library is used in Command Line Interface as well as in Visual Studio Extension.

NuGet Package

DevSkim library is available on NuGet Microsoft.DevSkim

Visual Studio Package Manager

PM> Install-Package Microsoft.DevSkim

Dotnet CLI

dotnet add package Microsoft.DevSkim 

Add to .csproj file

<ItemGroup>
   <PackageReference Include="Microsoft.DevSkim" Version="0.3.6" />
</ItemGroup>

Usage Workflow

DevSkim library consists of two main classes, Ruleset collection which loads and contains rules and RuleProcessor which applies given rule set to a text content and returns results.

Preparing Ruleset

The Ruleset can be created from a directory, file or free form text.

// verbose way
RuleSet rules = new RuleSet();
rules.AddDirectory("/home/user/rules);

// faster way
RuleSet fastRules = RuleSet.FromDirectory("/home/other/rules);

Applying Ruleset

// Load the rules
RuleSet rules = RuleSet.FromDirectory("/home/user/rules);

// Create RuleProcessor and pass the ruleset
RuleProcessor processor = new RuleProcessor(rules);

// Get content for analysis
string content = File.ReadAllText("main.cpp");

// Analyze content, using rules for C++
Issue[] issues = processor.Analyze(content, "cpp"); 

// Analyze content, using rules for C and C++
issues = processor.Analyze(content, new string[] { "c", "cpp"}); 

Auto detecting the language

The programing language of a content file can be auto detected from filename using Language class

string fileName = "main.cpp";

// Detect language
string language = Language.FromFileName(fileName);

string content = File.ReadAllText("main.cpp");

// Analyze content, using detected language
Issue[] issues = processor.Analyze(content, language); 
Clone this wiki locally