Exemplo n.º 1
0
        static void Main(string[] args)
        {
            bool slient = false;

            //args = new string[] { @"E:\QingdaoProject\Shumi.Qingdao.Design" };
            if (args != null && args.Length > 0)
            {
                string designRootPath = args[0];
                string configFile = Path.Combine(designRootPath, "EntityDesignConfig.xml");

                try
                {
                    XmlTextReader reader = new XmlTextReader(configFile);
                    XmlSerializer serializer = new XmlSerializer(typeof(EntityDesignConfiguration));
                    EntityDesignConfiguration config = (EntityDesignConfiguration)serializer.Deserialize(reader);

                    if (config != null)
                    {
                        Assembly ass = Assembly.LoadFrom(designRootPath + "\\bin\\" + config.CompileMode + "\\" + config.InputDllName);

                        System.Text.Encoding encoding = System.Text.Encoding.Default;
                        if (!string.IsNullOrEmpty(config.OutputCodeFileEncoding))
                        {
                            encoding = System.Text.Encoding.GetEncoding(config.OutputCodeFileEncoding);
                        }

                        if (!string.IsNullOrEmpty(config.EntityCodePath))
                        {
                            AdvOptForm adv = new AdvOptForm();

                            bool isDirectory;
                            string entityCodePath = config.EntityCodePath;
                            string filePath = ParseRelativePath(designRootPath, entityCodePath, out isDirectory);
                            string fileBody = new CodeGenHelper(config.OutputNamespace, adv).GenEntitiesEx(ass, config.OutputLanguage.ToLower() == "c#" ? 0 : 1);

                            if (Path.GetFileName(filePath) != string.Empty)
                            {
                                WriteFile(filePath, fileBody, encoding);
                            }
                            else
                            {
                                DirectoryInfo info = new DirectoryInfo(filePath);
                                if (!info.Exists) info.Create(); //不存在,则创建目录

                                FileAttributes attribute = info.Attributes;
                                if ((attribute & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
                                {
                                    info.Attributes = attribute ^ FileAttributes.ReadOnly;
                                }
                                Type[] types = ass.GetTypes();
                                if (types != null)
                                {
                                    List<Type> typelist = new List<Type>(types);
                                    typelist.RemoveAll(type =>
                                    {
                                        return !type.IsInterface;
                                    });

                                    string[] files = fileBody.Split(new string[] { "namespace", "Namespace" }, StringSplitOptions.RemoveEmptyEntries);
                                    IList<string> filelist = new List<string>(files);
                                    filelist.RemoveAt(0);

                                    Dictionary<string, string> dictTypeFiles = new Dictionary<string, string>();
                                    for (int index = 0; index < typelist.Count; index++)
                                    {
                                        if (config.OutputLanguage.ToLower() == "c#")
                                            dictTypeFiles[typelist[index].Name] = "namespace" + filelist[index];
                                        else
                                            dictTypeFiles[typelist[index].Name] = "Namespace" + filelist[index];
                                    }

                                    if (dictTypeFiles.Count > 0)
                                    {
                                        foreach (string txtKey in dictTypeFiles.Keys)
                                        {
                                            string path = string.Format("{0}\\{1}.cs", filePath.TrimEnd('\\'), txtKey);
                                            WriteFile(path, dictTypeFiles[txtKey], encoding);
                                        }
                                    }
                                }
                            }

                        }
                    }

                    slient = true;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    Application.Exit();
                }
            }

            if (!slient)
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new EntityDesign());
            }
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            bool slient = false;

            //args = new string[] { @"E:\QingdaoProject\Shumi.Qingdao.Design" };
            if (args != null && args.Length > 0)
            {
                string designRootPath = args[0];
                string configFile     = Path.Combine(designRootPath, "EntityDesignConfig.xml");

                try
                {
                    XmlTextReader             reader     = new XmlTextReader(configFile);
                    XmlSerializer             serializer = new XmlSerializer(typeof(EntityDesignConfiguration));
                    EntityDesignConfiguration config     = (EntityDesignConfiguration)serializer.Deserialize(reader);

                    if (config != null)
                    {
                        Assembly ass = Assembly.LoadFrom(designRootPath + "\\bin\\" + config.CompileMode + "\\" + config.InputDllName);

                        System.Text.Encoding encoding = System.Text.Encoding.Default;
                        if (!string.IsNullOrEmpty(config.OutputCodeFileEncoding))
                        {
                            encoding = System.Text.Encoding.GetEncoding(config.OutputCodeFileEncoding);
                        }

                        if (!string.IsNullOrEmpty(config.EntityCodePath))
                        {
                            AdvOptForm adv = new AdvOptForm();

                            bool   isDirectory;
                            string entityCodePath = config.EntityCodePath;
                            string filePath       = ParseRelativePath(designRootPath, entityCodePath, out isDirectory);
                            string fileBody       = new CodeGenHelper(config.OutputNamespace, adv).GenEntitiesEx(ass, config.OutputLanguage.ToLower() == "c#" ? 0 : 1);

                            if (Path.GetFileName(filePath) != string.Empty)
                            {
                                WriteFile(filePath, fileBody, encoding);
                            }
                            else
                            {
                                DirectoryInfo info = new DirectoryInfo(filePath);
                                if (!info.Exists)
                                {
                                    info.Create();               //不存在,则创建目录
                                }
                                FileAttributes attribute = info.Attributes;
                                if ((attribute & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
                                {
                                    info.Attributes = attribute ^ FileAttributes.ReadOnly;
                                }
                                Type[] types = ass.GetTypes();
                                if (types != null)
                                {
                                    List <Type> typelist = new List <Type>(types);
                                    typelist.RemoveAll(type =>
                                    {
                                        return(!type.IsInterface);
                                    });

                                    string[]       files    = fileBody.Split(new string[] { "namespace", "Namespace" }, StringSplitOptions.RemoveEmptyEntries);
                                    IList <string> filelist = new List <string>(files);
                                    filelist.RemoveAt(0);

                                    Dictionary <string, string> dictTypeFiles = new Dictionary <string, string>();
                                    for (int index = 0; index < typelist.Count; index++)
                                    {
                                        if (config.OutputLanguage.ToLower() == "c#")
                                        {
                                            dictTypeFiles[typelist[index].Name] = "namespace" + filelist[index];
                                        }
                                        else
                                        {
                                            dictTypeFiles[typelist[index].Name] = "Namespace" + filelist[index];
                                        }
                                    }

                                    if (dictTypeFiles.Count > 0)
                                    {
                                        foreach (string txtKey in dictTypeFiles.Keys)
                                        {
                                            string path = string.Format("{0}\\{1}.cs", filePath.TrimEnd('\\'), txtKey);
                                            WriteFile(path, dictTypeFiles[txtKey], encoding);
                                        }
                                    }
                                }
                            }
                        }
                    }

                    slient = true;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    Application.Exit();
                }
            }

            if (!slient)
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new EntityDesign());
            }
        }