Пример #1
0
            /// <summary>
            /// Creates a new instance of an external tool, using the specified template. Does not start it yet.
            /// </summary>
            internal Instance(ExternalTool template)
            {
                Helpers.Argument.ValidateIsNotNull(template, nameof(template));

                if (string.IsNullOrWhiteSpace(template.ExecutablePath))
                {
                    throw new ArgumentException("Executable path must be specified.", nameof(template));
                }

                if (template.WorkingDirectory != null && !Directory.Exists(template.WorkingDirectory))
                {
                    throw new ArgumentException("The working directory does not exist.", nameof(template));
                }

                _log = Log.Default.CreateChildSource(Path.GetFileName(template.ExecutablePath));

                var executablePath = template.ExecutablePath;

                // First, resolve the path.
                if (!Path.IsPathRooted(executablePath))
                {
                    var resolvedPath = Helpers.Filesystem.ResolvePath(executablePath);
                    _log.Debug($"Executable resolved to {resolvedPath}");

                    executablePath = resolvedPath;
                }

                // Then prepare the variables.
                ExecutablePath       = executablePath;
                Arguments            = template.Arguments ?? "";
                EnvironmentVariables = new Dictionary <string, string>(template.EnvironmentVariables ?? new Dictionary <string, string>());
                WorkingDirectory     = template.WorkingDirectory ?? Environment.CurrentDirectory;
                OutputFilePath       = template.OutputFilePath;

                var heuristics = template.ResultHeuristics ?? ExternalToolResultHeuristics.Default;

                StandardErrorIsNotError = heuristics.StandardErrorIsNotError;

                _standardInputProvider  = template.StandardInputProvider;
                _standardOutputConsumer = template.StandardOutputConsumer;
                _standardErrorConsumer  = template.StandardErrorConsumer;

                // We may need to censor the log line!
                CensoredArguments = Arguments;

                if (template.CensoredStrings?.Count > 0)
                {
                    foreach (var censoredString in template.CensoredStrings)
                    {
                        if (string.IsNullOrWhiteSpace(censoredString))
                        {
                            continue;
                        }

                        CensoredArguments = CensoredArguments.Replace(censoredString, "*********");
                    }
                }
            }
Пример #2
0
            /// <summary>
            /// Creates a new instance of an external tool, using the specified template. Does not start it yet.
            /// </summary>
            internal Instance(ExternalTool template, ILoggerFactory?loggerFactory)
            {
                Helpers.Argument.ValidateIsNotNull(template, nameof(template));

                if (string.IsNullOrWhiteSpace(template.ExecutablePath))
                {
                    throw new ArgumentException("Executable path must be specified.", nameof(template));
                }

                if (template.WorkingDirectory != null && !Directory.Exists(template.WorkingDirectory))
                {
                    throw new ArgumentException("The working directory does not exist.", nameof(template));
                }

                _shortName = Path.GetFileName(template.ExecutablePath);

                if (loggerFactory == null)
                {
                    Logger = NullLogger.Instance;
                }
                else
                {
                    Logger = loggerFactory.CreateLogger(_shortName);
                }

                var executablePath = template.ExecutablePath !;

                // First, resolve the path.
                if (!Path.IsPathRooted(executablePath))
                {
                    var resolvedPath = Helpers.Filesystem.ResolvePath(executablePath);
                    Logger.LogDebug("{ExecutablePath} resolved to {ResolvedPath}", executablePath, resolvedPath);

                    executablePath = resolvedPath;
                }

                // Then prepare the variables.
                ExecutablePath               = executablePath;
                Arguments                    = template.Arguments ?? "";
                EnvironmentVariables         = new Dictionary <string, string>(template.EnvironmentVariables ?? new Dictionary <string, string>());
                WorkingDirectory             = template.WorkingDirectory ?? Environment.CurrentDirectory;
                OutputFilePath               = template.OutputFilePath;
                ProcessPriority              = template.ProcessPriority;
                CaptureOutputStreamsToString = template.CaptureOutputStreamsToString;

                _standardInputProvider  = template.StandardInputProvider;
                _standardOutputConsumer = template.StandardOutputConsumer;
                _standardErrorConsumer  = template.StandardErrorConsumer;

                // We may need to censor the log line!
                CensoredArguments = Arguments;

                if (template.CensoredStrings?.Count > 0)
                {
                    foreach (var censoredString in template.CensoredStrings)
                    {
                        if (string.IsNullOrWhiteSpace(censoredString))
                        {
                            continue;
                        }

                        CensoredArguments = CensoredArguments.Replace(censoredString, "*********");
                    }
                }

                _process = Start();
            }