public static IMSBuildNuGetProjectSystem CreateMSBuildNuGetProjectSystem(EnvDTEProject envDTEProject, INuGetProjectContext nuGetProjectContext)
        {
            ThreadHelper.ThrowIfNotOnUIThread();

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

            if (String.IsNullOrEmpty(envDTEProject.FullName))
            {
                throw new InvalidOperationException(
                          String.Format(CultureInfo.CurrentCulture,
                                        Strings.DTE_ProjectUnsupported, EnvDTEProjectInfoUtility.GetName(envDTEProject)));
            }

            if (EnvDTEProjectUtility.SupportsINuGetProjectSystem(envDTEProject))
            {
                throw new InvalidOperationException(
                          string.Format(CultureInfo.CurrentCulture, Strings.DTE_ProjectUnsupported, typeof(IMSBuildNuGetProjectSystem).FullName));
            }

            var guids = VsHierarchyUtility.GetProjectTypeGuids(envDTEProject);

            if (guids.Contains(VsProjectTypes.CppProjectTypeGuid)) // Got a cpp project
            {
                return(new NativeProjectSystem(envDTEProject, nuGetProjectContext));
            }

            // Try to get a factory for the project type guid
            foreach (var guid in guids)
            {
                IMSBuildNuGetProjectSystemThunk factory;
                if (_factories.TryGetValue(guid, out factory))
                {
                    return(factory(envDTEProject, nuGetProjectContext));
                }
            }

            // Fall back to the default if we have no special project types
            return(new VSMSBuildNuGetProjectSystem(envDTEProject, nuGetProjectContext));
        }
Example #2
0
        private void AddEnvDTEProjectToCache(EnvDTE.Project envDTEProject)
        {
            ThreadHelper.ThrowIfNotOnUIThread();

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

            ProjectNames oldEnvDTEProjectName;

            _projectSystemCache.TryGetProjectNameByShortName(EnvDTEProjectInfoUtility.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;
            }
        }