Skip to content

Commit 615ebcd

Browse files
misc syntax sugar
1 parent 93fdbd9 commit 615ebcd

File tree

11 files changed

+59
-111
lines changed

11 files changed

+59
-111
lines changed

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

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

2121
public FunctionMessagingClient(string host, int port)
2222
{
@@ -41,19 +41,11 @@ public async Task WriteAsync(StreamingMessage message)
4141
}
4242
}
4343

44-
public async Task<bool> MoveNext()
45-
{
46-
if(isDisposed) return false;
47-
48-
return await _call.ResponseStream.MoveNext(CancellationToken.None);
49-
}
44+
public async Task<bool> MoveNext() =>
45+
!isDisposed && await _call.ResponseStream.MoveNext(CancellationToken.None);
5046

51-
public StreamingMessage GetCurrentMessage()
52-
{
53-
if(isDisposed) return null;
54-
55-
return _call.ResponseStream.Current;
56-
}
47+
public StreamingMessage GetCurrentMessage() =>
48+
isDisposed ? null : _call.ResponseStream.Current;
5749

5850
public void Dispose()
5951
{

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace Microsoft.Azure.Functions.PowerShellWorker
1010
{
1111
public class FunctionLoader
1212
{
13-
private readonly MapField<string, Function> _LoadedFunctions = new MapField<string, Function>();
13+
readonly MapField<string, Function> _LoadedFunctions = new MapField<string, Function>();
1414
public void Load(string functionId, RpcFunctionMetadata metadata)
1515
{
1616
// TODO: catch "load" issues at "func start" time.

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

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,35 +16,35 @@ namespace Microsoft.Azure.Functions.PowerShellWorker.PowerShell.Host
1616
/// applications. Not all members are implemented. Those that aren't throw a
1717
/// NotImplementedException.
1818
/// </summary>
19-
internal class AzureFunctionsHost : PSHost
19+
class AzureFunctionsHost : PSHost
2020
{
2121
/// <summary>
2222
/// The private reference of the logger.
2323
/// </summary>
24-
private RpcLogger _logger;
24+
RpcLogger _logger;
2525

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

3232
/// <summary>
3333
/// The culture info of the thread that created
3434
/// this object.
3535
/// </summary>
36-
private CultureInfo originalCultureInfo = System.Threading.Thread.CurrentThread.CurrentCulture;
36+
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-
private CultureInfo originalUICultureInfo = System.Threading.Thread.CurrentThread.CurrentUICulture;
42+
CultureInfo originalUICultureInfo = System.Threading.Thread.CurrentThread.CurrentUICulture;
4343

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

4949
/// <summary>
5050
/// Initializes a new instance of the Host class. Keep
@@ -99,17 +99,13 @@ public AzureFunctionsHost(RpcLogger logger)
9999
/// Not implemented by this class. The call fails with an exception.
100100
/// </summary>
101101
public override void EnterNestedPrompt()
102-
{
103-
throw new NotImplementedException("The method or operation is not implemented.");
104-
}
102+
=> throw new NotImplementedException("The method or operation is not implemented.");
105103

106104
/// <summary>
107105
/// Not implemented by this class. The call fails with an exception.
108106
/// </summary>
109107
public override void ExitNestedPrompt()
110-
{
111-
throw new NotImplementedException("The method or operation is not implemented.");
112-
}
108+
=> throw new NotImplementedException("The method or operation is not implemented.");
113109

114110
/// <summary>
115111
/// This API is called before an external application process is started. Typically
@@ -139,9 +135,7 @@ public override void NotifyEndApplication()
139135
/// </summary>
140136
/// <param name="exitCode">The exit code that the host application should use.</param>
141137
public override void SetShouldExit(int exitCode)
142-
{
143-
throw new NotImplementedException("The method or operation is not implemented.");
144-
}
138+
=> throw new NotImplementedException("The method or operation is not implemented.");
145139
}
146140
}
147141

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

Lines changed: 20 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,17 @@ namespace Microsoft.Azure.Functions.PowerShellWorker.PowerShell.Host
1919
/// applications. Few members are actually implemented. Those that aren't throw a
2020
/// NotImplementedException.
2121
/// </summary>
22-
internal class HostUserInterface : PSHostUserInterface
22+
class HostUserInterface : PSHostUserInterface
2323
{
2424
/// <summary>
2525
/// The private reference of the logger.
2626
/// </summary>
27-
private RpcLogger _logger;
27+
RpcLogger _logger;
2828

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

3434
/// <summary>
3535
/// Gets an instance of the PSRawUserInterface object for this host
@@ -50,10 +50,8 @@ public HostUserInterface(RpcLogger logger)
5050
/// <param name="descriptions">A collection of FieldDescription objects that
5151
/// describe each field of the prompt.</param>
5252
/// <returns>Throws a NotImplementedException exception because we don't need a prompt.</returns>
53-
public override Dictionary<string, PSObject> Prompt(string caption, string message, System.Collections.ObjectModel.Collection<FieldDescription> descriptions)
54-
{
53+
public override Dictionary<string, PSObject> Prompt(string caption, string message, System.Collections.ObjectModel.Collection<FieldDescription> descriptions) =>
5554
throw new NotImplementedException("The method or operation is not implemented.");
56-
}
5755

5856
/// <summary>
5957
/// Provides a set of choices that enable the user to choose a single option from a set of options.
@@ -65,10 +63,8 @@ public override Dictionary<string, PSObject> Prompt(string caption, string messa
6563
/// <param name="defaultChoice">The index of the label in the Choices parameter
6664
/// collection. To indicate no default choice, set to -1.</param>
6765
/// <returns>Throws a NotImplementedException exception because we don't need a prompt.</returns>
68-
public override int PromptForChoice(string caption, string message, System.Collections.ObjectModel.Collection<ChoiceDescription> choices, int defaultChoice)
69-
{
66+
public override int PromptForChoice(string caption, string message, System.Collections.ObjectModel.Collection<ChoiceDescription> choices, int defaultChoice) =>
7067
throw new NotImplementedException("The method or operation is not implemented.");
71-
}
7268

7369
/// <summary>
7470
/// Prompts the user for credentials with a specified prompt window caption,
@@ -79,10 +75,8 @@ public override int PromptForChoice(string caption, string message, System.Colle
7975
/// <param name="userName">The user name whose credential is to be prompted for.</param>
8076
/// <param name="targetName">The name of the target for which the credential is collected.</param>
8177
/// <returns>Throws a NotImplementedException exception because we don't need a prompt.</returns>
82-
public override PSCredential PromptForCredential(string caption, string message, string userName, string targetName)
83-
{
78+
public override PSCredential PromptForCredential(string caption, string message, string userName, string targetName) =>
8479
throw new NotImplementedException("The method or operation is not implemented.");
85-
}
8680

8781
/// <summary>
8882
/// Prompts the user for credentials by using a specified prompt window caption,
@@ -98,40 +92,31 @@ public override PSCredential PromptForCredential(string caption, string message,
9892
/// <param name="options">A PSCredentialUIOptions constant that identifies the UI
9993
/// behavior when it gathers the credentials.</param>
10094
/// <returns>Throws a NotImplementedException exception because we don't need a prompt.</returns>
101-
public override PSCredential PromptForCredential(string caption, string message, string userName, string targetName, PSCredentialTypes allowedCredentialTypes, PSCredentialUIOptions options)
102-
{
95+
public override PSCredential PromptForCredential(string caption, string message, string userName, string targetName, PSCredentialTypes allowedCredentialTypes, PSCredentialUIOptions options) =>
10396
throw new NotImplementedException("The method or operation is not implemented.");
104-
}
10597

10698
/// <summary>
10799
/// Reads characters that are entered by the user until a newline
108100
/// (carriage return) is encountered.
109101
/// </summary>
110102
/// <returns>Throws a NotImplemented exception because we are in a non-interactive experience.</returns>
111-
public override string ReadLine()
112-
{
103+
public override string ReadLine() =>
113104
throw new NotImplementedException("The method or operation is not implemented.");
114-
}
115105

116106
/// <summary>
117107
/// Reads characters entered by the user until a newline (carriage return)
118108
/// is encountered and returns the characters as a secure string.
119109
/// </summary>
120110
/// <returns>Throws a NotImplemented exception because we are in a non-interactive experience.</returns>
121-
public override System.Security.SecureString ReadLineAsSecureString()
122-
{
111+
public override System.Security.SecureString ReadLineAsSecureString() =>
123112
throw new NotImplementedException("The method or operation is not implemented.");
124-
}
125113

126114
/// <summary>
127115
/// Writes a new line character (carriage return) to the output display
128116
/// of the host.
129117
/// </summary>
130118
/// <param name="value">The characters to be written.</param>
131-
public override void Write(string value)
132-
{
133-
_logger.LogInformation(value);
134-
}
119+
public override void Write(string value) => _logger.LogInformation(value);
135120

136121
/// <summary>
137122
/// Writes characters to the output display of the host with possible
@@ -140,48 +125,37 @@ public override void Write(string value)
140125
/// <param name="foregroundColor">The color of the characters.</param>
141126
/// <param name="backgroundColor">The backgound color to use.</param>
142127
/// <param name="value">The characters to be written.</param>
143-
public override void Write(ConsoleColor foregroundColor, ConsoleColor backgroundColor, string value)
144-
{
145-
// Just ignore the colors.
128+
public override void Write(ConsoleColor foregroundColor, ConsoleColor backgroundColor, string value) =>
146129
_logger.LogInformation(value);
147-
}
148130

149131
/// <summary>
150132
/// Writes a debug message to the output display of the host.
151133
/// </summary>
152134
/// <param name="message">The debug message that is displayed.</param>
153-
public override void WriteDebugLine(string message)
154-
{
135+
public override void WriteDebugLine(string message) =>
155136
_logger.LogDebug(String.Format(CultureInfo.CurrentCulture, "DEBUG: {0}", message));
156-
}
157137

158138
/// <summary>
159139
/// Writes an error message to the output display of the host.
160140
/// </summary>
161141
/// <param name="value">The error message that is displayed.</param>
162-
public override void WriteErrorLine(string value)
163-
{
142+
public override void WriteErrorLine(string value) =>
164143
_logger.LogError(String.Format(CultureInfo.CurrentCulture, "ERROR: {0}", value));
165-
}
166144

167145
/// <summary>
168146
/// Writes a newline character (carriage return)
169147
/// to the output display of the host.
170148
/// </summary>
171-
public override void WriteLine()
172-
{
173-
//do nothing because we don't need to log empty lines
174-
}
149+
public override void WriteLine() {} //do nothing because we don't need to log empty lines
175150

176151
/// <summary>
177152
/// Writes a line of characters to the output display of the host
178153
/// and appends a newline character(carriage return).
179154
/// </summary>
180155
/// <param name="value">The line to be written.</param>
181-
public override void WriteLine(string value)
182-
{
156+
public override void WriteLine(string value) =>
183157
_logger.LogInformation(value);
184-
}
158+
185159

186160
/// <summary>
187161
/// Writes a line of characters to the output display of the host
@@ -190,39 +164,30 @@ public override void WriteLine(string value)
190164
/// <param name="foregroundColor">The forground color of the display. </param>
191165
/// <param name="backgroundColor">The background color of the display. </param>
192166
/// <param name="value">The line to be written.</param>
193-
public override void WriteLine(ConsoleColor foregroundColor, ConsoleColor backgroundColor, string value)
194-
{
195-
// Write to the log, ignore the colors
167+
public override void WriteLine(ConsoleColor foregroundColor, ConsoleColor backgroundColor, string value) =>
196168
_logger.LogInformation(value);
197-
}
198169

199170
/// <summary>
200171
/// Writes a progress report to the output display of the host.
201172
/// </summary>
202173
/// <param name="sourceId">Unique identifier of the source of the record. </param>
203174
/// <param name="record">A ProgressReport object.</param>
204-
public override void WriteProgress(long sourceId, ProgressRecord record)
205-
{
175+
public override void WriteProgress(long sourceId, ProgressRecord record) =>
206176
_logger.LogTrace(String.Format(CultureInfo.CurrentCulture, "PROGRESS: {0}", record.StatusDescription));
207-
}
208177

209178
/// <summary>
210179
/// Writes a verbose message to the output display of the host.
211180
/// </summary>
212181
/// <param name="message">The verbose message that is displayed.</param>
213-
public override void WriteVerboseLine(string message)
214-
{
182+
public override void WriteVerboseLine(string message) =>
215183
_logger.LogTrace(String.Format(CultureInfo.CurrentCulture, "VERBOSE: {0}", message));
216-
}
217184

218185
/// <summary>
219186
/// Writes a warning message to the output display of the host.
220187
/// </summary>
221188
/// <param name="message">The warning message that is displayed.</param>
222-
public override void WriteWarningLine(string message)
223-
{
189+
public override void WriteWarningLine(string message) =>
224190
_logger.LogWarning(String.Format(CultureInfo.CurrentCulture, "WARNING: {0}", message));
225-
}
226191
}
227192
}
228193

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace Microsoft.Azure.Functions.PowerShellWorker.PowerShell.Host
1414
/// class are implemented. More complex methods are not implemented and will
1515
/// throw a NotImplementedException.
1616
/// </summary>
17-
internal class RawUserInterface : PSHostRawUserInterface
17+
class RawUserInterface : PSHostRawUserInterface
1818
{
1919
/// <summary>
2020
/// Gets or sets the background color of text to be written.

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ 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-
private static string s_LogAndSetReturnValueScript = @"
24+
static string s_LogAndSetReturnValueScript = @"
2525
param([Parameter(ValueFromPipeline=$true)]$return)
2626
2727
$return | Out-Default
@@ -101,7 +101,7 @@ public static Hashtable ReturnBindingHashtable(this PowerShell ps, IDictionary<s
101101
}
102102
}
103103

104-
private static string BuildBindingHashtableScript(IDictionary<string, BindingInfo> outBindings)
104+
static string BuildBindingHashtableScript(IDictionary<string, BindingInfo> outBindings)
105105
{
106106
// Since all of the out bindings are stored in variables at this point,
107107
// we must construct a script that will return those output bindings in a hashtable
@@ -129,7 +129,7 @@ private static string BuildBindingHashtableScript(IDictionary<string, BindingInf
129129
}
130130

131131
// TODO: make sure this completely cleans up the runspace
132-
private static void CleanupRunspace(this PowerShell ps)
132+
static void CleanupRunspace(this PowerShell ps)
133133
{
134134
ps.Commands.Clear();
135135
}

src/Azure.Functions.PowerShell.Worker/Requests/HandleFunctionLoadRequest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace Microsoft.Azure.Functions.PowerShellWorker.Requests
1212
{
1313
using System.Management.Automation;
1414

15-
public class HandleFunctionLoadRequest
15+
public static class HandleFunctionLoadRequest
1616
{
1717
public static StreamingMessage Invoke(
1818
PowerShell powershell,

src/Azure.Functions.PowerShell.Worker/Requests/HandleInvocationRequest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace Microsoft.Azure.Functions.PowerShellWorker.Requests
1515
{
1616
using System.Management.Automation;
1717

18-
public class HandleInvocationRequest
18+
public static class HandleInvocationRequest
1919
{
2020
public static StreamingMessage Invoke(
2121
PowerShell powershell,

src/Azure.Functions.PowerShell.Worker/Requests/HandleWorkerInitRequest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace Microsoft.Azure.Functions.PowerShellWorker.Requests
1010
{
1111
using System.Management.Automation;
1212

13-
public class HandleWorkerInitRequest
13+
public static class HandleWorkerInitRequest
1414
{
1515
public static StreamingMessage Invoke(
1616
PowerShell powershell,

0 commit comments

Comments
 (0)