Easier Way to Read Process Output? #3837
RehanSaeed
started this conversation in
Ideas
Replies: 2 comments
-
I think a generic tool alias will that takes an ICollection for tool resolution would likely be a good addition. What I've currently use is a helper method I include in my scripts static CmdHandler Cmd;
Cmd = (path, args, redirectStandardOutput, workingDirectory) => {
IEnumerable<string> redirectedStandardOutput = null;
var settings = new ProcessSettings {
Arguments = args(new ProcessArgumentBuilder()),
RedirectStandardOutput = redirectStandardOutput,
WorkingDirectory = workingDirectory ?? Context.Environment.WorkingDirectory
};
var result = StartProcess(
path,
settings,
out redirectedStandardOutput);
var output = string.Join(System.Environment.NewLine, redirectedStandardOutput ?? Enumerable.Empty<string>());
if(0 != result)
{
throw new Exception($"Failed to execute tool {path.GetFilename()} ({result}) with args: {settings.Arguments.RenderSafe()}");
}
return output;
};
public delegate string CmdHandler(FilePath path, ArgumentHandler args, bool redirectStandardOutput, DirectoryPath workingDirectory = null);
public delegate ProcessArgumentBuilder ArgumentHandler(ProcessArgumentBuilder args); then use it something like #load "cmd.cake"
FilePath NpmPath = Context.Tools.Resolve("npm.cmd")
?? Context.Tools.Resolve("npm")
?? throw new FileNotFoundException("Failed to resolved NPM path");
var result = Cmd(
NpmPath,
arg => arg.Append("--version"),
true
);
Information("NPM Version: {0}", result); |
Beta Was this translation helpful? Give feedback.
0 replies
-
To add to this. Its currently an extra step to check the exit code. It'd be nice if |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I'm executing a process and reading the output like so:
This is pretty cumbersome. I'd really like something far simpler like simple-exec built into Cake build aliases so I don't have to include yet another dependency:
I don't believe anything like this currently exists unless there is an alias I'm missing?
Beta Was this translation helpful? Give feedback.
All reactions