示例#1
0
文件: Program.cs 项目: pingvinen/worm
        public static void Main(string[] args)
        {
            var parser = new Parser(new Worm.WormFactory());
            string workingDir = Directory.GetCurrentDirectory();

            DirectoryInfo libRoot = Directory.GetParent(workingDir).Parent.Parent.GetDirectories("fullflow-lib")[0];
            string modelProjectFile = String.Format("{0}/fullflow-lib.csproj", libRoot.FullName);

            PocoModel model = parser.Parse(modelProjectFile);

            foreach (PocoEntity entity in model.Entities)
            {
                Console.WriteLine("DbFactory: {0}", entity.DbFactory.GetType().Name);
                Console.WriteLine("PocoClassName: {0}", entity.PocoClassName);
                Console.WriteLine("PocoFilename: {0}", entity.PocoFilename);
                Console.WriteLine("PocoNamespace: {0}", entity.PocoNamespace);
                Console.WriteLine("TableName: {0}", entity.TableName);
                Console.WriteLine();

                foreach (PocoField field in entity.Fields)
                {
                    Console.WriteLine("AccessModifier: {0}", field.AccessModifier);
                    Console.WriteLine("AllowNull: {0}", field.AllowNull);
                    Console.WriteLine("ColumnName: {0}", field.ColumnName);
                    Console.WriteLine("HasGetter: {0}", field.HasGetter);
                    Console.WriteLine("HasSetter: {0}", field.HasSetter);
                    Console.WriteLine("IdGenerator: {0}", field.IdGenerator);
                    Console.WriteLine("IsEnum: {0}", field.IsEnum);
                    Console.WriteLine("IsPrimaryKey: {0}", field.IsPrimaryKey);
                    Console.WriteLine("Name: {0}", field.Name);
                    Console.WriteLine("StorageType: {0}", field.StorageType);
                    Console.WriteLine("Type: {0}", field.Type);
                    Console.WriteLine();
                }

                Console.WriteLine();
            }

            Engine eng = new Engine();
            Project proj = new Project(eng);
            proj.Load(modelProjectFile);
            BuildItemGroup compileBuildItemGroup = GetCompileIncludeBuildItemGroup(proj);

            var writer = new DbClassWriter(new WormFactory());
            foreach (PocoEntity entity in model.Entities)
            {
                CodeFile cf = writer.Generate(entity);
                //cf.Filename = cf.Filename.Replace("fullflowlib", "fullflow-lib");
                WriteCodeFile(libRoot, cf);
                AddFileToProject(compileBuildItemGroup, cf);
            }

            proj.Save(modelProjectFile);

            // compile the project again
            parser.Parse(modelProjectFile);
        }
        public void Test()
        {
            var dbClassWriter = new DbClassWriter(new WormFactory());
            var entity = new PocoEntity() {
                DbFactory = new MySqlWormDbFactory(String.Empty),
                PocoClassName = "TestEntity",
                PocoFilename = "TestEntity.cs",
                PocoNamespace = "Test.Entities",
                TableName = "test_entities",
                WormClassName = "WormTestEntity",
                WormFilename = "WormTestEntity.cs",
                WormNamespace = "Test.Entities.Db"
            };

            entity.Fields.Add(new PocoField() {
                AccessModifier = AccessModifier.Public,
                ColumnName = "id",
                HasGetter = true,
                HasSetter = true,
                IdGenerator = WormIdGenerator.AutoIncrement,
                IsPrimaryKey = true,
                Name = "Id",
                StorageType = "int",
                Type = "int"
            });

            entity.Fields.Add(new PocoField() {
                AccessModifier = AccessModifier.Public,
                ColumnName = "my_string",
                HasGetter = true,
                HasSetter = true,
                Name = "MyString",
                StorageType = "varchar(255)",
                Type = "string"
            });

            entity.Fields.Add(new PocoField() {
                AccessModifier = AccessModifier.Public,
                ColumnName = "my_int",
                HasGetter = true,
                HasSetter = true,
                Name = "MyInt",
                StorageType = "int",
                Type = "int"
            });

            var result = dbClassWriter.Generate(entity);

            System.IO.File.WriteAllText("/tmp/mysql_" + result.Filename, result.Content);

            Assert.AreEqual("WormTestEntity.cs", result.Filename);
        }