-
Notifications
You must be signed in to change notification settings - Fork 4k
/
SourceFileResolver.cs
155 lines (128 loc) · 5.93 KB
/
SourceFileResolver.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using Microsoft.CodeAnalysis.PooledObjects;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis
{
/// <summary>
/// Resolves references to source files specified in source code.
/// </summary>
public class SourceFileResolver : SourceReferenceResolver, IEquatable<SourceFileResolver>
{
public static SourceFileResolver Default { get; } = new SourceFileResolver(ImmutableArray<string>.Empty, baseDirectory: null);
private readonly string? _baseDirectory;
private readonly ImmutableArray<string> _searchPaths;
private readonly ImmutableArray<KeyValuePair<string, string>> _pathMap;
public SourceFileResolver(IEnumerable<string> searchPaths, string? baseDirectory)
: this(searchPaths.AsImmutableOrNull(), baseDirectory)
{
}
public SourceFileResolver(ImmutableArray<string> searchPaths, string? baseDirectory)
: this(searchPaths, baseDirectory, ImmutableArray<KeyValuePair<string, string>>.Empty)
{
}
public SourceFileResolver(
ImmutableArray<string> searchPaths,
string? baseDirectory,
ImmutableArray<KeyValuePair<string, string>> pathMap)
{
if (searchPaths.IsDefault)
{
throw new ArgumentNullException(nameof(searchPaths));
}
if (baseDirectory != null && PathUtilities.GetPathKind(baseDirectory) != PathKind.Absolute)
{
throw new ArgumentException(CodeAnalysisResources.AbsolutePathExpected, nameof(baseDirectory));
}
_baseDirectory = baseDirectory;
_searchPaths = searchPaths;
// The previous public API required paths to not end with a path separator.
// This broke handling of root paths (e.g. "/" cannot be represented), so
// the new requirement is for paths to always end with a path separator.
// However, because this is a public API, both conventions must be allowed,
// so normalize the paths here (instead of enforcing end-with-sep).
if (!pathMap.IsDefaultOrEmpty)
{
var pathMapBuilder = ArrayBuilder<KeyValuePair<string, string>>.GetInstance(pathMap.Length);
foreach (var (key, value) in pathMap)
{
if (key == null || key.Length == 0)
{
throw new ArgumentException(CodeAnalysisResources.EmptyKeyInPathMap, nameof(pathMap));
}
if (value == null)
{
throw new ArgumentException(CodeAnalysisResources.NullValueInPathMap, nameof(pathMap));
}
var normalizedKey = PathUtilities.EnsureTrailingSeparator(key);
var normalizedValue = PathUtilities.EnsureTrailingSeparator(value);
pathMapBuilder.Add(new KeyValuePair<string, string>(normalizedKey, normalizedValue));
}
_pathMap = pathMapBuilder.ToImmutableAndFree();
}
else
{
_pathMap = ImmutableArray<KeyValuePair<string, string>>.Empty;
}
}
public string? BaseDirectory => _baseDirectory;
public ImmutableArray<string> SearchPaths => _searchPaths;
public ImmutableArray<KeyValuePair<string, string>> PathMap => _pathMap;
public override string? NormalizePath(string path, string? baseFilePath)
{
string? normalizedPath = FileUtilities.NormalizeRelativePath(path, baseFilePath, _baseDirectory);
return (normalizedPath == null || _pathMap.IsDefaultOrEmpty) ? normalizedPath : PathUtilities.NormalizePathPrefix(normalizedPath, _pathMap);
}
public override string? ResolveReference(string path, string? baseFilePath)
{
string? resolvedPath = FileUtilities.ResolveRelativePath(path, baseFilePath, _baseDirectory, _searchPaths, FileExists);
if (resolvedPath == null)
{
return null;
}
return FileUtilities.TryNormalizeAbsolutePath(resolvedPath);
}
public override Stream OpenRead(string resolvedPath)
{
CompilerPathUtilities.RequireAbsolutePath(resolvedPath, nameof(resolvedPath));
return FileUtilities.OpenRead(resolvedPath);
}
protected virtual bool FileExists([NotNullWhen(true)] string? resolvedPath)
{
return File.Exists(resolvedPath);
}
public override bool Equals(object? obj)
{
// Explicitly check that we're not comparing against a derived type
if (obj == null || GetType() != obj.GetType())
{
return false;
}
return Equals((SourceFileResolver)obj);
}
public bool Equals(SourceFileResolver? other)
{
if (other is null)
{
return false;
}
return
string.Equals(_baseDirectory, other._baseDirectory, StringComparison.Ordinal) &&
_searchPaths.SequenceEqual(other._searchPaths, StringComparer.Ordinal) &&
_pathMap.SequenceEqual(other._pathMap);
}
public override int GetHashCode()
{
return Hash.Combine(_baseDirectory != null ? StringComparer.Ordinal.GetHashCode(_baseDirectory) : 0,
Hash.Combine(Hash.CombineValues(_searchPaths, StringComparer.Ordinal),
Hash.CombineValues(_pathMap)));
}
}
}