Exemplo n.º 1
0
        private static SyntaxNode GenerateContext(
            Language language,
            Solution solution,
            string @namespace,
            string name,
            IImmutableList<INamedTypeSymbol> interfaceSymbols,
            Func<INamedTypeSymbol, string> entityNamespaceSelector,
            Func<INamedTypeSymbol, string> entityNameSelector)
        {
            Compilation compilation;
            var brightstarAssemblyPath = GetBrightstarAssemblyLocation(solution);
            
            var references = new[]
            {
                MetadataReference.CreateFromFile(typeof(object).Assembly.Location),
                MetadataReference.CreateFromFile(brightstarAssemblyPath)
            };

            // we create our own compilation with which to generate the entity context
            switch (language)
            {
                case Language.CSharp:
                    compilation = CSharpCompilation.Create("Ignore", references: references);
                    break;
                case Language.VisualBasic:
                    compilation = VB.VisualBasicCompilation.Create("Ignore", references: references);
                    break;
                default:
                    throw new NotSupportedException();
            }

            var syntaxGenerator = SyntaxGenerator.GetGenerator(solution.Workspace, language.ToSyntaxGeneratorLanguageName());
            var generator = new EntityContextGenerator(
                language,
                syntaxGenerator,
                solution,
                compilation,
                @namespace,
                name,
                interfaceSymbols,
                entityNamespaceSelector,
                entityNameSelector);

            var context = generator
                .Generate();

            context = syntaxGenerator.WithLeadingComments(context, headerComment, language);

            return context
                .NormalizeWhitespace();
        }
Exemplo n.º 2
0
        /// <summary>
        /// 生成代码.
        /// </summary>
        /// <param name="codeNamespace">生成代码的命名空间.</param>
        /// <param name="tables">仅为这些表生成代码.</param>
        public void BuildTo(string codeNamespace, List <TableInfoModel> tables = null)
        {
            List <TableInfoModel> buildTables;

            if (tables != null && tables.Count > 0)
            {
                buildTables = tables;
            }
            else
            {
                buildTables = AllTables.Distinct(p => p.tableName).ToList();
            }
            if (buildTables == null || buildTables.Count < 1)
            {
                return;
            }
            // 创建代码生成器.
            CodeGenerator entityGenerator = null, agentGenerator = null, contGenerator = null;

            switch (Pattern)
            {
            case BuildPattern.BuildEntity:
                entityGenerator = new EntityCodeGenerator();
                entityGenerator.LangComments = Language;
                break;

            case BuildPattern.BuildAgent:
                agentGenerator = new EntityAgentCodeGenerator();
                agentGenerator.LangComments = Language;
                contGenerator = new EntityContextGenerator();
                contGenerator.LangComments = Language;
                break;

            default:
                entityGenerator = new EntityCodeGenerator();
                entityGenerator.LangComments = Language;
                agentGenerator = new EntityAgentCodeGenerator();
                agentGenerator.LangComments = Language;
                contGenerator = new EntityContextGenerator();
                contGenerator.LangComments = Language;
                break;
            }
            // 开始生成代码.
            CodeClassDeclaration  entityClass, contextClass, agentClass;
            List <TableInfoModel> currentTable;

            for (int i = 0; i < buildTables.Count; ++i)
            {
                int bfb = (int)(Convert.ToSingle(i) / buildTables.Count * 100);
                currentTable = AllTables.Where(item => item.tableName == buildTables[i].tableName).ToList();
                if (entityGenerator != null) // 生成实体类的代码.
                {
                    OnProgressChange(bfb, string.Format(Language.GetString("BuildingEntityClass"), buildTables[i].tableName));
                    entityClass = new CodeClassDeclaration(currentTable);
                    entityClass.BaseTypes.Add("DataEntity");
                    entityClass.CodeComment = buildTables[i].tableDescription;
                    entityClass.Namespace   = codeNamespace;
                    entityClass.Name        = buildTables[i].tableName;
                    entityClass.TableName   = buildTables[i].tableName;
                    entityGenerator.AddClass(entityClass);
                    entityGenerator.WriteTo(Path.Combine(OutputDir, string.Format("{0}.cs", buildTables[i].tableName)), true);
                }
                if (agentGenerator != null)
                {
                    OnProgressChange(bfb, string.Format(Language.GetString("BuildingAgentClass"), buildTables[i].tableName));
                    agentClass = new CodeClassDeclaration(currentTable);
                    agentClass.BaseTypes.Add("EntityAgent");
                    agentClass.CodeComment = string.Format(Language.GetString("AgentClassComment"), buildTables[i].tableDescription);
                    agentClass.Namespace   = codeNamespace;
                    agentClass.Name        = string.Format("{0}Agent", buildTables[i].tableName);
                    agentClass.TableName   = buildTables[i].tableName;
                    agentGenerator.AddClass(agentClass);

                    OnProgressChange(bfb, string.Format(Language.GetString("BuildingContextClass"), buildTables[i].tableName));
                    contextClass = new CodeClassDeclaration(currentTable);
                    contextClass.BaseTypes.Add(string.Format("TableContext<{0}Agent>", buildTables[i].tableName));
                    contextClass.CodeComment = string.Format(Language.GetString("ContextClassComment"), buildTables[i].tableName);
                    contextClass.Namespace   = codeNamespace;
                    contextClass.Name        = string.Format("{0}Context", buildTables[i].tableName);
                    contextClass.TableName   = buildTables[i].tableName;
                    agentGenerator.AddClass(contGenerator.BuildClass(contextClass));
                    agentGenerator.WriteTo(Path.Combine(OutputDir, string.Format("{0}Context.cs", buildTables[i].tableName)), true);
                }
            }
        }