Beispiel #1
0
        /// <summary>
        /// 生成代码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCodeGenerator_Click(object sender, EventArgs e)
        {
            try
            {
                var msg = this.txtCode.Text = string.Empty;

                if (this._selectedTemp == null)
                    msg = "请选择模板!";
                else if (string.IsNullOrEmpty(this.txtNameSpace.Text))
                {
                    this.txtNameSpace.Focus();

                    msg = "请输入命名空间!";
                }
                else if (string.IsNullOrEmpty(this.txtClassName.Text) && string.IsNullOrEmpty(this.labClassNameExtend.Text))
                {
                    this.txtClassName.Focus();

                    msg = "请输入类名!";
                }

                if (!string.IsNullOrEmpty(msg))
                {
                    MessageUnity.ShowWarningMsg(msg);

                    return;
                }

                this._codeBaseInfo.NameSpace = this.txtNameSpace.Text;
                this._codeBaseInfo.ClassName = this.txtClassName.Text;
                this._codeBaseInfo.ClassNameExtend = this.labClassNameExtend.Text;

                var output = string.Empty;

                if (this._selectedTemp.IsRanOnlyOnceByBath) //RanOnlyOnce
                    output = TableBll.CodeGenerator("dts", new List<string> { this._codeBaseInfo.ClassName }, this._selectedTemp.Path, null);
                else
                    output = TableBll.CodeGenerator(this._codeBaseInfo, this._selectedTemp.Path);

                if (output.StartsWith("error:"))
                    MessageUnity.ShowErrorMsg(output);

                this.txtCode.Text = output;

                this.tabControlOpt.SelectedTab = this.tabPageCode;
            }
            catch (Exception ex)
            {
                MessageUnity.ShowErrorMsg(ex.Message);
            }
        }
