-
Notifications
You must be signed in to change notification settings - Fork 473
/
Copy pathAvoidAssemblyLocationInSingleFile.cs
161 lines (141 loc) · 7.44 KB
/
AvoidAssemblyLocationInSingleFile.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using Analyzer.Utilities;
using Analyzer.Utilities.Extensions;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Operations;
namespace Microsoft.NetCore.Analyzers.Publish
{
/// <summary>
/// IL3000, IL3001: Do not use Assembly file path in single-file publish
/// </summary>
[DiagnosticAnalyzer(LanguageNames.CSharp, LanguageNames.VisualBasic)]
public sealed class AvoidAssemblyLocationInSingleFile : DiagnosticAnalyzer
{
public const string IL3000 = nameof(IL3000);
public const string IL3001 = nameof(IL3001);
internal static DiagnosticDescriptor LocationRule = DiagnosticDescriptorHelper.Create(
IL3000,
new LocalizableResourceString(nameof(MicrosoftNetCoreAnalyzersResources.AvoidAssemblyLocationInSingleFileTitle),
MicrosoftNetCoreAnalyzersResources.ResourceManager, typeof(MicrosoftNetCoreAnalyzersResources)),
new LocalizableResourceString(nameof(MicrosoftNetCoreAnalyzersResources.AvoidAssemblyLocationInSingleFileMessage),
MicrosoftNetCoreAnalyzersResources.ResourceManager, typeof(MicrosoftNetCoreAnalyzersResources)),
DiagnosticCategory.Publish,
RuleLevel.BuildWarning,
description: null,
isPortedFxCopRule: false,
isDataflowRule: false);
internal static DiagnosticDescriptor GetFilesRule = DiagnosticDescriptorHelper.Create(
IL3001,
new LocalizableResourceString(nameof(MicrosoftNetCoreAnalyzersResources.AvoidAssemblyLocationInSingleFileTitle),
MicrosoftNetCoreAnalyzersResources.ResourceManager, typeof(MicrosoftNetCoreAnalyzersResources)),
new LocalizableResourceString(nameof(MicrosoftNetCoreAnalyzersResources.AvoidAssemblyGetFilesInSingleFileMessage),
MicrosoftNetCoreAnalyzersResources.ResourceManager, typeof(MicrosoftNetCoreAnalyzersResources)),
DiagnosticCategory.Publish,
RuleLevel.BuildWarning,
description: null,
isPortedFxCopRule: false,
isDataflowRule: false);
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(LocationRule, GetFilesRule);
public override void Initialize(AnalysisContext context)
{
context.EnableConcurrentExecution();
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.ReportDiagnostics);
context.RegisterCompilationStartAction(context =>
{
var compilation = context.Compilation;
var isSingleFilePublish = context.Options.GetMSBuildPropertyValue(
MSBuildPropertyOptionNames.PublishSingleFile, compilation, context.CancellationToken);
if (!string.Equals(isSingleFilePublish?.Trim(), "true", StringComparison.OrdinalIgnoreCase))
{
return;
}
var includesAllContent = context.Options.GetMSBuildPropertyValue(
MSBuildPropertyOptionNames.IncludeAllContentForSelfExtract, compilation, context.CancellationToken);
if (string.Equals(includesAllContent?.Trim(), "true", StringComparison.OrdinalIgnoreCase))
{
return;
}
var propertiesBuilder = ImmutableArray.CreateBuilder<IPropertySymbol>();
var methodsBuilder = ImmutableArray.CreateBuilder<IMethodSymbol>();
if (compilation.TryGetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemReflectionAssembly, out var assemblyType))
{
// properties
AddIfNotNull(propertiesBuilder, TryGetSingleSymbol<IPropertySymbol>(assemblyType.GetMembers("Location")));
// methods
methodsBuilder.AddRange(assemblyType.GetMembers("GetFile").OfType<IMethodSymbol>());
methodsBuilder.AddRange(assemblyType.GetMembers("GetFiles").OfType<IMethodSymbol>());
}
if (compilation.TryGetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemReflectionAssemblyName, out var assemblyNameType))
{
AddIfNotNull(propertiesBuilder, TryGetSingleSymbol<IPropertySymbol>(assemblyNameType.GetMembers("CodeBase")));
AddIfNotNull(propertiesBuilder, TryGetSingleSymbol<IPropertySymbol>(assemblyNameType.GetMembers("EscapedCodeBase")));
}
var properties = propertiesBuilder.ToImmutable();
var methods = methodsBuilder.ToImmutable();
context.RegisterOperationAction(operationContext =>
{
var access = (IPropertyReferenceOperation)operationContext.Operation;
var property = access.Property;
if (!Contains(properties, property, SymbolEqualityComparer.Default))
{
return;
}
operationContext.ReportDiagnostic(access.CreateDiagnostic(LocationRule, property));
}, OperationKind.PropertyReference);
context.RegisterOperationAction(operationContext =>
{
var invocation = (IInvocationOperation)operationContext.Operation;
var targetMethod = invocation.TargetMethod;
if (!Contains(methods, targetMethod, SymbolEqualityComparer.Default))
{
return;
}
operationContext.ReportDiagnostic(invocation.CreateDiagnostic(GetFilesRule, targetMethod));
}, OperationKind.Invocation);
return;
static bool Contains<T, TComp>(ImmutableArray<T> list, T elem, TComp comparer)
where TComp : IEqualityComparer<T>
{
foreach (var e in list)
{
if (comparer.Equals(e, elem))
{
return true;
}
}
return false;
}
static TSymbol? TryGetSingleSymbol<TSymbol>(ImmutableArray<ISymbol> members) where TSymbol : class, ISymbol
{
TSymbol? candidate = null;
foreach (var m in members)
{
if (m is TSymbol tsym)
{
if (candidate is null)
{
candidate = tsym;
}
else
{
return null;
}
}
}
return candidate;
}
static void AddIfNotNull<TSymbol>(ImmutableArray<TSymbol>.Builder properties, TSymbol? p) where TSymbol : class, ISymbol
{
if (p is not null)
{
properties.Add(p);
}
}
});
}
}
}