Exemple #1
0
        public async Task <IVsProjectAdapter> CreateAdapterForDeferredProjectAsync(IVsHierarchy project)
        {
            Assumes.Present(project);

            await _threadingService.JoinableTaskFactory.SwitchToMainThreadAsync();

            var vsHierarchyItem = VsHierarchyItem.FromVsHierarchy(project);
            var fullProjectPath = VsHierarchyUtility.GetProjectPath(project);

            var uniqueName = string.Empty;

            _vsSolution.Value.GetUniqueNameOfProject(project, out uniqueName);

            var projectNames = new ProjectNames(
                fullName: fullProjectPath,
                uniqueName: uniqueName,
                shortName: Path.GetFileNameWithoutExtension(fullProjectPath),
                customUniqueName: GetCustomUniqueName(uniqueName));

            var workspaceBuildProperties = new WorkspaceProjectBuildProperties(
                fullProjectPath, _workspaceService.Value, _threadingService);

            var projectTypeGuid = await _workspaceService.Value.GetProjectTypeGuidAsync(fullProjectPath);

            return(new VsProjectAdapter(
                       vsHierarchyItem,
                       projectNames,
                       fullProjectPath,
                       projectTypeGuid,
                       EnsureProjectIsLoaded,
                       workspaceBuildProperties,
                       _threadingService,
                       _workspaceService.Value));
        }
        public async Task <IVsProjectAdapter> CreateAdapterForFullyLoadedProjectAsync(EnvDTE.Project dteProject)
        {
            Assumes.Present(dteProject);

            _threadingService.ThrowIfNotOnUIThread();

            var vsHierarchyItem = VsHierarchyItem.FromDteProject(dteProject);
            Func <IVsHierarchy, EnvDTE.Project> loadDteProject = _ => dteProject;

            IProjectBuildProperties vsBuildProperties;

            if (vsHierarchyItem.VsHierarchy is IVsBuildPropertyStorage)
            {
                vsBuildProperties = new VsManagedLanguagesProjectBuildProperties(
                    vsHierarchyItem.VsHierarchy as IVsBuildPropertyStorage, _threadingService);
            }
            else
            {
                vsBuildProperties = new VsCoreProjectBuildProperties(dteProject, _threadingService);
            }

            var projectNames = await ProjectNames.FromDTEProjectAsync(dteProject);

            var fullProjectPath = EnvDTEProjectInfoUtility.GetFullProjectPath(dteProject);

            return(new VsProjectAdapter(
                       vsHierarchyItem,
                       projectNames,
                       fullProjectPath,
                       loadDteProject,
                       vsBuildProperties,
                       _threadingService));
        }
Exemple #3
0
        public async Task <IVsProjectAdapter> CreateAdapterForFullyLoadedProjectAsync(EnvDTE.Project dteProject)
        {
            Assumes.Present(dteProject);

            // Get services while we might be on background thread
            var vsSolution5 = await _vsSolution5.GetValueAsync();

            // switch to main thread and use services we know must be done on main thread.
            await _threadingService.JoinableTaskFactory.SwitchToMainThreadAsync();

            var vsHierarchyItem = VsHierarchyItem.FromDteProject(dteProject);
            Func <IVsHierarchy, EnvDTE.Project> loadDteProject = _ => dteProject;

            var buildStorageProperty = vsHierarchyItem.VsHierarchy as IVsBuildPropertyStorage;
            var vsBuildProperties    = new VsProjectBuildProperties(
                dteProject, buildStorageProperty, _threadingService);

            var projectNames = await ProjectNames.FromDTEProjectAsync(dteProject, vsSolution5);

            var fullProjectPath = dteProject.GetFullProjectPath();

            return(new VsProjectAdapter(
                       vsHierarchyItem,
                       projectNames,
                       fullProjectPath,
                       dteProject.Kind,
                       loadDteProject,
                       vsBuildProperties,
                       _threadingService));
        }
