Exemple #1
0
        public void LoadModule(CliModuleBase module)
        {
            var cliModuleAttribute = module.GetType().GetCustomAttribute <CliModuleAttribute>();

            CliConsole.WriteLine($"module ({cliModuleAttribute.ModuleName})");
            ModuleNames.Add(cliModuleAttribute.ModuleName);
            MethodsByModules[cliModuleAttribute.ModuleName] = new List <string>();

            var methods = module.GetType().GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);

            foreach (MethodInfo methodInfo in methods.OrderBy(m => m.Name))
            {
                var cliProperty = methodInfo.GetCustomAttribute <CliPropertyAttribute>();
                var cliFunction = methodInfo.GetCustomAttribute <CliFunctionAttribute>();

                bool isProperty = cliProperty != null;

                string objectName = cliProperty?.ObjectName ?? cliFunction?.ObjectName;
                string itemName   = cliProperty?.PropertyName ?? cliFunction?.FunctionName;

                if (objectName == null)
                {
                    throw new InvalidDataException($"Method {methodInfo.Name} of {module.GetType().Name} should be decorated with one of {nameof(CliPropertyAttribute)} or {nameof(CliFunctionAttribute)}");
                }

                ObjectInstance instance;
                if (!_objects.ContainsKey(objectName))
                {
                    instance = _engine.JintEngine.Object.Construct(Arguments.Empty);
                    _engine.JintEngine.SetValue(objectName, instance);
                    _objects[objectName] = instance;
                }

                instance = _objects[objectName];
                var             @delegate      = CreateDelegate(methodInfo, module);
                DelegateWrapper nativeDelegate = new DelegateWrapper(_engine.JintEngine, @delegate);

                if (isProperty)
                {
                    CliConsole.WriteKeyword($"  {objectName}");
                    CliConsole.WriteLine($".{itemName}");

                    MethodsByModules[objectName].Add(itemName);
                    AddProperty(instance, itemName, nativeDelegate);
                }
                else
                {
                    CliConsole.WriteKeyword($"  {objectName}");
                    CliConsole.WriteLine($".{itemName}({string.Join(", ", methodInfo.GetParameters().Select(p => p.Name))})");

                    MethodsByModules[objectName].Add(itemName + "(");
                    AddMethod(instance, itemName, nativeDelegate);
                }
            }

            CliConsole.WriteLine();
        }
Exemple #2
0
        private static void Test()
        {
            CliConsole.WriteLine($"Connecting to {_nodeManager.CurrentUri}");
            JsValue result = _engine.Execute("web3.clientVersion");

            if (result != JsValue.Null)
            {
                CliConsole.WriteGood("Connected");
            }
//            Console.WriteLine(_serializer.Serialize(result.ToObject(), true));
            CliConsole.WriteLine();
        }
Exemple #3
0
        static void Main(string[] args)
        {
            _cliConsole     = new CliConsole();
            _colorScheme    = new DraculaColorScheme();
            _terminal       = _cliConsole.Init(_colorScheme);
            _logManager     = new OneLoggerLogManager(new CliLogger(_cliConsole));
            _historyManager = new StatementHistoryManager(_cliConsole);

            RegisterConverters();
            InitEngine();
            InitNodeManager();
            LoadCliModules();

            ReadLine.AutoCompletionHandler = new AutoCompletionHandler(ModuleLoader);
            _historyManager.Init();

            TestConnection();
            _cliConsole.WriteLine();

            RunEvalLoop();
        }
Exemple #4
0
        private static void RunEvalLoop()
        {
            try
            {
                if (File.Exists(HistoryFilePath))
                {
                    foreach (string line in File.ReadLines(HistoryFilePath).TakeLast(60))
                    {
                        if (line != _removedString)
                        {
                            ReadLine.AddHistory(line);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                CliConsole.WriteErrorLine($"Could not load cmd history from {HistoryFilePath} {e.Message}");
            }

            ReadLine.AutoCompletionHandler = new AutoCompletionHandler();

            while (true)
            {
                try
                {
                    if (_terminal != Terminal.Cmder)
                    {
                        Console.ForegroundColor = ColorScheme.Text;
                    }
                    int    bufferSize = 1024 * 16;
                    string statement;
                    using (Stream inStream = System.Console.OpenStandardInput(bufferSize))
                    {
                        Console.SetIn(new StreamReader(inStream, Console.InputEncoding, false, bufferSize));
                        CliConsole.WriteLessImportant("nethermind> ");
                        statement = _terminal == Terminal.Cygwin ? Console.ReadLine() : ReadLine.Read();
                        CleanStatement(statement);

                        if (!File.Exists(HistoryFilePath))
                        {
                            File.Create(HistoryFilePath).Dispose();
                        }

                        if (!SecuredCommands.Any(sc => statement.Contains(sc)))
                        {
                            ReadLine.AddHistory(statement);

                            using (var fileStream = File.AppendText(HistoryFilePath))
                            {
                                fileStream.WriteLine(statement);
                            }
                        }
                        else
                        {
                            ReadLine.AddHistory(_removedString);
                        }
                    }

                    if (statement == "exit")
                    {
                        break;
                    }

                    JsValue result = _engine.Execute(statement);
                    if (result.IsObject() && result.AsObject().Class == "Function")
                    {
                        CliConsole.WriteGood(result.ToString());
                        CliConsole.WriteLine();
                    }
                    else if (!result.IsNull())
                    {
                        string text = Serializer.Serialize(result.ToObject(), true);
//                        File.AppendAllText("C:\\temp\\cli.txt", text);
                        CliConsole.WriteGood(text);
                    }
                    else
                    {
                        CliConsole.WriteLessImportant("null");
                        CliConsole.WriteLine();
                    }

//                    bool isNull = result.IsNull();
//                    if (!isNull)
//                    {
//                        CliConsole.WriteString(result);
//                    }
                }
                catch (Exception e)
                {
                    CliConsole.WriteException(e);
                }
            }
        }