Skip to content

Commit 22b0f30

Browse files
authored
Merge pull request #43 from santisq/42-add-a-more-verbose-module-description
updates manifest description. bit of code refactoring
2 parents 74d6ac3 + dcdb5b4 commit 22b0f30

File tree

9 files changed

+79
-69
lines changed

9 files changed

+79
-69
lines changed

module/PSParallelPipeline.psd1

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
RootModule = 'bin/netstandard2.0/PSParallelPipeline.dll'
1212

1313
# Version number of this module.
14-
ModuleVersion = '1.2.0'
14+
ModuleVersion = '1.2.1'
1515

1616
# Supported PSEditions
1717
# CompatiblePSEditions = @()
@@ -29,7 +29,7 @@
2929
Copyright = '(c) Santiago Squarzon. All rights reserved.'
3030

3131
# Description of the functionality provided by this module
32-
Description = 'Enables parallel processing of pipeline input objects.'
32+
Description = 'Includes Invoke-Parallel cmdlet, allowing for parallel processing of input objects, sharing similar capabilities as ForEach-Object -Parallel.'
3333

3434
# Minimum version of the PowerShell engine required by this module
3535
PowerShellVersion = '5.1'
@@ -94,11 +94,13 @@
9494
# Tags applied to this module. These help with module discovery in online galleries.
9595
Tags = @(
9696
'parallel'
97-
'concurrency'
9897
'runspace'
9998
'parallel-processing'
100-
'powershell'
10199
'multithreading'
100+
'foreach'
101+
'pipeline'
102+
'threads'
103+
'ForEach-Object'
102104
)
103105

104106
# A URL to the license for this module.

