Skip to content

Commit

Permalink
adds code analysis rule sample template (#515)
Browse files Browse the repository at this point in the history
* adds code analysis rule sample template

* formatting fixes
  • Loading branch information
dzsquared authored Nov 15, 2024
1 parent 80f6f21 commit aacacda
Show file tree
Hide file tree
Showing 7 changed files with 288 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<IncludeBuildOutput>false</IncludeBuildOutput>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<NoDefaultExcludes>true</NoDefaultExcludes>
<TemplateIntermediateOutputPath>$(BaseIntermediateOutputPath)\$(Configuration)\$(MSBuildThisFileName)\sqlproject</TemplateIntermediateOutputPath>
<TemplateIntermediateOutputPath>$(BaseIntermediateOutputPath)\$(Configuration)\$(MSBuildThisFileName)</TemplateIntermediateOutputPath>
<!-- NU5128 warns about dependencies, template package has no dependencies. -->
<NoWarn>$(NoWarn);NU5128</NoWarn>
<NoDefaultExcludes>true</NoDefaultExcludes>
Expand All @@ -29,20 +29,45 @@
<RemoveDir Directories="$(TemplateIntermediateOutputPath)" />
</Target>

<!-- This target copies template files to intermediate output path, then sets the SDK version to the current assembly version -->
<!-- This target copies SQLproj template files to intermediate output path, then sets the SDK version to the current assembly version -->
<Target Name="CopyTemplateFiles" BeforeTargets="GenerateNuspec;Build">
<PropertyGroup>
<SqlProjTemplateIntermediateOutputPath>$(TemplateIntermediateOutputPath)\sqlproject</SqlProjTemplateIntermediateOutputPath>
</PropertyGroup>
<ItemGroup>
<TemplateFiles Include="sqlproject/**" />
</ItemGroup>
<Copy SourceFiles="@(TemplateFiles)" DestinationFiles="$(TemplateIntermediateOutputPath)/%(RecursiveDir)%(Filename)%(Extension)" />
<Copy SourceFiles="@(TemplateFiles)" DestinationFiles="$(SqlProjTemplateIntermediateOutputPath)/%(RecursiveDir)%(Filename)%(Extension)" />
<ReplaceFileText
InputFilename="$(TemplateIntermediateOutputPath)/SqlProject1.sqlproj"
OutputFilename="$(TemplateIntermediateOutputPath)/SqlProject1.sqlproj"
InputFilename="$(SqlProjTemplateIntermediateOutputPath)/SqlProject1.sqlproj"
OutputFilename="$(SqlProjTemplateIntermediateOutputPath)/SqlProject1.sqlproj"
MatchExpression="###ASSEMBLY_VERSION###"
ReplacementText="$(PackageVersion)" />
<ItemGroup>
<Content Remove="$(TemplateIntermediateOutputPath)\**" />
<Content Include="$(TemplateIntermediateOutputPath)\**" PackagePath="content/" Pack="true" />
<Content Remove="$(SqlProjTemplateIntermediateOutputPath)\**" />
<Content Include="$(SqlProjTemplateIntermediateOutputPath)\**" PackagePath="content/sqlproject/" Pack="true" />
</ItemGroup>
</Target>


<!-- This target copies code analysis template files to intermediate output path, then sets the SDK version to the current assembly version -->
<Target Name="CopyTemplateFiles_CodeAnalysis" BeforeTargets="GenerateNuspec;Build">
<Message Text="Using DacFx version '$(DacFxPackageVersion)'" Importance="high" />
<PropertyGroup>
<CodeAnalysisTemplateIntermediateOutputPath>$(TemplateIntermediateOutputPath)\sqlcodeanalysis</CodeAnalysisTemplateIntermediateOutputPath>
</PropertyGroup>
<ItemGroup>
<TemplateFilesCodeAnalysis Include="sqlcodeanalysis/**" />
</ItemGroup>
<Copy SourceFiles="@(TemplateFilesCodeAnalysis)" DestinationFiles="$(CodeAnalysisTemplateIntermediateOutputPath)/%(RecursiveDir)%(Filename)%(Extension)" />
<ReplaceFileText
InputFilename="$(CodeAnalysisTemplateIntermediateOutputPath)/SqlCodeAnalysis1.csproj"
OutputFilename="$(CodeAnalysisTemplateIntermediateOutputPath)/SqlCodeAnalysis1.csproj"
MatchExpression="###DACFX_ASSEMBLY_VERSION###"
ReplacementText="$(DacFxPackageVersion)" />
<ItemGroup>
<Content Remove="$(CodeAnalysisTemplateIntermediateOutputPath)\**" />
<Content Include="$(CodeAnalysisTemplateIntermediateOutputPath)\**" PackagePath="content/sqlcodeanalysis/" Pack="true" />
</ItemGroup>
</Target>

Expand Down
16 changes: 16 additions & 0 deletions src/Microsoft.Build.Sql.Templates/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ dotnet new install Microsoft.Build.Sql.Templates

## Using the templates

### SQL project

Creating a new project "AdventureWorks" (`-n` or `--name`):

Expand All @@ -36,6 +37,21 @@ Creating a new project "AdventureWorks" with a `.gitignore` file (-g):
dotnet new sqlproj -n "AdventureWorks" -g
```

### New sample code analysis rule

Creating a new sample code analysis rule "WaitForDelay" (`-n` or `--name`):

```bash
dotnet new sqlcodeanalysis -n "WaitForDelay"
```

Displaying help information for the SQL code analysis template (`-h`):

```bash
dotnet new sqlcodeanalysis -h
```


## Building the templates

If you want to customize or contribute to the templates, you will need to build and install the templates locally. The following instructions will help you get started.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"$schema": "http://json.schemastore.org/template",
"author": "Microsoft",
"classifications": [
"Database",
"SqlServer"
],
"identity": "Microsoft.Build.Sql.CodeAnalysis",
"name": "SQL Server Database Code Analysis",
"description": "A a .NET library project that contains the scaffolding for SQL Code Analysis",
"shortName": "sqlcodeanalysis",
"tags": {
"language": "SQL",
"type": "project"
},
"sourceName": "SqlCodeAnalysis1",
"preferNameDirectory": true,
"sources": [
{
"source": "./",
"target": "./",
"include": ["SqlCodeAnalysis1.csproj", "SqlCodeAnalysis1.cs", "README.md"]
}
]
}
45 changes: 45 additions & 0 deletions src/Microsoft.Build.Sql.Templates/sqlcodeanalysis/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# New SQL code analysis rule for SQL projects

## Build

To build the code analysis project, run the following command:

```bash
dotnet build
```

To package the code analysis project as a NuGet package for referencing in a SQL project, run the following command:

```bash
dotnet pack
```

🎉 Congrats! You have successfully built the project and now have a NuGet package to reference in your SQL project.

## Use the code analysis rule in SQL projects

To reference the code analysis project in a SQL project, we need to complete 2 steps:
1. Publish the code analysis project as a NuGet package.
1. Reference the code analysis project in the SQL project.

### Publish the code analysis project

We packaged the code analysis project as a NuGet package in the previous step and will publish it to a remote feed or a [local source](https://learn.microsoft.com/dotnet/core/tools/dotnet-nuget-add-source) (folder). Add a folder as a local feed by running the following command:

```bash
dotnet nuget add source c:\packages
```

Copy the NuGet package from `bin/Release` to the local source folder.

### Reference the code analysis project in the SQL project

The following example demonstrates how to reference the code analysis project in a SQL project:

```xml
<ItemGroup>
<PackageReference Include="Sample.WaitForDelay" Version="1.0.0" />
</ItemGroup>
```

Set either the SQL project property `<RunSqlCodeAnalysis>True</RunSqlCodeAnalysis>` or run `dotnet build /p:RunSqlCodeAnalysis=True` to generate code analysis output in the build log.
150 changes: 150 additions & 0 deletions src/Microsoft.Build.Sql.Templates/sqlcodeanalysis/SqlCodeAnalysis1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using Microsoft.SqlServer.Dac.CodeAnalysis;
using Microsoft.SqlServer.Dac.Model;
using Microsoft.SqlServer.TransactSql.ScriptDom;

namespace Sample.SqlCodeAnalysis1 {
/// <summary>
/// This is a rule that returns a warning message
/// whenever there is a WAITFOR DELAY statement appears inside a subroutine body.
/// This rule only applies to stored procedures, functions and triggers.
/// </summary>
[ExportCodeAnalysisRule(
id: RuleId,
displayName: RuleName,
Description = ProblemDescription,
Category = RuleCategory,
RuleScope = SqlRuleScope.Element)]
public sealed class AvoidWaitForDelayRule : SqlCodeAnalysisRule
{
/// <summary>
/// The Rule ID should resemble a fully-qualified class name. In the Visual Studio UI
/// rules are grouped by "Namespace + Category", and each rule is shown using "Short ID: DisplayName".
/// For this rule, that means the grouping will be "Public.Dac.Samples.Performance", with the rule
/// shown as "SR1004: Avoid using WaitFor Delay statements in stored procedures, functions and triggers."
/// </summary>
public const string RuleId = "Sample.SqlCodeAnalysis1.SSCA1004";
public const string RuleName = "Avoid using WaitFor Delay statements in stored procedures, functions and triggers.";
public const string ProblemDescription = "Avoid using WAITFOR DELAY in {0}";
public const string RuleCategory = "Performance";

public AvoidWaitForDelayRule()
{
// This rule supports Procedures, Functions and Triggers. Only those objects will be passed to the Analyze method
SupportedElementTypes = new[]
{
// Note: can use the ModelSchema definitions, or access the TypeClass for any of these types
ModelSchema.ExtendedProcedure,
ModelSchema.Procedure,
ModelSchema.TableValuedFunction,
ModelSchema.ScalarFunction,
ModelSchema.DatabaseDdlTrigger,
ModelSchema.DmlTrigger,
ModelSchema.ServerDdlTrigger,
};
}

/// <summary>
/// For element-scoped rules the Analyze method is executed once for every matching
/// object in the model.
/// </summary>
/// <param name="ruleExecutionContext">The context object contains the TSqlObject being
/// analyzed, a TSqlFragment
/// that's the AST representation of the object, the current rule's descriptor, and a
/// reference to the model being
/// analyzed.
/// </param>
/// <returns>A list of problems should be returned. These will be displayed in the Visual
/// Studio error list</returns>
public override IList<SqlRuleProblem> Analyze(
SqlRuleExecutionContext ruleExecutionContext)
{
var problems = new List<SqlRuleProblem>();
var modelElement = ruleExecutionContext.ModelElement;

// this rule does not apply to inline table-valued function
// we simply do not return any problem in that case.
if (IsInlineTableValuedFunction(modelElement))
{
return problems;
}

var elementName = GetElementName(ruleExecutionContext, modelElement);

// The rule execution context has all the objects we'll need, including the
// fragment representing the object,
// and a descriptor that lets us access rule metadata
var fragment = ruleExecutionContext.ScriptFragment;
var ruleDescriptor = ruleExecutionContext.RuleDescriptor;

// To process the fragment and identify WAITFOR DELAY statements we will use a
// visitor
var visitor = new WaitForDelayVisitor();
fragment.Accept(visitor);
var waitforDelayStatements = visitor.WaitForDelayStatements;

// Create problems for each WAITFOR DELAY statement found
// When creating a rule problem, always include the TSqlObject being analyzed. This
// is used to determine
// the name of the source this problem was found in and a best guess as to the
// line/column the problem was found at.
//
// In addition if you have a specific TSqlFragment that is related to the problem
// also include this
// since the most accurate source position information (start line and column) will
// be read from the fragment
foreach (WaitForStatement waitForStatement in waitforDelayStatements)
{
var problem = new SqlRuleProblem(
string.Format(CultureInfo.InvariantCulture, ruleDescriptor.DisplayDescription, elementName),
modelElement,
waitForStatement);
problems.Add(problem);
}

return problems;
}

private static string GetElementName(
SqlRuleExecutionContext ruleExecutionContext,
TSqlObject modelElement)
{
// Get the element name using the built in DisplayServices. This provides a number of
// useful formatting options to
// make a name user-readable
var displayServices = ruleExecutionContext.SchemaModel.DisplayServices;
var elementName = displayServices.GetElementName(
modelElement, ElementNameStyle.EscapedFullyQualifiedName);
return elementName;
}

private static bool IsInlineTableValuedFunction(TSqlObject modelElement)
{
return TableValuedFunction.TypeClass.Equals(modelElement.ObjectType)
&& modelElement.GetMetadata<FunctionType>(TableValuedFunction.FunctionType)
== FunctionType.InlineTableValuedFunction;
}
}

internal class WaitForDelayVisitor : TSqlConcreteFragmentVisitor
{
public IList<WaitForStatement> WaitForDelayStatements { get; private set; }

// Define the class constructor
public WaitForDelayVisitor()
{
WaitForDelayStatements = new List<WaitForStatement>();
}

public override void ExplicitVisit(WaitForStatement node)
{
// We are only interested in WAITFOR DELAY occurrences
if (node.WaitForOption == WaitForOption.Delay)
{
WaitForDelayStatements.Add(node);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<PackageId>Sample.SqlCodeAnalysis1</PackageId>
<PackageVersion>1.0.0</PackageVersion>
<TargetFramework>netstandard2.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.SqlServer.DacFx" Version="###DACFX_ASSEMBLY_VERSION###" PrivateAssets="All"/>
</ItemGroup>

<ItemGroup>
<None Include="bin\$(Configuration)\$(TargetFramework)\SqlCodeAnalysis1.dll"
Pack="true"
PackagePath="analyzers\dotnet\cs"
Visible="false" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"Database",
"SqlServer"
],
"identity": "Microsoft.Build.Sql",
"identity": "Microsoft.Build.Sql.Project",
"name": "SQL Server Database Project",
"description": "A project that creates a SQL Server Data-Tier Application package (.dacpac)",
"shortName": "sqlproj",
Expand Down

0 comments on commit aacacda

Please sign in to comment.