/// <summary>
        /// Executes the command.
        /// </summary>
        /// <param name="info">The information.</param>
        /// <returns></returns>
        public static string ExecuteCmd(ProcessExecutorInfo info)
        {
            var color = Console.ForegroundColor;

            if (!info.RedirectStandardOutput.HasValue)
            {
                info.RedirectStandardOutput = true;
            }

            if (!info.RedirectStandardError.HasValue)
            {
                info.RedirectStandardError = true;
            }

            var cmdProcess = new Process
            {
                StartInfo = info.GetStartInfo()
            };

            var commandOutput     = new StringBuilder();
            var commandOutputLock = new object();

            cmdProcess.ErrorDataReceived  += (sender, e) => HandleErrorReceived(e, info, commandOutput, commandOutputLock);
            cmdProcess.OutputDataReceived += (sender, e) => HandleDataReceived(e, info, commandOutput, commandOutputLock);
            cmdProcess.EnableRaisingEvents = true;
            cmdProcess.Start();
            cmdProcess.BeginOutputReadLine();
            cmdProcess.BeginErrorReadLine();

            cmdProcess.StandardInput.WriteLine("exit");  //Execute exit.

            cmdProcess.WaitForExit();
            Console.ForegroundColor = color;
            return(commandOutput.ToString());
        }
        /// <summary>
        /// Checks the file out.
        /// </summary>
        /// <param name="filePath">The file path.</param>
        /// <exception cref="System.Exception">
        /// Unable to check out file
        /// or
        /// File is read only, please checkout the file before running
        /// </exception>
        public void Checkout(string filePath)
        {
            if (!File.GetAttributes(filePath).HasFlag(FileAttributes.ReadOnly))
            {
                return;
            }

            string output;

            try
            {
                var info = new ProcessExecutorInfo($"\"{TfPath}\"", $"checkout {WrapPathInQuotes(filePath)}")
                {
                    WorkingDirectory = Directory.GetParent(filePath).FullName
                };

                output = ProcessExecutor.ExecuteCmd(info);
            }
            catch (Exception ex)
            {
                throw new Exception("Unable to check out file " + filePath + Environment.NewLine + ex);
            }

            if (File.GetAttributes(filePath).HasFlag(FileAttributes.ReadOnly))
            {
                throw new Exception("File \"" + filePath + "\" is read only even though it should have been checked out, please checkout the file before running.  Output: " + output);
            }
        }
Esempio n. 3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="VsTfsSourceControlProvider" /> class.
 /// </summary>
 /// <param name="tfPath">The tf path.</param>
 /// <param name="info">The default Process Executor Info information.</param>
 public VsTfsSourceControlProvider(string tfPath = null, ProcessExecutorInfo info = null)
 {
     TfPath = tfPath ?? Config.GetAppSettingOrDefault("DLaB.Common.VersionControl.TfsPath", GetDefaultTfPath);
     if (!File.Exists(TfPath))
     {
         throw new Exception($"No TF.exe was found at '{TfPath}'.  Please create/update the app setting for 'DLaB.Common.VersionControl.TfsPath' to a valid path");
     }
     DefaultProcessExectorInfo = info;
 }
Esempio n. 4
0
        protected void ExecuteNuGetRestoreForSolution()
        {
            var cmd = new ProcessExecutorInfo(NuGetPath, $"restore \"{SolutionPath}\" -NonInteractive");

            Logger.Show("Restoring Nuget for the solution.");
            Logger.AddDetail(cmd.FileName + " " + cmd.Arguments);
            var results = ProcessExecutor.ExecuteCmd(cmd);

            Logger.Show(results);
            UpdateProjectsPostSolutionRestore();
        }
        /// <summary>
        /// Adds the file.
        /// </summary>
        /// <param name="filePath">The file path.</param>
        public void Add(string filePath)
        {
            try
            {
                var info = new ProcessExecutorInfo($"\"{TfPath}\"", $"add {WrapPathInQuotes(filePath)}")
                {
                    WorkingDirectory = Directory.GetParent(filePath).FullName
                };

                ProcessExecutor.ExecuteCmd(info);
            }
            catch (Exception ex)
            {
                throw new Exception("Unable to Add the file " + filePath + Environment.NewLine + ex);
            }
        }
        /// <summary>
        /// Un-does the specified file path.
        /// </summary>
        /// <param name="filePath">The file path.</param>
        /// <exception cref="System.Exception">Unable to Undo Checkout If Unchanged for file  + filePath + Environment.NewLine + ex</exception>
        public void Undo(string filePath)
        {
            try
            {
                var info = new ProcessExecutorInfo($"\"{TfPath}\"", $"undo {WrapPathInQuotes(filePath)} /noprompt")
                {
                    WorkingDirectory = Directory.GetParent(filePath).FullName
                };

                ProcessExecutor.ExecuteCmd(info);
            }
            catch (Exception ex)
            {
                throw new Exception("Unable to Undo Checkout for file " + filePath + Environment.NewLine + ex);
            }
        }
 private static void HandleErrorReceived(DataReceivedEventArgs e, ProcessExecutorInfo info, StringBuilder sb, object sbLock)
 {
     lock (sbLock)
     {
         sb.AppendLine(e?.Data);
         if (info.OnErrorReceived == null)
         {
             var color = Console.ForegroundColor;
             Console.ForegroundColor = ConsoleColor.Red;
             Console.WriteLine(e?.Data);
             Console.ForegroundColor = color;
         }
         else
         {
             info.OnErrorReceived(e?.Data);
         }
     }
 }
        /// <summary>
        /// Returns true if the file was unchanged and an undo operation was performed
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public bool UndoCheckoutIfUnchanged(string filePath)
        {
            try
            {
                var info = new ProcessExecutorInfo($"\"{TfPath}\"", $"Diff {WrapPathInQuotes(filePath)}")
                {
                    WorkingDirectory = Directory.GetParent(filePath).FullName
                };

                var output = ProcessExecutor.ExecuteCmd(info);

                if (output.Trim() != "edit: " + filePath.Trim())
                {
                    return(false);
                }

                Undo(filePath);
                return(true);
            }
            catch (Exception ex)
            {
                throw new Exception("Unable to Undo Checkout If Unchanged for file " + filePath + Environment.NewLine + ex);
            }
        }