Exemple #1
0
        /// <summary>
        ///     Rather than popping up the window, read the result of the popup from
        ///     stdin and process the contents.  This is primarily used for testing purposes.
        /// </summary>
        /// <param name="popUp">Popup to run</param>
        /// <returns>Success or error code</returns>
        public int ReadFromStdIn(EditorPopUp popUp)
        {
            int result = Constants.ErrorCode;

            try
            {
                // File to write from stdin to, which will be processed by the popup closing handler
                string path    = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName(), popUp.Path);
                string dirPath = Path.GetDirectoryName(path);

                Directory.CreateDirectory(dirPath);
                using (System.IO.StreamWriter streamWriter = new StreamWriter(path))
                {
                    string line;
                    while ((line = Console.ReadLine()) != null)
                    {
                        streamWriter.WriteLineAsync(line);
                    }
                }

                // Now run the closed event and process the contents
                IList <Line> contents = popUp.OnClose(path);
                result = popUp.ProcessContents(contents);
                Directory.Delete(dirPath, true);
                if (result != Constants.SuccessCode)
                {
                    _logger.LogError("Inputs were invalid.");
                }

                return(result);
            }
            catch (Exception exc)
            {
                _logger.LogError(exc, "There was an exception processing YAML input from stdin.");
                result = Constants.ErrorCode;
            }

            return(result);
        }
Exemple #2
0
        public int PopUp(EditorPopUp popUp)
        {
            if (string.IsNullOrEmpty(_editorPath))
            {
                _logger.LogError("Failed to define an editor for the pop ups...");
                return(Constants.ErrorCode);
            }

            int result = Constants.ErrorCode;
            int tries  = Constants.MaxPopupTries;

            ParsedCommand parsedCommand = GetParsedCommand(_editorPath);

            try
            {
                string path    = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName(), popUp.Path);
                string dirPath = Path.GetDirectoryName(path);

                Directory.CreateDirectory(dirPath);
                File.WriteAllLines(path, popUp.Contents.Select(l => l.Text));

                while (tries-- > 0 && result != Constants.SuccessCode)
                {
                    using (Process process = new Process())
                    {
                        _popUpClosed = false;
                        process.EnableRaisingEvents = true;
                        process.Exited += (sender, e) =>
                        {
                            IList <Line> contents = popUp.OnClose(path);

                            result = popUp.ProcessContents(contents);

                            // If succeeded, delete the temp file, otherwise keep it around
                            // for another popup iteration.
                            if (result == Constants.SuccessCode)
                            {
                                Directory.Delete(dirPath, true);
                            }
                            else if (tries > 0)
                            {
                                _logger.LogError("Inputs were invalid, please try again...");
                            }
                            else
                            {
                                Directory.Delete(dirPath, true);
                                _logger.LogError("Maximum number of tries reached, aborting.");
                            }

                            _popUpClosed = true;
                        };
                        process.StartInfo.FileName        = parsedCommand.FileName;
                        process.StartInfo.UseShellExecute = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
                        process.StartInfo.Arguments       = $"{parsedCommand.Arguments} {path}";
                        process.Start();

                        int waitForMilliseconds = 100;
                        while (!_popUpClosed)
                        {
                            Thread.Sleep(waitForMilliseconds);
                        }
                    }
                }
            }
            catch (Exception exc)
            {
                _logger.LogError(exc, $"There was an exception while trying to pop up an editor window.");
                result = Constants.ErrorCode;
            }

            return(result);
        }