private static CodeStatement[] CreateAggregateMappingStatements(ClassMappingDescriptor descriptor, ICollection<MemberMappingDescriptor> members, CodeGeneratorContext context)
        {
            /*if (!member.IsAggregateExpression)										  todo: remove
                throw new OtisException(string.Format("Expression '{0}' is not a aggregate expression", member.Expression)); */

            AggregateExpressionBuilder expBuilder = new AggregateExpressionBuilder(descriptor, members, context.FunctionMap);
            return expBuilder.GetStatements();
        }
        public Stream Generate( CodeGeneratorContext context )
        {
            Arg.NotNull( context, nameof( context ) );

            var extension = Path.GetExtension( context.FilePath );
            var message = SR.CodeGeneratorFileTypeNotSupported.FormatDefault( extension );
            context.Progress.ReportWarning( message );
            return new MemoryStream();
        }
        internal static ICodeGenerator CreateCodeGenerator( CodeGeneratorContext context )
        {
            Contract.Requires( context != null );
            Contract.Ensures( Contract.Result<ICodeGenerator>() != null );

            var extension = Path.GetExtension( context.FilePath ).ToUpperInvariant();

            switch ( extension )
            {
                case ".CS":
                    return new CSharpDbContextCodeGenerator();
                default:
                    return new DefaultDbContextGenerator();
            }
        }
        public static CodeStatement[] CreateMappingStatements(ClassMappingDescriptor descriptor, CodeGeneratorContext context)
        {
            Dictionary<string, List<MemberMappingDescriptor>> aggregateGroups = new Dictionary<string, List<MemberMappingDescriptor>>();
            List<CodeStatement> statements = new List<CodeStatement>(20);
            foreach (MemberMappingDescriptor member in descriptor.MemberDescriptors)
            {
                if (member.IsAggregateExpression)
                {
                    // group all agregates by expression to avoid multiple traversals over same path
                    //string path = GetPath(member.Expression);
                    string path = GetPath(descriptor, member);
                    if(!aggregateGroups.ContainsKey(path))
                        aggregateGroups[path] = new List<MemberMappingDescriptor>(1);
                    aggregateGroups[path].Add(member);
                }
                else
                {
                    CodeStatement[] st = CreateNonAggregateMappingStatements(descriptor, member, context);
                    if(member.HasNullValue)
                    {
                        CodeStatement[] falseStatements = st;
                        CodeStatement[] trueStatements = new CodeStatement[1];
                        trueStatements[0] = new CodeAssignStatement(
                            new CodeVariableReferenceExpression("target." + member.Member),
                            new CodeSnippetExpression(member.NullValue.ToString()));

                        string checkExpression = GetNullablePartsCheckExpression(member);
                        CodeExpression ifExpression = new CodeSnippetExpression(checkExpression);

                        st = new CodeStatement[1];
                        st[0] = new CodeConditionStatement(ifExpression, trueStatements, falseStatements);
                    }
                    statements.AddRange(st);
                }
            }

            foreach (List<MemberMappingDescriptor> group in aggregateGroups.Values)
            {
                    CodeStatement[] st = CreateAggregateMappingStatements(descriptor, group, context);
                    statements.AddRange(st);
            }

            return statements.ToArray();
        }
Ejemplo n.º 5
0
        public AssemblerGenerator(CodeGeneratorContext context)
        {
            m_context = context;
            string ns;
            if (string.IsNullOrEmpty(m_context.AssemblerGenerationOptions.Namespace))
                ns = "NS" + Guid.NewGuid().ToString("N");
            else
                ns = m_context.AssemblerGenerationOptions.Namespace;

            m_generator = new ClassMappingGenerator(m_context);

            m_namespace = new CodeNamespace(ns);
            m_namespace.Imports.Add(new CodeNamespaceImport("System"));
            m_namespace.Imports.Add(new CodeNamespaceImport("System.Text"));
            m_namespace.Imports.Add(new CodeNamespaceImport("System.Collections"));
            m_namespace.Imports.Add(new CodeNamespaceImport("System.Collections.Generic"));
            m_namespace.Imports.Add(new CodeNamespaceImport("Otis"));
            m_assemblerClass = new CodeTypeDeclaration("Assembler");
            m_assemblerClass.IsClass = true;
            m_assemblerClass.Attributes = MemberAttributes.Public;
            m_namespace.Types.Add(m_assemblerClass);
            m_explicitAssemblies.Add(Assembly.GetExecutingAssembly().CodeBase.Substring(8));
        }
