Ejemplo n.º 1
0
        public CommonCompilation RoslynCompile(ICodeProject codeProject)
        {
            if (codeProject == null)
            {
                throw new ArgumentNullException("codeProject");
            }

            ProjectId projectId;
            var       solution = CreateSolution(codeProject, out projectId);

            var documentIds = new Dictionary <string, DocumentId>();

            foreach (var document in codeProject.Documents)
            {
                var documentId = DocumentId.CreateNewId(projectId, document.Name);

                var text = document.GetText();
                if (string.Equals(document.Name, "Content"))
                {
                    // This smells terrible.
                    text = BuildScript(text);
                }

                solution = solution.AddDocument(documentId, document.Name, new StringText(text));
                documentIds.Add(document.Name, documentId);
            }

            foreach (var documentId in solution.GetProject(projectId).DocumentIds)
            {
                var document      = solution.GetDocument(documentId);
                var root          = document.GetSyntaxRoot();
                var semanticModel = document.GetSemanticModel();

                var rewriter = new ConsoleRewriter("__Console", semanticModel);

                solution = solution.UpdateDocument(documentId, rewriter.Visit((SyntaxNode)root));
            }

            return(solution.GetProject(projectId).GetCompilation());
        }
Ejemplo n.º 2
0
        public CommonCompilation RoslynCompile(ICodeProject codeProject)
        {
            if (codeProject == null)
            {
                throw new ArgumentNullException("codeProject");
            }

            ProjectId projectId;
            var solution = CreateSolution(codeProject, out projectId);

            var documentIds = new Dictionary<string, DocumentId>();

            foreach (var document in codeProject.Documents)
            {
                var documentId = DocumentId.CreateNewId(projectId, document.Name);

                var text = document.GetText();
                if (string.Equals(document.Name, "Content"))
                {
                    // This smells terrible.
                    text = BuildScript(text);
                }

                solution = solution.AddDocument(documentId, document.Name, new StringText(text));
                documentIds.Add(document.Name, documentId);
            }

            foreach (var documentId in solution.GetProject(projectId).DocumentIds)
            {
                var document = solution.GetDocument(documentId);
                var root = document.GetSyntaxRoot();
                var semanticModel = document.GetSemanticModel();

                var rewriter = new ConsoleRewriter("__Console", semanticModel);

                solution = solution.UpdateDocument(documentId, rewriter.Visit((SyntaxNode)root));
            }

            return solution.GetProject(projectId).GetCompilation();
        }
Ejemplo n.º 3
0
        public CommonCompilation RoslynCompile(ICodeProgram codeProgram)
        {
            if (codeProgram == null)
            {
                throw new ArgumentNullException("codeProgram");
            }

            var references =
                codeProgram.References.Select(x => MetadataReference.CreateAssemblyReference(x.AssemblyName));

            var compilation =
                Compilation.Create(codeProgram.Name ?? "Untitled")
                           .WithReferences(references)
                           .WithOptions(DefaultCompilationOptions)
                           .AddSyntaxTrees(
                               SyntaxTree.ParseText(Console, options: DefaultParseOptions),
                               SyntaxTree.ParseText(EntryPoint, options: DefaultParseOptions));

            foreach (var document in codeProgram.Documents)
            {
                var text = document.IsEntryPoint ? BuildScript(document.Content) : document.Content;

                var tree = SyntaxTree.ParseText(text, document.Name, DefaultParseOptions);

                compilation = compilation.AddSyntaxTrees(tree);

                var rewriter = new ConsoleRewriter("__Console", compilation.GetSemanticModel(tree));

                compilation = compilation.ReplaceSyntaxTree(tree, tree.RewriteWith(rewriter));
            }

            return compilation;
        }