Repository navigation
Expand file tree
/
Copy pathIPowerShellRuntime.cs
More file actions
100 lines (80 loc) · 3.34 KB
/
Copy pathIPowerShellRuntime.cs
File metadata and controls
100 lines (80 loc) · 3.34 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
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Management.Automation;
using System.Threading.Tasks;
using SMA = System.Management.Automation;
namespace Microsoft.PowerShell.ScriptAnalyzer.Runtime
{
public interface IPowerShellCommandDatabase
{
IReadOnlyList<string> GetCommandAliases(string command);
string GetAliasTarget(string alias);
IReadOnlyList<string> GetAllNamesForCommand(string command);
}
public class SessionStateCommandDatabase : IPowerShellCommandDatabase
{
public static SessionStateCommandDatabase Create(CommandInvocationIntrinsics invokeCommandProvider)
{
var commandAliases = new Dictionary<string, IReadOnlyList<string>>(StringComparer.OrdinalIgnoreCase);
var aliasTargets = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
foreach (AliasInfo aliasInfo in invokeCommandProvider.GetCommands("*", CommandTypes.Alias, nameIsPattern: true))
{
aliasTargets[aliasInfo.Name] = aliasInfo.Definition;
if (commandAliases.TryGetValue(aliasInfo.Definition, out IReadOnlyList<string> aliases))
{
((List<string>)aliases).Add(aliasInfo.Name);
}
else
{
commandAliases[aliasInfo.Definition] = new List<string> { aliasInfo.Name };
}
}
return new SessionStateCommandDatabase(commandAliases, aliasTargets);
}
private readonly IReadOnlyDictionary<string, string> _aliasTargets;
private readonly IReadOnlyDictionary<string, IReadOnlyList<string>> _commandAliases;
private readonly ConcurrentDictionary<string, IReadOnlyList<string>> _commandNames;
private SessionStateCommandDatabase(
IReadOnlyDictionary<string, IReadOnlyList<string>> commandAliases,
IReadOnlyDictionary<string, string> aliasTargets)
{
_aliasTargets = aliasTargets;
_commandAliases = commandAliases;
_commandNames = new ConcurrentDictionary<string, IReadOnlyList<string>>(StringComparer.OrdinalIgnoreCase);
}
public string GetAliasTarget(string alias)
{
if (_aliasTargets.TryGetValue(alias, out string target))
{
return target;
}
return null;
}
public IReadOnlyList<string> GetCommandAliases(string command)
{
if (_commandAliases.TryGetValue(command, out IReadOnlyList<string> aliases))
{
return aliases;
}
return null;
}
public IReadOnlyList<string> GetAllNamesForCommand(string command)
{
return _commandNames.GetOrAdd(command, GenerateCommandNameList);
}
private IReadOnlyList<string> GenerateCommandNameList(string command)
{
var names = new List<string>();
if (_commandAliases.TryGetValue(command, out IReadOnlyList<string> aliases))
{
names.AddRange(aliases);
}
if (_aliasTargets.TryGetValue(command, out string target))
{
names.Add(target);
}
return names.Count > 0 ? names : null;
}
}
}