-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDirectories.cs
75 lines (63 loc) · 2.52 KB
/
Directories.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Build.Framework;
using System.IO;
using System.Collections;
namespace MsBuild.ProtocolBuffers
{
internal class Directories : IEnumerable<KeyValuePair<DirectoryPair, List<string>>>
{
private Dictionary<DirectoryPair, List<string>> Dict { get; } = new Dictionary<DirectoryPair, List<string>>();
public IEnumerable<string> Files(DirectoryPair directoryPair)
=> Dict.TryGetValue(directoryPair, out List<string> value) ? value : Enumerable.Empty<string>();
public void Add(DirectoryPair pair, string file)
{
if (Dict.TryGetValue(pair, out List<string> value))
value.Add(file);
else
Dict.Add(pair, new List<string>(Enumerable.Repeat(file, 1)));
}
public void Add(ITaskItem item)
{
var itemSpec = item.ItemSpec;
var input = item.GetMetadata("RelativeDir") ?? Path.GetDirectoryName(itemSpec);
var output = Path.GetDirectoryName(item.GetMetadata("OutputSpec"));
var directoryPair = new DirectoryPair(input, output);
var file = Path.GetFileName(itemSpec);
Add(directoryPair, file);
}
public IEnumerator<KeyValuePair<DirectoryPair, List<string>>> GetEnumerator()
=> ((IEnumerable<KeyValuePair<DirectoryPair, List<string>>>)Dict).GetEnumerator();
IEnumerator IEnumerable.GetEnumerator()
=> ((IEnumerable<KeyValuePair<DirectoryPair, List<string>>>)Dict).GetEnumerator();
}
internal struct DirectoryPair : IEquatable<DirectoryPair>
{
public string InputDir { get; }
public string OutputDir { get; }
public DirectoryPair(string inputDir, string outputDir)
{
InputDir = inputDir ?? throw new ArgumentNullException(nameof(inputDir));
OutputDir = outputDir ?? throw new ArgumentNullException(nameof(outputDir));
}
public override string ToString()
{
return $"{InputDir} -> {OutputDir}";
}
public override bool Equals(object obj)
{
return Equals((DirectoryPair)obj);
}
public bool Equals(DirectoryPair other)
{
return other.InputDir == InputDir && other.OutputDir == OutputDir;
}
public override int GetHashCode()
{
return InputDir.GetHashCode() ^ OutputDir.GetHashCode();
}
}
}