コード例 #1
0
        public CompilationContext(CSharpCompilation compilation,
                                  CompilationProjectContext compilationContext,
                                  IEnumerable <IMetadataReference> incomingReferences,
                                  Func <IList <ResourceDescriptor> > resourcesResolver)
        {
            Project = compilationContext;
            Modules = new List <ICompileModule>();

            _resourcesResolver = resourcesResolver;

            var projectContext = new ProjectContext
            {
                Name             = compilationContext.Target.Name,
                ProjectDirectory = compilationContext.ProjectDirectory,
                ProjectFilePath  = compilationContext.ProjectFilePath,
                TargetFramework  = compilationContext.Target.TargetFramework,
                Version          = compilationContext.Version?.ToString(),
                Configuration    = compilationContext.Target.Configuration
            };

            _beforeCompileContext = new BeforeCompileContext(
                compilation,
                projectContext,
                ResolveResources,
                () => new List <Diagnostic>(),
                () => new List <IMetadataReference>(incomingReferences)
                );
        }
コード例 #2
0
ファイル: CompilationContext.cs プロジェクト: rajeevkb/dnx
        public CompilationContext(CSharpCompilation compilation,
                                  CompilationProjectContext compilationContext,
                                  IEnumerable<IMetadataReference> incomingReferences,
                                  Func<IList<ResourceDescriptor>> resourcesResolver)
        {
            Project = compilationContext;
            Modules = new List<ICompileModule>();

            _resourcesResolver = resourcesResolver;

            var projectContext = new ProjectContext
            {
                Name = compilationContext.Target.Name,
                ProjectDirectory = compilationContext.ProjectDirectory,
                ProjectFilePath = compilationContext.ProjectFilePath,
                TargetFramework = compilationContext.Target.TargetFramework,
                Version = compilationContext.Version?.ToString(),
                Configuration = compilationContext.Target.Configuration
            };

            _beforeCompileContext = new BeforeCompileContext(
                compilation,
                projectContext,
                ResolveResources,
                () => new List<Diagnostic>(),
                () => new List<IMetadataReference>(incomingReferences)
             );
        }
コード例 #3
0
        public void BeforeCompile(BeforeCompileContext context)
        {
            var options = context.Compilation.Options;
            var builder = options.SpecificDiagnosticOptions.ToBuilder();

            // CS0649 Field is never assigned to, and will always have its default value null
            builder["CS0649"] = ReportDiagnostic.Suppress;

            context.Compilation = context.Compilation
                .WithOptions(options.WithSpecificDiagnosticOptions(builder.ToImmutable()));
        }
コード例 #4
0
    public void BeforeCompile(BeforeCompileContext context)
    {
        string analyzerAssemblyPath = Path.Combine(context.ProjectContext.ProjectDirectory, @"../../lib/DotNetDoodle.Analyzers.dll");
        ImmutableArray<DiagnosticAnalyzer> diagnosticAnalyzers = new AnalyzerFileReference(analyzerAssemblyPath, FromFileLoader.Instance).GetAnalyzers(LanguageNames.CSharp);
        var compilation = context.Compilation.WithAnalyzers(diagnosticAnalyzers);
        ImmutableArray<Diagnostic> diagsnostics = compilation.GetAnalyzerDiagnosticsAsync().Result;

        foreach (var diagsnostic in diagsnostics)
        {
            context.Diagnostics.Add(diagsnostic);
        }
    }
コード例 #5
0
        public void BeforeCompile(BeforeCompileContext context)
        {
            //Debugger.Launch();

            foreach(var syntaxTree in context.Compilation.SyntaxTrees)
            {
                var newTree = ModifyTree(syntaxTree);

                // Replace the compilation.
                context.Compilation = context.Compilation.ReplaceSyntaxTree(syntaxTree, newTree);
            }
        }
