<commit_before>﻿// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Newtonsoft.Json;
using System;
using System.Reflection;
using System.Collections.Generic;
using System.IO;
using Microsoft.Fx.CommandLine;
using System.Runtime.InteropServices;

namespace Microsoft.DotNet.Execute
{
    public class Executor
    {
        public string configFilePath;
        public string configFileName = "config.json";
        public string CommandSelectedByUser { get; set; }
        
        public Dictionary<string, string> SettingParameters { get; set; }
        public Dictionary<string, Dictionary<string, string>> CommandParameters { get; set; }
        
        public Executor()
        {
            SettingParameters = new Dictionary<string, string>();
            CommandParameters = new Dictionary<string, Dictionary<string, string>>();
            string executorDirectory = Path.GetDirectoryName(typeof(Executor).GetTypeInfo().Assembly.Location);
            configFilePath = Path.GetFullPath(Path.Combine(executorDirectory, @".."));
        }

        public Executor(string configFile=null)
        {
            SettingParameters = new Dictionary<string, string>();
            CommandParameters = new Dictionary<string, Dictionary<string, string>>();

            if (configFile == null)
            {
                string executorDirectory = Path.GetDirectoryName(typeof(Executor).GetTypeInfo().Assembly.Location);
                configFilePath = Path.GetFullPath(Path.Combine(executorDirectory, @".."));
            }
            else
            {
                configFilePath = Path.GetDirectoryName(configFile);
            }
        }

        public Setup OpenFile()
        {
            string configFile = Path.Combine(configFilePath, configFileName);
            if (File.Exists(configFile))
            {
                string jsonFile = File.ReadAllText(configFile);
                try
                {
                    Setup jsonSetup = JsonConvert.DeserializeObject<Setup>(jsonFile);
                    return jsonSetup;
                }
                catch (JsonSerializationException e)
                {
                    Console.WriteLine(e.Message);
                }
                catch (JsonReaderException e)
                {
                    Console.WriteLine(e.Message);
                }
            }
            return null;
        }

        public bool DefineParameters(string[] args, Setup setupInformation)
        {
            string userCommand = string.Empty;
            try
            {
                bool result = CommandLineParser.ParseForConsoleApplication(delegate (CommandLineParser parser)
                {
                    //Settings
                    foreach (KeyValuePair<string, Setting> option in setupInformation.Settings)
                    {
                        string temp = "";
                        parser.DefineOptionalQualifier(option.Key, ref temp, option.Value.Description, option.Value.DefaultValue, option.Value.Values);
                        SettingParameters[option.Key] = temp;
                    }

                    //Commands
                    foreach (KeyValuePair<string, Command> comm in setupInformation.Commands)
                    {
                        parser.DefineParameterSet(comm.Key, ref userCommand, comm.Key, string.Format("Help for {0}", comm.Key));
                        Dictionary<string, string> param = new Dictionary<string, string>();
                        foreach(KeyValuePair<string, AliasPerCommand> aliasInfo in comm.Value.Alias)
                        {
                            string temp = "";
                            parser.DefineOptionalQualifier(aliasInfo.Key, ref temp, aliasInfo.Value.Description, null, null);
                            if(!string.IsNullOrEmpty(temp) && !temp.Equals("true"))
                            {
                                List<string> keys = new List<string>(aliasInfo.Value.Settings.Keys);
                                if(keys.Count < 2)
                                {
                                    foreach (string key in keys)
                                    {
                                        setupInformation.Commands[comm.Key].Alias[aliasInfo.Key].Settings[key] = temp;
                                    }
                                }
                            }

                            param[aliasInfo.Key] = temp;
                        }
                        CommandParameters[comm.Key] = new Dictionary<string, string>(param);
                    }
                }, args, setupInformation);
                SettingParameters["ExtraParameters"] = CommandLineParser.ExtraParameters;
                CommandSelectedByUser = userCommand;
                setupInformation.SettingParameters = SettingParameters;
                return result;
            }
            catch (Exception e)
            {
                Console.Error.WriteLine("Error: {0} {1}", e.Message, e.StackTrace);
                return false;
            }
            
        }

