Ejemplo n.º 1
0
        private void Initialize(DefaultHostOptions options, IServiceProvider hostServices)
        {
            _applicationHostContext = new ApplicationHostContext(
                hostServices,
                _projectDirectory,
                options.PackageDirectory,
                options.Configuration,
                _targetFramework);

            Trace.TraceInformation("[{0}]: Project path: {1}", GetType().Name, _projectDirectory);
            Trace.TraceInformation("[{0}]: Project root: {1}", GetType().Name, _applicationHostContext.RootDirectory);
            Trace.TraceInformation("[{0}]: Packages path: {1}", GetType().Name, _applicationHostContext.PackagesDirectory);

            _project = _applicationHostContext.Project;

            if (Project == null)
            {
                throw new Exception("Unable to locate " + Project.ProjectFileName);
            }

            if (options.WatchFiles)
            {
                var watcher = new FileWatcher(_applicationHostContext.RootDirectory);
                _watcher           = watcher;
                watcher.OnChanged += _ =>
                {
                    _shutdown.RequestShutdownWaitForDebugger();
                };
            }
            else
            {
                _watcher = NoopWatcher.Instance;
            }

            var applicationEnvironment = new ApplicationEnvironment(Project, _targetFramework, options.Configuration);

            _applicationHostContext.AddService(typeof(IApplicationEnvironment), applicationEnvironment);
            _applicationHostContext.AddService(typeof(IApplicationShutdown), _shutdown);
            // TODO: Get rid of this and just use the IFileWatcher
            _applicationHostContext.AddService(typeof(IFileMonitor), _watcher);
            _applicationHostContext.AddService(typeof(IFileWatcher), _watcher);

            CallContextServiceLocator.Locator.ServiceProvider = ServiceProvider;
        }
