Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support NuGet based code analyzers #625

Merged
merged 17 commits into from
Oct 1, 2024
Next Next commit
Support NuGet based code analyzers
ErikEJ committed Sep 24, 2024
commit 9bd5c0b3fab25697e076b24ab210f76f2460b679
1 change: 1 addition & 0 deletions src/DacpacTool/BuildOptions.cs
Original file line number Diff line number Diff line change
@@ -22,6 +22,7 @@ public class BuildOptions : BaseOptions

public bool RunCodeAnalysis { get; set; }
public string CodeAnalysisRules { get; set; }
public FileInfo[] CodeAnalysisAssembly { get; set; }
ErikEJ marked this conversation as resolved.
Show resolved Hide resolved
public bool WarnAsError { get; set; }
public string SuppressWarnings { get; set; }
public FileInfo SuppressWarningsListFile { get; set; }
6 changes: 3 additions & 3 deletions src/DacpacTool/DacpacTool.csproj
Original file line number Diff line number Diff line change
@@ -17,10 +17,10 @@

<ItemGroup>
<!-- These packages contain DacFX analysis rules, which must be located with the executable in order to be discovered-->
<PackageReference Include="ErikEJ.DacFX.SqlServer.Rules" Version="1.0.0" IncludeAssets="runtime" />
<PackageReference Include="ErikEJ.DacFX.TSQLSmellSCA" Version="1.0.0" IncludeAssets="runtime" />
<PackageReference Include="ErikEJ.DacFX.SqlServer.Rules" Version="1.0.1" IncludeAssets="runtime" />
ErikEJ marked this conversation as resolved.
Show resolved Hide resolved
<PackageReference Include="ErikEJ.DacFX.TSQLSmellSCA" Version="1.0.1" IncludeAssets="runtime" />

<PackageReference Include="Microsoft.SqlServer.DacFx" Version="162.3.566" />
<PackageReference Include="Microsoft.SqlServer.DacFx" Version="162.4.92" />
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
<PackageReference Include="System.CommandLine.NamingConventionBinder" Version="2.0.0-beta4.22272.1" />
</ItemGroup>
9 changes: 7 additions & 2 deletions src/DacpacTool/PackageAnalyzer.cs
Original file line number Diff line number Diff line change
@@ -39,7 +39,7 @@ public void AddRulesFile(FileInfo inputFile)
}
}

