Skip to content

Commit ada91c6

Browse files
alpha sort all code
1 parent 615ebcd commit ada91c6

File tree

11 files changed

+210
-221
lines changed

11 files changed

+210
-221
lines changed

src/Azure.Functions.PowerShell.Worker.Messaging/FunctionMessagingClient.cs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,31 @@ namespace Azure.Functions.PowerShell.Worker.Messaging
1414
{
1515
public class FunctionMessagingClient : IDisposable
1616
{
17-
public bool isDisposed;
18-
AsyncDuplexStreamingCall<StreamingMessage, StreamingMessage> _call;
1917
SemaphoreSlim _writeStreamHandle = new SemaphoreSlim(1, 1);
18+
AsyncDuplexStreamingCall<StreamingMessage, StreamingMessage> _call;
19+
public bool isDisposed;
2020

2121
public FunctionMessagingClient(string host, int port)
2222
{
2323
Channel channel = new Channel(host, port, ChannelCredentials.Insecure);
2424
_call = new FunctionRpc.FunctionRpcClient(channel).EventStream();
2525
}
2626

27+
public void Dispose()
28+
{
29+
if (!isDisposed)
30+
{
31+
isDisposed = true;
32+
_call.Dispose();
33+
}
34+
}
35+
36+
public StreamingMessage GetCurrentMessage() =>
37+
isDisposed ? null : _call.ResponseStream.Current;
38+
39+
public async Task<bool> MoveNext() =>
40+
!isDisposed && await _call.ResponseStream.MoveNext(CancellationToken.None);
41+
2742
public async Task WriteAsync(StreamingMessage message)
2843
{
2944
if(isDisposed) return;
@@ -40,20 +55,5 @@ public async Task WriteAsync(StreamingMessage message)
4055
_writeStreamHandle.Release();
4156
}
4257
}
43-
44-
public async Task<bool> MoveNext() =>
45-
!isDisposed && await _call.ResponseStream.MoveNext(CancellationToken.None);
46-
47-
public StreamingMessage GetCurrentMessage() =>
48-
isDisposed ? null : _call.ResponseStream.Current;
49-
50-
public void Dispose()
51-
{
52-
if (!isDisposed)
53-
{
54-
isDisposed = true;
55-
_call.Dispose();
56-
}
57-
}
5858
}
5959
}

src/Azure.Functions.PowerShell.Worker/Function/FunctionInfo.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ namespace Microsoft.Azure.Functions.PowerShellWorker
1010
{
1111
public class FunctionInfo
1212
{
13-
public string Name {get; private set;}
14-
public string Directory {get; private set;}
1513
public MapField<string, BindingInfo> Bindings {get; private set;}
16-
public MapField<string, BindingInfo> OutputBindings {get; private set;}
14+
public string Directory {get; private set;}
1715
public string HttpOutputName {get; private set;}
16+
public string Name {get; private set;}
17+
public MapField<string, BindingInfo> OutputBindings {get; private set;}
1818

1919
public FunctionInfo(RpcFunctionMetadata metadata)
2020
{

src/Azure.Functions.PowerShell.Worker/Function/FunctionLoader.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ namespace Microsoft.Azure.Functions.PowerShellWorker
1111
public class FunctionLoader
1212
{
1313
readonly MapField<string, Function> _LoadedFunctions = new MapField<string, Function>();
14+
15+
public (string ScriptPath, string EntryPoint) GetFunc(string functionId) =>
16+
(_LoadedFunctions[functionId].ScriptPath, _LoadedFunctions[functionId].EntryPoint);
17+
18+
public FunctionInfo GetInfo(string functionId) => _LoadedFunctions[functionId].Info;
19+
1420
public void Load(string functionId, RpcFunctionMetadata metadata)
1521
{
1622
// TODO: catch "load" issues at "func start" time.
@@ -22,16 +28,12 @@ public void Load(string functionId, RpcFunctionMetadata metadata)
2228
EntryPoint = metadata.EntryPoint
2329
});
2430
}
25-
26-
public FunctionInfo GetInfo(string functionId) => _LoadedFunctions[functionId].Info;
27-
public (string ScriptPath, string EntryPoint) GetFunc(string functionId) =>
28-
(_LoadedFunctions[functionId].ScriptPath, _LoadedFunctions[functionId].EntryPoint);
2931
}
3032

3133
public class Function
3234
{
35+
public string EntryPoint {get; internal set;}
3336
public FunctionInfo Info {get; internal set;}
3437
public string ScriptPath {get; internal set;}
35-
public string EntryPoint {get; internal set;}
3638
}
3739
}

