public void ShellAdapterShallReturnTextFromCommand()
        {
            IShell            shellAdapter   = new ShellWrapper();
            CancellationToken cancelToken    = new CancellationToken();
            string            responseString = shellAdapter.Execute(
                command: "echo",
                args: new string[] { "hello world" }
                );

            Assert.Equal("hello world\n", responseString);
        }
示例#2
0
        private void ExecuteTargetSet(ITaskItem item)
        {
            string resolvedtargets = item.GetMetadata("Targets");

            if (resolvedtargets.EndsWith(";", StringComparison.OrdinalIgnoreCase))
            {
                resolvedtargets = resolvedtargets.Remove(resolvedtargets.Length - 1, 1);
            }

            this.LogTaskMessage(MessageImportance.High, string.Format(CultureInfo.CurrentCulture, "Building Target Set: {0} - {1}", item.ItemSpec, resolvedtargets));
            string properties = item.GetMetadata("Properties");

            if (!string.IsNullOrEmpty(properties))
            {
                properties = " /p:" + properties;
            }

            if (!string.IsNullOrEmpty(this.AdditionalProperties))
            {
                properties += " /p:" + this.AdditionalProperties;
            }

            string projectFile = this.ProjectFile == null ? this.BuildEngine.ProjectFileOfTaskNode : this.ProjectFile.ItemSpec;

            string logginginfo = string.Empty;
            string logfileName = item.GetMetadata("LogFileName");

            if (string.IsNullOrEmpty(logfileName))
            {
                logfileName = item.ItemSpec + ".txt";
            }
            else
            {
                string logfilePath = item.GetMetadata("LogFilePath");
                if (!string.IsNullOrEmpty(logfilePath))
                {
                    if (!Directory.Exists(logfilePath))
                    {
                        Directory.CreateDirectory(logfilePath);
                    }

                    logfileName = System.IO.Path.Combine(logfilePath, logfileName);
                }
            }

            if (this.MultiLog)
            {
                // note there is a bug in MSBuild loggers whereby the logger will append whenever it sees append in the arguments, so you can's say append=false.
                string append = string.Empty;
                if (this.multiLogAppend)
                {
                    append = "append=true;";
                }

                logginginfo = string.Format(CultureInfo.CurrentCulture, "/l:FileLogger,Microsoft.Build.Engine;{0}verbosity={1};logfile=\"{2}\"", append, this.MultiLogVerbosity, logfileName);
            }

            var exec = new ShellWrapper("msbuild.exe", "\"" + projectFile + "\"" +
                                        " /v:" + (this.MultiLog ? this.MultiLogResponseVerbosity : this.LogVerbosity) +
                                        " /t:" + resolvedtargets + properties + this.multiprocparameter +
                                        " /nr:" + this.nodereuse +
                                        (this.NoLogo ? " /nologo" : string.Empty) + " " + logginginfo);

            if (string.IsNullOrEmpty(this.WorkingDirectory) == false)
            {
                exec.WorkingDirectory = this.WorkingDirectory;
            }

            this.LogTaskMessage(string.Format(CultureInfo.CurrentCulture, "Executing {0} {1}", exec.Executable, exec.Arguments));

            // stderr is logged as errors
            exec.ErrorDataReceived += (sender, e) =>
            {
                if (e.Data != null)
                {
                    this.Log.LogError(e.Data);
                }
            };

            exec.OutputDataReceived += (sender, e) =>
            {
                if (e.Data != null)
                {
                    if (this.Log != null)
                    {
                        try
                        {
                            this.Log.LogMessage(MessageImportance.Normal, e.Data);
                        }
                        catch
                        {
                            // do nothing. We have a race condition here with the MSBuild host being killed and the logging still trying to occur.
                        }
                    }
                }
            };

            // execute the process
            int exitCode = exec.Execute();

            if (exitCode != 0)
            {
                string errorFile = string.Empty;
                if (this.MultiLog)
                {
                    errorFile = "Additional information may be found in: " + logfileName;
                }

                this.Log.LogError(string.Format(CultureInfo.InvariantCulture, "Error Code: {0}. {1}", exitCode, errorFile));

                if (this.MultiLog && this.MultiLogOpenOnFailure)
                {
                    FileInfo f = new FileInfo(this.BuildEngine.ProjectFileOfTaskNode);
                    System.Diagnostics.Process.Start(System.IO.Path.Combine(f.Directory.FullName, logfileName));
                }
            }
        }
        private void ExecuteTargetSet(ITaskItem item)
        {
            string resolvedtargets = item.GetMetadata("Targets");
            if (resolvedtargets.EndsWith(";", StringComparison.OrdinalIgnoreCase))
            {
                resolvedtargets = resolvedtargets.Remove(resolvedtargets.Length - 1, 1);
            }

            this.LogTaskMessage(MessageImportance.High, string.Format(CultureInfo.CurrentCulture, "Building Target Set: {0} - {1}", item.ItemSpec, resolvedtargets));
            string properties = item.GetMetadata("Properties");
            if (!string.IsNullOrEmpty(properties))
            {
                properties = " /p:" + properties;
            }

            if (!string.IsNullOrEmpty(this.AdditionalProperties))
            {
                properties += " /p:" + this.AdditionalProperties;
            }

            string projectFile = this.ProjectFile == null ? this.BuildEngine.ProjectFileOfTaskNode : this.ProjectFile.ItemSpec;
            
            string logginginfo = string.Empty;
            string logfileName = item.GetMetadata("LogFileName");
            if (string.IsNullOrEmpty(logfileName))
            {
                logfileName = item.ItemSpec + ".txt";
            }
            else
            {
                string logfilePath = item.GetMetadata("LogFilePath");
                if (!string.IsNullOrEmpty(logfilePath))
                {
                    if (!Directory.Exists(logfilePath))
                    {
                        Directory.CreateDirectory(logfilePath);
                    }

                    logfileName = System.IO.Path.Combine(logfilePath, logfileName);
                }
            }

            if (this.MultiLog)
            {
                // note there is a bug in MSBuild loggers whereby the logger will append whenever it sees append in the arguments, so you can's say append=false.
                string append = string.Empty;
                if (this.multiLogAppend)
                {
                    append = "append=true;";
                }

                logginginfo = string.Format(CultureInfo.CurrentCulture, "/l:FileLogger,Microsoft.Build.Engine;{0}verbosity={1};logfile=\"{2}\"", append, this.MultiLogVerbosity, logfileName);
            }

            var exec = new ShellWrapper("msbuild.exe", "\"" + projectFile + "\" /v:" + this.MultiLogResponseVerbosity + " /t:" + resolvedtargets + properties + this.multiprocparameter + " /nr:" + this.nodereuse + " " + logginginfo);
            if (string.IsNullOrEmpty(this.WorkingDirectory) == false)
            {
                exec.WorkingDirectory = this.WorkingDirectory;
            }

            this.LogTaskMessage(string.Format(CultureInfo.CurrentCulture, "Executing {0} {1}", exec.Executable, exec.Arguments));

            // stderr is logged as errors
            exec.ErrorDataReceived += (sender, e) =>
                                          {
                                              if (e.Data != null)
                                              {
                                                  this.Log.LogError(e.Data);
                                              }
                                          };

            exec.OutputDataReceived += (sender, e) =>
                {
                    if (e.Data != null)
                    {
                        if (this.Log != null)
                        {
                            try
                            {
                                this.Log.LogMessage(MessageImportance.Normal, e.Data);
                            }
                            catch
                            {
                                // do nothing. We have a race condition here with the MSBuild host being killed and the logging still trying to occur.
                            }
                        }
                    }
                };

            // execute the process
            int exitCode = exec.Execute();
            if (exitCode != 0)
            {
                string errorFile = string.Empty;
                if (this.MultiLog)
                {
                    errorFile = "Additional information may be found in: " + logfileName;
                }

                this.Log.LogError(string.Format(CultureInfo.InvariantCulture, "Error Code: {0}. {1}", exitCode, errorFile));

                if (this.MultiLog && this.MultiLogOpenOnFailure)
                {
                    FileInfo f = new FileInfo(this.BuildEngine.ProjectFileOfTaskNode);
                    System.Diagnostics.Process.Start(System.IO.Path.Combine(f.Directory.FullName, logfileName));
                }
            }
        }