public void LoadProjectSymbols() { SymbolProvider.ProjectSymbols.Clear(); var compilation = Microsoft.CodeAnalysis.CSharp.CSharpCompilation.Create(this.CurrentSolution.ToString()); foreach (var project in this.CurrentSolution.Projects) { compilation = compilation.AddReferences(project.MetadataReferences); var instance = ProjectInstances.FirstOrDefault(x => x.FullPath.Equals(project.FilePath)); if (instance.Properties.FirstOrDefault(x => x.Name.Equals("TargetPath", StringComparison.OrdinalIgnoreCase)) is Microsoft.Build.Execution.ProjectPropertyInstance prop && File.Exists(prop.EvaluatedValue)) { compilation = compilation.AddReferences(MetadataReference.CreateFromFile(prop.EvaluatedValue)); } var projectSymbols = new ProjectSymbolCache(project.Name); foreach (var refMeta in compilation.References) { var visitor = new GetAllSymbolsVisitor(); var assemblySymbol = (IAssemblySymbol)compilation.GetAssemblyOrModuleSymbol(refMeta); visitor.Visit(assemblySymbol.GlobalNamespace); projectSymbols.AssemblySymbols.Add(new AssemblySymbolCache(assemblySymbol, visitor.NamedTypes)); } SymbolProvider.ProjectSymbols.Add(projectSymbols); } }
public Task <SyntaxList <MemberDeclarationSyntax> > GenerateAsync(TransformationContext context, IProgress <Diagnostic> progress, CancellationToken cancellationToken) { var results = SyntaxFactory.List <MemberDeclarationSyntax>(); // Our generator is applied to any class that our attribute is applied to. var applyToClass = context.ProcessingNode as ClassDeclarationSyntax; var applyToStruct = context.ProcessingNode as StructDeclarationSyntax; var model = context.Compilation.GetSemanticModel(applyToClass?.SyntaxTree ?? applyToStruct.SyntaxTree); if (applyToClass != null) { var symbol = model.GetDeclaredSymbol(applyToClass); if (InheritsFrom("NetworkEngine.NetworkEntity", symbol)) { results = results.AddRange(GenerateSyncEntityClass(model, applyToClass)); results = GenerateMessages(model, applyToClass, results); } else if (InheritsFrom("NetworkEngine.SyncObject", symbol)) { results = results.AddRange(GenerateSyncObjClass(model, applyToClass)); results = GenerateObjMessages(model, applyToClass, results); } else if (applyToClass.Identifier.Text == SchemaGenerator.SchemaGenClassName) { List <INamedTypeSymbol> allTypesModel = new List <INamedTypeSymbol>(); GetAllSymbolsVisitor visitor = new GetAllSymbolsVisitor(allTypesModel); visitor.Visit(context.Compilation.GlobalNamespace); //results = results.Add(SyntaxFactory.ClassDeclaration("OUTPUT" + Path.Combine(context.ProjectDirectory, "DEFS_SCHEMA.json"))); SchemaGenerator.GenerateSchema(context.ProjectDirectory, allTypesModel); } else { results = results.Add(GenerateStaticSyncObj(model, applyToClass)); } } else if (applyToStruct != null) { var symbol = model.GetDeclaredSymbol(applyToStruct); results = results.Add(GenerateStaticSyncObj(model, applyToStruct)); } return(Task.FromResult <SyntaxList <MemberDeclarationSyntax> >(results)); }