<commit_before>﻿using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Xml;
using NuGet;

namespace Bootstrapper
{
    public class Program
    {
        public static int Main(string[] args)
        {
            string exePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), @"NuGet\NuGet.exe");
            try
            {
                var processInfo = new ProcessStartInfo(exePath)
                {
                    UseShellExecute = false,
                    WorkingDirectory = Environment.CurrentDirectory
                };

                if (!File.Exists(exePath))
                {
                    var document = GetConfigDocument();
                    EnsurePackageRestoreConsent(document);
                    ProxyCache.Instance = new ProxyCache(document);
                    // Register a console based credentials provider so that the user get's prompted if a password
                    // is required for the proxy
                    // Setup IHttpClient for the Gallery to locate packages
                    new HttpClient().DownloadData(exePath);
                }
                else if ((DateTime.UtcNow - File.GetLastWriteTimeUtc(exePath)).TotalDays > 10)
                {
                    // Check for updates to the exe every 10 days
                    processInfo.Arguments = "update -self";
                    RunProcess(processInfo);
                    File.SetLastWriteTimeUtc(exePath, DateTime.UtcNow);
                }
                // Convert the args list to a command line input. If an argument has any spaces in it, we need to wrap it with single quotes.
                processInfo.Arguments = String.Join(" ", args.Select(arg => arg.Any(Char.IsWhiteSpace) ? "'" + arg + "'" : arg));
                RunProcess(processInfo);
                return 0;
            }
            catch (Exception e)
            {
                WriteError(e);
            }

            return 1;
        }

        private static XmlDocument GetConfigDocument()
        {
            var configPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "NuGet", "NuGet.config");
            if (File.Exists(configPath))
            {
                var document = new XmlDocument();
                document.Load(configPath);
                return document;
            }
            return null;
        }

        private static void EnsurePackageRestoreConsent(XmlDocument document)
        {
            // Addressing this later.
            var node = document != null ? document.SelectSingleNode(@"configuration/packageRestore/add[@key='enabled']/@value") : null;
            var settingsValue = node != null ? node.Value.Trim() : "";
            var envValue = (Environment.GetEnvironmentVariable("EnableNuGetPackageRestore") ?? String.Empty).Trim();

            bool consent =  settingsValue.Equals("true", StringComparison.OrdinalIgnoreCase) || settingsValue == "1" ||
                            envValue.Equals("true", StringComparison.OrdinalIgnoreCase) || envValue == "1";
            if (!consent)
            {
                throw new InvalidOperationException(LocalizedResourceManager.GetString("RestoreConsent"));
            }
        }

        private static void RunProcess(ProcessStartInfo processInfo)<commit_msg>Modifying Bootstrapper to use CommandLine to parse args<commit_after>﻿using System;
using System.Diagnostics;
using System.IO;
using System.Xml;
using NuGet;

namespace Bootstrapper
{
    public class Program
    {
        public static int Main(string[] args)
        {
            string exePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), @"NuGet\NuGet.exe");
            try
            {
                var processInfo = new ProcessStartInfo(exePath)
                {
                    UseShellExecute = false,
                    WorkingDirectory = Environment.CurrentDirectory
                };

                if (!File.Exists(exePath))
                {
                    var document = GetConfigDocument();
                    EnsurePackageRestoreConsent(document);
                    ProxyCache.Instance = new ProxyCache(document);
                    // Register a console based credentials provider so that the user get's prompted if a password
                    // is required for the proxy
                    // Setup IHttpClient for the Gallery to locate packages
                    new HttpClient().DownloadData(exePath);
                }
                else if ((DateTime.UtcNow - File.GetLastWriteTimeUtc(exePath)).TotalDays > 10)
                {
                    // Check for updates to the exe every 10 days
                    processInfo.Arguments = "update -self";
                    RunProcess(processInfo);
                    File.SetLastWriteTimeUtc(exePath, DateTime.UtcNow);
                }

                processInfo.Arguments = ParseArgs();
                RunProcess(processInfo);
                return 0;
            }
            catch (Exception e)
            {
                WriteError(e);
            }

            return 1;
        }

        public static string ParseArgs()
        {
            string args = Environment.CommandLine.TrimEnd();
            // If the command line starts with quotes, then look for the first occurence of a quote following it. 
            int index = args.StartsWith("\"") ? args.IndexOf("\"", 1) : args.IndexOf(" ");
            if (index == -1 || index >= args.Length - 1)
            {
                return String.Empty;
            }
            return args.Substring(index + 1).Trim();
        }

        private static XmlDocument GetConfigDocument()
        {
            var configPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "NuGet", "NuGet.config");
            if (File.Exists(configPath))
            {
                var document = new XmlDocument();
                document.Load(configPath);
                return document;
            }
            return null;
        }

        private static void EnsurePackageRestoreConsent(XmlDocument document)
        {
            // Addressing this later.
            var node = document != null ? document.SelectSingleNode(@"configuration/packageRestore/add[@key='enabled']/@value") : null;
            var settingsValue = node != null ? node.Value.Trim() : "";
            var envValue = (Environment.GetEnvironmentVariable("EnableNuGetPackageRestore") ?? String.Empty).Trim();

            bool consent =  settingsValue.Equals("true", StringComparison.OrdinalIgnoreCase) || settingsValue == "1" ||
                            envValue.Equals("true", StringComparison.OrdinalIgnoreCase) || envValue == "1";
            if (!consent)
            {
                throw new InvalidOperationException(LocalizedResourceManager.GetString("RestoreConsent"));
            }
        }

        private static void RunProcess(ProcessStartInfo processInfo)
{'fraction_non_alphanumeric': 0.08796622097114708, 'fraction_numerical': 0.0028149190710767065, 'mean_word_length': 1.6926866237211065, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7630656', 'n_tokens_mistral': 1727, 'n_tokens_neox': 1599, 'n_words': 411}