File: Commands\SdkCommandSpec.cs
Web Access
Project: ..\..\..\test\Microsoft.NET.TestFramework\Microsoft.NET.TestFramework.csproj (Microsoft.NET.TestFramework)
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
 
using System.Diagnostics;
using Microsoft.DotNet.Cli.Utils;
 
namespace Microsoft.NET.TestFramework.Commands
{
    public class SdkCommandSpec
    {
        public string? FileName { get; set; }
        public List<string> Arguments { get; set; } = new List<string>();
 
        public Dictionary<string, string> Environment { get; set; } = new Dictionary<string, string>();
 
        public List<string> EnvironmentToRemove { get; } = new List<string>();
 
        public string? WorkingDirectory { get; set; }
 
        public bool RedirectStandardInput { get; set; }
 
        private string EscapeArgs()
        {
            //  Note: this doesn't handle invoking .cmd files via "cmd /c" on Windows, which probably won't be necessary here
            //  If it is, refer to the code in WindowsExePreferredCommandSpecFactory in Microsoft.DotNet.Cli.Utils
            return ArgumentEscaper.EscapeAndConcatenateArgArrayForProcessStart(Arguments);
        }
 
        public Command ToCommand(bool doNotEscapeArguments = false)
        {
            var process = new Process()
            {
                StartInfo = ToProcessStartInfo(doNotEscapeArguments)
            };
            var ret = new Command(process, trimTrailingNewlines: true);
            return ret;
        }
 
        public ProcessStartInfo ToProcessStartInfo(bool doNotEscapeArguments = false)
        {
            var ret = new ProcessStartInfo
            {
                FileName = FileName,
                Arguments = doNotEscapeArguments ? string.Join(" ", Arguments) : EscapeArgs(),
                UseShellExecute = false,
                RedirectStandardInput = RedirectStandardInput,
            };
            foreach (var kvp in Environment)
            {
                ret.Environment[kvp.Key] = kvp.Value;
            }
            foreach (var envToRemove in EnvironmentToRemove)
            {
                ret.Environment.Remove(envToRemove);
            }
 
            if (WorkingDirectory != null)
            {
                ret.WorkingDirectory = WorkingDirectory;
            }
 
            return ret;
        }
    }
}