|
| 1 | +#region License |
| 2 | +// Copyright (c) 2024, Fluent Migrator Project |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | +#endregion |
| 16 | + |
| 17 | +using System.Collections.Generic; |
| 18 | +using System.Collections.Immutable; |
| 19 | +using System.Linq; |
| 20 | + |
| 21 | +using FluentMigrator.Analyzers.Analysis; |
| 22 | + |
| 23 | +using Microsoft.CodeAnalysis; |
| 24 | +using Microsoft.CodeAnalysis.Diagnostics; |
| 25 | + |
| 26 | +namespace FluentMigrator.Analyzers |
| 27 | +{ |
| 28 | + [DiagnosticAnalyzer(LanguageNames.CSharp)] |
| 29 | + public class MigrationAttributeVersionShouldBeUniqueAnalyzer : FluentMigratorAnalyzer |
| 30 | + { |
| 31 | + public const string DiagnosticId = "FM0001"; |
| 32 | + private const string Category = "FluentMigrator"; |
| 33 | + |
| 34 | + // You can change these strings in the Resources.resx file. If you do not want your analyzer to be localize-able, you can use regular strings for Title and MessageFormat. |
| 35 | + private static readonly LocalizableString Title = new LocalizableResourceString(nameof(Resources.MigrationAttributeVersionShouldBeUniqueAnalyzerTitle), Resources.ResourceManager, typeof(Resources)); |
| 36 | + private static readonly LocalizableString MessageFormat = new LocalizableResourceString(nameof(Resources.MigrationAttributeVersionShouldBeUniqueAnalyzerMessageFormat), Resources.ResourceManager, typeof(Resources)); |
| 37 | + private static readonly LocalizableString Description = new LocalizableResourceString(nameof(Resources.MigrationAttributeVersionShouldBeUniqueAnalyzerDescription), Resources.ResourceManager, typeof(Resources)); |
| 38 | + |
| 39 | + private static readonly DiagnosticDescriptor Rule = new DiagnosticDescriptor(DiagnosticId, Title, MessageFormat, Category, DiagnosticSeverity.Warning, isEnabledByDefault: true, description: Description, customTags: "CompilationEnd"); |
| 40 | + |
| 41 | + public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(Rule); |
| 42 | + |
| 43 | + internal override void AnalyzeCompilation(CompilationStartAnalysisContext compilationStartContext, FluentMigratorContext fluentMigratorContext) |
| 44 | + { |
| 45 | + // Get all migration classes and their version number |
| 46 | + compilationStartContext.RegisterSymbolAction(symbolContext => |
| 47 | + { |
| 48 | + var typeSymbol = (INamedTypeSymbol)symbolContext.Symbol; |
| 49 | + var migrationVersion = GetMigrationAttributeVersion(fluentMigratorContext, typeSymbol.GetAttributes()); |
| 50 | + if (migrationVersion == null) |
| 51 | + { |
| 52 | + return; |
| 53 | + } |
| 54 | + |
| 55 | + fluentMigratorContext.Migrations.Add(new MigrationClassDeclaration(typeSymbol, migrationVersion.Value)); |
| 56 | + |
| 57 | + }, SymbolKind.NamedType); |
| 58 | + |
| 59 | + // At the end: report duplicates |
| 60 | + compilationStartContext.RegisterCompilationEndAction( |
| 61 | + context => |
| 62 | + { |
| 63 | + var duplicates = fluentMigratorContext.Migrations |
| 64 | + .OrderBy(m => m.Name) |
| 65 | + .GroupBy(m => m.Version) |
| 66 | + .Where(g => g.Count() > 1); |
| 67 | + |
| 68 | + foreach (var duplicate in duplicates) |
| 69 | + { |
| 70 | + var groupName = string.Join(", ", duplicate.Select(d => d.Name)); |
| 71 | + |
| 72 | + foreach (var tuple in duplicate) |
| 73 | + { |
| 74 | + context.ReportDiagnostic( |
| 75 | + Diagnostic.Create( |
| 76 | + Rule, |
| 77 | + tuple.Location, |
| 78 | + groupName, |
| 79 | + tuple.Version |
| 80 | + ) |
| 81 | + ); |
| 82 | + } |
| 83 | + } |
| 84 | + }); |
| 85 | + } |
| 86 | + |
| 87 | + private static long? GetMigrationAttributeVersion(FluentMigratorContext fluentMigratorContext, IEnumerable<AttributeData> attributes) |
| 88 | + => attributes |
| 89 | + .FirstOrDefault(a => fluentMigratorContext.MigrationAttributeType.IsAssignableFrom(a.AttributeClass)) |
| 90 | + ?.ConstructorArguments[0].Value as long?; |
| 91 | + } |
| 92 | +} |
0 commit comments