Beispiel #1
0
        public Assembly Load(AssemblyName assemblyName)
        {
            Project project;
            string  name = assemblyName.FullName;

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

            return(_cache.Get <Assembly>(assemblyName.Name, cacheContext => {
                var target = new CompilationTarget(
                    name,
                    project.GetTargetFramework(_applicationEnvironment.RuntimeFramework).FrameworkName,
                    _applicationEnvironment.Configuration,
                    null);

                _moduleContext = new ModuleLoaderContext(
                    _serviceProvider,
                    project.ProjectDirectory,
                    target.Configuration,
                    target.TargetFramework);

                foreach (var lib in _moduleContext.DependencyWalker.Libraries)
                {
                    _libraryManager.AddLibrary(lib.ToLibrary());
                }

                var compilationEngineFactory = new CompilationEngineFactory(
                    NoopWatcher.Instance, new CompilationCache());

                var engine = compilationEngineFactory.CreateEngine(new CompilationEngineContext(
                                                                       _moduleContext.LibraryManager,
                                                                       _moduleContext.ProjectGraphProvider,
                                                                       _moduleContext.ServiceProvider,
                                                                       target.TargetFramework,
                                                                       target.Configuration));

                _packageAssemblyLookup.AddLoader(
                    _moduleContext.NuGetDependencyProvider);

                var p = engine.LoadProject(project, null, _assemblyLoadContextAccessor.Default);

                var exports = engine.RootLibraryExporter.GetExport(project.Name);
                foreach (var metadataReference in exports.MetadataReferences)
                {
                    _libraryManager.AddMetadataReference(metadataReference);
                }

                return p;
            }));
        }
        public Assembly Load(AssemblyName assemblyName)
        {
            var projectPath = Path.Combine(_path, assemblyName.Name);

            if (!Project.HasProjectFile(projectPath))
            {
                return(null);
            }

            return(_cache.Get <Assembly>(assemblyName.Name, cacheContext => {
                var moduleContext = new ModuleLoaderContext(
                    projectPath,
                    _applicationEnvironment.RuntimeFramework);

                foreach (var lib in moduleContext.LibraryManager.GetLibraries())
                {
                    _libraryManager.AddLibrary(lib);
                }

                var engine = new CompilationEngine(new CompilationEngineContext(
                                                       _applicationEnvironment,
                                                       _assemblyLoadContextAccessor.Default,
                                                       new CompilationCache()));

                var exporter = engine.CreateProjectExporter(
                    moduleContext.Project, moduleContext.TargetFramework, _applicationEnvironment.Configuration);

                var exports = exporter.GetAllExports(moduleContext.Project.Name);
                foreach (var metadataReference in exports.MetadataReferences)
                {
                    _libraryManager.AddMetadataReference(metadataReference);
                }

                return engine.LoadProject(moduleContext.Project, null, _assemblyLoadContextAccessor.Default);
            }));
        }
Beispiel #3
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));
        }
        public Assembly Load(AssemblyName assemblyName)
        {
            var reference = _libraryManager.GetMetadataReference(assemblyName.Name);

            if (reference != null && reference is MetadataFileReference)
            {
                var fileReference = (MetadataFileReference)reference;

                var assembly = _assemblyLoadContextAccessor
                               .Default
                               .LoadFile(fileReference.Path);

                return(assembly);
            }

            var projectPath = Path.Combine(_path, assemblyName.Name);

            if (!Project.HasProjectFile(projectPath))
            {
                return(null);
            }

            var moduleContext = new ModuleLoaderContext(
                projectPath,
                _applicationEnvironment.RuntimeFramework);

            foreach (var lib in moduleContext.LibraryManager.GetLibraries())
            {
                _libraryManager.AddLibrary(lib);
            }

            var engine = new CompilationEngine(new CompilationEngineContext(
                                                   _applicationEnvironment,
                                                   _runtimeEnvironment,
                                                   _assemblyLoadContextAccessor.Default,
                                                   _compilationCache));

            var exporter = engine.CreateProjectExporter(
                moduleContext.Project, moduleContext.TargetFramework, _applicationEnvironment.Configuration);

            var exports = exporter.GetAllExports(moduleContext.Project.Name);

            foreach (var metadataReference in exports.MetadataReferences)
            {
                _libraryManager.AddMetadataReference(metadataReference);
            }

            var loadedProjectAssembly = engine.LoadProject(
                moduleContext.Project,
                _applicationEnvironment.RuntimeFramework,
                null,
                _assemblyLoadContextAccessor.Default,
                assemblyName);

            IList <LibraryDependency> flattenedList = moduleContext
                                                      .Project
                                                      .Dependencies
                                                      .SelectMany(x => Flatten(x))
                                                      .Where(x => x.Library.Type == LibraryTypes.Package)
                                                      .Distinct()
                                                      .ToList();

            foreach (var dependency in flattenedList)
            {
                foreach (var assemblyToLoad in dependency.Library.Assemblies)
                {
                    Assembly.Load(new AssemblyName(assemblyToLoad));
                }
            }

            return(loadedProjectAssembly);
        }