src/Azure.Functions.PowerShell.Worker/Http/HttpRequestContext.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ namespace Microsoft.Azure.Functions.PowerShellWorker
99
{
1010
public class HttpRequestContext
1111
{
12+
public object Body {get; set;}
13+
public MapField<string, string> Headers {get; set;}
1214
public string Method {get; set;}
1315
public string Url {get; set;}
1416
public string OriginalUrl {get; set;}
15-
public MapField<string, string> Headers {get; set;}
16-
public MapField<string, string> Query {get; set;}
1717
public MapField<string, string> Params {get; set;}
18-
public object Body {get; set;}
18+
public MapField<string, string> Query {get; set;}
1919
public object RawBody {get; set;}
2020
}
2121
}

src/Azure.Functions.PowerShell.Worker/Http/HttpResponseContext.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ namespace Microsoft.Azure.Functions.PowerShellWorker
99
{
1010
public class HttpResponseContext
1111
{
12-
public string StatusCode {get; set;} = "200";
13-
public Hashtable Headers {get; set;} = new Hashtable();
1412
public object Body {get; set;}
1513
public string ContentType {get; set;} = "text/plain";
1614
public bool EnableContentNegotiation {get; set;} = false;
15+
public Hashtable Headers {get; set;} = new Hashtable();
16+
public string StatusCode {get; set;} = "200";
1717
}
1818
}

src/Azure.Functions.PowerShell.Worker/PowerShell/Host/AzureFunctionsHost.cs

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -21,49 +21,42 @@ class AzureFunctionsHost : PSHost
2121
/// <summary>
2222
/// The private reference of the logger.
2323
/// </summary>
24-
RpcLogger _logger;
24+
RpcLogger _logger { get; set; }
2525

2626
/// <summary>
2727
/// Creates an instance of the PSHostUserInterface object for this
2828
/// application.
2929
/// </summary>
30-
HostUserInterface HostUI;
30+
HostUserInterface HostUI { get; set; }
3131

3232
/// <summary>
3333
/// The culture info of the thread that created
3434
/// this object.
3535
/// </summary>
36-
CultureInfo originalCultureInfo = System.Threading.Thread.CurrentThread.CurrentCulture;
36+
readonly CultureInfo originalCultureInfo = System.Threading.Thread.CurrentThread.CurrentCulture;
3737

3838
/// <summary>
3939
/// The UI culture info of the thread that created
4040
/// this object.
4141
/// </summary>
42-
CultureInfo originalUICultureInfo = System.Threading.Thread.CurrentThread.CurrentUICulture;
42+
readonly CultureInfo originalUICultureInfo = System.Threading.Thread.CurrentThread.CurrentUICulture;
4343

4444
/// <summary>
4545
/// The identifier of the PSHost implementation.
4646
/// </summary>
4747
Guid Id = Guid.NewGuid();
4848

49-
/// <summary>
50-
/// Initializes a new instance of the Host class. Keep
51-
/// a reference to the hosting application object so it can
52-
/// be informed of when to exit.
53-
/// </summary>
54-
/// <param name="program">A reference to the host application object.</param>
55-
5649
/// <summary>
5750
/// Gets the culture info to use - this implementation just snapshots the
5851
/// curture info of the thread that created this object.
5952
/// </summary>
60-
public override System.Globalization.CultureInfo CurrentCulture => originalCultureInfo;
61-
53+
public override CultureInfo CurrentCulture => originalCultureInfo;
54+
6255
/// <summary>
6356
/// Gets the UI culture info to use - this implementation just snapshots the
6457
/// UI curture info of the thread that created this object.
6558
/// </summary>
66-
public override System.Globalization.CultureInfo CurrentUICulture => originalUICultureInfo;
59+
public override CultureInfo CurrentUICulture => originalUICultureInfo;
6760

