Ejemplo n.º 1
0
        public BuildContext(IServiceProvider hostServices,
                            IApplicationEnvironment appEnv,
                            CompilationEngineFactory compilationFactory,
                            Runtime.Project project,
                            FrameworkName targetFramework,
                            string configuration,
                            string outputPath)
        {
            _project               = project;
            _targetFramework       = targetFramework;
            _configuration         = configuration;
            _targetFrameworkFolder = VersionUtility.GetShortFrameworkName(_targetFramework);
            _outputPath            = Path.Combine(outputPath, _targetFrameworkFolder);
            _hostServices          = hostServices;
            _appEnv = appEnv;

            _applicationHostContext = GetApplicationHostContext(project, targetFramework, configuration, compilationFactory.CompilationCache);
            var compilationEngine = compilationFactory.CreateEngine(
                new CompilationEngineContext(
                    _applicationHostContext.LibraryManager,
                    _applicationHostContext.ProjectGraphProvider,
                    NoopWatcher.Instance,
                    _applicationHostContext.ServiceProvider,
                    _targetFramework,
                    _configuration));

            _libraryExporter = compilationEngine.CreateProjectExporter(
                _project, _targetFramework, _configuration);
            _applicationHostContext.AddService(typeof(ILibraryExporter), _libraryExporter);
            _applicationHostContext.AddService(typeof(ICompilationEngine), compilationEngine);
        }
Ejemplo n.º 2
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 IAssemblyLoadContext Create(IServiceProvider serviceProvider)
        {
            var hostContextKey = Tuple.Create("RuntimeLoadContext", _project.Name, _appEnv.Configuration, _appEnv.RuntimeFramework);

            var appHostContext = _compilationFactory.CompilationCache.Cache.Get <ApplicationHostContext>(hostContextKey, ctx =>
            {
                var applicationHostContext = new ApplicationHostContext(serviceProvider,
                                                                        _project.ProjectDirectory,
                                                                        packagesDirectory: null,
                                                                        configuration: _appEnv.Configuration,
                                                                        targetFramework: _appEnv.RuntimeFramework);

                applicationHostContext.DependencyWalker.Walk(_project.Name, _project.Version, _appEnv.RuntimeFramework);

                // Watch all projects for project.json changes
                foreach (var library in applicationHostContext.DependencyWalker.Libraries)
                {
                    if (string.Equals(library.Type, "Project"))
                    {
                        ctx.Monitor(new FileWriteTimeCacheDependency(library.Path));
                    }
                }

                // Add a cache dependency on restore complete to reevaluate dependencies
                ctx.Monitor(_compilationFactory.CompilationCache.NamedCacheDependencyProvider.GetNamedDependency(_project.Name + "_Dependencies"));

                // Create compilation engine and add it to the services
                var compilationEngine = _compilationFactory.CreateEngine(new CompilationEngineContext(
                                                                             applicationHostContext.LibraryManager,
                                                                             applicationHostContext.ProjectGraphProvider,
                                                                             NoopWatcher.Instance,
                                                                             applicationHostContext.ServiceProvider,
                                                                             _appEnv.RuntimeFramework,
                                                                             _appEnv.Configuration));
                applicationHostContext.AddService(typeof(ICompilationEngine), compilationEngine);

                return(applicationHostContext);
            });

            var factory = new AssemblyLoadContextFactory(appHostContext.ServiceProvider);

            return(factory.Create(appHostContext.ServiceProvider));
        }