src/PSParallelPipeline/CommandCompleter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public IEnumerable<CompletionResult> CompleteArgument(
2222
{
2323
try
2424
{
25-
_builtinFuncs ??= new HashSet<string>(GetBuiltinFunctions());
25+
_builtinFuncs ??= [.. GetBuiltinFunctions()];
2626

2727
return CompletionCompleters
2828
.CompleteCommand(

src/PSParallelPipeline/Commands/InvokeParallelCommand.cs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@
22
using System.Collections;
33
using System.Management.Automation;
44
using System.Management.Automation.Runspaces;
5+
using PSParallelPipeline.Poly;
56

6-
namespace PSParallelPipeline;
7+
namespace PSParallelPipeline.Commands;
78

89
[Cmdlet(VerbsLifecycle.Invoke, "Parallel")]
910
[Alias("parallel")]
1011
[OutputType(typeof(object))]
1112
public sealed class InvokeParallelCommand : PSCmdlet, IDisposable
1213
{
14+
private Worker? _worker;
15+
1316
[Parameter(Position = 0, Mandatory = true)]
1417
public ScriptBlock ScriptBlock { get; set; } = null!;
1518

@@ -41,21 +44,12 @@ public sealed class InvokeParallelCommand : PSCmdlet, IDisposable
4144
[Alias("unr")]
4245
public SwitchParameter UseNewRunspace { get; set; }
4346

44-
private Worker? _worker;
45-
4647
protected override void BeginProcessing()
4748
{
48-
InitialSessionState iss = InitialSessionState.CreateDefault2();
49-
50-
if (Functions is not null)
51-
{
52-
iss.AddFunctions(Functions, this);
53-
}
54-
55-
if (Variables is not null)
56-
{
57-
iss.AddVariables(Variables, this);
58-
}
49+
InitialSessionState iss = InitialSessionState
50+
.CreateDefault2()
51+
.AddFunctions(Functions, this)
52+
.AddVariables(Variables, this);
5953

6054
PoolSettings poolSettings = new()
6155
{

src/PSParallelPipeline/Extensions.cs

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections;
33
using System.Collections.Generic;
4+
using System.Linq;
45
using System.Management.Automation;
56
using System.Management.Automation.Language;
67
using System.Management.Automation.Runspaces;
@@ -10,52 +11,67 @@ namespace PSParallelPipeline;
1011

1112
internal static class Extensions
1213
{
13-
internal static void AddFunctions(
14+
internal static InitialSessionState AddFunctions(
1415
this InitialSessionState initialSessionState,
15-
string[] functionsToAdd,
16+
string[]? functionsToAdd,
1617
PSCmdlet cmdlet)
1718
{
18-
foreach (string function in functionsToAdd)
19+
if (functionsToAdd is not null)
1920
{
20-
CommandInfo? commandInfo = cmdlet
21-
.InvokeCommand
22-
.GetCommand(function, CommandTypes.Function);
23-
24-
if (commandInfo is null)
21+
foreach (string function in functionsToAdd)
2522
{
26-
continue;
23+
CommandInfo? commandInfo = cmdlet
24+
.InvokeCommand
25+
.GetCommand(function, CommandTypes.Function);
26+
27+
if (commandInfo is null)
28+
{
29+
continue;
30+
}
31+
32+
initialSessionState.Commands.Add(new SessionStateFunctionEntry(
33+
name: function,
34+
definition: commandInfo.Definition));
2735
}
28-
29-
initialSessionState.Commands.Add(new SessionStateFunctionEntry(
30-
name: function,
31-
definition: commandInfo.Definition));
3236
}
37+
38+
return initialSessionState;
3339
}
3440

35-
internal static void AddVariables(
41+
internal static InitialSessionState AddVariables(
3642
this InitialSessionState initialSessionState,
37-
Hashtable variables,
43+
Hashtable? variables,
3844
PSCmdlet cmdlet)
3945
{
40-
foreach (DictionaryEntry pair in variables)
46+
if (variables is not null)
4147
{
42-
cmdlet.ThrowIfVariableIsScriptBlock(pair.Value);
43-
initialSessionState.Variables.Add(new SessionStateVariableEntry(
44-
name: LanguagePrimitives.ConvertTo<string>(pair.Key),
45-
value: pair.Value,
46-
description: null));
48+
foreach (DictionaryEntry pair in variables)
49+
{
50+
cmdlet.ThrowIfVariableIsScriptBlock(pair.Value);
51+
initialSessionState.Variables.Add(new SessionStateVariableEntry(
52+
name: LanguagePrimitives.ConvertTo<string>(pair.Key),
53+
value: pair.Value,
54+
description: null));
55+
}
4756
}
57+
58+
return initialSessionState;
4859
}
4960

5061
internal static Dictionary<string, object?> GetUsingParameters(
5162
this ScriptBlock script,
5263
PSCmdlet cmdlet)
5364
{
5465
Dictionary<string, object?> usingParams = [];
66+
IEnumerable<UsingExpressionAst> usingExpressionAsts = script.Ast
67+
.FindAll((a) => a is UsingExpressionAst, true)
68+
.Cast<UsingExpressionAst>();
5569

56-
foreach (UsingExpressionAst usingStatement in script.Ast.FindAll((a) => a is UsingExpressionAst, true))
70+
foreach (UsingExpressionAst usingStatement in usingExpressionAsts)
5771
{
58-
VariableExpressionAst backingVariableAst = UsingExpressionAst.ExtractUsingVariable(usingStatement);
72+
VariableExpressionAst backingVariableAst = UsingExpressionAst
73+
.ExtractUsingVariable(usingStatement);
74+
5975
string varPath = backingVariableAst.VariablePath.UserPath;
6076

6177
string varText = usingStatement.ToString();
@@ -89,7 +105,9 @@ internal static void AddVariables(
89105
object? value,
90106
ExpressionAst ast)
91107
{
92-
VariableExpressionAst usingVariable = (VariableExpressionAst)ast.Find(a => a is VariableExpressionAst, false);
108+
VariableExpressionAst usingVariable = (VariableExpressionAst)ast
109+
.Find(a => a is VariableExpressionAst, false);
110+
93111
ExpressionAst lookupAst = new ConstantExpressionAst(ast.Extent, value);
94112
Ast? currentAst = usingVariable;
95113

src/PSParallelPipeline/PSOutputData.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
using System.Management.Automation;
2-
31
namespace PSParallelPipeline;
42

53
internal enum Type

src/PSParallelPipeline/PSOutputStreams.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ internal sealed class PSOutputStreams : IDisposable
99
{
1010
private BlockingCollection<PSOutputData> OutputPipe { get => _worker.OutputPipe; }
1111

12-
private CancellationToken Token { get => _worker.Token; }
13-
1412
internal PSDataCollection<PSObject> Success { get; } = [];
1513

1614
internal PSDataCollection<ErrorRecord> Error { get; } = [];

src/PSParallelPipeline/Dbg/Dbg.cs renamed to src/PSParallelPipeline/Poly/Dbg.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System.Diagnostics;
22
using System.Diagnostics.CodeAnalysis;
33

4-
namespace PSParallelPipeline;
4+
namespace PSParallelPipeline.Poly;
55

66
internal static class Dbg
77
{

src/PSParallelPipeline/RunspacePool.cs

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,6 @@ internal RunspacePool(PoolSettings settings, Worker worker)
4242

4343
internal void Release() => _semaphore.Release();
4444

45-
private Runspace CreateRunspace()
46-
{
47-
Runspace rs = RunspaceFactory.CreateRunspace(InitialSessionState);
48-
rs.Open();
49-
return rs;
50-
}
51-
5245
internal void CompleteTask(PSTask psTask)
5346
{
5447
psTask.Dispose();
@@ -62,17 +55,6 @@ internal void CompleteTask(PSTask psTask)
6255
_pool.Enqueue(psTask.Runspace);
6356
}
6457

65-
private async Task<Runspace> GetRunspaceAsync()
66-
{
67-
await _semaphore.WaitAsync(Token);
68-
if (_pool.TryDequeue(out Runspace runspace))
69-
{
70-
return runspace;
71-
}
72-
73-
return CreateRunspace();
74-
}
75-
7658
internal async Task EnqueueAsync(PSTask psTask)
7759
{
7860
psTask.AddUsingStatements(UsingStatements);
@@ -88,17 +70,35 @@ internal async Task ProcessAllAsync()
8870
}
8971
}
9072

73+
internal CancellationTokenRegistration RegisterCancellation(Action callback) =>
74+
Token.Register(callback);
75+
76+
internal async Task WaitOnCancelAsync() => await Task.WhenAll(_tasks);
77+
9178
private async Task ProcessAnyAsync()
9279
{
9380
Task task = await Task.WhenAny(_tasks);
9481
_tasks.Remove(task);
9582
await task;
9683
}
9784

98-
internal CancellationTokenRegistration RegisterCancellation(Action callback) =>
99-
Token.Register(callback);
85+
private Runspace CreateRunspace()
86+
{
87+
Runspace rs = RunspaceFactory.CreateRunspace(InitialSessionState);
88+
rs.Open();
89+
return rs;
90+
}
10091

101-
internal async Task WaitOnCancelAsync() => await Task.WhenAll(_tasks);
92+
private async Task<Runspace> GetRunspaceAsync()
93+
{
94+
await _semaphore.WaitAsync(Token);
95+
if (_pool.TryDequeue(out Runspace runspace))
96+
{
97+
return runspace;
98+
}
99+
100+
return CreateRunspace();
101+
}
102102

103103
public void Dispose()
104104
{

0 commit comments

Comments
 (0)