6861
/// <summary>
6962
/// Gets an identifier for this host. This implementation always returns
@@ -98,44 +91,38 @@ public AzureFunctionsHost(RpcLogger logger)
9891
/// <summary>
9992
/// Not implemented by this class. The call fails with an exception.
10093
/// </summary>
101-
public override void EnterNestedPrompt()
102-
=> throw new NotImplementedException("The method or operation is not implemented.");
94+
public override void EnterNestedPrompt() =>
95+
throw new NotImplementedException("The method or operation is not implemented.");
10396

10497
/// <summary>
10598
/// Not implemented by this class. The call fails with an exception.
10699
/// </summary>
107-
public override void ExitNestedPrompt()
108-
=> throw new NotImplementedException("The method or operation is not implemented.");
100+
public override void ExitNestedPrompt() =>
101+
throw new NotImplementedException("The method or operation is not implemented.");
109102

110103
/// <summary>
111104
/// This API is called before an external application process is started. Typically
112105
/// it's used to save state that the child process may alter so the parent can
113106
/// restore that state when the child exits. In this, we don't need this so
114107
/// the method simple returns.
115108
/// </summary>
116-
public override void NotifyBeginApplication()
117-
{
118-
return; // Do nothing.
119-
}
109+
public override void NotifyBeginApplication() { return; } // Do nothing.
120110

121111
/// <summary>
122112
/// This API is called after an external application process finishes. Typically
123113
/// it's used to restore state that the child process may have altered. In this,
124114
/// we don't need this so the method simple returns.
125115
/// </summary>
126-
public override void NotifyEndApplication()
127-
{
128-
return; // Do nothing.
129-
}
116+
public override void NotifyEndApplication() { return; } // Do nothing.
130117

131118
/// <summary>
132119
/// Indicate to the host application that exit has
133120
/// been requested. Pass the exit code that the host
134121
/// application should use when exiting the process.
135122
/// </summary>
136123
/// <param name="exitCode">The exit code that the host application should use.</param>
137-
public override void SetShouldExit(int exitCode)
138-
=> throw new NotImplementedException("The method or operation is not implemented.");
124+
public override void SetShouldExit(int exitCode) =>
125+
throw new NotImplementedException("The method or operation is not implemented.");
139126
}
140127
}
141128

src/Azure.Functions.PowerShell.Worker/PowerShell/Host/HostUserInterface.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ class HostUserInterface : PSHostUserInterface
2424
/// <summary>
2525
/// The private reference of the logger.
2626
/// </summary>
27-
RpcLogger _logger;
27+
RpcLogger _logger { get; set; }
2828

2929
/// <summary>
3030
/// An instance of the PSRawUserInterface object.
3131
/// </summary>
32-
RawUserInterface RawUi = new RawUserInterface();
32+
readonly RawUserInterface RawUi = new RawUserInterface();
3333

3434
/// <summary>
3535
/// Gets an instance of the PSRawUserInterface object for this host

src/Azure.Functions.PowerShell.Worker/PowerShell/PowerShellWorkerExtensions.cs

Lines changed: 48 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -21,40 +21,45 @@ public static class PowerShellWorkerExtensions
2121
// It logs the item that comes and stores it as the $return out binding.
2222
// The last item stored as $return will be returned to the function host.
2323

24-
static string s_LogAndSetReturnValueScript = @"
24+
readonly static string s_LogAndSetReturnValueScript = @"
2525
param([Parameter(ValueFromPipeline=$true)]$return)
2626
2727
$return | Out-Default
2828
2929
Set-Variable -Name '$return' -Value $return -Scope global
3030
";
3131

