From f9fb9f287908d6e911355ae2da93cf61c51f439c Mon Sep 17 00:00:00 2001 From: Erik Ejlskov Jensen Date: Mon, 5 Aug 2024 13:41:00 +0200 Subject: [PATCH] Code cleanup from temporary enabling .NET analyzers (#601) * Code cleanup from temporary enabling .NET analyzers; all true latest true * fix build * remove .NET 7 * update readme * remove .NET 7 from pipeline --- .github/workflows/main.yml | 9 +--- README.md | 44 +++++++++---------- src/DacpacTool/BaseOptions.cs | 5 +-- src/DacpacTool/DacpacTool.csproj | 2 +- src/DacpacTool/DatabaseProperty.cs | 2 +- src/DacpacTool/DeployOptions.cs | 2 +- src/DacpacTool/IncludeVariableResolver.cs | 7 ++- src/DacpacTool/ModelValidationError.cs | 4 +- src/DacpacTool/PackageAnalyzer.cs | 11 ++--- src/DacpacTool/PackageBuilder.cs | 33 +++++++------- src/DacpacTool/PackageDeployer.cs | 4 +- src/DacpacTool/PropertyParser.cs | 8 ++-- src/DacpacTool/ScriptInspector.cs | 5 +-- src/DacpacTool/ScriptParser.cs | 9 +++- src/DacpacTool/SqlRuleProblemExtensions.cs | 6 +-- .../MSBuild.Sdk.SqlProj.csproj | 4 +- test/DacpacTool.Tests/DacpacTool.Tests.csproj | 6 +-- 17 files changed, 79 insertions(+), 82 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 87d52129..f0171d66 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -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: @@ -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: diff --git a/README.md b/README.md index e4e22b50..e20a2356 100644 --- a/README.md +++ b/README.md @@ -42,7 +42,7 @@ dotnet new sqlproj -s Sql130 You should now have a project file with the following contents: ```xml - + netstandard2.0 Sql130 @@ -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 - + netstandard2.0 Simple @@ -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 - + True ... @@ -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 - + 71558,71502 True @@ -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 - + ... @@ -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 - + ... @@ -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 - + True ... @@ -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 - + ... @@ -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 - + netstandard2.0 @@ -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 - + netstandard2.0 Sql160 @@ -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 - + netstandard2.0 @@ -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 - + netstandard2.0 @@ -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 - + netstandard2.0 160 @@ -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 - + netstandard2.0 @@ -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 - + netstandard2.0 @@ -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 - + netstandard2.0 @@ -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 - + ... your-project-url @@ -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 - + ... True @@ -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 - + True True @@ -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 - + True -SqlServer.Rules.SRD0006;-Smells.* @@ -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 - + True +!SqlServer.Rules.SRN0005;+!SqlServer.Rules.SRD* diff --git a/src/DacpacTool/BaseOptions.cs b/src/DacpacTool/BaseOptions.cs index c0b237cd..87b113db 100644 --- a/src/DacpacTool/BaseOptions.cs +++ b/src/DacpacTool/BaseOptions.cs @@ -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 { diff --git a/src/DacpacTool/DacpacTool.csproj b/src/DacpacTool/DacpacTool.csproj index 66ddf47f..0d701621 100644 --- a/src/DacpacTool/DacpacTool.csproj +++ b/src/DacpacTool/DacpacTool.csproj @@ -2,7 +2,7 @@ Exe - net6.0;net7.0;net8.0 + net6.0;net8.0 9.0 diff --git a/src/DacpacTool/DatabaseProperty.cs b/src/DacpacTool/DatabaseProperty.cs index 22022f81..595ac161 100644 --- a/src/DacpacTool/DatabaseProperty.cs +++ b/src/DacpacTool/DatabaseProperty.cs @@ -2,7 +2,7 @@ namespace MSBuild.Sdk.SqlProj.DacpacTool { - public class DatabaseProperty + internal class DatabaseProperty { private DatabaseProperty(string name, string value) { diff --git a/src/DacpacTool/DeployOptions.cs b/src/DacpacTool/DeployOptions.cs index 6a352054..9081a463 100644 --- a/src/DacpacTool/DeployOptions.cs +++ b/src/DacpacTool/DeployOptions.cs @@ -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; } } } diff --git a/src/DacpacTool/IncludeVariableResolver.cs b/src/DacpacTool/IncludeVariableResolver.cs index 39dc87d9..38ed9f1d 100644 --- a/src/DacpacTool/IncludeVariableResolver.cs +++ b/src/DacpacTool/IncludeVariableResolver.cs @@ -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; @@ -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); } } -} \ No newline at end of file +} diff --git a/src/DacpacTool/ModelValidationError.cs b/src/DacpacTool/ModelValidationError.cs index ba4c19a4..63affb3f 100644 --- a/src/DacpacTool/ModelValidationError.cs +++ b/src/DacpacTool/ModelValidationError.cs @@ -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; diff --git a/src/DacpacTool/PackageAnalyzer.cs b/src/DacpacTool/PackageAnalyzer.cs index fce2474c..e510abdc 100644 --- a/src/DacpacTool/PackageAnalyzer.cs +++ b/src/DacpacTool/PackageAnalyzer.cs @@ -13,6 +13,7 @@ public sealed class PackageAnalyzer private readonly HashSet _ignoredRules = new(); private readonly HashSet _ignoredRuleSets = new(); private readonly HashSet _errorRuleSets = new(); + private readonly char[] separator = new[] { ';' }; public PackageAnalyzer(IConsole console, string rulesExpression) { @@ -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); @@ -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]); } @@ -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) diff --git a/src/DacpacTool/PackageBuilder.cs b/src/DacpacTool/PackageBuilder.cs index 9831c352..cdada565 100644 --- a/src/DacpacTool/PackageBuilder.cs +++ b/src/DacpacTool/PackageBuilder.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Globalization; using System.IO; using System.IO.Packaging; using System.Linq; @@ -40,7 +41,7 @@ public void AddReference(string referenceFile, string externalParts = null, bool Model.AddReference(referenceFile, externalParts, suppressErrorsForMissingDependencies); } - private void ValidateReference(string referenceFile) + private static void ValidateReference(string referenceFile) { // Make sure the file exists if (!File.Exists(referenceFile)) @@ -50,7 +51,7 @@ private void ValidateReference(string referenceFile) // Make sure the file is a .dacpac file string fileType = Path.GetExtension(referenceFile); - if (fileType.ToLower() != ".dacpac") + if (!fileType.Equals(".dacpac", StringComparison.OrdinalIgnoreCase)) { throw new ArgumentException($"Invalid filetype {fileType}, was expecting .dacpac", nameof(referenceFile)); } @@ -200,13 +201,13 @@ public void SetProperty(string key, string value) // Convert value into the appropriate type depending on the key var propertyValue = key switch { - "QueryStoreIntervalLength" => int.Parse(value), - "QueryStoreFlushInterval" => int.Parse(value), + "QueryStoreIntervalLength" => int.Parse(value, CultureInfo.InvariantCulture), + "QueryStoreFlushInterval" => int.Parse(value, CultureInfo.InvariantCulture), "QueryStoreDesiredState" => Enum.Parse(typeof(QueryStoreDesiredState), value), "QueryStoreCaptureMode" => Enum.Parse(typeof(QueryStoreCaptureMode), value), "ParameterizationOption" => Enum.Parse(typeof(ParameterizationOption), value), "PageVerifyMode" => Enum.Parse(typeof(PageVerifyMode), value), - "QueryStoreMaxStorageSize" => int.Parse(value), + "QueryStoreMaxStorageSize" => int.Parse(value, CultureInfo.InvariantCulture), "NumericRoundAbortOn" => bool.Parse(value), "NestedTriggersOn" => bool.Parse(value), "HonorBrokerPriority" => bool.Parse(value), @@ -216,16 +217,16 @@ public void SetProperty(string key, string value) "DbScopedConfigQueryOptimizerHotfixes" => bool.Parse(value), "NonTransactedFileStreamAccess" => Enum.Parse(typeof(NonTransactedFileStreamAccess), value), "DbScopedConfigParameterSniffingSecondary" => bool.Parse(value), - "QueryStoreMaxPlansPerQuery" => int.Parse(value), + "QueryStoreMaxPlansPerQuery" => int.Parse(value, CultureInfo.InvariantCulture), "QuotedIdentifierOn" => bool.Parse(value), "VardecimalStorageFormatOn" => bool.Parse(value), - "TwoDigitYearCutoff" => short.Parse(value), + "TwoDigitYearCutoff" => short.Parse(value, CultureInfo.InvariantCulture), "Trustworthy" => bool.Parse(value), "TransformNoiseWords" => bool.Parse(value), "TornPageProtectionOn" => bool.Parse(value), "TargetRecoveryTimeUnit" => Enum.Parse(typeof(TimeUnit), value), - "QueryStoreStaleQueryThreshold" => int.Parse(value), - "TargetRecoveryTimePeriod" => int.Parse(value), + "QueryStoreStaleQueryThreshold" => int.Parse(value, CultureInfo.InvariantCulture), + "TargetRecoveryTimePeriod" => int.Parse(value, CultureInfo.InvariantCulture), "ServiceBrokerOption" => Enum.Parse(typeof(ServiceBrokerOption), value), "RecursiveTriggersOn" => bool.Parse(value), "DelayedDurabilityMode" => Enum.Parse(typeof(DelayedDurabilityMode), value), @@ -233,8 +234,8 @@ public void SetProperty(string key, string value) "ReadOnly" => bool.Parse(value), "SupplementalLoggingOn" => bool.Parse(value), "DbScopedConfigParameterSniffing" => bool.Parse(value), - "DbScopedConfigMaxDOPSecondary" => int.Parse(value), - "DbScopedConfigMaxDOP" => int.Parse(value), + "DbScopedConfigMaxDOPSecondary" => int.Parse(value, CultureInfo.InvariantCulture), + "DbScopedConfigMaxDOP" => int.Parse(value, CultureInfo.InvariantCulture), "AutoShrink" => bool.Parse(value), "AutoCreateStatisticsIncremental" => bool.Parse(value), "AutoCreateStatistics" => bool.Parse(value), @@ -263,9 +264,9 @@ public void SetProperty(string key, string value) "CursorCloseOnCommit" => bool.Parse(value), "Containment" => Enum.Parse(typeof(Containment), value), "ConcatNullYieldsNull" => bool.Parse(value), - "CompatibilityLevel" => int.Parse(value), + "CompatibilityLevel" => int.Parse(value, CultureInfo.InvariantCulture), "ChangeTrackingRetentionUnit" => Enum.Parse(typeof(TimeUnit), value), - "ChangeTrackingRetentionPeriod" => int.Parse(value), + "ChangeTrackingRetentionPeriod" => int.Parse(value, CultureInfo.InvariantCulture), "ChangeTrackingEnabled" => bool.Parse(value), "UserAccessOption" => Enum.Parse(typeof(UserAccessOption), value), "WithEncryption" => bool.Parse(value), @@ -344,13 +345,13 @@ private void AddScript(FileInfo script, FileInfo outputFile, string path) } } - private void WritePart(FileInfo file, Package package, string path) + private static void WritePart(FileInfo file, Package package, string path) { var part = package.CreatePart(new Uri(path, UriKind.Relative), "text/plain"); using (var stream = part.GetStream()) { - var parser = new ScriptParser(file.FullName, new IncludeVariableResolver()); + using var parser = new ScriptParser(file.FullName, new IncludeVariableResolver()); var buffer = Encoding.UTF8.GetBytes(parser.GenerateScript()); stream.Write(buffer, 0, buffer.Length); } @@ -380,7 +381,7 @@ public void AddFileWarningsToSuppress(FileInfo inputFile, string suppressionList } - private List ParseSuppressionList(string suppressionList) + private static List ParseSuppressionList(string suppressionList) { var result = new List(); if (!string.IsNullOrEmpty(suppressionList)) diff --git a/src/DacpacTool/PackageDeployer.cs b/src/DacpacTool/PackageDeployer.cs index e5ccb8c1..4582fc07 100644 --- a/src/DacpacTool/PackageDeployer.cs +++ b/src/DacpacTool/PackageDeployer.cs @@ -125,9 +125,9 @@ public void RunPostDeploymentScriptFromReferences(FileInfo dacpacPackage, string private void RunDeploymentScriptFromReferences(FileInfo dacpacPackage, string targetDatabaseName, bool isPreDeploy) { using var model = new TSqlModel(dacpacPackage.FullName, DacSchemaModelStorageType.Memory); - var references = model.GetReferencedDacPackages(); + var references = model.GetReferencedDacPackages().ToList(); - if (!references.Any()) + if (references.Count == 0) { return; } diff --git a/src/DacpacTool/PropertyParser.cs b/src/DacpacTool/PropertyParser.cs index 5078c8e2..ac5b6b67 100644 --- a/src/DacpacTool/PropertyParser.cs +++ b/src/DacpacTool/PropertyParser.cs @@ -13,7 +13,7 @@ namespace MSBuild.Sdk.SqlProj.DacpacTool /// public static class PropertyParser { - private static readonly IDictionary> CustomParsers = new Dictionary>(); + private static readonly Dictionary> CustomParsers = new Dictionary>(); static PropertyParser() { @@ -42,7 +42,7 @@ public static DacDeployOptions ExtractDeployOptions(this BuildOptions options) private static PropertyInfo GetDacDeployOptionsProperty(string propertyName) { var property = typeof(DacDeployOptions).GetProperties() - .SingleOrDefault(p => string.Compare(p.Name, propertyName, StringComparison.OrdinalIgnoreCase) == 0); + .SingleOrDefault(p => string.Equals(p.Name, propertyName, StringComparison.OrdinalIgnoreCase)); return property; } @@ -106,7 +106,7 @@ public static void SetDeployProperties(this DacDeployOptions deployOptions, stri foreach (var deployProperty in deployProperties.Where(p => string.IsNullOrWhiteSpace(p) == false)) { var databaseProperty = DatabaseProperty.Create(deployProperty); - var propertyValue =deployOptions.SetDeployProperty(databaseProperty.Name, databaseProperty.Value); + var propertyValue = deployOptions.SetDeployProperty(databaseProperty.Name, databaseProperty.Value); if (console != null) { @@ -114,7 +114,7 @@ public static void SetDeployProperties(this DacDeployOptions deployOptions, stri { ObjectType[] o => string.Join(',', o), DacAzureDatabaseSpecification s => $"{s.Edition},{s.MaximumSize},{s.ServiceObjective}", - _ => propertyValue == null ? "null" : propertyValue.ToString() + _ => propertyValue.ToString() }; console.WriteLine($"Setting property {databaseProperty.Name} to value {parsedValue}"); diff --git a/src/DacpacTool/ScriptInspector.cs b/src/DacpacTool/ScriptInspector.cs index 0f1b0e2c..d88bed5f 100644 --- a/src/DacpacTool/ScriptInspector.cs +++ b/src/DacpacTool/ScriptInspector.cs @@ -1,5 +1,4 @@ -using System; -using System.IO; +using System.IO; using System.Collections.Generic; namespace MSBuild.Sdk.SqlProj.DacpacTool @@ -24,7 +23,7 @@ public void AddPostDeploymentScript(FileInfo script) private void AddIncludedFiles(FileInfo file) { - var parser = new ScriptParser(file.FullName, new IncludeVariableResolver()); + using var parser = new ScriptParser(file.FullName, new IncludeVariableResolver()); _includedFiles.AddRange(parser.CollectFileNames()); } } diff --git a/src/DacpacTool/ScriptParser.cs b/src/DacpacTool/ScriptParser.cs index d4599d3f..3087a7fb 100644 --- a/src/DacpacTool/ScriptParser.cs +++ b/src/DacpacTool/ScriptParser.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.IO; using System.Text; @@ -8,7 +8,7 @@ namespace MSBuild.Sdk.SqlProj.DacpacTool { - public class ScriptParser : ICommandHandler + public sealed class ScriptParser : ICommandHandler, IDisposable { private readonly Parser _parser; private bool _parsed; @@ -96,5 +96,10 @@ BatchParserAction ICommandHandler.OnError(Token token, OnErrorAction action) return action == OnErrorAction.Ignore ? BatchParserAction.Continue : BatchParserAction.Abort; } + + public void Dispose() + { + _parser?.Dispose(); + } } } diff --git a/src/DacpacTool/SqlRuleProblemExtensions.cs b/src/DacpacTool/SqlRuleProblemExtensions.cs index 8992036f..a466e22b 100644 --- a/src/DacpacTool/SqlRuleProblemExtensions.cs +++ b/src/DacpacTool/SqlRuleProblemExtensions.cs @@ -9,7 +9,7 @@ namespace MSBuild.Sdk.SqlProj.DacpacTool /// /// A wrapper for that provides MSBuild compatible output and source document information. /// - public static class SqlRuleProblemExtensions + internal static class SqlRuleProblemExtensions { public static string GetOutputMessage(this SqlRuleProblem sqlRuleProblem, HashSet errorRules) { @@ -23,8 +23,8 @@ public static string GetOutputMessage(this SqlRuleProblem sqlRuleProblem, HashSe } var wildCardErrorRules = errorRules - .Where(r => r.EndsWith("*", StringComparison.OrdinalIgnoreCase)); - if (wildCardErrorRules.Any(s => sqlRuleProblem.RuleId.StartsWith(s[..^1]))) + .Where(r => r.EndsWith('*')); + if (wildCardErrorRules.Any(s => sqlRuleProblem.RuleId.StartsWith(s[..^1], StringComparison.OrdinalIgnoreCase))) { sqlRuleProblemSeverity = SqlRuleProblemSeverity.Error; } diff --git a/src/MSBuild.Sdk.SqlProj/MSBuild.Sdk.SqlProj.csproj b/src/MSBuild.Sdk.SqlProj/MSBuild.Sdk.SqlProj.csproj index 7d7417a3..8c92a2ae 100644 --- a/src/MSBuild.Sdk.SqlProj/MSBuild.Sdk.SqlProj.csproj +++ b/src/MSBuild.Sdk.SqlProj/MSBuild.Sdk.SqlProj.csproj @@ -1,4 +1,4 @@ - + @@ -31,7 +31,7 @@ - <_DacpacToolSupportedTfms>net6.0;net7.0;net8.0 + <_DacpacToolSupportedTfms>net6.0;net8.0 diff --git a/test/DacpacTool.Tests/DacpacTool.Tests.csproj b/test/DacpacTool.Tests/DacpacTool.Tests.csproj index 30358af2..eeaeb206 100644 --- a/test/DacpacTool.Tests/DacpacTool.Tests.csproj +++ b/test/DacpacTool.Tests/DacpacTool.Tests.csproj @@ -1,11 +1,11 @@ - + - net6.0;net7.0;net8.0 + net6.0;net8.0 false MSBuild.Sdk.SqlProj.DacpacTool.Tests MSBuild.Sdk.SqlProj.DacpacTool.Tests - 8.0 + 9.0