1

I have an issue with my (visual studio) pre-build action calling an executable, which requires a console input.

The code looks like:

using System;
using System.Diagnostics;
using System.Text;

namespace MyMinumumExample
{
    internal class MinimumExample
    {
        private StringBuilder stdOutput = null;

        private readonly string assemblyFilePath;
        private readonly Process gitProcess = new Process()
        {
            StartInfo = new ProcessStartInfo()
            {
                FileName = "git",
                RedirectStandardOutput = true,
                UseShellExecute = false,
            },
        };

        public MinimumExample(string path)
        {
            assemblyFilePath = path;
        }

        private static int Main(string[] args)
        {
            string path = args[0];
            var program = new MinimumExample(path);

            if (program.CheckIfFileIsDirty(path))
            {
                Console.WriteLine("Changes will be discarded after compiling. Do you want to proceed?");
                while (true)
                {
                    Console.Write("[y/n]: ");
                    ConsoleKeyInfo key = Console.ReadKey();
                    if (key.KeyChar == 'y')
                        break;

                    if (key.KeyChar == 'n')
                        return 1;

                    Console.WriteLine();
                }
            }
            return 0;
        }

        private bool CheckIfFileIsDirty(string path)
        {
            string gitCmdArgs = string.Format("diff --shortstat -- {0}", path);

            stdOutput = new StringBuilder();

            gitProcess.StartInfo.Arguments = gitCmdArgs;
            gitProcess.Start();

            gitProcess.BeginOutputReadLine();
            gitProcess.OutputDataReceived += GitProcessOutputHandler;

            gitProcess.WaitForExit();
            gitProcess.OutputDataReceived -= GitProcessOutputHandler;

            if (gitProcess.ExitCode != 0) throw new Exception(string.Format("Process 'git {0}' failed with code {1}", gitCmdArgs, gitProcess.ExitCode));

            return !string.IsNullOrWhiteSpace(stdOutput.ToString());
        }


        private void GitProcessOutputHandler(object sendingProcess, DataReceivedEventArgs outLine)
        {
            if (!string.IsNullOrWhiteSpace(outLine.Data))
            {
                stdOutput.Append(outLine.Data);
            }
        }
    }
}

If I run this from cmd-shell or from a batch file everything works fine. If I put it into the pre build action, I get an exeption in line 38:

ConsoleKeyInfo key = Console.ReadKey();

The pre build action looks like:

call $(SolutionDir)MinimumExample.exe $(ProjectDir)dirtyExampleFile.cs
IF %ERRORLEVEL% GTR 0 (
EXIT 1
)

If I call the executable with start <exe> the example works, but I don't get the exit code of the exe (but probably of the new cmd shell).

Either, I can get the exit code of cmd shell to verify that the executable did exit cleanly, or I find a way to read keyboard input from the build event console.

Does anyone has an idea, how to solve this?

Best regards and thanks in advance!

HarryKane
  • 129
  • 3
  • 13

1 Answers1

0

I found the solution to get the exit code: Using start with /wait option solved my problem.

PreBuildEvent is now:

start "Update version of $(ProjectName)" /wait $(SolutionDir)MinimumExample.exe $(ProjectDir)dirtyExampleFile.cs

HarryKane
  • 129
  • 3
  • 13