-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathProgressiveTextReader.cs
98 lines (82 loc) · 3.1 KB
/
ProgressiveTextReader.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
using System;
#if !NET40
using System.Threading.Tasks;
#endif
namespace JenkinsNET.Utilities
{
/// <summary>
/// Represents a method that will handle an
/// event when the job console output has changed.
/// </summary>
public delegate void ProgressiveTextChangedEventHandler(string newText);
/// <summary>
/// Utility class for reading progressive text output
/// from a running Jenkins Job.
/// </summary>
public class ProgressiveTextReader
{
private readonly JenkinsClient client;
private readonly string jobName;
private readonly string buildNumber;
private int readPos;
/// <summary>
/// Gets whether the reading of text has completed.
/// </summary>
public bool IsComplete {get; private set;}
/// <summary>
/// Occurs when the value of <see cref="Text"/> is changed.
/// </summary>
public event ProgressiveTextChangedEventHandler TextChanged;
/// <summary>
/// Gets the text that has been retrieved.
/// </summary>
public string Text {get; private set;}
/// <summary>
/// Creates a new instance of <see cref="ProgressiveTextReader"/>
/// attached to the specified Jenkins Job.
/// </summary>
/// <param name="client">The Jenkins client to use for network operations.</param>
/// <param name="jobName">The name of the Jenkins Job.</param>
/// <param name="buildNumber">The build-number of the running Jenkins Job.</param>
public ProgressiveTextReader(JenkinsClient client, string jobName, string buildNumber)
{
this.client = client ?? throw new ArgumentNullException(nameof(client));
this.jobName = jobName ?? throw new ArgumentNullException(nameof(jobName));
this.buildNumber = buildNumber ?? throw new ArgumentNullException(nameof(buildNumber));
}
/// <summary>
/// Retrieves and appends any additional text
/// returned by the running Jenkins Job.
/// </summary>
public void Update()
{
if (IsComplete) return;
var result = client.Builds.GetProgressiveText(jobName, buildNumber, readPos);
if (result.Size > 0) {
Text += result.Text;
TextChanged?.Invoke(result.Text);
readPos = result.Size;
}
if (!result.MoreData)
IsComplete = true;
}
#if !NET40 && NET_ASYNC
/// <summary>
/// Retrieves and appends any additional text returned
/// by the running Jenkins Job asynchronously.
/// </summary>
public async Task UpdateAsync()
{
if (IsComplete) return;
var result = await client.Builds.GetProgressiveTextAsync(jobName, buildNumber, readPos);
if (result.Size > 0) {
Text += result.Text;
TextChanged?.Invoke(result.Text);
readPos = result.Size;
}
if (!result.MoreData)
IsComplete = true;
}
#endif
}
}