Example #1
0
        /// <summary>
        /// 根据用户配置安装全部模块
        /// </summary>
        public static void Install()
        {
            if (Directory.Exists(moduleDir) == false)
            {
                Directory.CreateDirectory(moduleDir);
            }

            if (UserConfig.Exists() == false)
            {
                Console.WriteLine("用户配置文件 espjs.json 不存在.");
                return;
            }

            UserConfig config = UserConfig.Load();

            foreach (var item in config.Modules)
            {
                Add(item.Key, item.Value);
            }
            Console.WriteLine("模块安装成功.");
        }
Example #2
0
        public static bool TryGetValue(string name, out string value)
        {
            value = "";
            if (UserConfig.Exists() == false)
            {
                Console.WriteLine("用户配置文件 espjs.json 不存在.");
                return(false);
            }

            UserConfig config = UserConfig.Load();

            if (config.Scripts.TryGetValue(name, out string v))
            {
                value = v;
                return(true);
            }
            else
            {
                Console.WriteLine("未在 espjs.json 中定义脚本[" + name + "].");
                return(false);
            }
        }
Example #3
0
        public App(bool runOnce = false)
        {
            if (Uart.HasPort())
            {
                port = Uart.GetPort();
            }

            this.runOnce = runOnce;

            if (!runOnce)
            {
                Console.WriteLine("输入help获取帮助信息");
            }

            // 设置工作目录
            this.workDir = Directory.GetCurrentDirectory();
            string execFile = System.Reflection.Assembly.GetExecutingAssembly().Location;

            this.execDir = Directory.GetParent(execFile).FullName;

            // 加载基础配置
            this.config      = Config.Load();
            uart.sp.BaudRate = this.config.BaudRate;

            // 加载用户配置
            if (UserConfig.Exists())
            {
                this.hasUserConfig = true;
                this.userConfig    = UserConfig.Load();

                // 设置串口参数
                this.uart.sp.BaudRate = this.userConfig.BaudRate;
            }
            else
            {
                // 设置串口参数
                this.uart.sp.BaudRate = config.BaudRate;
            }
        }
Example #4
0
 public bool InIgnore(string path)
 {
     if (!UserConfig.Exists())
     {
         return(false);
     }
     foreach (string value in config.Ignore)
     {
         string rule = value.Replace("*", "");
         if (value.EndsWith("*"))
         {
             if (path.Length >= rule.Length && rule == path.Substring(0, rule.Length))
             {
                 return(true);
             }
         }
         else if (rule == path)
         {
             return(true);
         }
     }
     return(false);
 }
Example #5
0
        /// <summary>
        /// 添加模块
        /// </summary>
        /// <param name="name">模块名</param>
        /// <param name="url">模块地址</param>
        /// <returns></returns>
        public static bool Add(string name, string url)
        {
            try
            {
                if (url == "")
                {
                    url = Config.Load().Modules.Replace("[name]", name);
                }
                string code = GetWebContent(url);
                if (!Directory.Exists(moduleDir))
                {
                    Directory.CreateDirectory(moduleDir);
                }
                File.WriteAllText(moduleDir + name + ".min.js", code);
                Console.WriteLine("模块" + name + "下载完成");
                if (UserConfig.Exists())
                {
                    UserConfig config = UserConfig.Load();
                    if (config.Modules.ContainsKey(name))
                    {
                        config.Modules[name] = url;
                    }
                    else
                    {
                        config.Modules.Add(name, url);
                    }

                    config.Save();
                }
                return(true);
            }
            catch (System.Net.WebException)
            {
                Console.WriteLine("模块下载失败, 请检测模块是否存在");
                return(false);
            }
        }
Example #6
0
        /// <summary>
        /// 检测并运行基础命令
        /// </summary>
        /// <returns></returns>
        public bool IsBaseCommand()
        {
            string cmd = GetParam(0, "");

            switch (cmd)
            {
            case "":
                break;

            case "init":
                Init.Run(workDir, execDir);
                break;

            case "version":
                Console.WriteLine("当前版本: " + this.config.Version);
                break;

            case "debug":
                string debug = GetParam(1, "true");
                if (debug == "true")
                {
                    uart.showRunCode = true;
                    Console.WriteLine("开启调试");
                }
                else
                {
                    uart.showRunCode = false;
                    Console.WriteLine("关闭调试");
                }
                break;

            case "clear":
                uart.codeHistory.Clear();
                Console.Clear();
                break;

            case "atob":
                string atobStr = GetParamOrReadLine(1, "请输入要解密的字符串: ");
                Console.WriteLine(uart.Atob(atobStr));
                break;

            case "btoa":
                string btoaStr = GetParamOrReadLine(1, "请输入要加密的字符串: ");
                Console.WriteLine(uart.Btoa(btoaStr));
                break;

            case "help":
                Console.Write(File.ReadAllText(execDir + @"\help.txt"));
                break;

            case "exit":
            case "quit":
                Environment.Exit(0);
                break;

            case "port":
                Port();
                break;

            case "baud":
                if (args.Length == 1)
                {
                    Console.WriteLine("当前波特率: " + uart.sp.BaudRate);
                }
                else
                {
                    uart.sp.BaudRate = Convert.ToInt32(GetParam(1, "115200"));
                    if (UserConfig.Exists())
                    {
                        userConfig.BaudRate = uart.sp.BaudRate;
                        userConfig.Save();
                    }
                }
                break;

            case "module":
            case "modules":
                ModuleManage();
                break;

            case "run":
                if (Script.TryGetValue(GetParamOrReadLine(1, "请输入要执行的脚本名称: "), out string value))
                {
                    Run(value.Split(' '));
                }
                break;

            default:
                return(false);
            }
            return(true);
        }