private Compilation AddAssemblyReferences(IExecutionContext context, List <ISymbol> symbols, Compilation compilation) { IEnumerable <IFile> assemblyFiles = context.FileSystem.GetInputFiles(_assemblyGlobs) .Where(x => (x.Path.Extension == ".dll" || x.Path.Extension == ".exe") && x.Exists); MetadataReference[] assemblyReferences = assemblyFiles.Select(assemblyFile => { // Create the metadata reference for the compilation IFile xmlFile = context.FileSystem.GetFile(assemblyFile.Path.ChangeExtension("xml")); if (xmlFile.Exists) { Trace.Verbose($"Creating metadata reference for assembly {assemblyFile.Path.FullPath} with XML documentation file at {xmlFile.Path.FullPath}"); return(MetadataReference.CreateFromFile( assemblyFile.Path.FullPath, documentation: XmlDocumentationProvider.CreateFromFile(xmlFile.Path.FullPath))); } Trace.Verbose($"Creating metadata reference for assembly {assemblyFile.Path.FullPath} without XML documentation file"); return((MetadataReference)MetadataReference.CreateFromFile(assemblyFile.Path.FullPath)); }).ToArray(); if (assemblyReferences.Length > 0) { compilation = compilation.AddReferences(assemblyReferences); symbols.AddRange(assemblyReferences .Select(x => (IAssemblySymbol)compilation.GetAssemblyOrModuleSymbol(x)) .Select(x => _assemblySymbols ? x : (ISymbol)x.GlobalNamespace)); } return(compilation); }
public static EmitResult Compile(GameRelease release, string assemblyName, string code, CancellationToken cancel, out MemoryStream assemblyStream) { var gameCategory = release.ToCategory(); StringBuilder sb = new StringBuilder(); sb.AppendLine($"using System;"); sb.AppendLine($"using Noggog;"); sb.AppendLine($"using System.Threading;"); sb.AppendLine($"using System.Threading.Tasks;"); sb.AppendLine($"using System.Linq;"); sb.AppendLine($"using System.IO;"); sb.AppendLine($"using System.Collections;"); sb.AppendLine($"using System.Collections.Generic;"); sb.AppendLine($"using Mutagen.Bethesda.Synthesis;"); sb.AppendLine($"using Mutagen.Bethesda;"); sb.AppendLine($"using Mutagen.Bethesda.{release.ToCategory()};"); sb.AppendLine($"public class {ClassName}"); sb.AppendLine("{"); sb.AppendLine($"public async Task Run(Mutagen.Bethesda.Synthesis.SynthesisState<Mutagen.Bethesda.{gameCategory}.I{gameCategory}Mod, Mutagen.Bethesda.{gameCategory}.I{gameCategory}ModGetter> state)"); sb.AppendLine("{"); sb.AppendLine(code); sb.AppendLine("}"); sb.AppendLine("}"); code = sb.ToString(); SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(code); var options = new CSharpCompilationOptions( OutputKind.DynamicallyLinkedLibrary, optimizationLevel: OptimizationLevel.Release); Compilation compilation = CSharpCompilation.Create(assemblyName: assemblyName, options: options) .AddSyntaxTrees(syntaxTree) .AddReferences(new[] { MetadataReference.CreateFromFile(typeof(object).Assembly.Location), MetadataReference.CreateFromFile(typeof(Enumerable).Assembly.Location), MetadataReference.CreateFromFile(typeof(Task).Assembly.Location), MetadataReference.CreateFromFile(typeof(File).Assembly.Location), MetadataReference.CreateFromFile(Assembly.Load("netstandard").Location), MetadataReference.CreateFromFile(Assembly.Load("System.Runtime").Location), MetadataReference.CreateFromFile(Assembly.Load("Loqui").Location), MetadataReference.CreateFromFile(Assembly.Load("Noggog.CSharpExt").Location), MetadataReference.CreateFromFile(Assembly.Load("Mutagen.Bethesda.Kernel").Location), MetadataReference.CreateFromFile(Assembly.Load("Mutagen.Bethesda.Core").Location), MetadataReference.CreateFromFile(Assembly.Load("Mutagen.Bethesda.Synthesis").Location), }); foreach (var game in EnumExt.GetValues <GameCategory>()) { compilation = compilation.AddReferences(MetadataReference.CreateFromFile(Assembly.Load($"Mutagen.Bethesda.{game}").Location)); } assemblyStream = new MemoryStream(); return(compilation.Emit(assemblyStream, cancellationToken: cancel)); }
private static void CurrentDomain_AssemblyLoad(object sender, AssemblyLoadEventArgs e) { if (!e.LoadedAssembly.IsDynamic) { PortableExecutableReference metadataReference = MetadataReference.CreateFromFile(e.LoadedAssembly.Location); globalCompilation = globalCompilation.AddReferences(metadataReference); } }
/// <inheritdoc/> public void UpdateCompilation(MetadataReference?reference) { if (reference is not null) { CSharpCompilation compilation = Compilation; Compilation = Compilation.AddReferences(reference); OnUpdate(compilation); } }
/// <inheritdoc/> public void UpdateCompilation(IEnumerable <MetadataReference>?references) { if (references is not null) { CSharpCompilation compilation = Compilation; Compilation = Compilation.AddReferences(references); OnUpdate(compilation); } }
private MetadataResolutionContext(params MetadataReference[] references) { // TODO: This is a bit hacked if (references.OfType <PortableExecutableReference>().Any(pe => pe.FilePath == typeof(object).Assembly.Location)) { _compilation = _compilation.RemoveAllReferences(); } _compilation = _compilation.AddReferences(references); }
public void Generate() { Compilation = OriginalCompilation; var treeRemoveNodes = new Dictionary <SyntaxTree, List <SyntaxNode> >(); foreach (var neutralTypeContext in _typeCompilationContexts) { if (neutralTypeContext.EmitResult != null && !neutralTypeContext.EmitResult.Success) { // Only skip this reference if there was a failed emit continue; } MetadataReference neutralReference = neutralTypeContext.Reference; MetadataReference existingReference; if (_existingReferences.TryGetValue(neutralTypeContext.AssemblyName, out existingReference)) { neutralReference = existingReference; } Compilation = Compilation.AddReferences(neutralReference); foreach (var syntaxReference in neutralTypeContext.TypeSymbol.DeclaringSyntaxReferences) { var tree = syntaxReference.SyntaxTree; List <SyntaxNode> removeNodes; if (!treeRemoveNodes.TryGetValue(tree, out removeNodes)) { removeNodes = new List <SyntaxNode>(); treeRemoveNodes.Add(tree, removeNodes); } removeNodes.Add(syntaxReference.GetSyntax()); } } foreach (var treeRemoveNode in treeRemoveNodes) { var tree = treeRemoveNode.Key; var removeNodes = treeRemoveNode.Value; var root = tree.GetRoot(); // what it looks like when removed var newRoot = root.RemoveNodes(removeNodes, SyntaxRemoveOptions.KeepDirectives); var newTree = SyntaxFactory.SyntaxTree(newRoot, options: tree.Options, path: tree.FilePath, encoding: Encoding.UTF8); // update compilation with code removed Compilation = Compilation.ReplaceSyntaxTree(tree, newTree); } }
public EmitResult Generate(IDictionary <string, MetadataReference> existingReferences) { Compilation = CSharpCompilation.Create( assemblyName: AssemblyName, options: Worker.OriginalCompilation.Options, references: Worker.OriginalCompilation.References); foreach (var other in Requires.Keys) { if (other.EmitResult != null && !other.EmitResult.Success) { // Skip this reference if it hasn't beed emitted continue; } // If we're already referencing this assembly then skip it if (existingReferences.ContainsKey(other.AssemblyName)) { continue; } Compilation = Compilation.AddReferences(other.RealOrShallowReference()); } foreach (var syntaxReference in TypeSymbol.DeclaringSyntaxReferences) { var node = syntaxReference.GetSyntax(); var tree = syntaxReference.SyntaxTree; var root = tree.GetRoot(); var nodesToRemove = GetNodesToRemove(root, node).ToArray(); // what it looks like when removed var newRoot = root.RemoveNodes(nodesToRemove, SyntaxRemoveOptions.KeepDirectives); var newTree = SyntaxFactory.SyntaxTree(newRoot, options: tree.Options, path: tree.FilePath, encoding: Encoding.UTF8); // update compilation with code removed Compilation = Compilation.AddSyntaxTrees(newTree); } OutputStream = new MemoryStream(); EmitResult = Compilation.Emit(OutputStream); if (!EmitResult.Success) { return(EmitResult); } OutputStream.Position = 0; Reference = new MetadataImageReference(OutputStream); OutputStream.Position = 0; return(EmitResult); }
public static async Task <Compilation> GetCompilationAsync(string code) { Compilation compilation = null; await Task.Run(() => { var script = CSharpScript.Create <object>(code); var options = new CSharpCompilationOptions(OutputKind.ConsoleApplication, scriptClassName: "CodingTrainerExercise"); compilation = script.GetCompilation().WithOptions(options); compilation = compilation.AddReferences(References.ReferencedAssembliesData); }); return(compilation); }
/// <summary> /// Finds any plugin attributes defined on the compiling assembly and instantiates them. /// </summary> private bool InstantiatePlugins() { Compilation compilationWithRoslynRef; var pluginBaseClass = Compilation.GetTypeByMetadataName(ICompilerPluginAttributeName); // If we can't find the compiler plugin attribute defined in this assembly or its references, // create a new compilation that includes a reference to Microsoft.CodeAnalysis. // This allows assemblies using compiler plugins to not have references to Roslyn. if (pluginBaseClass == null) { var currentAssembly = typeof(CompilerPluginExecutor).GetTypeInfo().Assembly; var mdRef = MetadataReference.CreateFromAssemblyInternal(currentAssembly); compilationWithRoslynRef = Compilation.AddReferences(mdRef); pluginBaseClass = compilationWithRoslynRef.GetTypeByMetadataName(ICompilerPluginAttributeName); } else { compilationWithRoslynRef = Compilation; } _pluginAttributes = GetPluginAttributes(compilationWithRoslynRef, pluginBaseClass); if (_pluginAttributes == null) { return(false); } _plugins = new List <ICompilerPlugin>(_pluginAttributes.Count); foreach (var pluginAttribute in _pluginAttributes) { try { _plugins.Add(InstantiatePlugin(compilationWithRoslynRef, pluginAttribute)); } catch (Exception e) { INamedTypeSymbol attributeClass = pluginAttribute.AttributeClass; string attributeName = attributeClass.ToDisplayString(SymbolDisplayFormat.QualifiedNameOnlyFormat); AddExceptionDiagnostic(attributeName, e); return(false); } } return(true); }
public void Import(string text) { tree = VisualBasicSyntaxTree.ParseText(text); VisualBasicCompilationOptions options = new VisualBasicCompilationOptions(OutputKind.DynamicallyLinkedLibrary, optimizationLevel: OptimizationLevel.Debug).WithParseOptions(new VisualBasicParseOptions(kind: SourceCodeKind.Regular)); options = options.WithConcurrentBuild(false); compilation = VisualBasicCompilation.Create("Test", new[] { tree }); compilation = compilation.WithOptions(options); compilation.AddReferences(MetadataReference.CreateFromFile(typeof(object).Assembly.Location)); model = compilation.GetSemanticModel(tree); importer = new VisualBasicMooseImporter(); importer.Import(tree, model); MetaModel = importer.MetaModel; }
private Compilation AddProjectReferences(IEnumerable <Project> projects, List <ISymbol> symbols, Compilation compilation) { // Add a references to the compilation for each project in the solution MetadataReference[] compilationReferences = projects .Where(x => x.SupportsCompilation) .AsParallel() .Select(x => { Trace.Verbose($"Creating compilation reference for project {x.Name}"); return((MetadataReference)x.GetCompilationAsync().Result.ToMetadataReference(new[] { x.AssemblyName }.ToImmutableArray())); }) .ToArray(); if (compilationReferences.Length > 0) { compilation = compilation.AddReferences(compilationReferences); symbols.AddRange(compilationReferences .Select(x => (IAssemblySymbol)compilation.GetAssemblyOrModuleSymbol(x)) .Select(x => _assemblySymbols ? x : (ISymbol)x.GlobalNamespace)); } return(compilation); }
// Gets the semantic model and caches it // TODO: Write tests to check getting a semantic model against all the different query types - make sure the diagnostics generated are appropriate private SemanticModel GetSemanticModel() { if (!_generatedSemanticModel) { // Generate the semantic model Compilation compilation = null; if (_syntaxTree is CSharpSyntaxTree) { CSharpCompilationOptions options = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary); compilation = CSharpCompilation.Create("QueryCompilation").WithOptions(options); } else if (_syntaxTree is VisualBasicSyntaxTree) { VisualBasicCompilationOptions options = new VisualBasicCompilationOptions(OutputKind.DynamicallyLinkedLibrary); compilation = VisualBasicCompilation.Create("QueryCompilation").WithOptions(options); } if (compilation != null) { // Get assembly references from the current AppDomain (which will be the domain of the currently running query) // Make sure to exclude empty locations (created by in-memory assemblies, specifically AsyncBridge) // See http://stackoverflow.com/questions/28503569/roslyn-create-metadatareference-from-in-memory-assembly AppDomain appDomain = AppDomain.CurrentDomain; compilation = compilation .AddReferences(appDomain.GetAssemblies() .Where(x => !x.IsDynamic && !string.IsNullOrEmpty(x.Location)) .Select(x => MetadataReference.CreateFromFile(x.Location))) .AddSyntaxTrees(_syntaxTree); _semanticModel = compilation.GetSemanticModel(_syntaxTree); } _generatedSemanticModel = true; // Update the UI _semanticsCheckBox.Checked = true; _semanticsCheckBox.Enabled = false; } return(_semanticModel); }
public static void Go(Compilation compilation, string outDir, IEnumerable <string> extraTranslation, string ctorHelperName, HashSet <string> whitelist, bool buildFirst = false) { TranslationManager.Init(extraTranslation); Compilation = compilation.AddReferences(TranslationManager.References.Select(o => MetadataReference.CreateFromFile(o))); OutDir = outDir; CtorHelperName = ctorHelperName; if (whitelist == null || whitelist.Count == 0) { Whitelist = new List <Regex>() { new Regex(".*") } } ; else { Whitelist = whitelist.Select(o => new Regex(o)).ToList(); } Utility.Parallel(new Action[] { Build, Generate }, a => a(), !buildFirst); }
/// <inheritdoc /> public Compilation InitializeCompilation(Compilation compilation) => compilation.AddReferences(MetadataReference.CreateFromFile(typeof(Observable).Assembly.Location));
public IEnumerable <IDocument> Execute(IReadOnlyList <IDocument> inputs, IExecutionContext context) { List <ISymbol> symbols = new List <ISymbol>(); // Create the compilation (have to supply an XmlReferenceResolver to handle include XML doc comments) MetadataReference mscorlib = MetadataReference.CreateFromFile(typeof(object).Assembly.Location); Compilation compilation = CSharpCompilation .Create(CompilationAssemblyName) .WithReferences(mscorlib) .WithOptions(new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, xmlReferenceResolver: new XmlFileResolver(context.FileSystem.RootPath.FullPath))); // Handle input documents if (_inputDocuments) { // Get syntax trees (supply path so that XML doc includes can be resolved) ConcurrentBag <SyntaxTree> syntaxTrees = new ConcurrentBag <SyntaxTree>(); context.ParallelForEach(inputs, input => { using (Stream stream = input.GetStream()) { SourceText sourceText = SourceText.From(stream); syntaxTrees.Add(CSharpSyntaxTree.ParseText(sourceText, path: input.String(Keys.SourceFilePath, string.Empty))); } }); compilation = compilation.AddSyntaxTrees(syntaxTrees); } // Handle assemblies IEnumerable <IFile> assemblyFiles = context.FileSystem.GetInputFiles(_assemblyGlobs) .Where(x => (x.Path.Extension == ".dll" || x.Path.Extension == ".exe") && x.Exists); MetadataReference[] assemblyReferences = assemblyFiles.Select(assemblyFile => { // Create the metadata reference for the compilation IFile xmlFile = context.FileSystem.GetFile(assemblyFile.Path.ChangeExtension("xml")); if (xmlFile.Exists) { Trace.Verbose($"Creating metadata reference for assembly {assemblyFile.Path.FullPath} with XML documentation file"); using (Stream xmlStream = xmlFile.OpenRead()) { using (MemoryStream xmlBytes = new MemoryStream()) { xmlStream.CopyTo(xmlBytes); return(MetadataReference.CreateFromStream(assemblyFile.OpenRead(), documentation: XmlDocumentationProvider.CreateFromBytes(xmlBytes.ToArray()))); } } } Trace.Verbose($"Creating metadata reference for assembly {assemblyFile.Path.FullPath} without XML documentation file"); return((MetadataReference)MetadataReference.CreateFromStream(assemblyFile.OpenRead())); }).ToArray(); if (assemblyReferences.Length > 0) { compilation = compilation.AddReferences(assemblyReferences); symbols.AddRange(assemblyReferences .Select(x => (IAssemblySymbol)compilation.GetAssemblyOrModuleSymbol(x)) .Select(x => _assemblySymbols ? x : (ISymbol)x.GlobalNamespace)); } // Get and return the document tree symbols.Add(compilation.Assembly.GlobalNamespace); AnalyzeSymbolVisitor visitor = new AnalyzeSymbolVisitor(compilation, context, _symbolPredicate, _writePath ?? (x => DefaultWritePath(x, _writePathPrefix)), _cssClasses, _docsForImplicitSymbols, _assemblySymbols); foreach (ISymbol symbol in symbols) { visitor.Visit(symbol); } return(visitor.Finish()); }
/// <summary> /// Add reference to the project /// </summary> /// <param name="displayNameOrPath">Assembly name like System.Xml which can be used for search or full path assembly</param> public void AddReference(string displayNameOrPath) { var reference = LanguageService.CreateMetadataReference(displayNameOrPath); Compilation = Compilation.AddReferences(reference); }