示例#1
0
        public LexFormatScanners Compile(
            string segmentScannerCode,
            string recordScannerCode,
            Stream saveAssemblyTo = null)
        {
            segmentScannerCode = Trim(segmentScannerCode);
            recordScannerCode = Trim(recordScannerCode);

            var result = new LexFormatScanners();

            this.Diagnostics.WriteLine("Compiling segments scanner");

            var name = string.Format("LexFileFormat-{0}", Guid.NewGuid().ToString("N"));

            var segmentsSyntaxTree = this.CreateSyntaxTree(
                new StringBuilder()
                    .AppendFormat("%using {0};\n", typeof (ScanBase).Namespace).AppendLine()
                    .AppendLine("%namespace LogWatch.Features.Formats.Lex.Segments")
                    .AppendLine("%{")
                    .AppendLine(BufferWithEncodingAndByteCounterCode)
                    .AppendLine(SegmentsScannerClassCode)
                    .AppendLine(CommonScannerClassCode)
                    .AppendLine("%}")
                    .AppendLine()
                    .AppendLine(segmentScannerCode)
                    .ToString());

            if (segmentsSyntaxTree == null)
                return result;

            this.Diagnostics.WriteLine("Compiling records scanner");

            var recordsSyntaxTree = this.CreateSyntaxTree(
                new StringBuilder()
                    .AppendFormat("%using {0};\n", typeof (ScanBase).Namespace)
                    .AppendLine("%namespace LogWatch.Features.Formats.Lex.Records")
                    .AppendLine("%{")
                    .AppendLine(RecordsScannerClassCode)
                    .AppendLine(CommonScannerClassCode)
                    .AppendLine("%}")
                    .AppendLine(recordScannerCode)
                    .ToString());

            if (recordsSyntaxTree == null)
                return result;

            this.Diagnostics.WriteLine("Creating assembly");

            var compilation = this.CreateCompilation(name, segmentsSyntaxTree, recordsSyntaxTree);

            var assembly = AppDomain.CurrentDomain.DefineDynamicAssembly(
                new AssemblyName(name), AssemblyBuilderAccess.RunAndCollect);

            var module = assembly.DefineDynamicModule(name);

            var emitResult = compilation.Emit(module);

            foreach (var diagnostic in emitResult.Diagnostics)
                this.Diagnostics.WriteLine(diagnostic);

            if (!emitResult.Success)
                return result;

            if (saveAssemblyTo != null)
                compilation.Emit(saveAssemblyTo);

            return new LexFormatScanners {
                SegmentsScannerType = assembly.GetType(SegmentsScannerTypeName),
                RecordsScannerType = assembly.GetType(RecordsScannerTypeName),
                Success = true
            };
        }
示例#2
0
        public LexFormatScanners Compile(
            string segmentScannerCode,
            string recordScannerCode,
            Stream saveAssemblyTo = null)
        {
            segmentScannerCode = Trim(segmentScannerCode);
            recordScannerCode  = Trim(recordScannerCode);

            var result = new LexFormatScanners();

            this.Diagnostics.WriteLine("Compiling segments scanner");

            var name = string.Format("LexFileFormat-{0}", Guid.NewGuid().ToString("N"));

            var segmentsSyntaxTree = this.CreateSyntaxTree(
                new StringBuilder()
                .AppendFormat("%using {0};\n", typeof(ScanBase).Namespace).AppendLine()
                .AppendLine("%namespace LogWatch.Features.Formats.Lex.Segments")
                .AppendLine("%{")
                .AppendLine(BufferWithEncodingAndByteCounterCode)
                .AppendLine(SegmentsScannerClassCode)
                .AppendLine(CommonScannerClassCode)
                .AppendLine("%}")
                .AppendLine()
                .AppendLine(segmentScannerCode)
                .ToString());

            if (segmentsSyntaxTree == null)
            {
                return(result);
            }

            this.Diagnostics.WriteLine("Compiling records scanner");

            var recordsSyntaxTree = this.CreateSyntaxTree(
                new StringBuilder()
                .AppendFormat("%using {0};\n", typeof(ScanBase).Namespace)
                .AppendLine("%namespace LogWatch.Features.Formats.Lex.Records")
                .AppendLine("%{")
                .AppendLine(RecordsScannerClassCode)
                .AppendLine(CommonScannerClassCode)
                .AppendLine("%}")
                .AppendLine(recordScannerCode)
                .ToString());

            if (recordsSyntaxTree == null)
            {
                return(result);
            }

            this.Diagnostics.WriteLine("Creating assembly");

            var compilation = this.CreateCompilation(name, segmentsSyntaxTree, recordsSyntaxTree);

            var assembly = AppDomain.CurrentDomain.DefineDynamicAssembly(
                new AssemblyName(name), AssemblyBuilderAccess.RunAndCollect);

            var module = assembly.DefineDynamicModule(name);

            var emitResult = compilation.Emit(module);

            foreach (var diagnostic in emitResult.Diagnostics)
            {
                this.Diagnostics.WriteLine(diagnostic);
            }

            if (!emitResult.Success)
            {
                return(result);
            }

            if (saveAssemblyTo != null)
            {
                compilation.Emit(saveAssemblyTo);
            }

            return(new LexFormatScanners {
                SegmentsScannerType = assembly.GetType(SegmentsScannerTypeName),
                RecordsScannerType = assembly.GetType(RecordsScannerTypeName),
                Success = true
            });
        }