Exemplo n.º 1
0
        public ResultSummary Execute(ExecuteInfo execInfo)
        {
            _currentExecuteInfo = execInfo;

            Exception exception = null;

            StringBuilder sb = new StringBuilder();

            AppendLog(sb, "启动时间:", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

            AppendLog(sb, "启动参数:", ("脚本文件:" + execInfo.FilePath));

            try {
                ExecuteCsCode();

                InternalExecute();
            }
            catch (Exception ex) {
                exception = ex;
            }

            ResultSummary summary = new ResultSummary {
                ErrorCount = _errorCount
            };

            summary.Message = (_errorCount == 0)
                                ? string.Format("{0} 个任务都已成功执行。", execInfo.List.Count)
                                : string.Format("{0} 个任务执行结束,有 {1} 个执行失败。", execInfo.List.Count, _errorCount);

            if (exception == null)
            {
                AppendLog(sb, "执行结果:", summary.Message);
            }
            else
            {
                AppendLog(sb, "执行失败:", exception.ToString());
            }


            AppendLog(sb, "脚本内容:", execInfo.FileContext);

            //SafeWriteLogFile(sb.ToString());

            if (exception != null)
            {
                throw exception;
            }

            return(summary);
        }
Exemplo n.º 2
0
        private void SetTargetSite(ExecuteInfo execInfo)
        {
            string websiteAddress = execInfo.GetParameter("websiteAddress");

            if (string.IsNullOrEmpty(websiteAddress) == false)
            {
                if (websiteAddress.EndsWith("/"))
                {
                    websiteAddress = websiteAddress.Trim('/');
                }

                execInfo.TargetSite = websiteAddress;
            }

            // 如果没有指定网址参数,执行时就不替换URL
        }
Exemplo n.º 3
0
        private static void RunCommandLine(string[] args)
        {
            // 命令行格式:  ClownFish.PreheatWebSite.exe  "x:\xxxx\test1.preheat.script"

            string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, args[1]);

            ScriptParser parser   = new ScriptParser();
            ExecuteInfo  execInfo = parser.Parse(filePath);

            if (execInfo.List.Count == 0)
            {
                throw new InvalidOperationException("没有需要执行的任务。");
            }

            ScriptExecutor executor = new ScriptExecutor();

            executor.Execute(execInfo);
        }
Exemplo n.º 4
0
        private ExecuteInfo LoadScriptFile(string filePath)
        {
            ExecuteInfo execInfo = null;

            try {
                ScriptParser parser = new ScriptParser();
                execInfo = parser.Parse(filePath);
            }
            catch (Exception ex) {
                MessageBox.Show(ex.ToString(), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                return(null);
            }

            _currentExecuteInfo = execInfo;

            // 恢复控件状态
            txtScript.Text    = execInfo.FileContext;
            progressBar.Value = 0;
            linkOpenFile.Text = filePath;
            labMessage.Text   = "Ready.";


            listView1.BeginUpdate();
            listView1.Items.Clear();

            foreach (RequestInfo request in execInfo.List)
            {
                ListViewItem item = new ListViewItem(request.RelativeUrl);
                item.ImageIndex = ICON_ITEM;
                item.SubItems.Add(string.Empty);
                listView1.Items.Add(item);
            }
            listView1.EndUpdate();


            return(execInfo);
        }
Exemplo n.º 5
0
        /// <summary>
        /// 解析指定的脚本文件
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public ExecuteInfo Parse(string filePath)
        {
            if (string.IsNullOrEmpty(filePath))
            {
                throw new ArgumentNullException("filePath");
            }

            if (File.Exists(filePath) == false)
            {
                throw new FileNotFoundException("文件 " + filePath + " 不存在,或者没有权限访问。");
            }


            ExecuteInfo execInfo = new ExecuteInfo();

            execInfo.List        = new List <RequestInfo>();
            execInfo.FileContext = File.ReadAllText(filePath, Encoding.UTF8);
            execInfo.FilePath    = filePath;
            execInfo.Parameters  = new Dictionary <string, string>();


            // 检查有没有包含 C# 代码
            int csP1 = execInfo.FileContext.IndexOf(s_csCodeSeparatorLine);

            if (csP1 > 0)
            {
                csP1 += s_csCodeSeparatorLine.Length + 1;
                int csP2 = execInfo.FileContext.IndexOf(s_csCodeSeparatorLine, csP1);
                if (csP2 > csP1)
                {
                    // 找到 C# 代码块
                    execInfo.CsCode = execInfo.FileContext.Substring(csP1, csP2 - csP1).Trim();
                }
            }



            string        block     = null;
            Match         lastMatch = null;
            StringBuilder sb        = new StringBuilder();

            using (StringReader reader = new StringReader(execInfo.FileContext)) {
                string line = null;
                while ((line = reader.ReadLine()) != null)
                {
                    if (line.StartsWith("#"))                           // 忽略注释行
                    {
                        continue;
                    }

                    Match varMatch = s_varRegex.Match(line);
                    if (varMatch.Success)
                    {
                        string name  = varMatch.Groups["name"].Value;
                        string value = varMatch.Groups["value"].Value;
                        // 将读取到的变量保存起来。
                        execInfo.Parameters[name] = value;

                        // 如果是变量行定义,就跳过下面的解析过程。
                        continue;
                    }


                    Match match = s_httpRegex.Match(line);
                    if (match.Success)                              // 找到一个HTTP请求的开头

                    {
                        if (lastMatch != null)                              // 用当前行之前的内容构造一个RequestInfo对象

                        {
                            block = sb.ToString().Trim();
                            if (string.IsNullOrEmpty(block) == false)
                            {
                                RequestInfo request = CreateRequestInfo(block, lastMatch);
                                execInfo.List.Add(request);
                            }
                        }
                        lastMatch = match;
                        sb.Clear();
                    }

                    sb.AppendLine(line);
                }
            }

            // 处理剩余部分
            if (lastMatch != null)
            {
                block = sb.ToString().Trim();
                if (string.IsNullOrEmpty(block) == false)
                {
                    RequestInfo request = CreateRequestInfo(block, lastMatch);
                    execInfo.List.Add(request);
                }
            }

            SetTargetSite(execInfo);
            return(execInfo);
        }