Skip to content

Commit

Permalink
Code cleanup from temporary enabling .NET analyzers (#601)
Browse files Browse the repository at this point in the history
* Code cleanup from temporary enabling .NET analyzers;

    <AnalysisMode>all</AnalysisMode>
    <EnableNETAnalyzers>true</EnableNETAnalyzers>
    <AnalysisLevel>latest</AnalysisLevel>
    <EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>

* fix build

* remove .NET 7

* update readme

* remove .NET 7 from pipeline
  • Loading branch information
ErikEJ authored Aug 5, 2024
1 parent 8b29eba commit f9fb9f2
Show file tree
Hide file tree
Showing 17 changed files with 79 additions and 82 deletions.
9 changes: 2 additions & 7 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,12 @@ jobs:
fetch-depth: 0 # avoid shallow clone so nbgv can do its work.
submodules: 'true' # need to check out the SqlTools submodule to build successfully.

# Install .NET SDK's for 6.0 and 7.0
# Install .NET SDK's for 6.0 and 8.0
- name: Setup dotnet 6.0
uses: actions/setup-dotnet@v4
with:
dotnet-version: '6.0.x'

- name: Setup dotnet 7.0
uses: actions/setup-dotnet@v4
with:
dotnet-version: '7.0.x'

- name: Setup dotnet 8.0
uses: actions/setup-dotnet@v4
with:
Expand Down Expand Up @@ -112,7 +107,7 @@ jobs:
strategy:
matrix:
os: [ "ubuntu-22.04", "macos-13", "windows-2022" ]
dotnet: [ '6.0.x', '7.0.x', '8.0.x' ]
dotnet: [ '6.0.x', '8.0.x' ]
fail-fast: false

steps:
Expand Down
44 changes: 22 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ dotnet new sqlproj -s Sql130
You should now have a project file with the following contents:

```xml
<Project Sdk="MSBuild.Sdk.SqlProj/2.8.0">
<Project Sdk="MSBuild.Sdk.SqlProj/2.8.1">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<SqlServerVersion>Sql130</SqlServerVersion>
Expand Down Expand Up @@ -94,7 +94,7 @@ If you already have a SSDT (.sqlproj) project in your solution, you can keep tha
There are a lot of properties that can be set on the model in the resulting `.dacpac` file which can be influenced by setting those properties in the project file using the same name. For example, the snippet below sets the `RecoveryMode` property to `Simple`:

```xml
<Project Sdk="MSBuild.Sdk.SqlProj/2.8.0">
<Project Sdk="MSBuild.Sdk.SqlProj/2.8.1">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<RecoveryMode>Simple</RecoveryMode>
Expand All @@ -112,7 +112,7 @@ Like `.sqlproj` projects `MSBuild.Sdk.SqlProj` supports controlling T-SQL build
Treating warnings as errors can be optionally enabled by adding a property `TreatTSqlWarningsAsErrors` to the project file:

```xml
<Project Sdk="MSBuild.Sdk.SqlProj/2.8.0">
<Project Sdk="MSBuild.Sdk.SqlProj/2.8.1">
<PropertyGroup>
<TreatTSqlWarningsAsErrors>True</TreatTSqlWarningsAsErrors>
...
Expand All @@ -124,7 +124,7 @@ Treating warnings as errors can be optionally enabled by adding a property `Trea
To suppress specific warnings from being treated as errors, add a comma-separated list of warning codes to `SuppressTSqlWarnings` property in the project file:

```xml
<Project Sdk="MSBuild.Sdk.SqlProj/2.8.0">
<Project Sdk="MSBuild.Sdk.SqlProj/2.8.1">
<PropertyGroup>
<SuppressTSqlWarnings>71558,71502</SuppressTSqlWarnings>
<TreatTSqlWarningsAsErrors>True</TreatTSqlWarningsAsErrors>
Expand All @@ -136,7 +136,7 @@ To suppress specific warnings from being treated as errors, add a comma-separate
You can suppress warnings for a specific file by adding `SuppressTSqlWarnings` for this file:

```xml
<Project Sdk="MSBuild.Sdk.SqlProj/2.8.0">
<Project Sdk="MSBuild.Sdk.SqlProj/2.8.1">
<PropertyGroup>
...
</PropertyGroup>
Expand All @@ -156,7 +156,7 @@ Support for pre- and post-deployment scripts has been added in version 1.1.0. Th
To include these scripts into your `.dacpac` add the following to your `.csproj`:

```xml
<Project Sdk="MSBuild.Sdk.SqlProj/2.8.0">
<Project Sdk="MSBuild.Sdk.SqlProj/2.8.1">
<PropertyGroup>
...
</PropertyGroup>
Expand All @@ -173,7 +173,7 @@ It is important to note that scripts in the `Pre-Deployment` and `Post-Deploymen
By default the pre- and/or post-deployment script of referenced packages (both [PackageReference](#package-references) and [ProjectReference](#project-references)) are not run when using `dotnet publish`. As of version 1.11.0 this can be optionally enabled by adding a property `RunScriptsFromReferences` to the project file as in the below example:

```xml
<Project Sdk="MSBuild.Sdk.SqlProj/2.8.0">
<Project Sdk="MSBuild.Sdk.SqlProj/2.8.1">
<PropertyGroup>
<RunScriptsFromReferences>True</RunScriptsFromReferences>
...
Expand All @@ -189,7 +189,7 @@ By default the pre- and/or post-deployment script of referenced packages (both [
Especially when using pre- and post-deployment scripts, but also in other scenario's, it might be useful to define variables that can be controlled at deployment time. This is supported using SQLCMD variables, added in version 1.1.0. These variables can be defined in your project file using the following syntax:

```xml
<Project Sdk="MSBuild.Sdk.SqlProj/2.8.0">
<Project Sdk="MSBuild.Sdk.SqlProj/2.8.1">
<PropertyGroup>
...
</PropertyGroup>
Expand All @@ -213,7 +213,7 @@ Especially when using pre- and post-deployment scripts, but also in other scenar
`MSBuild.Sdk.SqlProj` supports referencing NuGet packages that contain `.dacpac` packages. These can be referenced by using the `PackageReference` format familiar to .NET developers. They can also be installed through the NuGet Package Manager in Visual Studio.

```xml
<Project Sdk="MSBuild.Sdk.SqlProj/2.8.0">
<Project Sdk="MSBuild.Sdk.SqlProj/2.8.1">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
Expand All @@ -227,7 +227,7 @@ Especially when using pre- and post-deployment scripts, but also in other scenar
It will assume that the `.dacpac` file is inside the `tools` folder of the referenced package and that it has the same name as the NuGet package. Referenced packages that do not adhere to this convention will be silently ignored. However, you have the ability to override this convention by using the `DacpacName` attribute on the `PackageReference` (introduced in version 2.5.0). For example:

```xml
<Project Sdk="MSBuild.Sdk.SqlProj/2.8.0">
<Project Sdk="MSBuild.Sdk.SqlProj/2.8.1">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<SqlServerVersion>Sql160</SqlServerVersion>
Expand All @@ -244,7 +244,7 @@ This will add a reference to the `tools\SomeOtherDacpac.dacpac` file inside the
By default, the package reference is treated as being part of the same database. For example, if the reference package contains a `.dacpac` that has a table and a stored procedure and you would `dotnet publish` the project the table and stored procedure from that package will be deployed along with the contents of your project to the same database. If this is not desired, you can add the `DatabaseVariableLiteralValue` item metadata to the `PackageReference` specifying a different database name:

```xml
<Project Sdk="MSBuild.Sdk.SqlProj/2.8.0">
<Project Sdk="MSBuild.Sdk.SqlProj/2.8.1">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
Expand All @@ -261,7 +261,7 @@ You can also use SQLCMD variables to set references, similar to the behavior of
>Note: Don't forget to define appropriate [SQLCMD variables](#sqlcmd-variables)
```xml
<Project Sdk="MSBuild.Sdk.SqlProj/2.8.0">
<Project Sdk="MSBuild.Sdk.SqlProj/2.8.1">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
Expand Down Expand Up @@ -302,7 +302,7 @@ sqlpackage
Microsoft has recently released NuGet packages containing the definitions of the `master` and `msdb` databases. This is useful if you want to reference objects from those databases within your own projects without getting warnings. To reference these, you'll need to use at least version 2.5.0 of MSBuild.Sdk.SqlProj as you'll need to use the `DacpacName` feature for package references described above. For example:

```xml
<Project Sdk="MSBuild.Sdk.SqlProj/2.8.0">
<Project Sdk="MSBuild.Sdk.SqlProj/2.8.1">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<SqlServerVersion>160</SqlServerVersion>
Expand All @@ -322,7 +322,7 @@ For other variants of SQL Server / Azure SQL Database there are dedicated packag
Similar to package references you can also reference another project by using a `ProjectReference`. These references can be added manually to the project file or they can be added through Visual Studio. For example, consider the following example:

```xml
<Project Sdk="MSBuild.Sdk.SqlProj/2.8.0">
<Project Sdk="MSBuild.Sdk.SqlProj/2.8.1">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
Expand All @@ -336,7 +336,7 @@ Similar to package references you can also reference another project by using a
This will ensure that `MyOtherProject` is built first and the resulting `.dacpac` will be referenced by this project. This means you can use the objects defined in the other project within the scope of this project. If the other project is representing an entirely different database, you can also use `DatabaseVariableLiteralValue` or SQLCMD variables on the `ProjectReference` similar to `PackageReference`:

```xml
<Project Sdk="MSBuild.Sdk.SqlProj/2.8.0">
<Project Sdk="MSBuild.Sdk.SqlProj/2.8.1">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
Expand Down Expand Up @@ -370,7 +370,7 @@ In order to solve circular references between databases that may have been incor
`SuppressMissingDependenciesErrors` to both [Package References](#package-references) and [ProjectReferences](#project-references)):

```xml
<Project Sdk="MSBuild.Sdk.SqlProj/2.8.0">
<Project Sdk="MSBuild.Sdk.SqlProj/2.8.1">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
Expand All @@ -387,12 +387,12 @@ In order to solve circular references between databases that may have been incor

## Packaging support

`MSBuild.Sdk.SqlProj` version 2.8.0 and later supports packaging your project into a [NuGet](https://www.nuget.org) package using the `dotnet pack` command.
`MSBuild.Sdk.SqlProj` version 2.8.1 and later supports packaging your project into a [NuGet](https://www.nuget.org) package using the `dotnet pack` command.

You'll need to set the `PackageProjectUrl` property in the `.csproj` like this:

```xml
<Project Sdk="MSBuild.Sdk.SqlProj/2.8.0">
<Project Sdk="MSBuild.Sdk.SqlProj/2.8.1">
<PropertyGroup>
...
<PackageProjectUrl>your-project-url</PackageProjectUrl>
Expand Down Expand Up @@ -464,7 +464,7 @@ To further customize the deployment process, you can use the following propertie
In addition to these properties, you can also set any of the [documented](https://docs.microsoft.com/dotnet/api/microsoft.sqlserver.dac.dacdeployoptions) deployment options. These are typically set in the project file, for example:

```xml
<Project Sdk="MSBuild.Sdk.SqlProj/2.8.0">
<Project Sdk="MSBuild.Sdk.SqlProj/2.8.1">
<PropertyGroup>
...
<BackupDatabaseBeforeChanges>True</BackupDatabaseBeforeChanges>
Expand All @@ -487,7 +487,7 @@ Most of those properties are simple values (like booleans, strings and integers)
Instead of using `dotnet publish` to deploy changes to a database, you can also have a full SQL script generated that will create the database from scratch and then run that script against a SQL Server. This can be achieved by adding the following to the project file:

```xml
<Project Sdk="MSBuild.Sdk.SqlProj/2.8.0">
<Project Sdk="MSBuild.Sdk.SqlProj/2.8.1">
<PropertyGroup>
<GenerateCreateScript>True</GenerateCreateScript>
<IncludeCompositeObjects>True</IncludeCompositeObjects>
Expand All @@ -513,7 +513,7 @@ Starting with version 2.7.0 of the SDK, there is support for running static code
Static code analysis can be enabled by adding the `RunSqlCodeAnalysis` property to the project file:

```xml
<Project Sdk="MSBuild.Sdk.SqlProj/2.8.0">
<Project Sdk="MSBuild.Sdk.SqlProj/2.8.1">
<PropertyGroup>
<RunSqlCodeAnalysis>True</RunSqlCodeAnalysis>
<CodeAnalysisRules>-SqlServer.Rules.SRD0006;-Smells.*</CodeAnalysisRules>
Expand All @@ -530,7 +530,7 @@ Any rule violations found during analysis are reported as build warnings.
Individual rule violations or groups of rules can be configured to be reported as build errors as shown below.

```xml
<Project Sdk="MSBuild.Sdk.SqlProj/2.8.0">
<Project Sdk="MSBuild.Sdk.SqlProj/2.8.1">
<PropertyGroup>
<RunSqlCodeAnalysis>True</RunSqlCodeAnalysis>
<CodeAnalysisRules>+!SqlServer.Rules.SRN0005;+!SqlServer.Rules.SRD*</CodeAnalysisRules>
Expand Down
5 changes: 1 addition & 4 deletions src/DacpacTool/BaseOptions.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System.IO;
using Microsoft.SqlServer.Dac.Model;

namespace MSBuild.Sdk.SqlProj.DacpacTool
namespace MSBuild.Sdk.SqlProj.DacpacTool
{
public abstract class BaseOptions
{
Expand Down
2 changes: 1 addition & 1 deletion src/DacpacTool/DacpacTool.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<TargetFrameworks>net6.0;net8.0</TargetFrameworks>
<LangVersion>9.0</LangVersion>
</PropertyGroup>

Expand Down
2 changes: 1 addition & 1 deletion src/DacpacTool/DatabaseProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace MSBuild.Sdk.SqlProj.DacpacTool
{
public class DatabaseProperty
internal class DatabaseProperty
{
private DatabaseProperty(string name, string value)
{
Expand Down
2 changes: 1 addition & 1 deletion src/DacpacTool/DeployOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ public class DeployOptions : BaseOptions
public string[] Property { get; set; }
public string[] SqlCmdVar { get; set; }
public bool RunScriptsFromReferences { get; set; }
public bool Encrypt { get; set; } = false;
public bool Encrypt { get; set; }
}
}
7 changes: 3 additions & 4 deletions src/DacpacTool/IncludeVariableResolver.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using Microsoft.SqlTools.ServiceLayer.BatchParser;
using Microsoft.SqlTools.ServiceLayer.BatchParser.ExecutionEngineCode;
Expand All @@ -26,8 +25,8 @@ public string GetVariable(PositionStruct pos, string name)

public void SetVariable(PositionStruct pos, string name, string value)
{
outputString.AppendFormat("Setting variable {0} to [{1}]\n", name, value);
outputString.AppendFormat(CultureInfo.InvariantCulture, "Setting variable {0} to [{1}]\n", name, value);
batchParserSqlCmd.SetVariable(pos, name, value);
}
}
}
}
4 changes: 2 additions & 2 deletions src/DacpacTool/ModelValidationError.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ public class ModelValidationError
public ModelValidationError(DacModelError modelError, string sourceName)
{
modelError = modelError ?? throw new ArgumentNullException(nameof(modelError));
_sourceName = String.IsNullOrEmpty(sourceName) ?
_sourceName = string.IsNullOrEmpty(sourceName) ?
modelError.SourceName :
sourceName.Replace("MSSQL::", string.Empty);
sourceName.Replace("MSSQL::", string.Empty, StringComparison.OrdinalIgnoreCase);
_line = modelError.Line;
_column = modelError.Column;
_errorType = modelError.ErrorType;
Expand Down
11 changes: 6 additions & 5 deletions src/DacpacTool/PackageAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public sealed class PackageAnalyzer
private readonly HashSet<string> _ignoredRules = new();
private readonly HashSet<string> _ignoredRuleSets = new();
private readonly HashSet<string> _errorRuleSets = new();
private readonly char[] separator = new[] { ';' };

public PackageAnalyzer(IConsole console, string rulesExpression)
{
Expand Down Expand Up @@ -48,7 +49,7 @@ public void Analyze(TSqlModel model, FileInfo outputFile)
{
service.SetProblemSuppressor(p =>
_ignoredRules.Contains(p.Rule.RuleId)
|| _ignoredRuleSets.Any(s => p.Rule.RuleId.StartsWith(s)));
|| _ignoredRuleSets.Any(s => p.Rule.RuleId.StartsWith(s, StringComparison.OrdinalIgnoreCase)));
}

var result = service.Analyze(model);
Expand Down Expand Up @@ -82,13 +83,13 @@ private void BuildRuleLists(string rulesExpression)
{
if (!string.IsNullOrWhiteSpace(rulesExpression))
{
foreach (var rule in rulesExpression.Split(new[] { ';' },
foreach (var rule in rulesExpression.Split(separator,
StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)
.Where(rule => rule
.StartsWith("-", StringComparison.OrdinalIgnoreCase)
.StartsWith('-')
&& rule.Length > 1))
{
if (rule.Length > 2 && rule.EndsWith("*", StringComparison.OrdinalIgnoreCase))
if (rule.Length > 2 && rule.EndsWith('*'))
{
_ignoredRuleSets.Add(rule[1..^1]);
}
Expand All @@ -97,7 +98,7 @@ private void BuildRuleLists(string rulesExpression)
_ignoredRules.Add(rule[1..]);
}
}
foreach (var rule in rulesExpression.Split(new[] { ';' },
foreach (var rule in rulesExpression.Split(separator,
StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)
.Where(rule => rule
.StartsWith("+!", StringComparison.OrdinalIgnoreCase)
Expand Down
Loading

0 comments on commit f9fb9f2

Please sign in to comment.