Пример #1
0
        public void Execute(GeneratorExecutionContext context)
        {
            var fullSyntaxPath = context.AdditionalFiles[0].Path;

            FileStream fs = null;

            try
            {
                fs = File.OpenRead(fullSyntaxPath);
            }
            catch (Exception ex) when(ex is DirectoryNotFoundException || ex is FileNotFoundException)
            {
                var fnf = Diagnostic.Create("SCG_FileNotFound", "SyntaxCodeGenerator", $"The syntax file at {fullSyntaxPath} was not found.", DiagnosticSeverity.Error, DiagnosticSeverity.Error, true, 0);

                context.ReportDiagnostic(fnf);
                return;
            }

            using (fs)
            {
                var reader = XmlReader.Create(fs, new XmlReaderSettings {
                    DtdProcessing = DtdProcessing.Prohibit
                });
                var  serializer = new XmlSerializer(typeof(Tree));
                Tree tree       = (Tree)serializer.Deserialize(reader);

                context.CancellationToken.ThrowIfCancellationRequested();

                var stringBuilder = new StringBuilder();
                var writer        = new StringWriter(stringBuilder);
                SourceWriter.WriteAll(writer, tree);

                context.AddSource("Syntax.generated.cs", stringBuilder.ToString());
            }
        }
        public Task <RichGenerationResult> GenerateRichAsync(TransformationContext context, IProgress <Diagnostic> progress, CancellationToken cancellationToken)
        {
            var fullSyntaxPath = Path.Combine(context.ProjectDirectory, _syntaxFile);

            FileStream fs = null;

            try
            {
                fs = File.OpenRead(fullSyntaxPath);
            }
            catch (Exception ex) when(ex is DirectoryNotFoundException || ex is FileNotFoundException)
            {
                var fnf = Diagnostic.Create("SCG_FileNotFound", "SyntaxCodeGenerator", $"The syntax file at {fullSyntaxPath} was not found.", DiagnosticSeverity.Error, DiagnosticSeverity.Error, true, 0);

                progress.Report(fnf);
                throw;
            }

            using (fs)
            {
                var reader = XmlReader.Create(fs, new XmlReaderSettings {
                    DtdProcessing = DtdProcessing.Prohibit
                });
                var  serializer = new XmlSerializer(typeof(Tree));
                Tree tree       = (Tree)serializer.Deserialize(reader);

                cancellationToken.ThrowIfCancellationRequested();

                var stringBuilder = new StringBuilder();
                var writer        = new StringWriter(stringBuilder);
                SourceWriter.WriteAll(writer, tree);

                var syntaxTree = CSharpSyntaxTree.ParseText(stringBuilder.ToString(), cancellationToken: cancellationToken);

                var root = syntaxTree.GetCompilationUnitRoot();
                var rgr  = new RichGenerationResult
                {
                    Usings  = root.Usings,
                    Members = root.Members
                };

                return(Task.FromResult(rgr));
            }
        }