private ICompilationService GetCompilationService()
        {
            var appEnvironment = PlatformServices.Default.Application;
            var loaderAccessor = PlatformServices.Default.AssemblyLoadContextAccessor;
            var libExporter = CompilationServices.Default.LibraryExporter;

            var emptyLibExport = new LibraryExport(metadataReferences: null);
            var mockLibExporter = new Mock<ILibraryExporter>();

            var input = "Microsoft.Extensions.CodeGeneration.Templating.Test";
            mockLibExporter
                .Setup(lm => lm.GetAllExports(input))
                .Returns(libExporter.GetAllExports(input));

            return new RoslynCompilationService(appEnvironment, loaderAccessor, mockLibExporter.Object);
        }
        private ICompilationService GetCompilationService()
        {
            var originalProvider = CallContextServiceLocator.Locator.ServiceProvider;
            var appEnvironment = (IApplicationEnvironment)originalProvider.GetService(typeof(IApplicationEnvironment));
            var loaderAccessor = (IAssemblyLoadContextAccessor)originalProvider.GetService(typeof(IAssemblyLoadContextAccessor));
            var libExporter = (ILibraryExporter)originalProvider.GetService(typeof(ILibraryExporter));

            var emptyLibExport = new LibraryExport(metadataReferences: null);
            var mockLibExporter = new Mock<ILibraryExporter>();

            var input = "Microsoft.Framework.CodeGeneration.Templating.Test";
            mockLibExporter
                .Setup(lm => lm.GetAllExports(input))
                .Returns(libExporter.GetAllExports(input));

            return new RoslynCompilationService(appEnvironment, loaderAccessor, mockLibExporter.Object);
        }
Beispiel #3
0
        private void ProcessExport(LibraryExport export,
                                   IDictionary <string, IMetadataReference> metadataReferences,
                                   IDictionary <string, ISourceReference> sourceReferences)
        {
            var references = new List <IMetadataReference>(export.MetadataReferences);

            foreach (var reference in references)
            {
                metadataReferences[reference.Name] = reference;
            }

            if (sourceReferences != null)
            {
                foreach (var sourceReference in export.SourceReferences)
                {
                    sourceReferences[sourceReference.Name] = sourceReference;
                }
            }
        }
Beispiel #4
0
        private LibraryExport GetAllExports(
            ProjectDescription project,
            string aspect,
            Func <LibraryDescription, bool> include)
        {
            var dependencyStopWatch = Stopwatch.StartNew();

            Logger.TraceInformation($"[{nameof(LibraryExporter)}]: Resolving references for '{project.Identity.Name}' {aspect}");

            var references       = new Dictionary <string, IMetadataReference>(StringComparer.OrdinalIgnoreCase);
            var sourceReferences = new Dictionary <string, ISourceReference>(StringComparer.OrdinalIgnoreCase);

            // Walk the dependency tree and resolve the library export for all references to this project
            var queue     = new Queue <Node>();
            var processed = new HashSet <string>(StringComparer.OrdinalIgnoreCase);

            var rootNode = new Node
            {
                Library = project
            };

            queue.Enqueue(rootNode);

            while (queue.Count > 0)
            {
                var node = queue.Dequeue();

                // Skip it if we've already seen it
                if (!processed.Add(node.Library.Identity.Name))
                {
                    continue;
                }

                if (include(node.Library))
                {
                    LibraryExport libraryExport = null;

                    if (node.Library == project)
                    {
                        // We know it's a project so skip the lookup
                        libraryExport = ExportProject(project, aspect: null, exporter: this);
                    }
                    else
                    {
                        libraryExport = GetExport(node.Library, aspect: null);
                    }

                    if (libraryExport != null)
                    {
                        if (node.Parent == rootNode)
                        {
                            // Only export sources from first level dependencies
                            ProcessExport(libraryExport, references, sourceReferences);
                        }
                        else
                        {
                            // Skip source exports from anything else
                            ProcessExport(libraryExport, references, sourceReferences: null);
                        }
                    }
                }

                foreach (var dependency in node.Library.Dependencies)
                {
                    var childNode = new Node
                    {
                        Library = LibraryManager.GetLibraryDescription(dependency.Name),
                        Parent  = node
                    };

                    queue.Enqueue(childNode);
                }
            }

            dependencyStopWatch.Stop();
            Logger.TraceInformation($"[{nameof(LibraryExporter)}]: Resolved {references.Count} references for '{project.Identity.Name}' in {dependencyStopWatch.ElapsedMilliseconds}ms");

            return(new LibraryExport(
                       references.Values.ToList(),
                       sourceReferences.Values.ToList()));
        }
Beispiel #5
0
        private void ProcessExport(LibraryExport export,
                                          IDictionary<string, IMetadataReference> metadataReferences,
                                          IDictionary<string, ISourceReference> sourceReferences)
        {
            var references = new List<IMetadataReference>(export.MetadataReferences);

            foreach (var reference in references)
            {
                metadataReferences[reference.Name] = reference;
            }

            if (sourceReferences != null)
            {
                foreach (var sourceReference in export.SourceReferences)
                {
                    sourceReferences[sourceReference.Name] = sourceReference;
                }
            }
        }
        private static ILibraryExporter GetLibraryExporter()
        {
            var fileReference = new Mock<IMetadataFileReference>();
            fileReference.SetupGet(f => f.Path)
                         .Returns(typeof(string).Assembly.Location);
            var libraryExport = new LibraryExport(fileReference.Object);

            var libraryExporter = new Mock<ILibraryExporter>();
            libraryExporter.Setup(l => l.GetAllExports(It.IsAny<string>()))
                          .Returns(libraryExport);
            return libraryExporter.Object;
        }
 public void AddAdditionalLibraryExportRegistrations(string name, LibraryExport additionalRegistration)
 {
     AdditionalLibraryExportRegistrations[name] = additionalRegistration;
 }
 private static IList<ISourceReference> JoinSourceReferences(
     LibraryExport internalExports,
     IReadOnlyList<LibraryExport> additionalExports)
 {
     return internalExports
         .SourceReferences
         .AsEnumerable()
         .Concat(
             additionalExports.SelectMany(x => x.SourceReferences)).ToList();
 }
 public LibraryExportWrapper(
     LibraryExport internalExports, IReadOnlyList<LibraryExport> additionalExports)
     : base(JoinMetadataReferences(internalExports, additionalExports),
           JoinSourceReferences(internalExports, additionalExports))
 {
 }