<commit_before>﻿// Copyright 2018 Maintainers of NUKE.
// Distributed under the MIT License.
// https://github.com/nuke-build/nuke/blob/master/LICENSE

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using Nuke.Common.BuildServers;
using Nuke.Common.IO;
using Nuke.Common.OutputSinks;
using Nuke.Common.Tooling;
using Nuke.Common.Utilities;

namespace Nuke.Common.Execution
{
    internal static class BuildExecutor
    {
        public const string DefaultTarget = "default";

        public static int Execute<T>(Expression<Func<T, Target>> defaultTargetExpression)
            where T : NukeBuild
        {
            var executionList = default(IReadOnlyCollection<TargetDefinition>);
            var build = CreateBuildInstance(defaultTargetExpression);

            HandleCompletion(build);

            try
            {
                build.OnBuildCreated();
                
                Logger.OutputSink = build.OutputSink;
                Logger.LogLevel = NukeBuild.LogLevel;
                ToolPathResolver.NuGetPackagesConfigFile = build.NuGetPackagesConfigFile;

                Logger.Log($"NUKE Execution Engine {typeof(BuildExecutor).Assembly.GetInformationalText()}");
                Logger.Log(FigletTransform.GetText("NUKE"));

                HandleEarlyExits(build);
                ProcessManager.CheckPathEnvironmentVariable();
                InjectionService.InjectValues(build);

                executionList = TargetDefinitionLoader.GetExecutingTargets(build, NukeBuild.InvokedTargets, NukeBuild.SkippedTargets);
                RequirementService.ValidateRequirements(executionList, build);

                build.OnBuildInitialized();

                Execute(build, executionList);

                return 0;
            }
            catch (Exception exception)
            {
                Logger.Error(exception);
                return -1;
            }
            finally
            {
                if (Logger.OutputSink is SevereMessagesOutputSink outputSink)
                {
                    Logger.Log();
                    WriteWarningsAndErrors(outputSink);
                }

                if (executionList != null)
                {
                    Logger.Log();
                    WriteSummary(executionList);
                }

                build.OnBuildFinished();
            }
        }<commit_msg>Add parameter for attaching to VisualStudio debugger<commit_after>﻿// Copyright 2018 Maintainers of NUKE.
// Distributed under the MIT License.
// https://github.com/nuke-build/nuke/blob/master/LICENSE

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Threading;
using Nuke.Common.IO;
using Nuke.Common.OutputSinks;
using Nuke.Common.Tooling;
using Nuke.Common.Utilities;

namespace Nuke.Common.Execution
{
    internal static class BuildExecutor
    {
        public const string DefaultTarget = "default";
        private const int c_debuggerAttachTimeout = 10_000;

        public static int Execute<T>(Expression<Func<T, Target>> defaultTargetExpression)
            where T : NukeBuild
        {
            var executionList = default(IReadOnlyCollection<TargetDefinition>);
            var build = CreateBuildInstance(defaultTargetExpression);

            HandleCompletion(build);
            AttachVisualStudioDebugger();

            try
            {
                build.OnBuildCreated();
                
                Logger.OutputSink = build.OutputSink;
                Logger.LogLevel = NukeBuild.LogLevel;
                ToolPathResolver.NuGetPackagesConfigFile = build.NuGetPackagesConfigFile;

                Logger.Log($"NUKE Execution Engine {typeof(BuildExecutor).Assembly.GetInformationalText()}");
                Logger.Log(FigletTransform.GetText("NUKE"));

                HandleEarlyExits(build);
                ProcessManager.CheckPathEnvironmentVariable();
                InjectionService.InjectValues(build);

                executionList = TargetDefinitionLoader.GetExecutingTargets(build, NukeBuild.InvokedTargets, NukeBuild.SkippedTargets);
                RequirementService.ValidateRequirements(executionList, build);

                build.OnBuildInitialized();

                Execute(build, executionList);

                return 0;
            }
            catch (Exception exception)
            {
                Logger.Error(exception);
                return -1;
            }
            finally
            {
                if (Logger.OutputSink is SevereMessagesOutputSink outputSink)
                {
                    Logger.Log();
                    WriteWarningsAndErrors(outputSink);
                }

                if (executionList != null)
                {
                    Logger.Log();
                    WriteSummary(executionList);
                }

                build.OnBuildFinished();
            }
        }

        private static void AttachVisualStudioDebugger()
        {
            if (!ParameterService.Instance.GetParameter<bool>(nameof(AttachVisualStudioDebugger)))
                return;
            
            File.WriteAllText(NukeBuild.TemporaryDirectory / "visual-studio.dbg",
                Process.GetCurrentProcess().Id.ToString());
            ControlFlow.Assert(SpinWait.SpinUntil(() => Debugger.IsAttached, millisecondsTimeout: c_debuggerAttachTimeout),
                $"VisualStudio debugger was not attached within timeout of {c_debuggerAttachTimeout} milliseconds.");
        }
{'fraction_non_alphanumeric': 0.0808133472367049, 'fraction_numerical': 0.002954466458116093, 'mean_word_length': 1.9664948453608246, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 13, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2641812', 'n_tokens_mistral': 1446, 'n_tokens_neox': 1360, 'n_words': 258}