-
Notifications
You must be signed in to change notification settings - Fork 222
/
ParameterLoader.cs
91 lines (83 loc) · 3.71 KB
/
ParameterLoader.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
/*
* SonarAnalyzer for .NET
* Copyright (C) 2015-2023 SonarSource SA
* mailto: contact AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
using System.Globalization;
using System.Reflection;
namespace SonarAnalyzer.Helpers
{
internal static class ParameterLoader
{
/**
* At each compilation, parse the configuration file and set the rule parameters on
*
* There is no caching mechanism because inside the IDE, the configuration file can change when the user:
* - changes something inside the configuration file
* - loads a different solution in the IDE
*
* If caching needs to be done in the future, it should take into account:
* - diffing the contents of the configuration file
* - associating the file with a unique identifier for the build project
*/
internal static void SetParameterValues(ParametrizedDiagnosticAnalyzer parameteredAnalyzer, SonarLintXmlReader sonarLintXml)
{
if (!sonarLintXml.ParametrizedRules.Any())
{
return;
}
var propertyParameterPairs = parameteredAnalyzer.GetType()
.GetRuntimeProperties()
.Select(x => new { Property = x, Descriptor = x.GetCustomAttributes<RuleParameterAttribute>().SingleOrDefault() })
.Where(x => x.Descriptor is not null);
var ids = new HashSet<string>(parameteredAnalyzer.SupportedDiagnostics.Select(diagnostic => diagnostic.Id));
foreach (var propertyParameterPair in propertyParameterPairs)
{
var parameter = sonarLintXml.ParametrizedRules.FirstOrDefault(x => ids.Contains(x.Key));
var parameterValue = parameter?.Parameters.FirstOrDefault(x => x.Key == propertyParameterPair.Descriptor.Key);
if (TryConvertToParameterType(parameterValue?.Value, propertyParameterPair.Descriptor.Type, out var value))
{
propertyParameterPair.Property.SetValue(parameteredAnalyzer, value);
}
}
}
private static bool TryConvertToParameterType(string parameter, PropertyType type, out object result)
{
if (parameter == null)
{
result = null;
return false;
}
switch (type)
{
case PropertyType.Text:
case PropertyType.String:
result = parameter;
return true;
case PropertyType.Integer when int.TryParse(parameter, NumberStyles.None, CultureInfo.InvariantCulture, out var parsedInt):
result = parsedInt;
return true;
case PropertyType.Boolean when bool.TryParse(parameter, out var parsedBool):
result = parsedBool;
return true;
default:
result = null;
return false;
}
}
}
}