Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
ExternalProcessCommand.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Diagnostics;
5 using System.IO;
6 using System.Threading.Tasks;
7 using SiliconStudio.Core.Diagnostics;
8 
9 namespace SiliconStudio.BuildEngine
10 {
11  [Description("Run external process")]
13  {
14  /// <inheritdoc/>
15  public override string Title { get { string title = "External Process "; try { title += Path.GetFileName(ProcessPath) ?? "[Process]"; } catch { title += "[INVALID PATH]"; } return title; } }
16 
17  public string ProcessPath { get; set; }
18  public string Arguments { get; set; }
19 
20  /// <summary>
21  /// An optional return code from the command
22  /// </summary>
23  public int ExitCode;
24 
25  /// <summary>
26  /// The spawned process
27  /// </summary>
28  private Process process;
29 
30  private Logger logger;
31 
32  protected override Task<ResultStatus> DoCommandOverride(ICommandContext commandContext)
33  {
34  logger = commandContext.Logger;
35  if (!File.Exists(ProcessPath))
36  {
37  logger.Error("Unable to find binary file " + ProcessPath);
38  return Task.FromResult(ResultStatus.Failed);
39  }
40 
41  var startInfo = new ProcessStartInfo
42  {
43  FileName = ProcessPath,
44  Arguments = Arguments,
45  WorkingDirectory = ".",
46  UseShellExecute = false,
47  RedirectStandardOutput = true
48  };
49  process = Process.Start(startInfo);
50  process.OutputDataReceived += OnOutputDataReceived;
51  process.BeginOutputReadLine();
52  process.WaitForExit();
53 
54  ExitCode = process.ExitCode;
55 
56  return Task.FromResult(CancellationToken.IsCancellationRequested ? ResultStatus.Cancelled : (ExitCode == 0 ? ResultStatus.Successful : ResultStatus.Failed));
57  }
58 
59  private void OnOutputDataReceived(object sender, DataReceivedEventArgs e)
60  {
61  logger.Debug(e.Data);
62  }
63 
64  public override void Cancel()
65  {
66  process.Kill();
67  }
68 
69  public override string ToString()
70  {
71  return "External process " + (ProcessPath ?? "[Process]") + (Arguments != null ? " " + Arguments : "");
72  }
73  }
74 }
Base implementation for ILogger.
Definition: Logger.cs:10
System.IO.File File
int ExitCode
An optional return code from the command
override Task< ResultStatus > DoCommandOverride(ICommandContext commandContext)
The method to override containing the actual command code. It is called by the DoCommand function ...
override void Cancel()
Callback called by Builder.CancelBuild. It can be useful for commands in a blocking call that can be ...