/
PipelineReader.cs
76 lines (64 loc) · 2.07 KB
/
PipelineReader.cs
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using PSRule.Data;
using System;
using System.Collections.Concurrent;
using System.Management.Automation;
namespace PSRule.Pipeline
{
internal sealed class PipelineReader
{
private readonly VisitTargetObject _Input;
private readonly InputFileInfo[] _InputPath;
private readonly ConcurrentQueue<PSObject> _Queue;
public PipelineReader(VisitTargetObject input, InputFileInfo[] inputPath)
{
_Input = input;
_InputPath = inputPath;
_Queue = new ConcurrentQueue<PSObject>();
}
public int Count => _Queue.Count;
public bool IsEmpty => _Queue.IsEmpty;
public void Enqueue(PSObject sourceObject, bool skipExpansion = false)
{
if (sourceObject == null)
return;
if (_Input == null || skipExpansion)
{
sourceObject.ConvertTargetInfoProperty();
_Queue.Enqueue(sourceObject);
return;
}
// Visit the object, which may change or expand the object
var input = _Input(sourceObject);
if (input == null)
return;
foreach (var item in input)
{
sourceObject.ConvertTargetInfoProperty();
_Queue.Enqueue(item);
}
}
public bool TryDequeue(out PSObject sourceObject)
{
return _Queue.TryDequeue(out sourceObject);
}
public void Open()
{
if (_InputPath == null || _InputPath.Length == 0)
return;
// Read each file
for (var i = 0; i < _InputPath.Length; i++)
{
if (_InputPath[i].IsUrl)
{
Enqueue(PSObject.AsPSObject(new Uri(_InputPath[i].FullName)));
}
else
{
Enqueue(PSObject.AsPSObject(_InputPath[i]));
}
}
}
}
}