public void Analyze(TSqlModel model, FileInfo outputFile)
public void Analyze(TSqlModel model, FileInfo outputFile, FileInfo[] analyzers)
{
ArgumentNullException.ThrowIfNull(model);
ArgumentNullException.ThrowIfNull(outputFile);
@@ -48,7 +48,12 @@ public void Analyze(TSqlModel model, FileInfo outputFile)
try
{
var factory = new CodeAnalysisServiceFactory();
var service = factory.CreateAnalysisService(model);
var settings = new CodeAnalysisServiceSettings
{
AssemblyLookupPath = string.Join(';', analyzers.Select(a => a.DirectoryName)),
};

var service = factory.CreateAnalysisService(model, settings);

if (_ignoredRules.Count > 0 || _ignoredRuleSets.Count > 0)
{
3 changes: 2 additions & 1 deletion src/DacpacTool/Program.cs
Original file line number Diff line number Diff line change
@@ -33,6 +33,7 @@ static async Task<int> Main(string[] args)

new Option<bool>(new string[] { "--runcodeanalysis", "-an" }, "Run static code analysis"),
new Option<string>(new string[] { "--codeanalysisrules", "-ar" }, "List of rules to suppress in format '-Microsoft.Rules.Data.SR0001;-Microsoft.Rules.Data.SR0008'"),
new Option<FileInfo[]>(new string[] { "--codeanalysisassembly", "-aa" }, "Custom code analysis rule assembly to use"),

new Option<bool>(new string[] { "--warnaserror" }, "Treat T-SQL Warnings As Errors"),
new Option<bool>(new string[] { "--generatecreatescript", "-gcs" }, "Generate create script for package"),
@@ -212,7 +213,7 @@ private static int BuildDacpac(BuildOptions options)
analyzer.AddRulesFile(inputFile);
}

analyzer.Analyze(packageBuilder.Model, options.Output);
analyzer.Analyze(packageBuilder.Model, options.Output, options.CodeAnalysisAssembly ?? Array.Empty<FileInfo>());
}

return 0;
3 changes: 2 additions & 1 deletion src/MSBuild.Sdk.SqlProj/Sdk/Sdk.targets
Original file line number Diff line number Diff line change
@@ -229,14 +229,15 @@
<PostDeploymentScriptArgument>@(PostDeploy->'--postdeploy %(Identity)', ' ')</PostDeploymentScriptArgument>
<RefactorLogScriptArgument>@(RefactorLog->'--refactorlog %(Identity)', ' ')</RefactorLogScriptArgument>
<RunSqlCodeAnalysisArgument Condition="'$(RunSqlCodeAnalysis)' == 'True'">-an</RunSqlCodeAnalysisArgument>
<CodeAnalysisAssemblyLookupPathsArgument Condition="'$(RunSqlCodeAnalysis)' == 'True'">@(Analyzer->'-aa &quot;%(Identity)&quot;', ' ')</CodeAnalysisAssemblyLookupPathsArgument>
<CodeAnalysisRulesArgument Condition="'$(CodeAnalysisRules)'!=''">-ar &quot;$(CodeAnalysisRules)&quot;</CodeAnalysisRulesArgument>
<DebugArgument Condition="'$(MSBuildSdkSqlProjDebug)' == 'True'">--debug</DebugArgument>
<TreatTSqlWarningsAsErrorsArgument Condition="'$(TreatTSqlWarningsAsErrors)' == 'True' Or ('$(TreatWarningsAsErrors)' == 'True' And '$(TreatTSqlWarningsAsErrors)' == '')">--warnaserror</TreatTSqlWarningsAsErrorsArgument>
<GenerateCreateScriptArgument Condition="'$(GenerateCreateScript)' == 'True'">--generatecreatescript</GenerateCreateScriptArgument>
<TargetDatabaseNameArgument Condition="'$(TargetDatabaseName)' != '$(MSBuildProjectName)'">-tdn &quot;$(TargetDatabaseName)&quot;</TargetDatabaseNameArgument>
<SuppressTSqlWarningsArgument Condition="'$(SuppressTSqlWarnings)'!=''">-spw &quot;$(SuppressTSqlWarnings)&quot;</SuppressTSqlWarningsArgument>
<WarningsSuppressionListArgument Condition="'@(WarningsSuppressionFiles->'%(Identity)')'!=''">-spl &quot;$(IntermediateOutputPath)$(MSBuildProjectName).WarningsSuppression.txt&quot;</WarningsSuppressionListArgument>
<DacpacToolCommand>dotnet &quot;$(DacpacToolExe)&quot; build $(OutputPathArgument) $(MetadataArguments) $(SqlServerVersionArgument) $(InputFileArguments) $(ReferenceArguments) $(SqlCmdVariableArguments) $(BuildPropertyArguments) $(DeployPropertyArguments) $(PreDeploymentScriptArgument) $(PostDeploymentScriptArgument) $(RefactorLogScriptArgument) $(TreatTSqlWarningsAsErrorsArgument) $(SuppressTSqlWarningsArgument) $(WarningsSuppressionListArgument) $(DebugArgument) $(GenerateCreateScriptArgument) $(TargetDatabaseNameArgument) $(RunSqlCodeAnalysisArgument) $(CodeAnalysisRulesArgument)</DacpacToolCommand>
<DacpacToolCommand>dotnet &quot;$(DacpacToolExe)&quot; build $(OutputPathArgument) $(MetadataArguments) $(SqlServerVersionArgument) $(InputFileArguments) $(ReferenceArguments) $(SqlCmdVariableArguments) $(BuildPropertyArguments) $(DeployPropertyArguments) $(PreDeploymentScriptArgument) $(PostDeploymentScriptArgument) $(RefactorLogScriptArgument) $(TreatTSqlWarningsAsErrorsArgument) $(SuppressTSqlWarningsArgument) $(WarningsSuppressionListArgument) $(DebugArgument) $(GenerateCreateScriptArgument) $(TargetDatabaseNameArgument) $(RunSqlCodeAnalysisArgument) $(CodeAnalysisRulesArgument) $(CodeAnalysisAssemblyLookupPathsArgument)</DacpacToolCommand>
</PropertyGroup>
<!-- Run it, except during design-time builds -->
<Message Importance="Low" Text="Running command: $(DacpacToolCommand)" />
13 changes: 7 additions & 6 deletions test/DacpacTool.Tests/PackageAnalyzerTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.IO;
using System;
using System.IO;
using System.Linq;
using Microsoft.SqlServer.Dac.Model;
using Microsoft.VisualStudio.TestTools.UnitTesting;
@@ -21,7 +22,7 @@ public void RunsAnalyzer()
var packageAnalyzer = new PackageAnalyzer(_console, null);

// Act
packageAnalyzer.Analyze(result.model, result.fileInfo);
packageAnalyzer.Analyze(result.model, result.fileInfo, Array.Empty<FileInfo>());

// Assert
testConsole.Lines.Count.ShouldBe(15);
@@ -42,7 +43,7 @@ public void RunsAnalyzerWithSupressions()
var packageAnalyzer = new PackageAnalyzer(_console, "-SqlServer.Rules.SRD0006;-Smells.SML005;-SqlServer.Rules.SRD999;+!SqlServer.Rules.SRN0002;");

// Act
packageAnalyzer.Analyze(result.model, result.fileInfo);
packageAnalyzer.Analyze(result.model, result.fileInfo, Array.Empty<FileInfo>());

// Assert
testConsole.Lines.Count.ShouldBe(13);
@@ -64,7 +65,7 @@ public void RunsAnalyzerWithWildcardSupressions()
var packageAnalyzer = new PackageAnalyzer(_console, "-SqlServer.Rules.SRD*");

// Act
packageAnalyzer.Analyze(result.model, result.fileInfo);
packageAnalyzer.Analyze(result.model, result.fileInfo, Array.Empty<FileInfo>());

// Assert
testConsole.Lines.Count.ShouldBe(13);
@@ -84,7 +85,7 @@ public void RunsAnalyzerWithWarningsAsErrors()
var packageAnalyzer = new PackageAnalyzer(_console, "+!SqlServer.Rules.SRD0006");

// Act
packageAnalyzer.Analyze(result.model, result.fileInfo);
packageAnalyzer.Analyze(result.model, result.fileInfo, Array.Empty<FileInfo>());

// Assert
testConsole.Lines.Count.ShouldBe(15);
@@ -105,7 +106,7 @@ public void RunsAnalyzerWithWarningsAsErrorsUsingWildcard()
var packageAnalyzer = new PackageAnalyzer(_console, "+!SqlServer.Rules.SRD*");

// Act
packageAnalyzer.Analyze(result.model, result.fileInfo);
packageAnalyzer.Analyze(result.model, result.fileInfo, Array.Empty<FileInfo>());

// Assert
testConsole.Lines.Count.ShouldBe(15);
Binary file not shown.
Binary file not shown.
9 changes: 4 additions & 5 deletions test/TestProjectWithAnalyzers/TestProjectWithAnalyzers.csproj
Original file line number Diff line number Diff line change
@@ -2,16 +2,15 @@
<Import Project="$(MSBuildThisFileDirectory)..\..\src\MSBuild.Sdk.SqlProj\Sdk\Sdk.props" />

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFramework>netstandard2.1</TargetFramework>
<SqlServerVersion>Sql150</SqlServerVersion>
<RunSqlCodeAnalysis>True</RunSqlCodeAnalysis>
<CodeAnalysisRules>-SqlServer.Rules.SRD0006;-Smells.*;+!SqlServer.Rules.SRN0002</CodeAnalysisRules>
</PropertyGroup>

<Import Project="$(MSBuildThisFileDirectory)..\..\src\MSBuild.Sdk.SqlProj\Sdk\Sdk.targets" />

<ItemGroup>
<Content Include="Rules\SqlServer.Dac.dll" />
<Content Include="Rules\SqlServer.Rules.dll" />
<PackageReference Include="ErikEJ.DacFX.SqlServer.Rules" Version="1.0.1" />
ErikEJ marked this conversation as resolved.
Show resolved Hide resolved
</ItemGroup>

<Import Project="$(MSBuildThisFileDirectory)..\..\src\MSBuild.Sdk.SqlProj\Sdk\Sdk.targets" />
</Project>