Beispiel #2
0
        private void Core()
        {
            var index        = 1;
            var tables       = this.lbTargetTable.Items;
            var tempList     = this._selectedTemps;
            var codeBaseDict = new Dictionary <string, CodeBaseInfo>();

            //step1 获取表结构信息
            var taskArg1 = new TaskArg();

            taskArg1.BeginAction = () =>
            {
                foreach (string table in tables)
                {
                    var dt = TableBll.GetDBTableInfo(this._dbInfo.ConnectionString, table);

                    var codeBaseInfo = TableBll.GetCodeBaseInfoByDBTable(dt);
                    codeBaseInfo.NameSpace = this.txtNameSpace.Text;
                    codeBaseInfo.ClassName = TableBll.GetClassName(dt.TableName);

                    codeBaseDict.Add(table, codeBaseInfo);

                    Thread.Sleep(1);

                    TaskCenter.Instance.ReportProgress(
                        new TaskReportProgressArg
                    {
                        Index = index++,
                        Count = tables.Count,
                        Msg   = string.Format("step1/3 获取 {0} 结构信息......", table)
                    });
                }

                return(null);
            };
            taskArg1.EndAction = (result) =>
            {
                index = 1;
            };

            //step2 创建模板文件夹
            var taskArg2 = new TaskArg();

            taskArg2.BeginAction = () =>
            {
                foreach (var temp in tempList)
                {
                    //一个模板一个子文件夹
                    var savePath = Path.Combine(this.txtOutputDirectory.Text, temp.DirName);

                    if (!Directory.Exists(savePath))
                    {
                        Directory.CreateDirectory(savePath);
                    }

                    Thread.Sleep(100);

                    TaskCenter.Instance.ReportProgress(
                        new TaskReportProgressArg
                    {
                        Index = index++,
                        Count = tempList.Count,
                        Msg   = string.Format("step2/3 创建模板文件夹{0}......", temp.DirName)
                    });
                }

                return(null);
            };
            taskArg2.EndAction = (result) =>
            {
                index = 1;
            };

            //step3 生成代码
            var taskArg3 = new TaskArg();

            taskArg3.BeginAction = () =>
            {
                var list = tempList.Where(d => !d.IsRanOnlyOnceByBath);

                foreach (var temp in list)
                {
                    foreach (string table in tables)
                    {
                        var codeBaseInfo = codeBaseDict[table];
                        codeBaseInfo.ClassNameExtend = temp.ClassNameExtend;

                        var output = TableBll.CodeGenerator(codeBaseInfo, temp.Path);
                        var msg    = string.Format(@"{0}\{1}", temp.Name, table);

                        if (output.StartsWith("error:"))
                        {
                            this._failureTableList.Add(msg);
                        }
                        else
                        {
                            var fileName = string.IsNullOrEmpty(temp.FileName) ? codeBaseInfo.ClassName : temp.FileName;
                            fileName = string.Format("{0}{1}{2}", fileName, temp.ClassNameExtend, temp.FileNameExtend);

                            //一个模板一个子文件夹
                            var savePath = Path.Combine(this.txtOutputDirectory.Text, temp.DirName);

                            if (temp.IsCreateChildDirByTableName)
                            {
                                savePath = Path.Combine(savePath, codeBaseInfo.ClassName);

                                if (!Directory.Exists(savePath))
                                {
                                    Directory.CreateDirectory(savePath);
                                }
                            }

                            savePath = Path.Combine(savePath, fileName);

                            File.WriteAllText(savePath, output, Encoding.UTF8);

                            this._successTableList.Add(msg);
                        }

                        Thread.Sleep(1);

                        TaskCenter.Instance.ReportProgress(
                            new TaskReportProgressArg
                        {
                            Index = index++,
                            Count = list.Count() * tables.Count,
                            Msg   = string.Format("step3/3 生成代码{0}/{1}......", temp.Name, table)
                        });
                    }
                }

                index = 1;

                //RanOnlyOnce
                list = tempList.Where(d => d.IsRanOnlyOnceByBath);

                foreach (var temp in list)
                {
                    var output = TableBll.CodeGenerator("dts", codeBaseDict.Values.Select(d => d.ClassName).ToList(), temp.Path, null);
                    var msg    = temp.Name;

                    if (output.StartsWith("error:"))
                    {
                        this._failureTableList.Add(msg);
                    }
                    else
                    {
                        var fileName = string.Format("{0}{1}{2}", temp.FileName, temp.ClassNameExtend, temp.FileNameExtend);
                        var savePath = Path.Combine(this.txtOutputDirectory.Text, temp.DirName);

                        savePath = Path.Combine(savePath, fileName);

                        File.WriteAllText(savePath, output, Encoding.UTF8);

                        this._successTableList.Add(msg);

                        Thread.Sleep(1);

                        TaskCenter.Instance.ReportProgress(
                            new TaskReportProgressArg
                        {
                            Index = index++,
                            Count = list.Count(),
                            Msg   = string.Format("step3/3 生成代码{0}......", temp.Name)
                        });
                    }
                }

                return(null);
            };
            taskArg3.EndAction = (result) =>
            {
                this.Invoke(new MethodInvoker(() =>
                {
                    var msg = "导出完成";

                    //if (e.Error != null)
                    //{
                    //    msg = e.Error.Message;

                    //    this._successTableList.Clear();
                    //    this._failureTableList.Clear();
                    //}
                    //else if (e.Cancelled)
                    //    msg = "导出被取消!";
                    //else
                    //    msg = "导出完成!";

                    this.SetContorlsEnabled(true);

                    var form = new BathCodeGeneratorResultForm(msg, this.txtOutputDirectory.Text, this._successTableList, this._failureTableList);
                    form.ShowDialog();
                }));
            };

            TaskCenter.Instance.AddTask(taskArg1);
            TaskCenter.Instance.AddTask(taskArg2);
            TaskCenter.Instance.AddTask(taskArg3);
            TaskCenter.Instance.ExecuteTasks();
        }