Ejemplo n.º 6
0
        public void AddMapping(ClassMappingDescriptor descriptor, CodeGeneratorContext context)
        {
            CodeMemberMethod methodAssembleFrom = m_generator.CreateTypeTransformationMethod(descriptor);
            m_assemblerClass.Members.Add(methodAssembleFrom);

            CodeMemberMethod methodAssemble = m_generator.CreateInPlaceTransformationMethod(descriptor);
            m_assemblerClass.Members.Add(methodAssemble);

            CodeMemberMethod methodAssembleValueType = m_generator.CreateInPlaceTransformationMethodForValueTypes(descriptor);
            m_assemblerClass.Members.Add(methodAssembleValueType);

            CodeMemberMethod methodToList = m_generator.CreateToListMethod(descriptor);
            m_assemblerClass.Members.Add(methodToList);

            CodeMemberMethod methodToArray = m_generator.CreateToArrayMethod(descriptor);
            m_assemblerClass.Members.Add(methodToArray);

            string interfaceType = string.Format(	"IAssembler<{0}, {1}>",
                                                    TypeHelper.GetTypeDefinition(descriptor.TargetType),
                                                    TypeHelper.GetTypeDefinition(descriptor.SourceType));

            m_assemblerClass.BaseTypes.Add(interfaceType);
            AddReferencedAssemblies(descriptor);
        }
 public override void GenerateCode(Span target, CodeGeneratorContext context)
 {
     context.CodeTreeBuilder.AddSetBaseTypeChunk(GetBaseType(context, BaseType), target);
 }
Ejemplo n.º 8
0
 /// <summary>
 /// Instantiates a new instance of the <see cref="CodeBuilderContext"/> object.
 /// </summary>
 /// <param name="generatorContext">A <see cref="CodeGeneratorContext"/> to copy information from.</param>
 /// <param name="errorSink">
 /// The <see cref="ErrorSink"/> used to collect <see cref="RazorError"/>s encountered
 /// when parsing the current Razor document.
 /// </param>
 public CodeBuilderContext(CodeGeneratorContext generatorContext, ErrorSink errorSink)
     : base(generatorContext)
 {
     ErrorSink = errorSink;
     ExpressionRenderingMode = ExpressionRenderingMode.WriteToOutput;
 }
 protected override string ResolveType(CodeGeneratorContext context, string baseType)
 {
     return GetBaseType(context, baseType);
 }
Ejemplo n.º 10
0
 public CSharpCodeVisitor(CSharpCodeWriter writer, CodeGeneratorContext context)
     : base(writer, context)
 {
     _paddingBuilder = new CSharpPaddingBuilder(context.Host);
 }
Ejemplo n.º 11
0
 /// <summary>
 /// Generates the code content using the provided context.
 /// </summary>
 /// <param name="context">The <see cref="">context</see> used to generate content.</param>
 /// <returns>A <see cref="Stream">stream</see> containing the generated content.</returns>
 protected override Stream Generate( CodeGeneratorContext context )
 {
     var generator = DbContextGeneratorFactory.CreateCodeGenerator( context );
     return generator.Generate( context );
 }
Ejemplo n.º 12
0
 public void WriteStartInstrumentationContext(CodeGeneratorContext context, SyntaxTreeNode syntaxNode, bool isLiteral)
 {
     WriteStartMethodInvocation(context.Host.GeneratedClassContext.BeginContextMethodName);
     Write(syntaxNode.Start.AbsoluteIndex.ToString(CultureInfo.InvariantCulture));
     WriteParameterSeparator();
     Write(syntaxNode.Length.ToString(CultureInfo.InvariantCulture));
     WriteParameterSeparator();
     Write(isLiteral ? "true" : "false");
     WriteEndMethodInvocation();
 }
Ejemplo n.º 13
0
 protected override void Initialize(CodeGeneratorContext context)
 {
     base.Initialize(context);
 }
Ejemplo n.º 14
0
 public CSharpCodeBuilder(CodeGeneratorContext context)
     : base(context)
 {
 }
Ejemplo n.º 15
0
        protected override void Initialize(CodeGeneratorContext context)
        {
            base.Initialize(context);

            context.GeneratedClass.Members.Insert(0, new CodeSnippetTypeMember(HiddenLinePragma));
        }
Ejemplo n.º 16
0
 public override void PostProcessGeneratedCode(CodeGeneratorContext context)
 {
     _codeTransformer.ProcessGeneratedCode(context.CompileUnit, context.Namespace, context.GeneratedClass, context.TargetMethod);
 }
Ejemplo n.º 17
0
 public override void MapVariables(CodeGeneratorContext context)
 {
     base.MapVariables(context);
     lnkOut.DataMapIdx = (short)_DataIndex;
 }