Exemple #4
0
        /// <summary>
        /// Removes a project from the project name dictionary.
        /// </summary>
        private void RemoveProjectName(ProjectNames projectNames)
        {
            Debug.Assert(_readerWriterLock.IsWriteLockHeld);

            _projectNamesCache.Remove(projectNames.CustomUniqueName);
            _projectNamesCache.Remove(projectNames.UniqueName);
            _projectNamesCache.Remove(projectNames.FullName);
            _primaryCache.Remove(projectNames.FullName);
        }
Exemple #5
0
        private void UpdateProjectNamesCache(ProjectNames projectNames)
        {
            Debug.Assert(_readerWriterLock.IsWriteLockHeld);

            AddShortName(projectNames);

            _projectNamesCache[projectNames.CustomUniqueName] = projectNames;
            _projectNamesCache[projectNames.UniqueName]       = projectNames;
            _projectNamesCache[projectNames.FullName]         = projectNames;
        }
Exemple #6
0
        /// <summary>
        /// Adds an entry to the short name cach.
        /// </summary>
        private void AddShortName(ProjectNames projectNames)
        {
            Debug.Assert(_readerWriterLock.IsWriteLockHeld);

            HashSet <ProjectNames> values;

            if (!_shortNameCache.TryGetValue(projectNames.ShortName, out values))
            {
                values = new HashSet <ProjectNames>();
                _shortNameCache.Add(projectNames.ShortName, values);
            }

            values.Add(projectNames);
        }
        public bool AddProjectRestoreInfoSource(ProjectNames projectNames, object restoreInfoSource)
        {
            if (projectNames == null)
            {
                throw new ArgumentNullException(nameof(projectNames));
            }

            if (projectNames.FullName == null)
            {
                throw new ArgumentException(
                          message: string.Format(CultureInfo.CurrentCulture, Strings.PropertyCannotBeNull, nameof(projectNames.FullName)),
                          paramName: nameof(projectNames));
            }

            if (restoreInfoSource == null)
            {
                throw new ArgumentNullException(nameof(restoreInfoSource));
            }

            _readerWriterLock.EnterWriteLock();

            try
            {
                if (!_projectNamesCache.ContainsKey(projectNames.FullName))
                {
                    UpdateProjectNamesCache(projectNames);
                }

                AddOrUpdateCacheEntry(
                    projectNames.FullName,
                    addEntryFactory: k => new CacheEntry
                {
                    ProjectNames             = projectNames,
                    ProjectRestoreInfoSource = restoreInfoSource,
                },
                    updateEntryFactory: (k, e) =>
                {
                    e.ProjectRestoreInfoSource = restoreInfoSource;
                    return(e);
                });
            }
            finally
            {
                _readerWriterLock.ExitWriteLock();
            }
            // Do not fire a cache update event when the restore project info source is updated
            // as it provides no value to any other components other than restore.

            return(true);
        }
