private static async Task RunAsync(this TargetCollection targets, List <string> names, Options options, Output output, Logger log, Func <Exception, bool> messageOnly, List <string> args)
        {
            if (options.UnknownOptions.Count > 0)
            {
                throw new InvalidUsageException($"Unknown option{(options.UnknownOptions.Count > 1 ? "s" : "")} {options.UnknownOptions.Spaced()}. \"--help\" for usage.");
            }

            await log.Verbose($"Args: {string.Join(" ", args)}").Tax();

            if (options.ShowHelp)
            {
                await output.Usage(targets).Tax();

                return;
            }

            if (options.ListTree || options.ListDependencies || options.ListInputs || options.ListTargets)
            {
                var rootTargets          = names.Any() ? names : targets.Select(target => target.Name).OrderBy(name => name).ToList();
                var maxDepth             = options.ListTree ? int.MaxValue : options.ListDependencies ? 1 : 0;
                var maxDepthToShowInputs = options.ListTree ? int.MaxValue : 0;
                await output.Targets(targets, rootTargets, maxDepth, maxDepthToShowInputs, options.ListInputs).Tax();

                return;
            }

            if (names.Count == 0)
            {
                names.Add("default");
            }

            await targets.RunAsync(names, options.SkipDependencies, options.DryRun, options.Parallel, log, messageOnly).Tax();
        }
示例#2
0
        private static async Task RunAsync(this TargetCollection targets, List <string> args, IConsole console)
        {
            var clear            = false;
            var dryRun           = false;
            var listDependencies = false;
            var listInputs       = false;
            var listTargets      = false;
            var noColor          = false;
            var parallel         = false;
            var skipDependencies = false;
            var verbose          = false;
            var host             = Host.Unknown;
            var showHelp         = false;

            var helpOptions    = new[] { "--help", "-h", "-?" };
            var optionsArgs    = args.Where(arg => arg.StartsWith("-", StringComparison.Ordinal)).ToList();
            var unknownOptions = new List <string>();

            foreach (var option in optionsArgs)
            {
                switch (option)
                {
                case "-c":
                case "--clear":
                    clear = true;
                    break;

                case "-n":
                case "--dry-run":
                    dryRun = true;
                    break;

                case "-D":
                case "--list-dependencies":
                    listDependencies = true;
                    break;

                case "-I":
                case "--list-inputs":
                    listInputs = true;
                    break;

                case "-T":
                case "--list-targets":
                    listTargets = true;
                    break;

                case "-N":
                case "--no-color":
                    noColor = true;
                    break;

                case "-p":
                case "--parallel":
                    parallel = true;
                    break;

                case "-s":
                case "--skip-dependencies":
                    skipDependencies = true;
                    break;

                case "-v":
                case "--verbose":
                    verbose = true;
                    break;

                case "--appveyor":
                    host = Host.Appveyor;
                    break;

                case "--travis":
                    host = Host.Travis;
                    break;

                case "--teamcity":
                    host = Host.TeamCity;
                    break;

                default:
                    if (helpOptions.Contains(option, StringComparer.OrdinalIgnoreCase))
                    {
                        showHelp = true;
                    }
                    else
                    {
                        unknownOptions.Add(option);
                    }

                    break;
                }
            }

            if (unknownOptions.Count > 0)
            {
                throw new BullseyeException($"Unknown option{(unknownOptions.Count > 1 ? "s" : "")} {unknownOptions.Spaced()}. \"--help\" for usage.");
            }

            if (clear)
            {
                console.Clear();
            }

            var operatingSystem = OperatingSystem.Unknown;

            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                operatingSystem = OperatingSystem.Windows;
            }
            else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
            {
                operatingSystem = OperatingSystem.Linux;
            }
            else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
            {
                operatingSystem = OperatingSystem.MacOS;
            }

            if (!noColor && operatingSystem == OperatingSystem.Windows)
            {
                await WindowsConsole.TryEnableVirtualTerminalProcessing(console.Out, verbose).ConfigureAwait(false);
            }

            var isHostForced = true;

            if (host == Host.Unknown)
            {
                isHostForced = false;

                if (Environment.GetEnvironmentVariable("APPVEYOR")?.ToUpperInvariant() == "TRUE")
                {
                    host = Host.Appveyor;
                }
                else if (!string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable("TRAVIS_OS_NAME")))
                {
                    host = Host.Travis;
                }
                else if (!string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable("TEAMCITY_PROJECT_NAME")))
                {
                    host = Host.TeamCity;
                }
            }

            var palette = new Palette(noColor, host, operatingSystem);
            var log     = new Logger(console, skipDependencies, dryRun, parallel, palette, verbose);

            await log.Version().ConfigureAwait(false);

            await log.Verbose($"Host: {host}{(host != Host.Unknown ? $" ({(isHostForced ? "forced" : "detected")})" : "")}").ConfigureAwait(false);

            await log.Verbose($"OS: {operatingSystem}").ConfigureAwait(false);

            await log.Verbose($"Args: {string.Join(" ", args)}").ConfigureAwait(false);

            if (showHelp)
            {
                await console.Out.WriteLineAsync(GetUsage(palette)).ConfigureAwait(false);

                return;
            }

            if (listDependencies || listInputs || listTargets)
            {
                await console.Out.WriteLineAsync(targets.ToString(listDependencies, listInputs, palette)).ConfigureAwait(false);

                return;
            }

            var names = args.Where(arg => !arg.StartsWith("-")).ToList();

            if (names.Count == 0)
            {
                names.Add("default");
            }

            await targets.RunAsync(names, skipDependencies, dryRun, parallel, log).ConfigureAwait(false);
        }