コード例 #6
0
ファイル: CompileModule.cs プロジェクト: leloulight/Entropy
        public void BeforeCompile(BeforeCompileContext context)
        {
            context.Compilation = context.Compilation.AddSyntaxTrees(
                SyntaxFactory.ParseSyntaxTree(@"
namespace " + context.ProjectContext.Name + @"
{
    public class Foo
    {
        public void Bar()
        {
            System.Console.WriteLine(""Hello from generated code"");
        }
    }
}
")
            );
        }
コード例 #7
0
        /// <inheritdoc />
        /// <remarks>Pre-compiles all Razor views in the application.</remarks>
        public virtual void BeforeCompile(BeforeCompileContext context)
        {
            var compilerOptionsProvider = _appServices.GetRequiredService<ICompilerOptionsProvider>();
            var loadContextAccessor = _appServices.GetRequiredService<IAssemblyLoadContextAccessor>();
            var compilationSettings = GetCompilationSettings(compilerOptionsProvider, context.ProjectContext);
            var fileProvider = new PhysicalFileProvider(context.ProjectContext.ProjectDirectory);

            var viewCompiler = new RazorPreCompiler(
                context,
                loadContextAccessor,
                fileProvider,
                _memoryCache,
                compilationSettings)
            {
                GenerateSymbols = GenerateSymbols
            };

            viewCompiler.CompileViews();
        }
コード例 #8
0
 public void BeforeCompile(BeforeCompileContext context)
 {
     
 }
コード例 #9
0
 protected override bool EnablePreCompilation(BeforeCompileContext context) => true;
コード例 #10
0
        public void BeforeCompile(BeforeCompileContext context) {
            const string typeName = "Record";

            //Debugger.Launch();

            var xxx = context.Compilation.SyntaxTrees
                        .Select(s => new {
                            Tree = s,
                            Root = s.GetRoot(),
                            Class = s.GetRoot().DescendantNodes()
                              .OfType<ClassDeclarationSyntax>()
                              .Where(cs => cs.Identifier.ValueText == typeName)
                              .SingleOrDefault()
                        })
                        .Where(a => a.Class != null)
                        .Single();

            var members = xxx.Class.Members
                .Select(m => m.WithoutTrivia())
                .OfType<PropertyDeclarationSyntax>()
                .Select(m => {
                    var name = m.ChildTokens()
                        .Where(t => t.Kind() == SyntaxKind.IdentifierToken)
                        .Select(t => t.Text)
                        .First();

                    var type = m.Type.ToString();
                    return new { name, type };
                })
                .ToList();

            var ctorParams = members
                .Select(m => SyntaxFactory.Parameter(SyntaxFactory.Identifier(m.name.ToLowerInvariant()))
                                .WithType(SyntaxFactory.ParseTypeName(m.type)))
                .ToArray();

            var assignments = members
                .Select(m => SyntaxFactory.ExpressionStatement(
                                SyntaxFactory.AssignmentExpression(
                                    kind: SyntaxKind.SimpleAssignmentExpression,
                                    left: SyntaxFactory.IdentifierName(m.name),
                                    right: SyntaxFactory.IdentifierName(m.name.ToLowerInvariant())
                                )))
                .ToArray();

            var ctor =
                SyntaxFactory.ConstructorDeclaration(typeName)
                    .AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword))
                    .AddParameterListParameters(ctorParams)
                    .AddBodyStatements(assignments);

            var newClass = xxx.Class.AddMembers(ctor);
            var newRoot = (CSharpSyntaxNode)xxx.Root.ReplaceNode(xxx.Class, newClass);
            var newTree = CSharpSyntaxTree.Create(newRoot);

            //Debugger.Launch();
            //context.AddError(node.Class.Members.Count().ToString());

            context.Compilation = context.Compilation.ReplaceSyntaxTree(xxx.Tree, newTree);

        }
コード例 #11
0
ファイル: CompileHello.cs プロジェクト: JunTaoLuo/TestSN
 public void BeforeCompile(BeforeCompileContext context)
 {
     Console.WriteLine("Hello Before Compile");
 }
コード例 #12
0
ファイル: Internalization.cs プロジェクト: leloulight/dnx
        public void BeforeCompile(BeforeCompileContext context)
        {
            var candidates = new List<string>();

            candidates.Add(Path.Combine(context.ProjectContext.ProjectDirectory, "..", "..", "submodules"));

            if (context.ProjectContext.Name != "Microsoft.Extensions.Runtime")
            {
                candidates.Add(Path.Combine(context.ProjectContext.ProjectDirectory, "..", "Microsoft.Extensions.Runtime.Hosting"));
            }

            var submodulesDir = Path.Combine(context.ProjectContext.ProjectDirectory, "..", "..", "submodules");
            var replacementDict = new Dictionary<SyntaxTree, SyntaxTree>();
            var removeList = new List<SyntaxTree>();

            foreach (var tree in context.Compilation.SyntaxTrees)
            {
                if (string.IsNullOrEmpty(tree.FilePath) ||
                    !candidates.Any(c => IsChildOfDirectory(dir: c, candidate: tree.FilePath)))
                {
                    continue;
                }
                
                if (string.Equals("AssemblyInfo.cs", Path.GetFileName(tree.FilePath),
                    StringComparison.OrdinalIgnoreCase))
                {
                    removeList.Add(tree);
                    continue;
                }

                var root = tree.GetRoot();

                var targetSyntaxKinds = new[] {
                    SyntaxKind.ClassDeclaration,
                    SyntaxKind.InterfaceDeclaration,
                    SyntaxKind.StructDeclaration,
                    SyntaxKind.EnumDeclaration
                };

                var typeDeclarations = root.DescendantNodes()
                    .Where(x => targetSyntaxKinds.Contains(x.Kind()))
                    .OfType<BaseTypeDeclarationSyntax>();
                var publicKeywordTokens = new List<SyntaxToken>();

                foreach (var declaration in typeDeclarations)
                {
                    var publicKeywordToken = declaration.Modifiers
                        .SingleOrDefault(x => x.Kind() == SyntaxKind.PublicKeyword);
                    if (publicKeywordToken != default(SyntaxToken))
                    {
                        publicKeywordTokens.Add(publicKeywordToken);
                    }
                }

                if (publicKeywordTokens.Any())
                {
                    root = root.ReplaceTokens(publicKeywordTokens,
                        (_, oldToken) => SyntaxFactory.ParseToken("internal").WithTriviaFrom(oldToken));
                }

                replacementDict.Add(tree,
                    SyntaxFactory.SyntaxTree(root, tree.Options, tree.FilePath, tree.GetText().Encoding));
            }

            context.Compilation = context.Compilation.RemoveSyntaxTrees(removeList);
            foreach (var pair in replacementDict)
            {
                context.Compilation = context.Compilation.ReplaceSyntaxTree(pair.Key, pair.Value);
            }
        }
コード例 #13
0
 public void BeforeCompile(BeforeCompileContext context)
 {
     ScanCompilation(context.Diagnostics, context.Compilation);
 }
コード例 #14
0
 public void BeforeCompile(BeforeCompileContext context)
 {
     var optionsOverflowCheck = context.Compilation.Options.WithOverflowChecks(true);
     context.Compilation = context.Compilation.WithOptions(optionsOverflowCheck);
 }