forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandFactory.cs
More file actions
132 lines (111 loc) · 4.96 KB
/
Copy pathCommandFactory.cs
File metadata and controls
132 lines (111 loc) · 4.96 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
/********************************************************************++
Copyright (c) Microsoft Corporation. All rights reserved.
--********************************************************************/
using System;
using System.Text.RegularExpressions;
using System.Management.Automation;
namespace System.Management.Automation
{
/// <summary>
/// Command factory provides a generic interface to create different types of commands.
/// </summary>
internal class CommandFactory
{
#region private_members
private ExecutionContext context;
#endregion private_members
#region public_properties
/// <summary>
/// Execution context under which the command should be created.
/// </summary>
internal ExecutionContext Context
{
get {return context;}
set {context = value;}
}
#endregion public_properties
#region ctor
/// <summary>
/// Initializes the new instance of CommandFactory class.
/// </summary>
internal CommandFactory()
{
}
/// <summary>
/// Initializes the new instance of CommandFactory class.
/// </summary>
/// <param name="context">Execution context.</param>
internal CommandFactory(ExecutionContext context)
{
this.Context = context;
}
#endregion ctor
#region public_methods
/// <summary>
/// Creates a command object corresponing to specified name. The command processor will use global scope.
/// </summary>
/// <param name="commandName">Creates a command object corresponing to specified name.</param>
/// <param name="commandOrigin"> Location where the command was dispatched from. </param>
/// <returns>Created command processor object.</returns>
/// <exception cref="InvalidOperationException">
/// Thrown if session state does not contain the CommandDiscovery instance.
/// </exception>
internal CommandProcessorBase CreateCommand(string commandName, CommandOrigin commandOrigin)
{
return _CreateCommand(commandName, commandOrigin, false);
}
/// <summary>
/// Creates a command object corresponing to specified name.
/// </summary>
/// <param name="commandName">Creates a command object corresponing to specified name.</param>
/// <param name="commandOrigin"> Location where the command was dispatched from. </param>
/// <param name="useLocalScope">
/// True if command processor should use local scope to execute the command,
/// False otherwise.
/// </param>
/// <returns>Created command processor object.</returns>
/// <exception cref="InvalidOperationException">
/// Thrown if session state does not contain the CommandDiscovery instance.
/// </exception>
internal CommandProcessorBase CreateCommand(string commandName,
CommandOrigin commandOrigin, bool? useLocalScope)
{
return _CreateCommand(commandName, commandOrigin, useLocalScope);
}
/// <summary>
/// Creates a command object corresponing to specified name.
/// </summary>
/// <param name="commandName">Creates a command object corresponing to specified name.</param>
/// <param name="executionContext">Execution Context.</param>
/// <param name="commandOrigin"> Location where the command was dispatched from. </param>
/// <returns>Created command processor object.</returns>
/// <exception cref="InvalidOperationException">
/// Thrown if session state does not contain the CommandDiscovery instance.
/// </exception>
internal CommandProcessorBase CreateCommand(string commandName, ExecutionContext executionContext, CommandOrigin commandOrigin)
{
this.Context = executionContext;
return _CreateCommand(commandName, commandOrigin, false);
}
#endregion public_methods
#region helper_methods
private CommandProcessorBase _CreateCommand(string commandName,
CommandOrigin commandOrigin, bool? useLocalScope)
{
if (context == null)
{
throw PSTraceSource.NewInvalidOperationException(DiscoveryExceptions.ExecutionContextNotSet);
}
// Look for a cmdlet...
CommandDiscovery discovery = this.context.CommandDiscovery;
if (discovery == null)
{
throw PSTraceSource.NewInvalidOperationException(DiscoveryExceptions.CommandDiscoveryMissing);
}
// Look for the command using command discovery mechanisms. This will resolve
// aliases, functions, filters, cmdlets, scripts, and applications.
return discovery.LookupCommandProcessor(commandName, commandOrigin, useLocalScope);
}
#endregion helper_methods
}
}