32-
public static PowerShell SetGlobalVariables(this PowerShell ps, Hashtable triggerMetadata, IList<ParameterBinding> inputData)
32+
static string BuildBindingHashtableScript(IDictionary<string, BindingInfo> outBindings)
3333
{
34-
try {
35-
// Set the global $Context variable which contains trigger metadata
36-
ps.AddCommand("Set-Variable").AddParameters( new Hashtable {
37-
{ "Name", "Context"},
38-
{ "Scope", "Global"},
39-
{ "Value", triggerMetadata}
40-
}).Invoke();
34+
// Since all of the out bindings are stored in variables at this point,
35+
// we must construct a script that will return those output bindings in a hashtable
36+
StringBuilder script = new StringBuilder();
37+
script.AppendLine("@{");
38+
foreach (KeyValuePair<string, BindingInfo> binding in outBindings)
39+
{
40+
script.Append("'");
41+
script.Append(binding.Key);
4142

42-
// Sets a global variable for each input binding
43-
foreach (ParameterBinding binding in inputData)
43+
// since $return has a dollar sign, we have to treat it differently
44+
if (binding.Key == "$return")
4445
{
45-
ps.AddCommand("Set-Variable").AddParameters( new Hashtable {
46-
{ "Name", binding.Name},
47-
{ "Scope", "Global"},
48-
{ "Value", binding.Data.ToObject()}
49-
}).Invoke();
46+
script.Append("' = ");
5047
}
51-
return ps;
52-
}
53-
catch(Exception e)
54-
{
55-
ps.CleanupRunspace();
56-
throw e;
48+
else
49+
{
50+
script.Append("' = $");
51+
}
52+
script.AppendLine(binding.Key);
5753
}
54+
script.AppendLine("}");
55+
56+
return script.ToString();
57+
}
58+
59+
// TODO: make sure this completely cleans up the runspace
60+
static void CleanupRunspace(this PowerShell ps)
61+
{
62+
ps.Commands.Clear();
5863
}
5964

6065
public static PowerShell InvokeFunctionAndSetGlobalReturn(this PowerShell ps, string scriptPath, string entryPoint)
@@ -101,37 +106,32 @@ public static Hashtable ReturnBindingHashtable(this PowerShell ps, IDictionary<s
101106
}
102107
}
103108

104-
static string BuildBindingHashtableScript(IDictionary<string, BindingInfo> outBindings)
109+
public static PowerShell SetGlobalVariables(this PowerShell ps, Hashtable triggerMetadata, IList<ParameterBinding> inputData)
105110
{
106-
// Since all of the out bindings are stored in variables at this point,
107-
// we must construct a script that will return those output bindings in a hashtable
108-
StringBuilder script = new StringBuilder();
109-
script.AppendLine("@{");
110-
foreach (KeyValuePair<string, BindingInfo> binding in outBindings)
111-
{
112-
script.Append("'");
113-
script.Append(binding.Key);
111+
try {
112+
// Set the global $Context variable which contains trigger metadata
113+
ps.AddCommand("Set-Variable").AddParameters( new Hashtable {
114+
{ "Name", "Context"},
115+
{ "Scope", "Global"},
116+
{ "Value", triggerMetadata}
117+
}).Invoke();
114118

115-
// since $return has a dollar sign, we have to treat it differently
116-
if (binding.Key == "$return")
117-
{
118-
script.Append("' = ");
119-
}
120-
else
119+
// Sets a global variable for each input binding
120+
foreach (ParameterBinding binding in inputData)
121121
{
122-
script.Append("' = $");
122+
ps.AddCommand("Set-Variable").AddParameters( new Hashtable {
123+
{ "Name", binding.Name},
124+
{ "Scope", "Global"},
125+
{ "Value", binding.Data.ToObject()}
126+
}).Invoke();
123127
}
124-
script.AppendLine(binding.Key);
128+
return ps;
129+
}
130+
catch(Exception e)
131+
{
132+
ps.CleanupRunspace();
133+
throw e;
125134
}
126-
script.AppendLine("}");
127-
128-
return script.ToString();
129-
}
130-
131-
// TODO: make sure this completely cleans up the runspace
132-
static void CleanupRunspace(this PowerShell ps)
133-
{
134-
ps.Commands.Clear();
135135
}
136136
}
137137
}

0 commit comments

Comments
 (0)