Exemple #8
0
        /// <summary>
        /// Removes a project from the short name cache.
        /// </summary>
        /// <param name="projectNames">The short name of the project.</param>
        private void RemoveShortName(ProjectNames projectNames)
        {
            Debug.Assert(_readerWriterLock.IsWriteLockHeld);

            HashSet <ProjectNames> values;

            if (_shortNameCache.TryGetValue(projectNames.ShortName, out values))
            {
                values.Remove(projectNames);

                // Remove the item from the dictionary if we've removed the last project
                if (values.Count == 0)
                {
                    _shortNameCache.Remove(projectNames.ShortName);
                }
            }
        }
        public bool AddProjectRestoreInfo(ProjectNames projectNames, DependencyGraphSpec projectRestoreInfo, IReadOnlyList <IAssetsLogMessage> additionalMessages)
        {
            if (projectNames == null)
            {
                throw new ArgumentNullException(nameof(projectNames));
            }

            if (projectNames.FullName == null)
            {
                throw new ArgumentException(
                          message: string.Format(CultureInfo.CurrentCulture, Strings.PropertyCannotBeNull, nameof(projectNames.FullName)),
                          paramName: nameof(projectNames));
            }

            _readerWriterLock.EnterWriteLock();

            try
            {
                if (!_projectNamesCache.ContainsKey(projectNames.FullName))
                {
                    UpdateProjectNamesCache(projectNames);
                }

                AddOrUpdateCacheEntry(
                    projectNames.FullName,
                    addEntryFactory: k => new CacheEntry
                {
                    ProjectRestoreInfo = projectRestoreInfo,
                    AdditionalMessages = additionalMessages
                },
                    updateEntryFactory: (k, e) =>
                {
                    e.ProjectRestoreInfo = projectRestoreInfo;
                    e.AdditionalMessages = additionalMessages;
                    return(e);
                });
            }
            finally
            {
                _readerWriterLock.ExitWriteLock();
            }

            FireCacheUpdatedEvent(projectNames.FullName);

            return(true);
        }
        public VsProjectAdapter(
            VsHierarchyItem vsHierarchyItem,
            ProjectNames projectNames,
            string fullProjectPath,
            Func <IVsHierarchy, EnvDTE.Project> loadDteProject,
            IProjectBuildProperties buildProperties,
            IVsProjectThreadingService threadingService)
        {
            Assumes.Present(vsHierarchyItem);

            _vsHierarchyItem  = vsHierarchyItem;
            _dteProject       = new Lazy <EnvDTE.Project>(() => loadDteProject(_vsHierarchyItem.VsHierarchy));
            _threadingService = threadingService;
            FullProjectPath   = fullProjectPath;
            ProjectNames      = projectNames;
            BuildProperties   = buildProperties;
        }
Exemple #11
0
        public bool TryGetProjectNameByShortName(string name, out ProjectNames projectNames)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentException(CommonResources.Argument_Cannot_Be_Null_Or_Empty, nameof(name));
            }

            _readerWriterLock.EnterReadLock();

            try
            {
                return(TryGetProjectNameByShortNameWithoutLock(name, out projectNames));
            }
            finally
            {
                _readerWriterLock.ExitReadLock();
            }
        }
Exemple #12
0
        private bool TryGetProjectNameByShortNameWithoutLock(string name, out ProjectNames projectNames)
        {
            Debug.Assert(_readerWriterLock.IsReadLockHeld);

            projectNames = null;

            HashSet <ProjectNames> values;

            if (_shortNameCache.TryGetValue(name, out values))
            {
                // If there is only one project name instance, that means the short name is unambiguous, in which
                // case we can return that one project.
                projectNames = values.Count == 1 ? values.Single() : null;

                return(projectNames != null);
            }

            return(false);
        }
        public bool AddProject(ProjectNames projectNames, IVsProjectAdapter vsProjectAdapter, NuGetProject nuGetProject)
        {
            if (projectNames == null)
            {
                throw new ArgumentNullException(nameof(projectNames));
            }

            if (projectNames.FullName == null)
            {
                throw new ArgumentException(
                          message: string.Format(CultureInfo.CurrentCulture, Strings.PropertyCannotBeNull, nameof(projectNames.FullName)),
                          paramName: nameof(projectNames));
            }

            _readerWriterLock.EnterWriteLock();

            try
            {
                UpdateProjectNamesCache(projectNames);

                AddOrUpdateCacheEntry(
                    projectNames.FullName,
                    addEntryFactory: k => new CacheEntry
                {
                    NuGetProject     = nuGetProject,
                    VsProjectAdapter = vsProjectAdapter,
                    ProjectNames     = projectNames
                },
                    updateEntryFactory: (k, e) =>
                {
                    e.NuGetProject     = nuGetProject;
                    e.VsProjectAdapter = vsProjectAdapter;
                    e.ProjectNames     = projectNames;
                    return(e);
                });
            }
            finally
            {
                _readerWriterLock.ExitWriteLock();
            }

            return(true);
        }
