-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCheckAssemblyInfo.cs
More file actions
135 lines (104 loc) · 6.7 KB
/
Copy pathCheckAssemblyInfo.cs
File metadata and controls
135 lines (104 loc) · 6.7 KB
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
/*
* This file is part of the Buildings and Habitats object Model (BHoM)
* Copyright (c) 2015 - 2025, the respective contributors. All rights reserved.
*
* Each contributor holds copyright over their respective contributions.
* The project versioning (Git) records all such contribution source information.
*
*
* The BHoM 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.0 of the License, or
* (at your option) any later version.
*
* The BHoM 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 code. If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
*/
using BH.oM.Test.CodeCompliance;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Xml.Linq;
using BH.oM.Test.CodeCompliance.Attributes;
using BH.oM.Test;
using BH.oM.Test.Results;
namespace BH.Engine.Test.CodeCompliance
{
public static partial class Compute
{
public static TestResult CheckAssemblyInfo(this string assemblyInfoPath, string assemblyDescriptionOrg)
{
if(string.IsNullOrWhiteSpace(assemblyInfoPath))
return Create.TestResult(TestStatus.Pass);
if (Path.GetFileName(assemblyInfoPath) != "AssemblyInfo.cs")
return Create.TestResult(TestStatus.Pass);
if (!File.Exists(assemblyInfoPath))
return Create.TestResult(TestStatus.Pass);
TestResult finalResult = Create.TestResult(TestStatus.Pass);
string documentationLink = "AssemblyInfo-compliance";
List<string> fileLines = ReadFileContents(assemblyInfoPath);
finalResult = CheckAssemblyVersion(fileLines, finalResult, assemblyInfoPath, documentationLink);
finalResult = CheckAssemblyFileVersion(fileLines, finalResult, assemblyInfoPath, documentationLink);
finalResult = CheckAssemblyDescription(fileLines, finalResult, assemblyInfoPath, documentationLink, assemblyDescriptionOrg);
return finalResult;
}
private static TestResult CheckAssemblyVersion(this List<string> fileLines, TestResult finalResult, string assemblyInfoPath, string documentationLink)
{
string currentAssemblyVersion = Query.FullCurrentAssemblyVersion();
string searchLine = $"[assembly: AssemblyVersion(\"";
string foundLine = fileLines.Where(x => x.StartsWith(searchLine)).FirstOrDefault();
if (string.IsNullOrEmpty(foundLine))
return finalResult.Merge(Create.TestResult(TestStatus.Error, new List<Error> { Create.Error($"Assembly Version should be set to {currentAssemblyVersion}", Create.Location(assemblyInfoPath, Create.LineSpan(1, 1)), documentationLink) }));
else
{
string allowedLine = $"{searchLine}{Query.FullCurrentAssemblyVersion()}\")]";
int line = fileLines.IndexOf(foundLine) + 1;
if (!foundLine.Contains(allowedLine))
return finalResult.Merge(Create.TestResult(TestStatus.Error, new List<Error> { Create.Error($"Assembly Version should be set to {currentAssemblyVersion}", Create.Location(assemblyInfoPath, Create.LineSpan(line, line)), documentationLink) }));
}
return finalResult;
}
private static TestResult CheckAssemblyFileVersion(this List<string> fileLines, TestResult finalResult, string assemblyInfoPath, string documentationLink)
{
string currentAssemblyVersion = Query.FullCurrentAssemblyFileVersion();
string searchLine = $"[assembly: AssemblyFileVersion(\"";
string foundLine = fileLines.Where(x => x.StartsWith(searchLine)).FirstOrDefault();
if (string.IsNullOrEmpty(foundLine))
return finalResult.Merge(Create.TestResult(TestStatus.Error, new List<Error> { Create.Error($"Assembly File Version should be set to {currentAssemblyVersion}", Create.Location(assemblyInfoPath, Create.LineSpan(1, 1)), documentationLink) }));
else
{
string allowedLine = $"{searchLine}{Query.FullCurrentAssemblyFileVersion()}\")]";
int line = fileLines.IndexOf(foundLine) + 1;
if (!foundLine.Contains(allowedLine))
return finalResult.Merge(Create.TestResult(TestStatus.Error, new List<Error> { Create.Error($"Assembly File Version should be set to {currentAssemblyVersion}", Create.Location(assemblyInfoPath, Create.LineSpan(line, line)), documentationLink) }));
}
return finalResult;
}
private static TestResult CheckAssemblyDescription(this List<string> fileLines, TestResult finalResult, string assemblyInfoPath, string documentationLink, string descriptionUrl)
{
string searchLine = $"[assembly: AssemblyDescription(\"";
string foundLine = fileLines.Where(x => x.StartsWith(searchLine)).FirstOrDefault();
if (string.IsNullOrEmpty(foundLine))
return finalResult.Merge(Create.TestResult(TestStatus.Error, new List<Error> { Create.Error($"Assembly Description should contain the URL to the GitHub organisation which owns the repository", Create.Location(assemblyInfoPath, Create.LineSpan(1, 1)), documentationLink) }));
else
{
string allowedLine = $"{searchLine}{descriptionUrl}\")]";
int line = fileLines.IndexOf(foundLine) + 1;
if (!foundLine.Contains(allowedLine))
return finalResult.Merge(Create.TestResult(TestStatus.Error, new List<Error> { Create.Error($"Assembly Description should contain the URL to the GitHub organisation which owns the repository", Create.Location(assemblyInfoPath, Create.LineSpan(line, line)), documentationLink) }));
}
return finalResult;
}
}
}