예제 #1
0
        public static async Task <(Output, Logger)> Initialize(Options options, string logPrefix)
        {
            if (logPrefix == null)
            {
                logPrefix = "Bullseye";
                var entryAssembly = Assembly.GetEntryAssembly();
                if (entryAssembly == null)
                {
                    await Console.Error.WriteLineAsync($"{logPrefix}: Failed to get the entry assembly. Using default log prefix \"{logPrefix}\".").Tax();
                }
                else
                {
                    logPrefix = entryAssembly.GetName().Name;
                }
            }

            if (options.Clear)
            {
                try
                {
                    Console.Clear();
                }
#pragma warning disable CA1031 // Do not catch general exception types
                catch (Exception ex)
#pragma warning restore CA1031 // Do not catch general exception types
                {
                    await Console.Error.WriteLineAsync($"{logPrefix}: Failed to clear the console: {ex}").Tax();
                }
            }

            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 (!options.NoColor && operatingSystem == OperatingSystem.Windows)
            {
                await WindowsConsole.TryEnableVirtualTerminalProcessing(options.Verbose?Console.Error : NullTextWriter.Instance, logPrefix).Tax();
            }

            var isHostDetected = false;
            if (options.Host == Host.Unknown)
            {
                isHostDetected = true;

                if (Environment.GetEnvironmentVariable("APPVEYOR")?.ToUpperInvariant() == "TRUE")
                {
                    options.Host = Host.Appveyor;
                }
                else if (Environment.GetEnvironmentVariable("TF_BUILD")?.ToUpperInvariant() == "TRUE")
                {
                    options.Host = Host.AzurePipelines;
                }
                else if (Environment.GetEnvironmentVariable("GITHUB_ACTIONS")?.ToUpperInvariant() == "TRUE")
                {
                    options.Host = Host.GitHubActions;
                }
                else if (Environment.GetEnvironmentVariable("GITLAB_CI")?.ToUpperInvariant() == "TRUE")
                {
                    options.Host = Host.GitLabCI;
                }
                else if (!string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable("TRAVIS_OS_NAME")))
                {
                    options.Host = Host.Travis;
                }
                else if (!string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable("TEAMCITY_PROJECT_NAME")))
                {
                    options.Host = Host.TeamCity;
                }
                else if (Environment.GetEnvironmentVariable("TERM_PROGRAM")?.ToUpperInvariant() == "VSCODE")
                {
                    options.Host = Host.VisualStudioCode;
                }
            }

            var palette = new Palette(options.NoColor, options.NoExtendedChars, options.Host, operatingSystem);
            var output  = new Output(Console.Out, palette);
            var log     = new Logger(Console.Error, logPrefix, options.SkipDependencies, options.DryRun, options.Parallel, palette, options.Verbose);

            await log.Version().Tax();

            await log.Verbose($"Host: {options.Host}{(options.Host != Host.Unknown ? $" ({(isHostDetected ? "detected" : "forced")})" : "")}").Tax();

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

            return(output, log);
        }
        private static async Task RunAsync(TargetCollection targets, List <string> names, Options options, Func <Exception, bool> messageOnly, string logPrefix, bool exit, Func <Logger, Task> logArgs)
        {
            targets     = targets ?? new TargetCollection();
            options     = options ?? new Options();
            messageOnly = messageOnly ?? (_ => false);

            if (logPrefix == null)
            {
                logPrefix = "Bullseye";
                var entryAssembly = Assembly.GetEntryAssembly();
                if (entryAssembly == null)
                {
                    await Console.Error.WriteLineAsync($"{logPrefix}: Failed to get the entry assembly. Using default log prefix \"{logPrefix}\".").Tax();
                }
                else
                {
                    logPrefix = entryAssembly.GetName().Name;
                }
            }

            if (options.Clear)
            {
                try
                {
                    Console.Clear();
                }
#pragma warning disable CA1031 // Do not catch general exception types
                catch (Exception ex)
#pragma warning restore CA1031 // Do not catch general exception types
                {
                    await Console.Error.WriteLineAsync($"{logPrefix}: Failed to clear the console: {ex}").Tax();
                }
            }

            var operatingSystem =
                RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
                    ? OperatingSystem.Windows
                    : RuntimeInformation.IsOSPlatform(OSPlatform.Linux)
                        ? OperatingSystem.Linux
                        : RuntimeInformation.IsOSPlatform(OSPlatform.OSX)
                            ? OperatingSystem.MacOS
                            : OperatingSystem.Unknown;

            if (!options.NoColor && operatingSystem == OperatingSystem.Windows)
            {
                await WindowsConsole.TryEnableVirtualTerminalProcessing(options.Verbose?Console.Error : NullTextWriter.Instance, logPrefix).Tax();
            }

            var(host, isHostDetected) = options.Host.DetectIfUnknown();

            var palette = new Palette(options.NoColor, options.NoExtendedChars, host, operatingSystem);
            var output  = new Output(Console.Out, palette);
            var log     = new Logger(Console.Error, logPrefix, options.SkipDependencies, options.DryRun, options.Parallel, palette, options.Verbose);

            await log.Version(() => typeof(TargetCollectionExtensions).Assembly.GetVersion()).Tax();

            await log.Verbose(() => $"Host: {host}{(host != Host.Unknown ? $" ({(isHostDetected ? "detected" : "forced")})" : "")}").Tax();

            await log.Verbose(() => $"OS: {operatingSystem}").Tax();

            if (logArgs != null)
            {
                await logArgs(log).Tax();
            }

            if (exit)
            {
                try
                {
                    await RunAsync(targets, names, options, messageOnly, output, log).Tax();
                }
                catch (InvalidUsageException ex)
                {
                    await log.Error(ex.Message).Tax();

                    Environment.Exit(2);
                }
                catch (TargetFailedException)
                {
                    Environment.Exit(1);
                }

                Environment.Exit(0);
            }
            else
            {
                await RunAsync(targets, names, options, messageOnly, output, log).Tax();
            }
        }
