-
Notifications
You must be signed in to change notification settings - Fork 243
Expand file tree
/
Copy pathProjectDescriptionReader.cs
More file actions
157 lines (130 loc) · 5.9 KB
/
ProjectDescriptionReader.cs
File metadata and controls
157 lines (130 loc) · 5.9 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
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.Json;
namespace Microsoft.DotNet.MSIdentity.Project
{
public class ProjectDescriptionReader
{
private const string ProjectTypeIdPrefix = "dotnet-";
private readonly IEnumerable<string> _files;
private List<ProjectDescription>? _projectDescriptions;
public ProjectDescriptionReader(IEnumerable<string> files)
{
_files = files;
}
public ProjectDescription? GetProjectDescription(string? projectTypeId)
{
if (!string.IsNullOrEmpty(projectTypeId) && !projectTypeId.Equals(ProjectTypeIdPrefix))
{
return ProjectDescriptions.FirstOrDefault(p => string.Equals(projectTypeId, p.Identifier));
}
// TODO: could be both a Web app and WEB API.
foreach (ProjectDescription projectDescription in ProjectDescriptions)
{
var mergedMatches = projectDescription.GetMergedMatchesForProjectType(ProjectDescriptions);
if (mergedMatches.Any(matches => HasMatch(matches)))
{
return projectDescription;
}
}
// If projectDescription cannot be inferred, default to Web App
return ProjectDescriptions.FirstOrDefault(p => string.Equals($"{ProjectTypeIdPrefix}{ProjectTypes.WebApp}", p.Identifier));
}
static readonly JsonSerializerOptions serializerOptionsWithComments = new JsonSerializerOptions
{
ReadCommentHandling = JsonCommentHandling.Skip
};
public List<ProjectDescription> ProjectDescriptions
{
get
{
_projectDescriptions ??= AppProvisioningTool.Properties
.Where(p => p.Name.StartsWith("dotnet") && p.PropertyType == typeof(byte[]))
.Select(p => GetProjectDescription(p))
.ToList();
return _projectDescriptions;
}
}
internal static ProjectDescription GetProjectDescription(PropertyInfo propertyInfo)
{
byte[] content = (propertyInfo.GetValue(null) as byte[])!;
string jsonText = Encoding.UTF8.GetString(content);
var projectDescription = JsonSerializer.Deserialize<ProjectDescription>(jsonText, serializerOptionsWithComments);
if (projectDescription == null)
{
throw new FormatException($"Resource file { propertyInfo.Name } could not be parsed. ");
}
if (!projectDescription.IsValid())
{
throw new FormatException($"Resource file {propertyInfo.Name} is missing Identitier or ProjectRelativeFolder is null. ");
}
return projectDescription;
}
/// <summary>
/// Checks for a match given a list of matches and a list of files
/// </summary>
/// <param name="matches"></param>
/// <returns></returns>
private bool HasMatch(MatchesForProjectType matches)
{
if (string.IsNullOrEmpty(matches.FileRelativePath))
{
return HasMatchingDirectory(matches.FolderRelativePath, matches.FileExtension, matches.MatchAny);
}
return HasFileWithMatch(matches.FileRelativePath, matches.FolderRelativePath, matches.MatchAny);
}
private bool HasMatchingDirectory(string? folderRelativePath, string? fileExtension, string[]? matchAny)
{
if (string.IsNullOrEmpty(folderRelativePath))
{
return false;
}
var matchingPaths = _files.Where(file => DirectoryMatches(file, folderRelativePath, fileExtension));
return AnyFileContainsMatch(matchAny, matchingPaths);
}
private bool HasFileWithMatch(string fileRelativePath, string? folderRelativePath, string[]? matchAny)
{
var matchingFilePaths = _files.Where(filePath => Path.GetFileName(filePath).Equals(fileRelativePath, StringComparison.OrdinalIgnoreCase));
if (!string.IsNullOrEmpty(folderRelativePath))
{
matchingFilePaths = matchingFilePaths.Where(filePath => DirectoryMatches(filePath, folderRelativePath));
}
return AnyFileContainsMatch(matchAny, matchingFilePaths);
}
private static bool DirectoryMatches(string filePath, string folderRelativePath, string? fileExtension = null)
{
var directoryPath = Path.GetDirectoryName(filePath);
var folderFound = directoryPath?.Contains(folderRelativePath, StringComparison.OrdinalIgnoreCase) ?? false;
var extensionMatches = fileExtension?.Equals(Path.GetExtension(filePath)) ?? true; // If extension is null, no need to match
return folderFound && extensionMatches;
}
private static bool AnyFileContainsMatch(string[]? matchAny, IEnumerable<string> matchingPaths)
{
if (matchAny is null)
{
return matchingPaths.Any();
}
var matchingFiles = matchingPaths.Where(filePath => FileMatches(filePath, matchAny));
return matchingFiles.Any(); // If MatchAny is not null, at least file needs to contain a match
}
private static bool FileMatches(string filePath, string[] matchAny)
{
try
{
var fileContent = File.ReadAllText(filePath);
var fileMatches = matchAny.Where(match => fileContent.Contains(match, StringComparison.OrdinalIgnoreCase));
return fileMatches.Any();
}
catch
{
return false;
}
}
}
}