private async Task <AggregateWorkspaceProjectContext> CreateProjectContextAsyncCore()
        {
            string languageName = await GetLanguageServiceName().ConfigureAwait(false);

            if (string.IsNullOrEmpty(languageName))
            {
                return(null);
            }

            Guid projectGuid = await GetProjectGuidAsync().ConfigureAwait(false);

            string targetPath = await GetTargetPathAsync().ConfigureAwait(false);

            if (string.IsNullOrEmpty(targetPath))
            {
                return(null);
            }

            // Don't initialize until the project has been loaded into the IDE and available in Solution Explorer
            await _asyncLoadDashboard.ProjectLoadedInHostWithCancellation(_commonServices.Project).ConfigureAwait(false);

            // TODO: https://github.com/dotnet/roslyn-project-system/issues/353
            return(await _taskScheduler.RunAsync(TaskSchedulerPriority.UIThreadBackgroundPriority, async() =>
            {
                await _commonServices.ThreadingService.SwitchToUIThread();

                var projectData = GetProjectData();

                // Get the set of active configured projects ignoring target framework.
#pragma warning disable CS0618 // Type or member is obsolete
                var configuredProjectsMap = await _activeConfiguredProjectsProvider.GetActiveConfiguredProjectsMapAsync().ConfigureAwait(true);
#pragma warning restore CS0618 // Type or member is obsolete

                // Get the unconfigured project host object (shared host object).
                var configuredProjectsToRemove = new HashSet <ConfiguredProject>(_configuredProjectHostObjectsMap.Keys);
                var activeProjectConfiguration = _commonServices.ActiveConfiguredProject.ProjectConfiguration;

                var innerProjectContextsBuilder = ImmutableDictionary.CreateBuilder <string, IWorkspaceProjectContext>();
                string activeTargetFramework = string.Empty;
                IConfiguredProjectHostObject activeIntellisenseProjectHostObject = null;

                foreach (var kvp in configuredProjectsMap)
                {
                    var targetFramework = kvp.Key;
                    var configuredProject = kvp.Value;
                    if (!TryGetConfiguredProjectState(configuredProject, out IWorkspaceProjectContext workspaceProjectContext, out IConfiguredProjectHostObject configuredProjectHostObject))
                    {
                        // Get the target path for the configured project.
                        var projectProperties = configuredProject.Services.ExportProvider.GetExportedValue <ProjectProperties>();
                        var configurationGeneralProperties = await projectProperties.GetConfigurationGeneralPropertiesAsync().ConfigureAwait(true);
                        targetPath = (string)await configurationGeneralProperties.TargetPath.GetValueAsync().ConfigureAwait(true);
                        var targetFrameworkMoniker = (string)await configurationGeneralProperties.TargetFrameworkMoniker.GetValueAsync().ConfigureAwait(true);
                        var displayName = GetDisplayName(configuredProject, projectData, targetFramework);
                        configuredProjectHostObject = _projectHostProvider.GetConfiguredProjectHostObject(_unconfiguredProjectHostObject, displayName, targetFrameworkMoniker);

                        // TODO: https://github.com/dotnet/roslyn-project-system/issues/353
                        await _commonServices.ThreadingService.SwitchToUIThread();
                        workspaceProjectContext = _contextFactory.Value.CreateProjectContext(languageName, displayName, projectData.FullPath, projectGuid, configuredProjectHostObject, targetPath);

                        // By default, set "LastDesignTimeBuildSucceeded = false" to turn off diagnostics until first design time build succeeds for this project.
                        workspaceProjectContext.LastDesignTimeBuildSucceeded = false;

                        AddConfiguredProjectState(configuredProject, workspaceProjectContext, configuredProjectHostObject);
                    }

                    innerProjectContextsBuilder.Add(targetFramework, workspaceProjectContext);

                    if (activeIntellisenseProjectHostObject == null && configuredProject.ProjectConfiguration.Equals(activeProjectConfiguration))
                    {
                        activeIntellisenseProjectHostObject = configuredProjectHostObject;
                        activeTargetFramework = targetFramework;
                    }
                }

                _unconfiguredProjectHostObject.ActiveIntellisenseProjectHostObject = activeIntellisenseProjectHostObject;

                return new AggregateWorkspaceProjectContext(innerProjectContextsBuilder.ToImmutable(), configuredProjectsMap, activeTargetFramework, _unconfiguredProjectHostObject);
            }));
 private void AddConfiguredProjectState(ConfiguredProject configuredProject, IWorkspaceProjectContext workspaceProjectContext, IConfiguredProjectHostObject configuredProjectHostObject)
 {
     lock (_gate)
     {
         _configuredProjectContextsMap.Add(configuredProject, workspaceProjectContext);
         _configuredProjectHostObjectsMap.Add(configuredProject, configuredProjectHostObject);
     }
 }
 private bool TryGetConfiguredProjectState(ConfiguredProject configuredProject, out IWorkspaceProjectContext workspaceProjectContext, out IConfiguredProjectHostObject configuredProjectHostObject)
 {
     lock (_gate)
     {
         if (_configuredProjectContextsMap.TryGetValue(configuredProject, out workspaceProjectContext))
         {
             configuredProjectHostObject = _configuredProjectHostObjectsMap[configuredProject];
             return(true);
         }
         else
         {
             workspaceProjectContext     = null;
             configuredProjectHostObject = null;
             return(false);
         }
     }
 }
Пример #4
0
        private async Task <AggregateWorkspaceProjectContext> CreateProjectContextAsyncCore()
        {
            string languageName = await GetLanguageServiceName().ConfigureAwait(false);

            if (string.IsNullOrEmpty(languageName))
            {
                return(null);
            }

            Guid projectGuid = await _projectGuidService.GetProjectGuidAsync()
                               .ConfigureAwait(false);

            string targetPath = await GetTargetPathAsync().ConfigureAwait(false);

            if (string.IsNullOrEmpty(targetPath))
            {
                return(null);
            }


            // TODO: https://github.com/dotnet/roslyn-project-system/issues/353
            await _commonServices.ThreadingService.SwitchToUIThread();

            ProjectData projectData = GetProjectData();

            // Get the set of active configured projects ignoring target framework.
#pragma warning disable CS0618 // Type or member is obsolete
            ImmutableDictionary <string, ConfiguredProject> configuredProjectsMap = await _activeConfiguredProjectsProvider.GetActiveConfiguredProjectsMapAsync().ConfigureAwait(true);

#pragma warning restore CS0618 // Type or member is obsolete

            // Get the unconfigured project host object (shared host object).
            var configuredProjectsToRemove = new HashSet <ConfiguredProject>(_configuredProjectHostObjectsMap.Keys);
            ProjectConfiguration activeProjectConfiguration = _commonServices.ActiveConfiguredProject.ProjectConfiguration;

            ImmutableDictionary <string, IWorkspaceProjectContext> .Builder innerProjectContextsBuilder = ImmutableDictionary.CreateBuilder <string, IWorkspaceProjectContext>();
            string activeTargetFramework = string.Empty;
            IConfiguredProjectHostObject activeIntellisenseProjectHostObject = null;

            foreach (KeyValuePair <string, ConfiguredProject> kvp in configuredProjectsMap)
            {
                string            targetFramework   = kvp.Key;
                ConfiguredProject configuredProject = kvp.Value;
                if (!TryGetConfiguredProjectState(configuredProject, out IWorkspaceProjectContext workspaceProjectContext, out IConfiguredProjectHostObject configuredProjectHostObject))
                {
                    // Get the target path for the configured project.
                    ProjectProperties    projectProperties = configuredProject.Services.ExportProvider.GetExportedValue <ProjectProperties>();
                    ConfigurationGeneral configurationGeneralProperties = await projectProperties.GetConfigurationGeneralPropertiesAsync().ConfigureAwait(true);

                    targetPath = (string)await configurationGeneralProperties.TargetPath.GetValueAsync().ConfigureAwait(true);

                    string targetFrameworkMoniker = (string)await configurationGeneralProperties.TargetFrameworkMoniker.GetValueAsync().ConfigureAwait(true);

                    string workspaceProjectContextId = GetWorkspaceContextId(configuredProject);
                    configuredProjectHostObject = _projectHostProvider.GetConfiguredProjectHostObject(_unconfiguredProjectHostObject, workspaceProjectContextId, targetFrameworkMoniker);

                    // TODO: https://github.com/dotnet/roslyn-project-system/issues/353
                    await _commonServices.ThreadingService.SwitchToUIThread();

                    // NOTE: Despite CreateProjectContext taking a "displayName"; it's actually sets both "WorkspaceProjectContextId", "DisplayName", and default "AssemblyName".
                    // Unlike the latter properties, we cannot change WorkspaceProjectContextId once set, so we pass it as the display name.
                    workspaceProjectContext             = _contextFactory.Value.CreateProjectContext(languageName, workspaceProjectContextId, projectData.FullPath, projectGuid, configuredProjectHostObject, targetPath);
                    workspaceProjectContext.DisplayName = GetDisplayName(configuredProject, projectData, targetFramework);

                    // By default, set "LastDesignTimeBuildSucceeded = false" to turn off diagnostics until first design time build succeeds for this project.
                    workspaceProjectContext.LastDesignTimeBuildSucceeded = false;

                    AddConfiguredProjectState(configuredProject, workspaceProjectContext, configuredProjectHostObject);
                }

                innerProjectContextsBuilder.Add(targetFramework, workspaceProjectContext);

                if (activeIntellisenseProjectHostObject == null && configuredProject.ProjectConfiguration.Equals(activeProjectConfiguration))
                {
                    activeIntellisenseProjectHostObject = configuredProjectHostObject;
                    activeTargetFramework = targetFramework;
                }
            }

            _unconfiguredProjectHostObject.ActiveIntellisenseProjectHostObject = activeIntellisenseProjectHostObject;

            return(new AggregateWorkspaceProjectContext(innerProjectContextsBuilder.ToImmutable(), configuredProjectsMap, activeTargetFramework, _unconfiguredProjectHostObject));
        }
        private static VsContainedLanguageComponentsFactory CreateInstance(IVsContainedLanguageFactory containedLanguageFactory = null, IVsProject4 project = null, ProjectProperties properties = null, IConfiguredProjectHostObject hostObject = null, ILanguageServiceHost languageServiceHost = null)
        {
            var hostProvider    = IProjectHostProviderFactory.ImplementActiveIntellisenseProjectHostObject(hostObject);
            var serviceProvider = IOleAsyncServiceProviderFactory.ImplementQueryServiceAsync(containedLanguageFactory, new Guid(LanguageServiceId));

            var projectVsServices = new IUnconfiguredProjectVsServicesMock();

            projectVsServices.ImplementVsProject(project);
            projectVsServices.ImplementThreadingService(IProjectThreadingServiceFactory.Create());
            projectVsServices.ImplementActiveConfiguredProjectProperties(properties);

            return(CreateInstance(serviceProvider, projectVsServices.Object, hostProvider, languageServiceHost));
        }
Пример #6
0
        public static IProjectHostProvider ImplementActiveIntellisenseProjectHostObject(IConfiguredProjectHostObject hostObject)
        {
            var hostObjectMock = new Mock <IUnconfiguredProjectHostObject>();

            hostObjectMock.Setup(o => o.ActiveIntellisenseProjectHostObject)
            .Returns(hostObject);

            var mock = new Mock <IProjectHostProvider>();

            mock.Setup(p => p.UnconfiguredProjectHostObject)
            .Returns(hostObjectMock.Object);

            return(mock.Object);
        }