示例#1
0
        private static int ParsedOptions(Options options)
        {
            string[] args = options.Value.ToArray();

            // Start logic

            Dictionary <string, string> profilerEnvironmentVariables = Utils.GetProfilerEnvironmentVariables(RunnerFolder, Platform, options);

            if (options.SetEnvironmentVariables)
            {
                Console.WriteLine("Setting up the environment variables.");
                CIConfiguration.SetupCIEnvironmentVariables(profilerEnvironmentVariables);
            }
            else
            {
                string cmdLine = string.Join(' ', args);
                if (!string.IsNullOrWhiteSpace(cmdLine))
                {
                    Console.WriteLine("Running: " + cmdLine);

                    ProcessStartInfo processInfo = Utils.GetProcessStartInfo(args[0], Environment.CurrentDirectory, profilerEnvironmentVariables);
                    if (args.Length > 1)
                    {
                        processInfo.Arguments = string.Join(' ', args.Skip(1).ToArray());
                    }

                    return(Utils.RunProcess(processInfo, _tokenSource.Token));
                }
            }

            return(0);
        }
        public override int Execute(CommandContext context, ConfigureCiSettings settings)
        {
            var profilerEnvironmentVariables = Utils.GetProfilerEnvironmentVariables(
                ApplicationContext.RunnerFolder,
                ApplicationContext.Platform,
                settings);

            if (profilerEnvironmentVariables == null)
            {
                return(1);
            }

            // Enable CI Visibility mode
            profilerEnvironmentVariables[Configuration.ConfigurationKeys.CIVisibility.Enabled] = "1";

            if (!TryExtractCiName(settings, out var ciName))
            {
                return(1);
            }

            AnsiConsole.WriteLine("Setting up the environment variables.");

            if (!CIConfiguration.SetupCIEnvironmentVariables(profilerEnvironmentVariables, ciName))
            {
                return(1);
            }

            return(0);
        }
        public override int Execute(CommandContext context, LegacySettings options)
        {
            var args = options.Command ?? context.Remaining.Raw;

            // Start logic

            var profilerEnvironmentVariables = Utils.GetProfilerEnvironmentVariables(
                ApplicationContext.RunnerFolder,
                ApplicationContext.Platform,
                options);

            if (profilerEnvironmentVariables is null)
            {
                return(1);
            }

            // We try to autodetect the CI Visibility Mode
            if (!options.EnableCIVisibilityMode)
            {
                // Support for VSTest.Console.exe and dotcover
                if (args.Count > 0 && (
                        string.Equals(args[0], "VSTest.Console", StringComparison.OrdinalIgnoreCase) ||
                        string.Equals(args[0], "dotcover", StringComparison.OrdinalIgnoreCase)))
                {
                    options.EnableCIVisibilityMode = true;
                }

                // Support for dotnet test and dotnet vstest command
                if (args.Count > 1 && string.Equals(args[0], "dotnet", StringComparison.OrdinalIgnoreCase))
                {
                    if (string.Equals(args[1], "test", StringComparison.OrdinalIgnoreCase) ||
                        string.Equals(args[1], "vstest", StringComparison.OrdinalIgnoreCase))
                    {
                        options.EnableCIVisibilityMode = true;
                    }
                }
            }

            if (options.EnableCIVisibilityMode)
            {
                // Enable CI Visibility mode by configuration
                profilerEnvironmentVariables[Configuration.ConfigurationKeys.CIVisibility.Enabled] = "1";
            }

            if (options.SetEnvironmentVariables)
            {
                AnsiConsole.WriteLine("Setting up the environment variables.");
                CIConfiguration.SetupCIEnvironmentVariables(profilerEnvironmentVariables, null);
            }
            else if (!string.IsNullOrEmpty(options.CrankImportFile))
            {
                return(Crank.Importer.Process(options.CrankImportFile));
            }
            else
            {
                string cmdLine = string.Join(' ', args);
                if (!string.IsNullOrWhiteSpace(cmdLine))
                {
                    // CI Visibility mode is enabled.
                    // If the agentless feature flag is enabled, we check for ApiKey
                    // If the agentless feature flag is disabled, we check if we have connection to the agent before running the process.
                    if (options.EnableCIVisibilityMode)
                    {
                        var ciVisibilitySettings = Ci.Configuration.CIVisibilitySettings.FromDefaultSources();
                        if (ciVisibilitySettings.Agentless)
                        {
                            if (string.IsNullOrWhiteSpace(ciVisibilitySettings.ApiKey))
                            {
                                Utils.WriteError("An API key is required in Agentless mode.");
                                return(1);
                            }
                        }
                        else if (!Utils.CheckAgentConnectionAsync(options.AgentUrl).GetAwaiter().GetResult())
                        {
                            return(1);
                        }
                    }

                    AnsiConsole.WriteLine("Running: " + cmdLine);

                    var arguments = args.Count > 1 ? string.Join(' ', args.Skip(1).ToArray()) : null;

                    if (Program.CallbackForTests != null)
                    {
                        Program.CallbackForTests(args[0], arguments, profilerEnvironmentVariables);
                        return(0);
                    }

                    var processInfo = Utils.GetProcessStartInfo(args[0], Environment.CurrentDirectory, profilerEnvironmentVariables);
                    if (args.Count > 1)
                    {
                        processInfo.Arguments = arguments;
                    }

                    return(Utils.RunProcess(processInfo, ApplicationContext.TokenSource.Token));
                }
            }

            return(0);
        }