Skip to content

Commit 0955917

Browse files
authored
Merge pull request #126 from nils-a/feature/GH-97
(GH-97) Added StandardErrorAction and StandardOutputAction in NpmSettings
2 parents 85d6530 + 8ff34af commit 0955917

File tree

4 files changed

+129
-4
lines changed

4 files changed

+129
-4
lines changed

src/Cake.Npm.Tests/NpmSettingsExtensionsTests.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
namespace Cake.Npm.Tests
22
{
3+
using System;
4+
35
using Cake.Npm.Install;
46
using Shouldly;
57
using Xunit;
@@ -83,6 +85,34 @@ public void Should_Set_WorkingDirectory()
8385
// Then
8486
settings.WorkingDirectory.ToString().ShouldBe(@"c:/temp");
8587
}
88+
89+
[Fact]
90+
public void Should_Set_StandardOutputAction()
91+
{
92+
// Given
93+
var settings = new NpmInstallSettings();
94+
Action<string> action = x => { };
95+
96+
// When
97+
settings.SetRedirectedStandardOutputHandler(action);
98+
99+
// Then
100+
settings.StandardOutputAction.ShouldBe(action);
101+
}
102+
103+
[Fact]
104+
public void Should_Set_StandardErrorAction()
105+
{
106+
// Given
107+
var settings = new NpmInstallSettings();
108+
Action<string> action = x => { };
109+
110+
// When
111+
settings.SetRedirectedStandardErrorHandler(action);
112+
113+
// Then
114+
settings.StandardErrorAction.ShouldBe(action);
115+
}
86116
}
87117
}
88118
}

src/Cake.Npm/NpmSettings.cs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
namespace Cake.Npm
22
{
3+
using System;
4+
35
using Core;
46
using Core.Diagnostics;
57
using Core.IO;
@@ -27,15 +29,39 @@ protected NpmSettings(string command)
2729
public NpmLogLevel LogLevel { get; set; }
2830

2931
/// <summary>
30-
/// Gets or sets the process option to redirect standard error
32+
/// Gets or sets the process option to redirect standard error output.
3133
/// </summary>
34+
/// <remarks>
35+
/// To retrieve and process the standard error output
36+
/// <see cref="StandardErrorAction"/> needs to be set.
37+
/// </remarks>
3238
public bool RedirectStandardError { get; set; }
3339

3440
/// <summary>
35-
/// Gets or sets the process option to redirect standard output
41+
/// Gets or sets an action to retrieve and process standard error output.
42+
/// </summary>
43+
/// <remarks>
44+
/// Setting a standard error action implicitely set <see cref="RedirectStandardError"/>.
45+
/// </remarks>
46+
public Action<string> StandardErrorAction { get; set; }
47+
48+
/// <summary>
49+
/// Gets or sets the process option to redirect standard output.
3650
/// </summary>
51+
/// <remarks>
52+
/// To retrieve and process the standard error output
53+
/// <see cref="StandardOutputAction"/> needs to be set.
54+
/// </remarks>
3755
public bool RedirectStandardOutput { get; set; }
3856

57+
/// <summary>
58+
/// Gets or sets an action to retrieve and process standard output.
59+
/// </summary>
60+
/// <remarks>
61+
/// Setting a standard error action implicitely set <see cref="RedirectStandardOutput"/>.
62+
/// </remarks>
63+
public Action<string> StandardOutputAction { get; set; }
64+
3965
/// <summary>
4066
/// Gets or sets the Log level set by Cake.
4167
/// </summary>

src/Cake.Npm/NpmSettingsExtensions.cs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,51 @@ public static NpmSettings FromPath(this NpmSettings settings, DirectoryPath path
4848

4949
return settings;
5050
}
51+
52+
/// <summary>
53+
/// Sets the StandardError-Action
54+
/// </summary>
55+
/// <param name="settings">The settings.</param>
56+
/// <param name="standardErrorAction">The StandardError-Action.</param>
57+
/// <returns>The <paramref name="settings"/> instance with <see cref="NpmSettings.StandardErrorAction"/> set to <paramref name="standardErrorAction"/>.</returns>
58+
public static NpmSettings SetRedirectedStandardErrorHandler(this NpmSettings settings, Action<string> standardErrorAction)
59+
{
60+
if (settings == null)
61+
{
62+
throw new ArgumentNullException(nameof(settings));
63+
}
64+
65+
if (standardErrorAction == null)
66+
{
67+
throw new ArgumentNullException(nameof(standardErrorAction));
68+
}
69+
70+
settings.StandardErrorAction = standardErrorAction;
71+
72+
return settings;
73+
}
74+
75+
/// <summary>
76+
/// Sets the StandardOutput-Action
77+
/// </summary>
78+
/// <param name="settings">The settings.</param>
79+
/// <param name="standardOutputAction">The StandardOutput-Action.</param>
80+
/// <returns>The <paramref name="settings"/> instance with <see cref="NpmSettings.StandardOutputAction"/> set to <paramref name="standardOutputAction"/>.</returns>
81+
public static NpmSettings SetRedirectedStandardOutputHandler(this NpmSettings settings, Action<string> standardOutputAction)
82+
{
83+
if (settings == null)
84+
{
85+
throw new ArgumentNullException(nameof(settings));
86+
}
87+
88+
if (standardOutputAction == null)
89+
{
90+
throw new ArgumentNullException(nameof(standardOutputAction));
91+
}
92+
93+
settings.StandardOutputAction = standardOutputAction;
94+
95+
return settings;
96+
}
5197
}
5298
}

src/Cake.Npm/NpmTool.cs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,31 @@ protected void RunCore(TSettings settings, ProcessSettings processSettings, Acti
9090
settings.CakeVerbosityLevel = CakeLog.Verbosity;
9191
}
9292

93-
processSettings.RedirectStandardError = settings.RedirectStandardError;
94-
processSettings.RedirectStandardOutput = settings.RedirectStandardOutput;
93+
processSettings.RedirectStandardOutput =
94+
settings.RedirectStandardOutput ||
95+
settings.StandardOutputAction != null;
96+
97+
if (settings.StandardOutputAction != null)
98+
{
99+
processSettings.RedirectedStandardOutputHandler = x =>
100+
{
101+
settings.StandardOutputAction(x);
102+
return x;
103+
};
104+
}
105+
106+
processSettings.RedirectStandardError =
107+
settings.RedirectStandardError ||
108+
settings.StandardErrorAction != null;
109+
110+
if (settings.StandardErrorAction != null)
111+
{
112+
processSettings.RedirectedStandardErrorHandler = x =>
113+
{
114+
settings.StandardErrorAction(x);
115+
return x;
116+
};
117+
}
95118

96119
var args = GetArguments(settings);
97120
Run(settings, args, processSettings, postAction);

0 commit comments

Comments
 (0)