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;

            var projectK = GetProjectKProject(dteProject);

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

            result = new ProjectKNuGetProject(
                projectK,
                dteProject.Name,
                EnvDTEProjectInfoUtility.GetCustomUniqueName(dteProject),
                VsHierarchyUtility.GetProjectId(dteProject));

            return(true);
        }
        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;

            var project = new EnvDTEProjectAdapter(dteProject);

            if (!project.IsLegacyCSProjPackageReferenceProject)
            {
                return(false);
            }

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

            result = new LegacyCSProjPackageReferenceProject(
                project,
                VsHierarchyUtility.GetProjectId(dteProject));

            return(true);
        }
示例#3
0
        public async Task <NuGetProject> UpdateNuGetProjectToPackageRef(NuGetProject oldProject)
        {
#if VS14
            // do nothing for VS 2015 and simply return the existing NuGetProject
            if (NuGetProjectUpdated != null)
            {
                NuGetProjectUpdated(this, new NuGetProjectEventArgs(oldProject));
            }

            return(await Task.FromResult(oldProject));
#else
            if (oldProject == null)
            {
                throw new ArgumentException(
                          Strings.Argument_Cannot_Be_Null_Or_Empty,
                          nameof(oldProject));
            }

            var projectName = GetNuGetProjectSafeName(oldProject);
            var dteProject  = GetDTEProject(projectName);

            ProjectNames oldEnvDTEProjectName;
            _projectSystemCache.TryGetProjectNames(projectName, out oldEnvDTEProjectName);

            RemoveEnvDTEProjectFromCache(projectName);

            var nuGetProject = await NuGetUIThreadHelper.JoinableTaskFactory.RunAsync(async() =>
            {
                await NuGetUIThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

                var settings = ServiceLocator.GetInstance <ISettings>();

                var context = new ProjectSystemProviderContext(
                    EmptyNuGetProjectContext,
                    () => PackagesFolderPathUtility.GetPackagesFolderPath(this, settings));

                return(new LegacyCSProjPackageReferenceProject(
                           new EnvDTEProjectAdapter(dteProject),
                           VsHierarchyUtility.GetProjectId(dteProject)));
            });

            var added = _projectSystemCache.AddProject(oldEnvDTEProjectName, dteProject, nuGetProject);

            if (DefaultNuGetProjectName == null)
            {
                DefaultNuGetProjectName = projectName;
            }

            if (NuGetProjectUpdated != null)
            {
                NuGetProjectUpdated(this, new NuGetProjectEventArgs(nuGetProject));
            }

            return(nuGetProject);
#endif
        }
示例#4
0
        private NuGetProject CreateNuGetProject(Project envDTEProject, INuGetProjectContext projectContext = null)
        {
            var settings = ServiceLocator.GetInstance <ISettings>();

            var context = new ProjectSystemProviderContext(
                projectContext ?? EmptyNuGetProjectContext,
                () => PackagesFolderPathUtility.GetPackagesFolderPath(this, settings));

            NuGetProject result;

            if (_projectSystemFactory.TryCreateNuGetProject(envDTEProject, context, out result))
            {
                return(result);
            }

            return(null);
        }
        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();

            var exceptions = new List <Exception>();

            result = _providers
                     .Select(p =>
            {
                try
                {
                    NuGetProject nuGetProject;
                    if (p.TryCreateNuGetProject(dteProject, context, out nuGetProject))
                    {
                        return(nuGetProject);
                    }
                }
                catch (Exception e)
                {
                    // Ignore failures. If this method returns null, the problem falls
                    // into one of the other NuGet project types.
                    exceptions.Add(e);
                }

                return(null);
            })
                     .FirstOrDefault(p => p != null);

            if (result == null)
            {
                exceptions.ForEach(ExceptionHelper.WriteWarningToActivityLog);
            }

            return(result != null);
        }
示例#6
0
        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 = _providers
                     .Select(p =>
            {
                try
                {
                    NuGetProject nuGetProject;
                    if (p.TryCreateNuGetProject(dteProject, context, out nuGetProject))
                    {
                        return(nuGetProject);
                    }
                }
                catch
                {
                    // Ignore failures. If this method returns null, the problem falls
                    // into one of the other NuGet project types.
                }

                return(null);
            })
                     .FirstOrDefault(p => p != null);

            return(result != null);
        }
        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);
        }
        public bool TryCreateNuGetProject(EnvDTE.Project envDTEProject, ProjectSystemProviderContext context, out NuGetProject result)
        {
            if (envDTEProject == null)
            {
                throw new ArgumentNullException(nameof(envDTEProject));
            }

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

            ThreadHelper.ThrowIfNotOnUIThread();

            result = null;

            var msBuildNuGetProjectSystem = MSBuildNuGetProjectSystemFactory.CreateMSBuildNuGetProjectSystem(
                envDTEProject,
                context.ProjectContext);

            var isWebSite = msBuildNuGetProjectSystem is WebSiteProjectSystem;

            // Web sites cannot have project.json
            if (!isWebSite)
            {
                // Find the project file path
                var projectFilePath = EnvDTEProjectUtility.GetFullProjectPath(envDTEProject);

                if (!string.IsNullOrEmpty(projectFilePath))
                {
                    var msbuildProjectFile         = new FileInfo(projectFilePath);
                    var projectNameFromMSBuildPath = Path.GetFileNameWithoutExtension(msbuildProjectFile.Name);

                    // Treat projects with project.json as build integrated projects
                    // Search for projectName.project.json first, then project.json
                    // If the name cannot be determined, search only for project.json
                    string projectJsonPath = null;
                    if (string.IsNullOrEmpty(projectNameFromMSBuildPath))
                    {
                        projectJsonPath = Path.Combine(msbuildProjectFile.DirectoryName,
                                                       ProjectJsonPathUtilities.ProjectConfigFileName);
                    }
                    else
                    {
                        projectJsonPath = ProjectJsonPathUtilities.GetProjectConfigPath(msbuildProjectFile.DirectoryName,
                                                                                        projectNameFromMSBuildPath);
                    }

                    if (File.Exists(projectJsonPath))
                    {
                        result = new ProjectJsonBuildIntegratedProjectSystem(
                            projectJsonPath,
                            msbuildProjectFile.FullName,
                            envDTEProject,
                            EnvDTEProjectUtility.GetCustomUniqueName(envDTEProject));
                    }
                }
            }

            // Create a normal MSBuild project if no project.json was found
            if (result == null)
            {
                var folderNuGetProjectFullPath = context.PackagesPathFactory();

                // Project folder path is the packages config folder path
                var packagesConfigFolderPath = EnvDTEProjectUtility.GetFullPath(envDTEProject);

                result = new VSMSBuildNuGetProject(
                    envDTEProject,
                    msBuildNuGetProjectSystem,
                    folderNuGetProjectFullPath,
                    packagesConfigFolderPath);
            }

            return(result != null);
        }