Exemple #14
0
        public bool AddProjectRestoreInfo(ProjectNames projectNames, DependencyGraphSpec projectRestoreInfo)
        {
            if (projectNames == null)
            {
                throw new ArgumentNullException(nameof(projectNames));
            }

            if (projectNames.FullName == null)
            {
                throw new ArgumentNullException(nameof(projectNames.FullName));
            }

            _readerWriterLock.EnterWriteLock();

            try
            {
                if (!_projectNamesCache.ContainsKey(projectNames.FullName))
                {
                    UpdateProjectNamesCache(projectNames);
                }

                AddOrUpdateCacheEntry(
                    projectNames.FullName,
                    addEntryFactory: k => new CacheEntry
                {
                    ProjectRestoreInfo = projectRestoreInfo
                },
                    updateEntryFactory: (k, e) =>
                {
                    e.ProjectRestoreInfo = projectRestoreInfo;
                    return(e);
                });
            }
            finally
            {
                _readerWriterLock.ExitWriteLock();
            }

            FireCacheUpdatedEvent(projectNames.FullName);

            return(true);
        }
Exemple #15
0
        public bool AddProject(ProjectNames projectNames, IVsProjectAdapter vsProjectAdapter, NuGetProject nuGetProject)
        {
            if (projectNames == null)
            {
                throw new ArgumentNullException(nameof(projectNames));
            }

            if (projectNames.FullName == null)
            {
                throw new ArgumentNullException(nameof(projectNames.FullName));
            }

            _readerWriterLock.EnterWriteLock();

            try
            {
                UpdateProjectNamesCache(projectNames);

                AddOrUpdateCacheEntry(
                    projectNames.FullName,
                    addEntryFactory: k => new CacheEntry
                {
                    NuGetProject     = nuGetProject,
                    VsProjectAdapter = vsProjectAdapter,
                    ProjectNames     = projectNames
                },
                    updateEntryFactory: (k, e) =>
                {
                    e.NuGetProject     = nuGetProject;
                    e.VsProjectAdapter = vsProjectAdapter;
                    e.ProjectNames     = projectNames;
                    UpdateProjectIdInPackageSpec(nuGetProject, projectNames.UniqueName, e.ProjectRestoreInfo);
                    return(e);
                });
            }
            finally
            {
                _readerWriterLock.ExitWriteLock();
            }

            return(true);
        }
        public bool AddProject(ProjectNames projectNames, EnvDTE.Project dteProject, NuGetProject nuGetProject)
        {
            if (projectNames == null)
            {
                throw new ArgumentNullException(nameof(projectNames));
            }

            if (projectNames.FullName == null)
            {
                throw new ArgumentNullException(nameof(projectNames.FullName));
            }

            _readerWriterLock.EnterWriteLock();

            try
            {
                UpdateProjectNamesCache(projectNames);

                AddOrUpdateCacheEntry(
                    projectNames.FullName,
                    addEntryFactory: k => new CacheEntry
                {
                    NuGetProject  = nuGetProject,
                    EnvDTEProject = dteProject,
                    ProjectNames  = projectNames
                },
                    updateEntryFactory: (k, e) =>
                {
                    e.NuGetProject  = nuGetProject;
                    e.EnvDTEProject = dteProject;
                    e.ProjectNames  = projectNames;
                    return(e);
                });
            }
            finally
            {
                _readerWriterLock.ExitWriteLock();
            }

            return(true);
        }
Exemple #17
0
        public VsProjectAdapter(
            VsHierarchyItem vsHierarchyItem,
            ProjectNames projectNames,
            string fullProjectPath,
            string projectTypeGuid,
            Func <IVsHierarchy, EnvDTE.Project> loadDteProject,
            IProjectBuildProperties buildProperties,
            IVsProjectThreadingService threadingService,
            IDeferredProjectWorkspaceService workspaceService = null)
        {
            Assumes.Present(vsHierarchyItem);

            _vsHierarchyItem  = vsHierarchyItem;
            _dteProject       = new Lazy <EnvDTE.Project>(() => loadDteProject(_vsHierarchyItem.VsHierarchy));
            _workspaceService = workspaceService;
            _threadingService = threadingService;
            _projectTypeGuid  = projectTypeGuid;

            FullProjectPath = fullProjectPath;
            ProjectNames    = projectNames;
            BuildProperties = buildProperties;
        }
