From bcd6b090950f30497c40368f763a0ab7dfa24c8e Mon Sep 17 00:00:00 2001 From: Victor Hurdugaci Date: Fri, 12 Feb 2016 15:47:32 -0800 Subject: [PATCH] Add support in dotnet run for pid files --- src/Microsoft.DotNet.Cli.Utils/Command.cs | 13 +++++++++++++ src/Microsoft.DotNet.Cli.Utils/ICommand.cs | 2 ++ src/dotnet/commands/dotnet-run/Program.cs | 1 + src/dotnet/commands/dotnet-run/RunCommand.cs | 2 ++ 4 files changed, 18 insertions(+) diff --git a/src/Microsoft.DotNet.Cli.Utils/Command.cs b/src/Microsoft.DotNet.Cli.Utils/Command.cs index 1480c2c7fc..b038259ecc 100644 --- a/src/Microsoft.DotNet.Cli.Utils/Command.cs +++ b/src/Microsoft.DotNet.Cli.Utils/Command.cs @@ -20,6 +20,8 @@ public class Command : ICommand private bool _running = false; + private string _pidFile; + private Command(CommandSpec commandSpec) { var psi = new ProcessStartInfo @@ -101,6 +103,11 @@ public CommandResult Execute() _process.Start(); Reporter.Verbose.WriteLine($"Process ID: {_process.Id}"); + if (!string.IsNullOrEmpty(_pidFile)) + { + Reporter.Verbose.WriteLine($"Process ID (PID) file: {_pidFile}"); + File.WriteAllText(_pidFile, _process.Id.ToString()); + } var threadOut = _stdOut.BeginRead(_process.StandardOutput); var threadErr = _stdErr.BeginRead(_process.StandardError); @@ -136,6 +143,12 @@ public ICommand WorkingDirectory(string projectDirectory) return this; } + public ICommand PidFile(string file) + { + _pidFile = file; + return this; + } + public ICommand EnvironmentVariable(string name, string value) { #if NET451 diff --git a/src/Microsoft.DotNet.Cli.Utils/ICommand.cs b/src/Microsoft.DotNet.Cli.Utils/ICommand.cs index 1b7cde7b65..9bb70982dc 100644 --- a/src/Microsoft.DotNet.Cli.Utils/ICommand.cs +++ b/src/Microsoft.DotNet.Cli.Utils/ICommand.cs @@ -12,6 +12,8 @@ public interface ICommand ICommand WorkingDirectory(string projectDirectory); + ICommand PidFile(string file); + ICommand EnvironmentVariable(string name, string value); ICommand CaptureStdOut(); diff --git a/src/dotnet/commands/dotnet-run/Program.cs b/src/dotnet/commands/dotnet-run/Program.cs index fe5ab79c78..e61c9ee9b1 100644 --- a/src/dotnet/commands/dotnet-run/Program.cs +++ b/src/dotnet/commands/dotnet-run/Program.cs @@ -29,6 +29,7 @@ public static int Run(string[] args) syntax.DefineOption("f|framework", ref runCmd.Framework, "Compile a specific framework"); syntax.DefineOption("c|configuration", ref runCmd.Configuration, "Configuration under which to build"); syntax.DefineOption("p|project", ref runCmd.Project, "The path to the project to run (defaults to the current directory). Can be a path to a project.json or a project directory"); + syntax.DefineOption("pf|pid-file", ref runCmd.PidFile, "A file that contains the process id of the executed application"); syntax.DefineOption("h|help", ref help, "Help for compile native."); diff --git a/src/dotnet/commands/dotnet-run/RunCommand.cs b/src/dotnet/commands/dotnet-run/RunCommand.cs index 15284086bf..b52e4d93b5 100644 --- a/src/dotnet/commands/dotnet-run/RunCommand.cs +++ b/src/dotnet/commands/dotnet-run/RunCommand.cs @@ -18,6 +18,7 @@ public partial class RunCommand public string Framework = null; public string Configuration = null; public string Project = null; + public string PidFile = null; public IReadOnlyList Args = null; ProjectContext _context; @@ -150,6 +151,7 @@ private int RunExecutable() .ForwardStdOut() .ForwardStdErr() .EnvironmentVariable("DOTNET_HOME", dotnetHome) + .PidFile(PidFile) .Execute() .ExitCode;