public CompilationContext(CSharpCompilation compilation, ICompilationProject project, FrameworkName targetFramework, string configuration, IEnumerable <IMetadataReference> incomingReferences, Func <IList <ResourceDescription> > resourcesResolver) { Project = project; Modules = new List <ICompileModule>(); _resourcesResolver = resourcesResolver; var projectContext = new ProjectContext { Name = project.Name, ProjectDirectory = project.ProjectDirectory, ProjectFilePath = project.ProjectFilePath, TargetFramework = targetFramework, Version = project.Version?.ToString(), Configuration = configuration }; _beforeCompileContext = new BeforeCompileContext { Compilation = compilation, ProjectContext = projectContext, Resources = new LazyList <ResourceDescription>(ResolveResources), Diagnostics = new List <Diagnostic>(), MetadataReferences = new List <IMetadataReference>(incomingReferences) }; }
public void BeforeCompile(BeforeCompileContext context) { var projectName = context.ProjectContext.Name; var members = GetMembers(context.Compilation).ToImmutableArray(); if (members.Length > 0) { var path = InitializeDirectory(context, projectName); var assemblyName = context.Compilation.Assembly.Name; var assembly = new Assembly(assemblyName, members); CreateDocument(path, assembly); } }
public void BeforeCompile(BeforeCompileContext context) { // Get our Greeter class. var syntaxMatch = context.Compilation.SyntaxTrees .Select(s => new { Tree = s, Root = s.GetRoot(), Class = s.GetRoot().DescendantNodes().OfType<ClassDeclarationSyntax>().Where(cs => cs.Identifier.ValueText == "Greeter").SingleOrDefault() }) .Where(a => a.Class != null) .Single(); var tree = syntaxMatch.Tree; var root = syntaxMatch.Root; var classSyntax = syntaxMatch.Class; // Get the method declaration. var methodSyntax = classSyntax.Members .OfType<MethodDeclarationSyntax>() .Where(ms => ms.Identifier.ValueText == "GetMessage") .Single(); // Let's implement the body. var returnStatement = F.ReturnStatement( F.LiteralExpression( K.StringLiteralExpression, F.Literal(@"""Hello World!"""))); // Get the body block var bodyBlock = methodSyntax.Body; // Create a new body block, with our new statement. var newBodyBlock = F.Block(new StatementSyntax[] { returnStatement }); // Get the revised root var newRoot = (CompilationUnitSyntax)root.ReplaceNode(bodyBlock, newBodyBlock); // Create a new syntax tree. var newTree = T.Create(newRoot); // Replace the compilation. context.Compilation = context.Compilation.ReplaceSyntaxTree(tree, newTree); }
/// <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(); }
/// <summary> /// Initializes a new instance of <see cref="PrecompilationTagHelperTypeResolver"/>. /// </summary> /// <param name="compileContext">The <see cref="BeforeCompileContext"/>.</param> /// <param name="loadContext">The <see cref="IAssemblyLoadContext"/>.</param> public PrecompilationTagHelperTypeResolver([NotNull] BeforeCompileContext compileContext, [NotNull] IAssemblyLoadContext loadContext) { _compileContext = compileContext; _loadContext = loadContext; }
public void BeforeCompile(BeforeCompileContext context) { var candidates = new List<string>(); candidates.Add(Path.Combine(context.ProjectContext.ProjectDirectory, "..", "..", "submodules")); if (context.ProjectContext.Name != "Microsoft.Framework.Runtime") { candidates.Add(Path.Combine(context.ProjectContext.ProjectDirectory, "..", "Microsoft.Framework.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); } }
private static string InitializeDirectory(BeforeCompileContext context, string projectName) { var projectDirectory = context.ProjectContext.ProjectDirectory; var fileName = $"{projectName}.ExternalConfiguration.xml"; var configuration = context.ProjectContext.Configuration; var binDirectory = Path.Combine(projectDirectory, "bin", configuration); Directory.CreateDirectory(binDirectory); return Path.Combine(binDirectory, fileName); }