Ejemplo n.º 18
0
 public override void GenerateInit(CodeGeneratorContext context, TextWriter __CPP)
 {
     __CPP.WriteLine("set_val({0}, {1});", lnkOut.MappedValueName, tbInitVal.Text);
     __CPP.WriteLine("set_val({0}_CHANGED,0);", lnkOut.MappedValueName);
 }
Ejemplo n.º 19
0
 public override void GenerateDataMap(CodeGeneratorContext context, TextWriter __H, TextWriter __CPP)
 {
     __H.WriteLine("#define {0} {1}", lnkOut.MappedValueName, _DataIndex);
     __H.WriteLine("#define {0}_CHANGED {1}", lnkOut.MappedValueName, _DataIndex + 1);
 }
        private static void EvaluateAndGenerateCode( CodeGeneratorContext context, StreamWriter writer )
        {
            Contract.Requires( context != null );
            Contract.Requires( writer != null );

            var ast = ParseText( context.FileContents );
            var root = ast.GetRoot();
            var classes = FindClasses( root );

            if ( classes.Count == 0 )
            {
                writer.WriteLine( SR.DbContextClassNotFound.FormatDefault( Comment ) );
                return;
            }

            var usings = FindUsings( root );
            var declarations = FindInterfaceDeclarations( classes );

            if ( declarations.Count == 0 )
                writer.WriteLine( SR.NoInterfacesFound.FormatDefault( typeof( IReadOnlyRepository<> ), typeof( IRepository<> ), typeof( IUnitOfWork<> ) ) );
            else
                ImplementInterfaces( context, usings, declarations, writer );
        }
Ejemplo n.º 21
0
 public CodeGenerator(CodeGeneratorContext context)
 {
     _context = context;
 }
 private string GetBaseType(CodeGeneratorContext context, string baseType)
 {
     var host = (RazorEngineHost)context.Host;
     return _builtBaseTypeName(host.DefaultBaseTemplateType, baseType);
 }
Ejemplo n.º 23
0
 public void WriteEndInstrumentationContext(CodeGeneratorContext context)
 {
     WriteMethodInvocation(context.Host.GeneratedClassContext.EndContextMethodName);
 }
        private static CodeStatement[] CreateNonAggregateMappingStatements(ClassMappingDescriptor descriptor, MemberMappingDescriptor member, CodeGeneratorContext context)
        {
            if (member.Projections.Count > 0)
                return CreateProjectionMapping(member);

            if (member.Type == typeof(string) || member.Type.IsPrimitive)
                return CreateSimpleMapping(member);

            if (member.IsArray)
                return CreateArrayMappingStatements(member);

            if (member.IsList)
                return CreateListMappingStatements(member);

            return CreateAssemblerMapping(member);
        }
        private static void ImplementInterfaces(
            CodeGeneratorContext context,
            IReadOnlyList<UsingDirectiveSyntax> usings,
            IReadOnlyList<InterfaceDeclaration> declarations,
            TextWriter writer )
        {
            Contract.Requires( context != null );
            Contract.Requires( declarations != null );
            Contract.Requires( writer != null );

            var iterator = declarations.GetEnumerator();

            if ( !iterator.MoveNext() )
                return;

            var indentingWriter = new IndentingTextWriter( writer );
            var implementedInterfaces = new HashSet<string>();
            var hasNamespace = WriteStartClass( indentingWriter, context.DefaultNamespace, usings, iterator.Current.DefiningClass );

            ImplementInterface( indentingWriter, iterator.Current, implementedInterfaces );

            while ( iterator.MoveNext() )
            {
                writer.WriteLine();
                ImplementInterface( indentingWriter, iterator.Current, implementedInterfaces );
            }

            WriteEndClass( indentingWriter, hasNamespace );
        }
 public ClassMappingGenerator(CodeGeneratorContext context)
 {
     m_context = context;
 }
        public Stream Generate( CodeGeneratorContext context )
        {
            Arg.NotNull( context, nameof( context ) );

            var stream = new MemoryStream();
            var writer = new StreamWriter( stream );

            try
            {
                EvaluateAndGenerateCode( context, writer );
            }
            catch
            {
                writer.Close();
                throw;
            }

            writer.Flush();
            stream.Seek( 0L, SeekOrigin.Begin );

            return stream;
        }
Ejemplo n.º 28
0
 public override void GenerateDataMap(CodeGeneratorContext context, TextWriter __H, TextWriter __CPP)
 {
     __H.WriteLine("#define {0}_DOWN {1}", lnkOut.MappedValueName, __buttons.GetDownConstant(__ButtonIndex));
     __H.WriteLine("#define {0}_PRESSED {1}", lnkOut.MappedValueName, __buttons.GetPressedConstant(__ButtonIndex));
 }