Ejemplo n.º 2
0
        private void Initialize(DefaultHostOptions options, IServiceProvider hostServices)
        {
            var cacheContextAccessor = new CacheContextAccessor();
            var cache = new Cache(cacheContextAccessor);
            var namedCacheDependencyProvider = new NamedCacheDependencyProvider();

            _applicationHostContext = new ApplicationHostContext(
                hostServices,
                _projectDirectory,
                options.PackageDirectory,
                options.Configuration,
                _targetFramework,
                cache,
                cacheContextAccessor,
                namedCacheDependencyProvider);

            Logger.TraceInformation("[{0}]: Project path: {1}", GetType().Name, _projectDirectory);
            Logger.TraceInformation("[{0}]: Project root: {1}", GetType().Name, _applicationHostContext.RootDirectory);
            Logger.TraceInformation("[{0}]: Packages path: {1}", GetType().Name, _applicationHostContext.PackagesDirectory);

            _project = _applicationHostContext.Project;

            if (Project == null)
            {
                throw new Exception("Unable to locate " + Project.ProjectFileName);
            }

            if (options.WatchFiles)
            {
                var watcher = new FileWatcher(_applicationHostContext.RootDirectory);
                _watcher           = watcher;
                watcher.OnChanged += _ =>
                {
                    _shutdown.RequestShutdownWaitForDebugger();
                };
            }
            else
            {
                _watcher = NoopWatcher.Instance;
            }

            _applicationHostContext.AddService(typeof(IApplicationShutdown), _shutdown);
            // TODO: Get rid of this and just use the IFileWatcher
            _applicationHostContext.AddService(typeof(IFileMonitor), _watcher);
            _applicationHostContext.AddService(typeof(IFileWatcher), _watcher);

            if (options.CompilationServerPort.HasValue)
            {
                // Using this ctor because it works on mono, this is hard coded to ipv4
                // right now. Mono will eventually have the dualmode overload
                var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                socket.Connect(new IPEndPoint(IPAddress.Loopback, options.CompilationServerPort.Value));

                var networkStream = new NetworkStream(socket);

                _applicationHostContext.AddService(typeof(IDesignTimeHostCompiler),
                                                   new DesignTimeHostCompiler(_shutdown, _watcher, networkStream), includeInManifest: false);

                // Change the project reference provider
                Project.DefaultLanguageService = new TypeInformation(
                    typeof(DefaultHost).GetTypeInfo().Assembly.GetName().Name,
                    typeof(DesignTimeHostProjectReferenceProvider).FullName);
            }

            CallContextServiceLocator.Locator.ServiceProvider = ServiceProvider;
        }
        public ILibraryExport GetLibraryExport(ILibraryKey target)
        {
            Project project;

            // Can't find a project file with the name so bail
            if (!_projectResolver.TryResolveProject(target.Name, out project))
            {
                return(null);
            }

            Logger.TraceInformation("[{0}]: GetLibraryExport({1}, {2}, {3}, {4})", GetType().Name, target.Name, target.TargetFramework, target.Configuration, target.Aspect);

            var targetFrameworkInformation = project.GetTargetFramework(target.TargetFramework);

            // This is the target framework defined in the project. If there were no target frameworks
            // defined then this is the targetFramework specified
            if (targetFrameworkInformation.FrameworkName != null)
            {
                target = target.ChangeTargetFramework(targetFrameworkInformation.FrameworkName);
            }

            var key = Tuple.Create(
                target.Name,
                target.TargetFramework,
                target.Configuration,
                target.Aspect);

            var cache = (ICache)_serviceProvider.GetService(typeof(ICache));
            var cacheContextAccessor         = (ICacheContextAccessor)_serviceProvider.GetService(typeof(ICacheContextAccessor));
            var namedCacheDependencyProvider = (INamedCacheDependencyProvider)_serviceProvider.GetService(typeof(INamedCacheDependencyProvider));
            var loadContextFactory           = (IAssemblyLoadContextFactory)_serviceProvider.GetService(typeof(IAssemblyLoadContextFactory));

            return(cache.Get <ILibraryExport>(key, ctx =>
            {
                var metadataReferences = new List <IMetadataReference>();
                var sourceReferences = new List <ISourceReference>();

                if (!string.IsNullOrEmpty(targetFrameworkInformation.AssemblyPath))
                {
                    var assemblyPath = ResolvePath(project, target.Configuration, targetFrameworkInformation.AssemblyPath);
                    var pdbPath = ResolvePath(project, target.Configuration, targetFrameworkInformation.PdbPath);

                    metadataReferences.Add(new CompiledProjectMetadataReference(project, assemblyPath, pdbPath));
                }
                else
                {
                    var provider = project.CompilerServices?.ProjectCompiler ?? Project.DefaultCompiler;

                    // Find the default project exporter
                    var projectCompiler = _projectCompilers.GetOrAdd(provider, typeInfo =>
                    {
                        return CompilerServices.CreateService <IProjectCompiler>(_serviceProvider, _projectLoadContext.Value, typeInfo);
                    });

                    Logger.TraceInformation("[{0}]: GetProjectReference({1}, {2}, {3}, {4})", provider.TypeName, target.Name, target.TargetFramework, target.Configuration, target.Aspect);

                    // Get the exports for the project dependencies
                    var projectExport = new Lazy <ILibraryExport>(() =>
                    {
                        // TODO: Cache?
                        var context = new ApplicationHostContext(_serviceProvider,
                                                                 project.ProjectDirectory,
                                                                 packagesDirectory: null,
                                                                 configuration: target.Configuration,
                                                                 targetFramework: target.TargetFramework,
                                                                 cache: cache,
                                                                 cacheContextAccessor: cacheContextAccessor,
                                                                 namedCacheDependencyProvider: namedCacheDependencyProvider,
                                                                 loadContextFactory: loadContextFactory);

                        context.DependencyWalker.Walk(project.Name, project.Version, target.TargetFramework);

                        return ProjectExportProviderHelper.GetExportsRecursive(
                            cache,
                            context.LibraryManager,
                            context.LibraryExportProvider,
                            target,
                            dependenciesOnly: true);
                    });

                    // Resolve the project export
                    IMetadataProjectReference projectReference = projectCompiler.CompileProject(
                        project,
                        target,
                        () => projectExport.Value,
                        () => CompositeResourceProvider.Default.GetResources(project));

                    metadataReferences.Add(projectReference);

                    // Shared sources
                    foreach (var sharedFile in project.Files.SharedFiles)
                    {
                        sourceReferences.Add(new SourceFileReference(sharedFile));
                    }
                }

                return new LibraryExport(metadataReferences, sourceReferences);
            }));
        }