Exemple #18
0
        private void AddEnvDTEProjectToCache(EnvDTE.Project envDTEProject)
        {
            ThreadHelper.ThrowIfNotOnUIThread();

            if (!EnvDTEProjectUtility.IsSupported(envDTEProject))
            {
                return;
            }

            ProjectNames oldEnvDTEProjectName;

            _projectSystemCache.TryGetProjectNameByShortName(EnvDTEProjectUtility.GetName(envDTEProject), out oldEnvDTEProjectName);

            // Create the NuGet project first. If this throws we bail out and do not change the cache.
            var nuGetProject = CreateNuGetProject(envDTEProject);

            // Then create the project name from the project.
            var newEnvDTEProjectName = ProjectNames.FromDTEProject(envDTEProject);

            // Finally, try to add the project to the cache.
            var added = _projectSystemCache.AddProject(newEnvDTEProjectName, envDTEProject, nuGetProject);

            if (added)
            {
                // Emit project specific telemetry as we are adding the project to the cache.
                // This ensures we do not emit the events over and over while the solution is
                // open.
                NuGetProjectTelemetryService.Instance.EmitNuGetProject(nuGetProject);
            }

            if (string.IsNullOrEmpty(DefaultNuGetProjectName) ||
                newEnvDTEProjectName.ShortName.Equals(DefaultNuGetProjectName, StringComparison.OrdinalIgnoreCase))
            {
                DefaultNuGetProjectName = oldEnvDTEProjectName != null ?
                                          oldEnvDTEProjectName.CustomUniqueName :
                                          newEnvDTEProjectName.ShortName;
            }
        }
        public bool TryCreateNuGetProject(EnvDTE.Project dteProject, ProjectSystemProviderContext context, out NuGetProject result)
        {
            if (dteProject == null)
            {
                throw new ArgumentNullException(nameof(dteProject));
            }

            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            ThreadHelper.ThrowIfNotOnUIThread();

            result = null;

            // The project must be an IVsHierarchy.
            var hierarchy = VsHierarchyUtility.ToVsHierarchy(dteProject);

            if (hierarchy == null)
            {
                return(false);
            }

            // Check if the project is not CPS capable or if it is CPS capable then it does not have TargetFramework(s), if so then return false
            if (!hierarchy.IsCapabilityMatch("CPS"))
            {
                return(false);
            }

            var buildPropertyStorage = hierarchy as IVsBuildPropertyStorage;

            // read MSBuild property RestoreProjectStyle, TargetFramework, and TargetFrameworks
            var restoreProjectStyle = VsHierarchyUtility.GetMSBuildProperty(buildPropertyStorage, RestoreProjectStyle);

            var targetFramework = VsHierarchyUtility.GetMSBuildProperty(buildPropertyStorage, TargetFramework);

            var targetFrameworks = VsHierarchyUtility.GetMSBuildProperty(buildPropertyStorage, TargetFrameworks);

            // check for RestoreProjectStyle property is set and if not set to PackageReference then return false
            if (!(string.IsNullOrEmpty(restoreProjectStyle) ||
                  restoreProjectStyle.Equals(ProjectStyle.PackageReference.ToString(), StringComparison.OrdinalIgnoreCase)))
            {
                return(false);
            }
            // check whether TargetFramework or TargetFrameworks property is set, else return false
            else if (string.IsNullOrEmpty(targetFramework) && string.IsNullOrEmpty(targetFrameworks))
            {
                return(false);
            }

            // Lazy load the CPS enabled JoinableTaskFactory for the UI.
            NuGetUIThreadHelper.SetJoinableTaskFactoryFromService(ProjectServiceAccessor.Value as IProjectServiceAccessor);

            var projectNames        = ProjectNames.FromDTEProject(dteProject);
            var fullProjectPath     = EnvDTEProjectInfoUtility.GetFullProjectPath(dteProject);
            var unconfiguredProject = GetUnconfiguredProject(dteProject);

            result = new CpsPackageReferenceProject(
                dteProject.Name,
                EnvDTEProjectInfoUtility.GetCustomUniqueName(dteProject),
                fullProjectPath,
                _projectSystemCache,
                dteProject,
                unconfiguredProject,
                VsHierarchyUtility.GetProjectId(dteProject));

            return(true);
        }