예제 #1
0
        private DependencyInfo ResolveProjectDepencies(Project project, string configuration, FrameworkName frameworkName)
        {
            var cacheKey = Tuple.Create("DependencyInfo", project.Name, configuration, frameworkName);

            return(_cache.Get <DependencyInfo>(cacheKey, ctx =>
            {
                var applicationHostContext = GetApplicationHostContext(project, configuration, frameworkName);

                var libraryManager = applicationHostContext.LibraryManager;
                var frameworkResolver = applicationHostContext.FrameworkReferenceResolver;

                var info = new DependencyInfo
                {
                    Dependencies = new Dictionary <string, DependencyDescription>(),
                    ProjectReferences = new List <ProjectReference>(),
                    HostContext = applicationHostContext,
                    References = new List <string>(),
                    RawReferences = new Dictionary <string, byte[]>()
                };

                foreach (var library in applicationHostContext.DependencyWalker.Libraries)
                {
                    var description = CreateDependencyDescription(library);
                    info.Dependencies[description.Name] = description;

                    // Skip unresolved libraries
                    if (!library.Resolved)
                    {
                        continue;
                    }

                    if (string.Equals(library.Type, "Project") &&
                        !string.Equals(library.Identity.Name, project.Name))
                    {
                        Project referencedProject;
                        if (!Project.TryGetProject(library.Path, out referencedProject))
                        {
                            // Should never happen
                            continue;
                        }

                        var targetFrameworkInformation = referencedProject.GetTargetFramework(library.Framework);

                        // If this is an assembly reference then treat it like a file reference
                        if (!string.IsNullOrEmpty(targetFrameworkInformation.AssemblyPath) &&
                            string.IsNullOrEmpty(targetFrameworkInformation.WrappedProject))
                        {
                            string assemblyPath = GetProjectRelativeFullPath(referencedProject, targetFrameworkInformation.AssemblyPath);
                            info.References.Add(assemblyPath);

                            description.Path = assemblyPath;
                            description.Type = "Assembly";
                        }
                        else
                        {
                            string wrappedProjectPath = null;

                            if (!string.IsNullOrEmpty(targetFrameworkInformation.WrappedProject))
                            {
                                wrappedProjectPath = GetProjectRelativeFullPath(referencedProject, targetFrameworkInformation.WrappedProject);
                            }

                            info.ProjectReferences.Add(new ProjectReference
                            {
                                Framework = new FrameworkData
                                {
                                    ShortName = VersionUtility.GetShortFrameworkName(library.Framework),
                                    FrameworkName = library.Framework.ToString(),
                                    FriendlyName = frameworkResolver.GetFriendlyFrameworkName(library.Framework)
                                },
                                Path = library.Path,
                                WrappedProjectPath = wrappedProjectPath
                            });
                        }
                    }
                }

                var exportWithoutProjects = ProjectExportProviderHelper.GetExportsRecursive(
                    _cache,
                    applicationHostContext.LibraryManager,
                    applicationHostContext.LibraryExportProvider,
                    new LibraryKey
                {
                    Configuration = configuration,
                    TargetFramework = frameworkName,
                    Name = project.Name
                },
                    library => library.Type != "Project");

                foreach (var reference in exportWithoutProjects.MetadataReferences)
                {
                    var fileReference = reference as IMetadataFileReference;
                    if (fileReference != null)
                    {
                        info.References.Add(fileReference.Path);
                    }

                    var embedded = reference as IMetadataEmbeddedReference;
                    if (embedded != null)
                    {
                        info.RawReferences[embedded.Name] = embedded.Contents;
                    }
                }

                return info;
            }));
        }
