Skip to content
Gabe Stocco edited this page Nov 2, 2023 · 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.CST.DevSkim and API documentation is automatically published on GitHub Pages.

Visual Studio Package Manager

PM> Install-Package Microsoft.CST.DevSkim

Dotnet CLI

dotnet add package Microsoft.CST.DevSkim 

Add to .csproj file

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

Usage Workflow

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

Preparing DevSkimRuleset

The DevSkimRuleset can be created from a directory, file or free form text or from the default embedded rules.

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

Applying Ruleset

// Load the rules
DevSkimRuleset rules = DevSkimRuleset.LoadDefaultRules()

// Create options
DevSkimRuleProcessorOptions opts = new DevSkimRuleProcessorOptions();

// Create DevSkimRuleProcessor and pass the ruleset
DevSkimRuleProcessor processor = new DevSkimRuleProcessor(rules, opts);

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

// Analyze content, using rules for C++
IEnumerable<Issue> issues = processor.Analyze(content, "main.cpp");