        public static int Main(string[] args)
        {
            string[] parseArgs;
            Executor executor;
            if(args.Length > 0 && args[0].Contains("config.json"))
            {
                executor = new Executor(args[0]);
                parseArgs = new string[args.Length - 1];
                Array.Copy(args, 1, parseArgs, 0, args.Length - 1);
            }
            else
            {
                executor = new Executor();
                parseArgs = args;
            }

            Setup jsonSetup = executor.OpenFile();
            if (jsonSetup == null)
            {
                Console.Error.WriteLine("Error: Could not load Json configuration file.");
                return 1;
            }
            string os = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "windows": "unix";

            jsonSetup.prepareValues(os, executor.SettingParameters, executor.configFilePath);
            if (executor.DefineParameters(parseArgs, jsonSetup))
            {
                List<string> paramSelected = new List<string>();
                foreach (KeyValuePair<string, string> param in executor.CommandParameters[executor.CommandSelectedByUser])
                {
                    if (!string.IsNullOrEmpty(param.Value))<commit_msg>Add error message when no command is passed to run.exe (#966)<commit_after>﻿// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using Newtonsoft.Json;
using System;
using System.Reflection;
using System.Collections.Generic;
using System.IO;
using Microsoft.Fx.CommandLine;
using System.Runtime.InteropServices;

namespace Microsoft.DotNet.Execute
{
    public class Executor
    {
        public string configFilePath;
        public string configFileName = "config.json";
        public string CommandSelectedByUser { get; set; }
        
        public Dictionary<string, string> SettingParameters { get; set; }
        public Dictionary<string, Dictionary<string, string>> CommandParameters { get; set; }
        
        public Executor()
        {
            SettingParameters = new Dictionary<string, string>();
            CommandParameters = new Dictionary<string, Dictionary<string, string>>();
            string executorDirectory = Path.GetDirectoryName(typeof(Executor).GetTypeInfo().Assembly.Location);
            configFilePath = Path.GetFullPath(Path.Combine(executorDirectory, @".."));
        }

        public Executor(string configFile=null)
        {
            SettingParameters = new Dictionary<string, string>();
            CommandParameters = new Dictionary<string, Dictionary<string, string>>();

            if (configFile == null)
            {
                string executorDirectory = Path.GetDirectoryName(typeof(Executor).GetTypeInfo().Assembly.Location);
                configFilePath = Path.GetFullPath(Path.Combine(executorDirectory, @".."));
            }
            else
            {
                configFilePath = Path.GetDirectoryName(configFile);
            }
        }

        public Setup OpenFile()
        {
            string configFile = Path.Combine(configFilePath, configFileName);
            if (File.Exists(configFile))
            {
                string jsonFile = File.ReadAllText(configFile);
                try
                {
                    Setup jsonSetup = JsonConvert.DeserializeObject<Setup>(jsonFile);
                    return jsonSetup;
                }
                catch (JsonSerializationException e)
                {
                    Console.WriteLine(e.Message);
                }
                catch (JsonReaderException e)
                {
                    Console.WriteLine(e.Message);
                }
            }
            return null;
        }

        public bool DefineParameters(string[] args, Setup setupInformation)
        {
            string userCommand = string.Empty;
            try
            {
                bool result = CommandLineParser.ParseForConsoleApplication(delegate (CommandLineParser parser)
                {
                    //Settings
                    foreach (KeyValuePair<string, Setting> option in setupInformation.Settings)
                    {
                        string temp = "";
                        parser.DefineOptionalQualifier(option.Key, ref temp, option.Value.Description, option.Value.DefaultValue, option.Value.Values);
                        SettingParameters[option.Key] = temp;
                    }

                    //Commands
                    foreach (KeyValuePair<string, Command> comm in setupInformation.Commands)
                    {
                        parser.DefineParameterSet(comm.Key, ref userCommand, comm.Key, string.Format("Help for {0}", comm.Key));
                        Dictionary<string, string> param = new Dictionary<string, string>();
                        foreach(KeyValuePair<string, AliasPerCommand> aliasInfo in comm.Value.Alias)
                        {
                            string temp = "";
                            parser.DefineOptionalQualifier(aliasInfo.Key, ref temp, aliasInfo.Value.Description, null, null);
                            if(!string.IsNullOrEmpty(temp) && !temp.Equals("true"))
                            {
                                List<string> keys = new List<string>(aliasInfo.Value.Settings.Keys);
                                if(keys.Count < 2)
                                {
                                    foreach (string key in keys)
                                    {
                                        setupInformation.Commands[comm.Key].Alias[aliasInfo.Key].Settings[key] = temp;
                                    }
                                }
                            }

                            param[aliasInfo.Key] = temp;
                        }
                        CommandParameters[comm.Key] = new Dictionary<string, string>(param);
                    }
                }, args, setupInformation);
                SettingParameters["ExtraParameters"] = CommandLineParser.ExtraParameters;
                CommandSelectedByUser = userCommand;
                setupInformation.SettingParameters = SettingParameters;
                return result;
            }
            catch (Exception e)
            {
                Console.Error.WriteLine("Error: {0} {1}", e.Message, e.StackTrace);
                return false;
            }
            
        }

        public static int Main(string[] args)
        {
            string[] parseArgs;
            Executor executor;
            if(args.Length > 0 && args[0].Contains("config.json"))
            {
                executor = new Executor(args[0]);
                parseArgs = new string[args.Length - 1];
                Array.Copy(args, 1, parseArgs, 0, args.Length - 1);
            }
            else
            {
                executor = new Executor();
                parseArgs = args;
            }

            Setup jsonSetup = executor.OpenFile();
            if (jsonSetup == null)
            {
                Console.Error.WriteLine("Error: Could not load Json configuration file.");
                return 1;
            }
            string os = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "windows": "unix";

            jsonSetup.prepareValues(os, executor.SettingParameters, executor.configFilePath);
            if (executor.DefineParameters(parseArgs, jsonSetup))
            {
                if(string.IsNullOrEmpty(executor.CommandSelectedByUser))
                {
                    Console.Error.WriteLine("Error: No command was passed. Use -? for help.");
                    return 1;
                }

                List<string> paramSelected = new List<string>();
                foreach (KeyValuePair<string, string> param in executor.CommandParameters[executor.CommandSelectedByUser])
                {
                    if (!string.IsNullOrEmpty(param.Value))
{'fraction_non_alphanumeric': 0.08651190565051878, 'fraction_numerical': 0.0020900201537657685, 'mean_word_length': 1.467403314917127, 'pattern_counts': {'":': 2, '<': 47, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6158548', 'n_tokens_mistral': 3042, 'n_tokens_neox': 2883, 'n_words': 662}