예제 #2
0
        public Assembly Load(AssemblyName assemblyName)
        {
            Project project;
            string  name = assemblyName.FullName;

            if (!Project.TryGetProject(_virtualPathProvider.Combine(_path, name), out project))
            {
                return(null);
            }

            var cache = (ICache)_serviceProvider.GetService(typeof(ICache));

            var target = new CompilationTarget(
                name,
                project.GetTargetFramework(_applicationEnvironment.RuntimeFramework).FrameworkName,
                _applicationEnvironment.Configuration,
                null);

            ModuleLoaderContext moduleContext = new ModuleLoaderContext(
                _serviceProvider,
                project.ProjectDirectory);

            moduleContext.DependencyWalker.Walk(name, project.Version, target.TargetFramework);

            var cacheContextAccessor = (ICacheContextAccessor)_serviceProvider.GetService(typeof(ICacheContextAccessor));
            var loadContextFactory   = (IAssemblyLoadContextFactory)_serviceProvider.GetService(typeof(IAssemblyLoadContextFactory)) ?? new AssemblyLoadContextFactory(_serviceProvider);

            var compiler = new InternalRoslynCompiler(
                cache,
                cacheContextAccessor,
                new NamedCacheDependencyProvider(),
                loadContextFactory,
                _fileWatcher,
                _applicationEnvironment,
                _serviceProvider);

            _orchardLibraryManager.AddAdditionalRegistrations(moduleContext.DependencyWalker.Libraries);

            var exports = ProjectExportProviderHelper.GetExportsRecursive(
                _orchardLibraryManager,
                moduleContext.LibraryExportProvider,
                new CompilationTarget(name, target.TargetFramework, target.Configuration, target.Aspect),
                true);

            _orchardLibraryManager.AddAdditionalLibraryExportRegistrations(name, exports);

            foreach (var dependency in project.Dependencies)
            {
                if (!_orchardLibraryManager.MetadataReferences.ContainsKey(dependency.Name))
                {
                    continue;
                }

                exports.MetadataReferences.Add(_orchardLibraryManager.MetadataReferences[dependency.Name]);
            }

            var compliationContext = compiler.CompileProject(
                project.ToCompilationContext(target),
                exports.MetadataReferences,
                exports.SourceReferences,
                () => CompositeResourceProvider.Default.GetResources(project));

            var roslynProjectReference = new RoslynProjectReference(compliationContext);

            _orchardLibraryManager.AddMetadataReference(name, roslynProjectReference);

            var loadContext = _assemblyLoadContextAccessor.Default;

            return(roslynProjectReference.Load(loadContext));
        }
예제 #3
0
        private static DependencyInformation ResolveDependencyInfo(DnxProject project, FrameworkName frameworkName)
        {
            var cacheKey = Tuple.Create("DependencyInformation", project.Name, "Debug", frameworkName);

            return(cache.Get <DependencyInformation>(cacheKey, ctx =>
            {
                var applicationHostContext = GetApplicationHostContext(project, "Debug", frameworkName);

                var info = new DependencyInformation
                {
                    HostContext = applicationHostContext,
                    ProjectReferences = new List <ProjectReference>(),
                    References = new List <string>(),
                    ExportedSourcesFiles = new List <string>()
                };

                foreach (var library in applicationHostContext.DependencyWalker.Libraries)
                {
                    // Skip unresolved libraries
                    if (!library.Resolved)
                    {
                        continue;
                    }

                    if (string.Equals(library.Type, "Project") &&
                        !string.Equals(library.Identity.Name, project.Name))
                    {
                        DnxProject referencedProject = GetProject(library.Path);

                        if (referencedProject == null)
                        {
                            // Should never happen
                            continue;
                        }

                        var targetFrameworkInformation = referencedProject.GetTargetFramework(library.Framework);

                        // If this is an assembly reference then treat it like a file reference
                        if (!string.IsNullOrEmpty(targetFrameworkInformation.AssemblyPath) &&
                            string.IsNullOrEmpty(targetFrameworkInformation.WrappedProject))
                        {
                            string assemblyPath = GetProjectRelativeFullPath(referencedProject, targetFrameworkInformation.AssemblyPath);
                            info.References.Add(assemblyPath);
                        }
                        else
                        {
                            string wrappedProjectPath = null;

                            if (!string.IsNullOrEmpty(targetFrameworkInformation.WrappedProject))
                            {
                                wrappedProjectPath = GetProjectRelativeFullPath(referencedProject, targetFrameworkInformation.WrappedProject);
                            }

                            info.ProjectReferences.Add(new ProjectReference
                            {
                                Name = referencedProject.Name,
                                Framework = library.Framework,
                                Path = library.Path,
                                WrappedProjectPath = wrappedProjectPath,
                                Project = referencedProject
                            });
                        }
                    }
                }

                var exportWithoutProjects = ProjectExportProviderHelper.GetExportsRecursive(
                    applicationHostContext.LibraryManager,
                    applicationHostContext.LibraryExportProvider,
                    new CompilationTarget("Debug", frameworkName, "Debug", null),
                    library => library.Type != "Project");

                foreach (var reference in exportWithoutProjects.MetadataReferences)
                {
                    var fileReference = reference as IMetadataFileReference;
                    if (fileReference != null)
                    {
                        info.References.Add(fileReference.Path);
                    }
                }

                foreach (var sourceFileReference in exportWithoutProjects.SourceReferences.OfType <ISourceFileReference>())
                {
                    info.ExportedSourcesFiles.Add(sourceFileReference.Path);
                }

                return info;
            }));
        }