Skip to content

Commit 7fa07e4

Browse files
committed
Determined that module qualified cmdlets MUST be added as aliases and cannot be added as Cmdlets themselves. Added logic to correct Cmdlet and Alias entries in InitialSessionState such that subsequent calls to these cmdlets work as expected.
1 parent c616d61 commit 7fa07e4

File tree

1 file changed

+91
-7
lines changed

1 file changed

+91
-7
lines changed

src/PowerShellEditorServices/Services/PowerShellContext/PowerShellContextService.cs

Lines changed: 91 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,102 @@ namespace Microsoft.PowerShell.EditorServices.Services
2929

3030
public static class InitialSessionStateExtensions
3131
{
32-
public static void AddCommandAndAliasToInitialSessionState<T>(this InitialSessionState iss, string alias)
32+
public static void AddCommandAndAliasToInitialSessionState<T>(this InitialSessionState iss, string moduleQualifiedCmdletName)
3333
{
34-
var shortName = alias.Split('\\').LastOrDefault();
35-
36-
if(!iss.Commands.Any(a => string.Compare(a.Name, shortName, true) == 0))
34+
var shortName = moduleQualifiedCmdletName.Split('\\').LastOrDefault();
35+
var existingShortCmdlet = iss.Commands.FirstOrDefault(a => string.Compare(a.Name, shortName, true) == 0);
36+
var existingLongCmdlet = iss.Commands.FirstOrDefault(a => string.Compare(a.Name, moduleQualifiedCmdletName, true) == 0);
37+
// Can't have both short and long defined as CmdletEntries. One has to be an alias.
38+
if(existingShortCmdlet is not SessionStateCmdletEntry)
3739
{
40+
if(existingShortCmdlet is not null)
41+
{
42+
iss.Commands.Remove(shortName, existingShortCmdlet.GetType());
43+
}
3844
iss.Commands.Add(new SessionStateCmdletEntry(shortName, typeof(T), null));
3945
}
40-
if(!iss.Commands.Any(a => string.Compare(a.Name, alias) == 0))
46+
if(existingLongCmdlet is not SessionStateAliasEntry)
4147
{
42-
iss.Commands.Add(new SessionStateAliasEntry(alias, shortName, null));
43-
}
48+
if(existingLongCmdlet is not null)
49+
{
50+
iss.Commands.Remove(moduleQualifiedCmdletName, existingLongCmdlet.GetType());
51+
}
52+
iss.Commands.Add(new SessionStateAliasEntry(moduleQualifiedCmdletName, shortName, null));
53+
//throw new Exception($"Unsupported state, {moduleQualifiedCmdletName} and {shortName} defined as Cmdlet types. One needs to be an alias of the other");
54+
}
55+
// if(existingShortCmdlet is SessionStateCmdletEntry)
56+
// {
57+
// if(existingLongCmdlet is null)
58+
// {
59+
// iss.Commands.Add(new SessionStateAliasEntry(shortName, moduleQualifiedCmdletName, null));
60+
// }
61+
// else
62+
// {
63+
64+
// }
65+
// }
66+
// }
67+
68+
// if(existingLongCmdlet is not null)
69+
// else if(existingShortCmdlet is SessionStateAliasEntry)
70+
// {
71+
// if(existingLongCmdlet is null)
72+
// {
73+
// iss.Commands.Add(new SessionStateCmdletEntry(moduleQualifiedCmdletName, typeof(T), null));
74+
// }
75+
// else
76+
// {
77+
// if(existingLongCmdlet is not null && existingLongCmdlet is SessionStateAliasEntry)
78+
// {
79+
// throw new Exception($"Unsupported state, {moduleQualifiedCmdletName} and {shortName} defined as alias types. One needs to be an alias of the other");
80+
// }
81+
// }
82+
// }
83+
// else
84+
// {
85+
// throw new Exception($"Unsupported state, {shortName} exists as an unsupported type. {existingShortCmdlet.GetType().Name}. Needs to be SessionStateCmdletEntry or SessionStateAliasEntry.");
86+
// }
87+
// }
88+
// else
89+
// {
90+
// if(existingLongCmdlet is null)
91+
// {
92+
// if(existingShortCmdlet is null)
93+
// {
94+
// //iss.Commands.Add(new SessionStateCmdletEntry(moduleQualifiedCmdletName, typeof(T), null));
95+
// //iss.Commands.Add(new SessionStateAliasEntry(shortName, moduleQualifiedCmdletName, null));
96+
// iss.Commands.Add(new SessionStateCmdletEntry(shortName, typeof(T), null));
97+
// iss.Commands.Add(new SessionStateAliasEntry(moduleQualifiedCmdletName, shortName, null));
98+
// }
99+
// else
100+
// {
101+
// if(existingShortCmdlet is SessionStateAliasEntry)
102+
// {
103+
// iss.Commands.Add(new SessionStateCmdletEntry(moduleQualifiedCmdletName, typeof(T), null));
104+
// }
105+
// else if(existingShortCmdlet is SessionStateCmdletEntry)
106+
// {
107+
// iss.Commands.Add(new SessionStateAliasEntry(moduleQualifiedCmdletName, shortName, null));
108+
// }
109+
// else
110+
// {
111+
// throw new Exception($"Unsupported state, {shortName} exists as an unsupported type. {existingShortCmdlet.GetType().Name}. Needs to be SessionStateCmdletEntry or SessionStateAliasEntry.");
112+
// }
113+
// }
114+
115+
// }
116+
// else
117+
// {
118+
// if((existingLongCmdlet is SessionStateAliasEntry && existingShortCmdlet is SessionStateCmdletEntry) || (existingLongCmdlet is SessionStateCmdletEntry && existingShortCmdlet is SessionStateAliasEntry))
119+
// {
120+
// // All good
121+
// }
122+
// else
123+
// {
124+
// throw new Exception($"Unsupported state, {shortName} defined as type {existingShortCmdlet.GetType().Name} type and {moduleQualifiedCmdletName} defined as {existingLongCmdlet.GetType().Name}. One needs to be an alias of the other.");
125+
// }
126+
// }
127+
// }
44128
}
45129
}
46130
/// <summary>

0 commit comments

Comments
 (0)