예제 #3
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);
        }
예제 #4
0
        public static async Task <Logger> Initialize(this IConsole console, Options options)
        {
            if (options.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 (!options.NoColor && operatingSystem == OperatingSystem.Windows)
            {
                await WindowsConsole.TryEnableVirtualTerminalProcessing(console.Out, options.Verbose).ConfigureAwait(false);
            }

            var isHostDetected = false;

            if (options.Host == Host.Unknown)
            {
                isHostDetected = true;

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

            var palette = new Palette(options.NoColor, options.Host, operatingSystem);
            var log     = new Logger(console, options.SkipDependencies, options.DryRun, options.Parallel, palette, options.Verbose);

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

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

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

            return(log);
        }
예제 #5
0
        public static async Task <Logger> Initialize(Options options)
        {
            if (options.Clear)
            {
                try
                {
                    Console.Clear();
                }
#pragma warning disable CA1031 // Do not catch general exception types
                catch (Exception ex)
#pragma warning restore CA1031 // Do not catch general exception types
                {
                    await Console.Out.WriteLineAsync($"Bullseye: Failed to clear the console: {ex}").Tax();
                }
            }

            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 (!options.NoColor && operatingSystem == OperatingSystem.Windows)
            {
                await WindowsConsole.TryEnableVirtualTerminalProcessing(options.Verbose?Console.Out : NullTextWriter.Instance).Tax();
            }

            var isHostDetected = false;
            if (options.Host == Host.Unknown)
            {
                isHostDetected = true;

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

            var palette = new Palette(options.NoColor, options.Host, operatingSystem);
            var log     = new Logger(Console.Out, options.SkipDependencies, options.DryRun, options.Parallel, palette, options.Verbose);

            await log.Version().Tax();

            await log.Verbose($"Host: {options.Host}{(options.Host != Host.Unknown ? $" ({(isHostDetected ? "detected" : "forced")})" : "")}").Tax();

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

            return(log);
        }